Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / utils / test / JalviewJSTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package test;
22 import jalview.gui.JvOptionPane;
23 import jalview.util.JSONUtils;
24
25 import java.awt.BorderLayout;
26 import java.awt.ComponentOrientation;
27 import java.awt.Container;
28 import java.awt.Dimension;
29 import java.awt.Font;
30 import java.awt.GridLayout;
31 import java.awt.MediaTracker;
32 import java.io.File;
33 import java.text.DecimalFormat;
34 import java.util.HashMap;
35 import java.util.Locale;
36 import java.util.Map;
37
38 import javax.swing.ImageIcon;
39 import javax.swing.JButton;
40 import javax.swing.JCheckBox;
41 import javax.swing.JCheckBoxMenuItem;
42 import javax.swing.JFrame;
43 import javax.swing.JLabel;
44 import javax.swing.JMenu;
45 import javax.swing.JMenuBar;
46 import javax.swing.JPanel;
47 import javax.swing.JRadioButton;
48 import javax.swing.JRadioButtonMenuItem;
49 import javax.swing.SwingConstants;
50 import javax.swing.WindowConstants;
51 import javax.swing.border.TitledBorder;
52
53 import org.json.simple.parser.ParseException;
54
55 /**
56  * A class with a main method entry point for ad hoc tests of JalviewJS
57  * behaviour. The J2S transpiler should generate an html entry point for this
58  * class, allowing comparison between Java and Javascript execution.
59  */
60 public class JalviewJSTest extends JPanel
61 {
62   public static void main(String[] args)
63   {
64     new JalviewJSTest().doTest6();
65   }
66   
67   void doTest6()
68   {
69     /*
70      * check for transpiler fix associated with JSONParser yylex.java use of charAt()
71      * instead of codePointAt(); moved here from PDBFTSRestClient
72      */
73
74     String s = "e";
75     char c = 'c';
76     char f = 'f';
77     s += c | f;
78     int x = c & f;
79     int y = 2 & c;
80     int z = c ^ 5;
81     String result = s + x + y + z;
82     System.out.println("Expected " + "e103982102, found " + result);
83     try
84     {
85       Map<String, Object> jsonObj = (Map<String, Object>) JSONUtils
86               .parse("{\"a\":3}");
87       System.out.println(jsonObj);
88     } catch (ParseException e)
89     {
90       e.printStackTrace();
91     }
92   }
93
94   /**
95    * Dialog message truncation
96    */
97   void doTest5()
98   {
99     JFrame main = new JFrame();
100     main.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
101     main.setContentPane(new JPanel());
102     main.setMinimumSize(new Dimension(100,  100));
103     main.pack();
104     main.setVisible(true);
105     
106     /*
107      * like AlignFrame.delete_actionPerformed()
108      * except here it works fine, also in JS
109      */
110     JvOptionPane dialog = JvOptionPane.newOptionDialog(null);
111     Object[] options = new Object[] { "OK", "Cancel" };
112         String msg = "<html>Deleting all sequences will close the alignment window.<br>Confirm deletion or Cancel.";
113         dialog.showDialog(msg, "", JvOptionPane.DEFAULT_OPTION, JvOptionPane.PLAIN_MESSAGE, null, options, options[0]);
114
115   }
116
117   void doTest4() 
118   {
119           Float fl = new Float(0f);
120           System.out.println(fl.isInfinite());
121           System.out.println(fl.isNaN());
122
123           System.out.println(Float.isInfinite(0f));
124           System.out.println(Float.isFinite(0f));
125           System.out.println(Float.isNaN(0f));
126   }
127
128   void doTest3() 
129   {
130             System.out.println("Mungo".toLowerCase(Locale.getDefault()));
131             System.out.println("Mungo".toLowerCase(Locale.ENGLISH));
132     System.out.println("Mungo".toLowerCase(Locale.ROOT));
133   }
134   
135   void doTest2() {
136           Map<File, String> map = new HashMap<>();
137           File f1 = new File("/var/folders/y/xyz");
138           File f2 = new File("/var/folders/y/xyz");
139           map.put(f1,  "hello world");
140           System.out.println("f1.equals(f2) = " + f1.equals(f2));
141           System.out.println("f1 hashCode = " + f1.hashCode());
142           System.out.println("f2 hashCode = " + f2.hashCode());
143           System.out.println(map.get(f2));
144   }
145   
146   /**
147    * Put some content in a JFrame and show it
148    */
149   void doTest1()
150   {
151           System.out.println("ab;c;".split(";"));
152     new DecimalFormat("###,###").format((Integer) 1);
153     JFrame main = new JFrame();
154     main.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
155     JMenu menu = new JMenu("testing");
156     menu.setHorizontalAlignment(SwingConstants.RIGHT);
157     main.setContentPane(getVisualPaneContent(menu));
158     main.setJMenuBar(new JMenuBar());
159     main.getJMenuBar().add(menu);
160     main.pack();
161     main.setVisible(true);
162   }
163
164   /**
165    * Builds a cut-down 'Preferences Visual tab' for a minimal test of layout
166    * problems
167  * @param menu 
168    */
169   Container getVisualPaneContent(JMenu menu)
170   {
171     JPanel panel = new JPanel();
172     panel.setPreferredSize(new Dimension(400, 500));
173     panel.setOpaque(true);
174     panel.setLayout(new BorderLayout());
175
176     JPanel firstColumn = new JPanel();
177     firstColumn.setLayout(new GridLayout(10, 1));
178     firstColumn.setBorder(new TitledBorder("column 1"));
179
180     /*
181      * bug 21/08/18:
182      * - checkbox label and text extend outside the enclosing panel in JS
183      */
184     Font font = new Font("Verdana", Font.PLAIN, 11);
185
186     JLabel l1 = new JLabel(getImage("test2.png"));
187     l1.setText("trailing right");
188     l1.setHorizontalTextPosition(SwingConstants.TRAILING);
189     l1.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
190     l1.setHorizontalAlignment(SwingConstants.RIGHT);
191
192     JLabel l2 = new JLabel(getImage("test2.png"));
193     l2.setText("leading left");
194     l2.setFont(font);
195     l2.setHorizontalTextPosition(SwingConstants.LEADING);
196     l2.setHorizontalAlignment(SwingConstants.LEFT);
197
198     JButton b1 = new JButton("right left");
199     b1.setIcon(getImage("test2.png"));
200     b1.setFont(font);
201     b1.setHorizontalTextPosition(SwingConstants.RIGHT);
202     b1.setHorizontalAlignment(SwingConstants.LEFT);
203
204     firstColumn.add(l1);
205     firstColumn.add(l2);
206     firstColumn.add(b1);
207
208     
209     JCheckBox cb3 = new JCheckBox("leading,left-to-right,rt");
210     cb3.setFont(font);
211     cb3.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
212     cb3.setHorizontalTextPosition(SwingConstants.LEADING);
213     cb3.setHorizontalAlignment(SwingConstants.TRAILING);
214
215     JCheckBox cb4 = new JCheckBox("leading,right-to-left");
216     cb4.setFont(font);
217     cb4.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
218     cb4.setHorizontalTextPosition(SwingConstants.LEADING);
219
220     JCheckBox cb5 = new JCheckBox("trailing,left-to-right");
221     cb5.setFont(font);
222     cb5.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
223     cb5.setHorizontalTextPosition(SwingConstants.TRAILING);
224
225     JRadioButton rb1 = new JRadioButton("trailing,right-to-left");
226     rb1.setFont(font);
227     rb1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
228     rb1.setHorizontalTextPosition(SwingConstants.TRAILING);
229
230     JRadioButton rb2 = new JRadioButton("right,left-to-right");
231     rb2.setFont(font);
232     rb2.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
233     rb2.setHorizontalTextPosition(SwingConstants.RIGHT);
234
235     JRadioButton rb3 = new JRadioButton("right,right-to-left");
236     rb3.setFont(font);
237     rb3.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
238     rb3.setHorizontalTextPosition(SwingConstants.RIGHT);
239
240
241     
242     firstColumn.add(cb3);
243     firstColumn.add(cb4);
244     firstColumn.add(cb5);
245     firstColumn.add(rb1);
246     firstColumn.add(rb2);
247     firstColumn.add(rb3);
248     firstColumn.setBounds(200, 20, 200, 500);
249
250     JCheckBoxMenuItem cb3m = new JCheckBoxMenuItem("leading,left-to-right");
251     cb3m.setFont(font);
252     cb3m.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
253     cb3m.setHorizontalTextPosition(SwingConstants.LEADING);
254
255     JCheckBoxMenuItem cb4m = new JCheckBoxMenuItem("leading,right-to-left");
256     cb4m.setFont(font);
257     cb4m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
258     cb4m.setHorizontalTextPosition(SwingConstants.LEADING);
259
260     JCheckBoxMenuItem cb5m = new JCheckBoxMenuItem("trailing,left-to-right");
261     cb5m.setFont(font);
262     cb5m.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
263     cb5m.setHorizontalTextPosition(SwingConstants.TRAILING);
264
265     JCheckBoxMenuItem cb6m = new JCheckBoxMenuItem("trailing,right-to-left");
266     cb6m.setFont(font);
267     cb6m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
268     cb6m.setHorizontalTextPosition(SwingConstants.TRAILING);
269
270     JRadioButtonMenuItem rb1m = new JRadioButtonMenuItem("trailing,right-to-left");
271     rb1m.setFont(font);
272     rb1m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
273     rb1m.setHorizontalTextPosition(SwingConstants.TRAILING);
274
275     JRadioButtonMenuItem rb2m = new JRadioButtonMenuItem("right,left-to-right");
276     rb2m.setFont(font);
277     rb2m.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
278     rb2m.setHorizontalTextPosition(SwingConstants.RIGHT);
279
280     JRadioButtonMenuItem rb3m = new JRadioButtonMenuItem("right,right-to-left");
281     rb3m.setFont(font);
282     rb3m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
283     rb3m.setHorizontalTextPosition(SwingConstants.RIGHT);
284
285     JMenu m1 = new JMenu("left");
286     JMenu m2 = new JMenu("right");
287     menu.add(m1);
288     menu.add(m2);
289     m1.add(cb3m);
290     m2.add(cb4m);
291     m1.add(cb5m);
292     m2.add(cb6m);
293     m2.add(rb1m);
294     m1.add(rb2m);
295     m2.add(rb3m);
296     
297     JPanel theTab = new JPanel();
298     
299     theTab.setLayout(null);
300     theTab.add(firstColumn);
301     panel.add(theTab);
302
303     return panel;
304   }
305
306 private ImageIcon getImage(String name) {
307     ImageIcon icon = new ImageIcon(getClass().getResource(name));
308
309     while(icon.getImageLoadStatus() == MediaTracker.LOADING)
310     {
311       try {
312         Thread.sleep(10);
313       } catch (InterruptedException e) {
314       }
315     }
316     return icon;
317 }
318 }