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