a2026a8aec8c5ec036e0817e0f57f32d5e15277d
[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.equals(f2) = " + f1.equals(f2));
47           System.out.println("f1 hashCode = " + f1.hashCode());
48           System.out.println("f2 hashCode = " + f2.hashCode());
49           System.out.println(map.get(f2));
50   }
51   /**
52    * Put some content in a JFrame and show it
53    */
54   void doTest()
55   {
56           System.out.println("ab;c;".split(";"));
57     new DecimalFormat("###,###").format((Integer) 1);
58     JFrame main = new JFrame();
59     main.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
60     JMenu menu = new JMenu("testing");
61     menu.setHorizontalAlignment(SwingConstants.RIGHT);
62     main.setContentPane(getVisualPaneContent(menu));
63     main.setJMenuBar(new JMenuBar());
64     main.getJMenuBar().add(menu);
65     main.pack();
66     main.setVisible(true);
67   }
68
69   /**
70    * Builds a cut-down 'Preferences Visual tab' for a minimal test of layout
71    * problems
72  * @param menu 
73    */
74   Container getVisualPaneContent(JMenu menu)
75   {
76     JPanel panel = new JPanel();
77     panel.setPreferredSize(new Dimension(400, 500));
78     panel.setOpaque(true);
79     panel.setLayout(new BorderLayout());
80
81     JPanel firstColumn = new JPanel();
82     firstColumn.setLayout(new GridLayout(10, 1));
83     firstColumn.setBorder(new TitledBorder("column 1"));
84
85     /*
86      * bug 21/08/18:
87      * - checkbox label and text extend outside the enclosing panel in JS
88      */
89     Font font = new Font("Verdana", Font.PLAIN, 11);
90
91     JLabel l1 = new JLabel(getImage("test2.png"));
92     l1.setText("trailing right");
93     l1.setHorizontalTextPosition(SwingConstants.TRAILING);
94     l1.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
95     l1.setHorizontalAlignment(SwingConstants.RIGHT);
96
97     JLabel l2 = new JLabel(getImage("test2.png"));
98     l2.setText("leading left");
99     l2.setFont(font);
100     l2.setHorizontalTextPosition(SwingConstants.LEADING);
101     l2.setHorizontalAlignment(SwingConstants.LEFT);
102
103     JButton b1 = new JButton("right left");
104     b1.setIcon(getImage("test2.png"));
105     b1.setFont(font);
106     b1.setHorizontalTextPosition(SwingConstants.RIGHT);
107     b1.setHorizontalAlignment(SwingConstants.LEFT);
108
109     firstColumn.add(l1);
110     firstColumn.add(l2);
111     firstColumn.add(b1);
112
113     
114     JCheckBox cb3 = new JCheckBox("leading,left-to-right,rt");
115     cb3.setFont(font);
116     cb3.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
117     cb3.setHorizontalTextPosition(SwingConstants.LEADING);
118     cb3.setHorizontalAlignment(SwingConstants.TRAILING);
119
120     JCheckBox cb4 = new JCheckBox("leading,right-to-left");
121     cb4.setFont(font);
122     cb4.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
123     cb4.setHorizontalTextPosition(SwingConstants.LEADING);
124
125     JCheckBox cb5 = new JCheckBox("trailing,left-to-right");
126     cb5.setFont(font);
127     cb5.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
128     cb5.setHorizontalTextPosition(SwingConstants.TRAILING);
129
130     JRadioButton rb1 = new JRadioButton("trailing,right-to-left");
131     rb1.setFont(font);
132     rb1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
133     rb1.setHorizontalTextPosition(SwingConstants.TRAILING);
134
135     JRadioButton rb2 = new JRadioButton("right,left-to-right");
136     rb2.setFont(font);
137     rb2.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
138     rb2.setHorizontalTextPosition(SwingConstants.RIGHT);
139
140     JRadioButton rb3 = new JRadioButton("right,right-to-left");
141     rb3.setFont(font);
142     rb3.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
143     rb3.setHorizontalTextPosition(SwingConstants.RIGHT);
144
145
146     
147     firstColumn.add(cb3);
148     firstColumn.add(cb4);
149     firstColumn.add(cb5);
150     firstColumn.add(rb1);
151     firstColumn.add(rb2);
152     firstColumn.add(rb3);
153     firstColumn.setBounds(200, 20, 200, 500);
154
155     JCheckBoxMenuItem cb3m = new JCheckBoxMenuItem("leading,left-to-right");
156     cb3m.setFont(font);
157     cb3m.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
158     cb3m.setHorizontalTextPosition(SwingConstants.LEADING);
159
160     JCheckBoxMenuItem cb4m = new JCheckBoxMenuItem("leading,right-to-left");
161     cb4m.setFont(font);
162     cb4m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
163     cb4m.setHorizontalTextPosition(SwingConstants.LEADING);
164
165     JCheckBoxMenuItem cb5m = new JCheckBoxMenuItem("trailing,left-to-right");
166     cb5m.setFont(font);
167     cb5m.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
168     cb5m.setHorizontalTextPosition(SwingConstants.TRAILING);
169
170     JCheckBoxMenuItem cb6m = new JCheckBoxMenuItem("trailing,right-to-left");
171     cb6m.setFont(font);
172     cb6m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
173     cb6m.setHorizontalTextPosition(SwingConstants.TRAILING);
174
175     JRadioButtonMenuItem rb1m = new JRadioButtonMenuItem("trailing,right-to-left");
176     rb1m.setFont(font);
177     rb1m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
178     rb1m.setHorizontalTextPosition(SwingConstants.TRAILING);
179
180     JRadioButtonMenuItem rb2m = new JRadioButtonMenuItem("right,left-to-right");
181     rb2m.setFont(font);
182     rb2m.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
183     rb2m.setHorizontalTextPosition(SwingConstants.RIGHT);
184
185     JRadioButtonMenuItem rb3m = new JRadioButtonMenuItem("right,right-to-left");
186     rb3m.setFont(font);
187     rb3m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
188     rb3m.setHorizontalTextPosition(SwingConstants.RIGHT);
189
190     JMenu m1 = new JMenu("left");
191     JMenu m2 = new JMenu("right");
192     menu.add(m1);
193     menu.add(m2);
194     m1.add(cb3m);
195     m2.add(cb4m);
196     m1.add(cb5m);
197     m2.add(cb6m);
198     m2.add(rb1m);
199     m1.add(rb2m);
200     m2.add(rb3m);
201     
202     JPanel theTab = new JPanel();
203     
204     theTab.setLayout(null);
205     theTab.add(firstColumn);
206     panel.add(theTab);
207
208     return panel;
209   }
210
211 private ImageIcon getImage(String name) {
212     ImageIcon icon = new ImageIcon(getClass().getResource(name));
213
214     while(icon.getImageLoadStatus() == MediaTracker.LOADING)
215                 try {
216                         Thread.sleep(10);
217                 } catch (InterruptedException e) {
218                 }
219     return icon;
220 }
221 }