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