JAL-3026 tweak to test class
[jalview.git] / utils / test / JalviewJSTest.java
1 package test;
2 import java.awt.BorderLayout;
3 import java.awt.ComponentOrientation;
4 import java.awt.Container;
5 import java.awt.Dimension;
6 import java.awt.Font;
7 import java.awt.GridLayout;
8 import java.awt.MediaTracker;
9 import java.io.File;
10 import java.text.DecimalFormat;
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import javax.swing.ImageIcon;
15 import javax.swing.JButton;
16 import javax.swing.JCheckBox;
17 import javax.swing.JCheckBoxMenuItem;
18 import javax.swing.JFrame;
19 import javax.swing.JLabel;
20 import javax.swing.JMenu;
21 import javax.swing.JMenuBar;
22 import javax.swing.JPanel;
23 import javax.swing.JRadioButton;
24 import javax.swing.JRadioButtonMenuItem;
25 import javax.swing.SwingConstants;
26 import javax.swing.WindowConstants;
27 import javax.swing.border.TitledBorder;
28
29 /**
30  * A class with a main method entry point for ad hoc tests of JalviewJS
31  * behaviour. The J2S transpiler should generate an html entry point for this
32  * class, allowing comparison between Java and Javascript execution.
33  */
34 public class JalviewJSTest extends JPanel
35 {
36   public static void main(String[] args)
37   {
38     new JalviewJSTest().doTest2();
39   }
40
41   void doTest2() {
42           Map<File, String> map = new HashMap<>();
43           File f1 = new File("/var/folders/y/xyz");
44           File f2 = new File("/var/folders/y/xyz");
45           map.put(f1,  "hello world");
46           System.out.println("f1 hashCode = " + f1.hashCode());
47           System.out.println("f2 hashCode = " + f2.hashCode());
48           System.out.println(map.get(f2));
49   }
50   /**
51    * Put some content in a JFrame and show it
52    */
53   void doTest()
54   {
55           System.out.println("ab;c;".split(";"));
56     new DecimalFormat("###,###").format((Integer) 1);
57     JFrame main = new JFrame();
58     main.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
59     JMenu menu = new JMenu("testing");
60     menu.setHorizontalAlignment(SwingConstants.RIGHT);
61     main.setContentPane(getVisualPaneContent(menu));
62     main.setJMenuBar(new JMenuBar());
63     main.getJMenuBar().add(menu);
64     main.pack();
65     main.setVisible(true);
66   }
67
68   /**
69    * Builds a cut-down 'Preferences Visual tab' for a minimal test of layout
70    * problems
71  * @param menu 
72    */
73   Container getVisualPaneContent(JMenu menu)
74   {
75     JPanel panel = new JPanel();
76     panel.setPreferredSize(new Dimension(400, 500));
77     panel.setOpaque(true);
78     panel.setLayout(new BorderLayout());
79
80     JPanel firstColumn = new JPanel();
81     firstColumn.setLayout(new GridLayout(10, 1));
82     firstColumn.setBorder(new TitledBorder("column 1"));
83
84     /*
85      * bug 21/08/18:
86      * - checkbox label and text extend outside the enclosing panel in JS
87      */
88     Font font = new Font("Verdana", Font.PLAIN, 11);
89
90     JLabel l1 = new JLabel(getImage("test2.png"));
91     l1.setText("trailing right");
92     l1.setHorizontalTextPosition(SwingConstants.TRAILING);
93     l1.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
94     l1.setHorizontalAlignment(SwingConstants.RIGHT);
95
96     JLabel l2 = new JLabel(getImage("test2.png"));
97     l2.setText("leading left");
98     l2.setFont(font);
99     l2.setHorizontalTextPosition(SwingConstants.LEADING);
100     l2.setHorizontalAlignment(SwingConstants.LEFT);
101
102     JButton b1 = new JButton("right left");
103     b1.setIcon(getImage("test2.png"));
104     b1.setFont(font);
105     b1.setHorizontalTextPosition(SwingConstants.RIGHT);
106     b1.setHorizontalAlignment(SwingConstants.LEFT);
107
108     firstColumn.add(l1);
109     firstColumn.add(l2);
110     firstColumn.add(b1);
111
112     
113     JCheckBox cb3 = new JCheckBox("leading,left-to-right,rt");
114     cb3.setFont(font);
115     cb3.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
116     cb3.setHorizontalTextPosition(SwingConstants.LEADING);
117     cb3.setHorizontalAlignment(SwingConstants.TRAILING);
118
119     JCheckBox cb4 = new JCheckBox("leading,right-to-left");
120     cb4.setFont(font);
121     cb4.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
122     cb4.setHorizontalTextPosition(SwingConstants.LEADING);
123
124     JCheckBox cb5 = new JCheckBox("trailing,left-to-right");
125     cb5.setFont(font);
126     cb5.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
127     cb5.setHorizontalTextPosition(SwingConstants.TRAILING);
128
129     JRadioButton rb1 = new JRadioButton("trailing,right-to-left");
130     rb1.setFont(font);
131     rb1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
132     rb1.setHorizontalTextPosition(SwingConstants.TRAILING);
133
134     JRadioButton rb2 = new JRadioButton("right,left-to-right");
135     rb2.setFont(font);
136     rb2.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
137     rb2.setHorizontalTextPosition(SwingConstants.RIGHT);
138
139     JRadioButton rb3 = new JRadioButton("right,right-to-left");
140     rb3.setFont(font);
141     rb3.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
142     rb3.setHorizontalTextPosition(SwingConstants.RIGHT);
143
144
145     
146     firstColumn.add(cb3);
147     firstColumn.add(cb4);
148     firstColumn.add(cb5);
149     firstColumn.add(rb1);
150     firstColumn.add(rb2);
151     firstColumn.add(rb3);
152     firstColumn.setBounds(200, 20, 200, 500);
153
154     JCheckBoxMenuItem cb3m = new JCheckBoxMenuItem("leading,left-to-right");
155     cb3m.setFont(font);
156     cb3m.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
157     cb3m.setHorizontalTextPosition(SwingConstants.LEADING);
158
159     JCheckBoxMenuItem cb4m = new JCheckBoxMenuItem("leading,right-to-left");
160     cb4m.setFont(font);
161     cb4m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
162     cb4m.setHorizontalTextPosition(SwingConstants.LEADING);
163
164     JCheckBoxMenuItem cb5m = new JCheckBoxMenuItem("trailing,left-to-right");
165     cb5m.setFont(font);
166     cb5m.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
167     cb5m.setHorizontalTextPosition(SwingConstants.TRAILING);
168
169     JCheckBoxMenuItem cb6m = new JCheckBoxMenuItem("trailing,right-to-left");
170     cb6m.setFont(font);
171     cb6m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
172     cb6m.setHorizontalTextPosition(SwingConstants.TRAILING);
173
174     JRadioButtonMenuItem rb1m = new JRadioButtonMenuItem("trailing,right-to-left");
175     rb1m.setFont(font);
176     rb1m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
177     rb1m.setHorizontalTextPosition(SwingConstants.TRAILING);
178
179     JRadioButtonMenuItem rb2m = new JRadioButtonMenuItem("right,left-to-right");
180     rb2m.setFont(font);
181     rb2m.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
182     rb2m.setHorizontalTextPosition(SwingConstants.RIGHT);
183
184     JRadioButtonMenuItem rb3m = new JRadioButtonMenuItem("right,right-to-left");
185     rb3m.setFont(font);
186     rb3m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
187     rb3m.setHorizontalTextPosition(SwingConstants.RIGHT);
188
189     JMenu m1 = new JMenu("left");
190     JMenu m2 = new JMenu("right");
191     menu.add(m1);
192     menu.add(m2);
193     m1.add(cb3m);
194     m2.add(cb4m);
195     m1.add(cb5m);
196     m2.add(cb6m);
197     m2.add(rb1m);
198     m1.add(rb2m);
199     m2.add(rb3m);
200     
201     JPanel theTab = new JPanel();
202     
203     theTab.setLayout(null);
204     theTab.add(firstColumn);
205     panel.add(theTab);
206
207     return panel;
208   }
209
210 private ImageIcon getImage(String name) {
211     ImageIcon icon = new ImageIcon(getClass().getResource(name));
212
213     while(icon.getImageLoadStatus() == MediaTracker.LOADING)
214                 try {
215                         Thread.sleep(10);
216                 } catch (InterruptedException e) {
217                 }
218     return icon;
219 }
220 }