View Javadoc

1   /*
2    *                Doelan development code
3    *
4    * This code may be freely distributed and modified under the
5    * terms of the GNU General Public Licence.  This should
6    * be distributed with the code. If you do not have a copy,
7    * see:
8    *
9    *      http://www.gnu.org/copyleft/gpl.txt
10   *
11   * Copyright (c) 2004-2005 ENS Microarray Platform
12   * Copyright for this code is held jointly by the individual
13   * authors.  These should be listed in @author doc comments.
14   *
15   * For more information on the Doelan project and its aims,
16   * or to join the Doelan mailing list, visit the home page
17   * at:
18   *
19   *      http://www.transcriptome.ens.fr/doelan
20   */
21  
22  package fr.ens.transcriptome.doelan.gui;
23  
24  import java.applet.Applet;
25  import java.awt.Color;
26  import java.awt.Container;
27  import java.awt.Toolkit;
28  import java.lang.reflect.InvocationTargetException;
29  import java.net.URL;
30  
31  import javax.swing.JApplet;
32  import javax.swing.JFrame;
33  import javax.swing.JLabel;
34  import javax.swing.JOptionPane;
35  import javax.swing.LookAndFeel;
36  import javax.swing.SwingUtilities;
37  import javax.swing.ToolTipManager;
38  import javax.swing.UIManager;
39  import javax.swing.UnsupportedLookAndFeelException;
40  
41  import org.apache.log4j.Logger;
42  
43  import fr.ens.transcriptome.doelan.Core;
44  import fr.ens.transcriptome.doelan.DoelanRegistery;
45  import fr.ens.transcriptome.nividic.util.NividicUtils;
46  import fr.ens.transcriptome.nividic.util.SystemUtils;
47  
48  /***
49   * Common GUI for applet and normal application.
50   * @author Laurent jourdren TODO modify the about information to use Globals
51   *               TODO update directory and file management
52   */
53  public class CommonWindow {
54  
55    private static final String WINDOWS_PLAF = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
56    //private static final String METAL_PLAF =
57    // "javax.swing.plaf.metal.MetalLookAndFeel";
58    private static final String GTK_PLAF = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
59  
60    //For log system
61    private static Logger log = Logger.getLogger(CommonWindow.class);
62  
63    private static CommonWindow cw;
64  
65    private Container container;
66    private Core core = Core.getCore();
67  
68    private JLabel statusBar;
69  
70    private MainTabWidget mainTab;
71    private TestSuiteTabWidget testSuiteTab;
72    private ReportTabWidget reportTab;
73  
74    private javax.swing.JTabbedPane tabbedPane;
75  
76    //
77    // Getters
78    //
79  
80    /***
81     * Get the container
82     * @return The container
83     */
84    private Container getContainer() {
85      return container;
86    }
87  
88    /***
89     * Set the unique command window object.
90     * @return the unique command window
91     */
92    private static CommonWindow getCommonWindow() {
93      return cw;
94    }
95  
96    //
97    // Setters
98    //
99  
100   /***
101    * Set the Graphical Container.
102    * @param container The container to set
103    */
104   public void setContainer(final Container container) {
105     this.container = container;
106   }
107 
108   /***
109    * Set the unique command window object.
110    */
111   private void setCommonWindow(final CommonWindow cw) {
112     CommonWindow.cw = cw;
113   }
114 
115   //
116   // Other methods
117   //
118 
119   /***
120    * Find the best look and feel for the application.
121    */
122   private void setUILookAndFeel() {
123 
124     if (SystemUtils.isWindowsSystem())
125       setLookAndFeel(WINDOWS_PLAF);
126     else if (SystemUtils.isMacOsX())
127       NividicUtils.nop();
128     else if (System.getProperty("os.name").startsWith("Linux")
129         && System.getProperty("java.version").startsWith("1.5."))
130       setLookAndFeel(GTK_PLAF);
131     /*
132      * else {
133      * //setLookAndFeel("com.jgoodies.plaf.plastic.Plastic3DLookAndFeel"); //
134      * jgoodies /* Plastic3DLookAndFeel laf = new Plastic3DLookAndFeel();
135      * Plastic3DLookAndFeel
136      * .setTabStyle(Plastic3DLookAndFeel.TAB_STYLE_METAL_VALUE);
137      * Plastic3DLookAndFeel.setHighContrastFocusColorsEnabled(true);
138      * Plastic3DLookAndFeel .setMyCurrentTheme(new
139      * com.jgoodies.plaf.plastic.theme.Silver()); setLookAndFeel(laf); }
140      */
141 
142     SwingUtilities.updateComponentTreeUI(getContainer());
143   }
144 
145   /***
146    * Set a look and feel for the application.
147    * @param uiClassName Class name of the look and feel
148    */
149   private static void setLookAndFeel(final String uiClassName) {
150 
151     if (uiClassName == null)
152       return;
153 
154     if (!javax.swing.SwingUtilities.isEventDispatchThread()) {
155       try {
156         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
157           public void run() {
158             setLookAndFeel(uiClassName);
159           }
160         });
161       } catch (InterruptedException e) {
162         log.error("Error when trying to use the Event dispatch thread.");
163       } catch (InvocationTargetException e) {
164         log.error("Error when trying to use the Event dispatch thread.");
165       }
166 
167     } else {
168 
169       try {
170 
171         //UIManager.setLookAndFeel(uiClassName);
172 
173         Class c = CommonWindow.class.getClassLoader().loadClass(uiClassName);
174         LookAndFeel laf = (LookAndFeel) c.newInstance();
175 
176         UIManager.setLookAndFeel(laf);
177 
178       } catch (ClassNotFoundException e) {
179         log.error("PLAF error, class not found: " + uiClassName);
180         return;
181       } catch (InstantiationException e) {
182         log.error("PLAF error, instantiation exception: " + uiClassName);
183         return;
184       } catch (IllegalAccessException e) {
185         log.error("PLAF error, illegal access: " + uiClassName);
186         return;
187       } catch (UnsupportedLookAndFeelException e) {
188         log.error("PLAF error, unssopported look and feel: " + uiClassName);
189         return;
190       }
191 
192     }
193 
194   }
195 
196   /***
197    * Set a look and feel for the application.
198    * @param lookAndFeel The look and feel
199    */
200   /*
201    * private void setLookAndFeel(final LookAndFeel lookAndFeel) { if
202    * (lookAndFeel == null) return; try { UIManager.setLookAndFeel(lookAndFeel); }
203    * catch (UnsupportedLookAndFeelException e) { final String defaultPLAF =
204    * UIManager.getSystemLookAndFeelClassName(); if
205    * (lookAndFeel.equals(defaultPLAF)) return; log.error("Invalid look and feel: " +
206    * lookAndFeel); setLookAndFeel(defaultPLAF); }
207    * SwingUtilities.updateComponentTreeUI(getContainer()); }
208    */
209 
210   /***
211    * Show a message to the user.
212    * @param message Message to show
213    * @param error true if message is an error
214    */
215   public static void showMessage(final String message, final boolean error) {
216 
217     if (DoelanRegistery.isAppletMode()) {
218       if (error)
219         getCommonWindow().statusBar.setForeground(Color.RED);
220       else
221         getCommonWindow().statusBar.setForeground(Color.BLACK);
222       getCommonWindow().statusBar.setText(message);
223       Toolkit.getDefaultToolkit().beep();
224     } else
225       JOptionPane.showMessageDialog(getCommonWindow().getContainer(), message,
226           error ? "Error" : "Message", error ? JOptionPane.ERROR_MESSAGE
227               : JOptionPane.WARNING_MESSAGE);
228   }
229 
230   /***
231    * Set the message for the status bar.
232    * @param message Message to set
233    */
234   public static void showStatusBarMessage(final String message) {
235     getCommonWindow().statusBar.setText(message);
236   }
237 
238   /***
239    * Initiatilze the application.
240    */
241   public void init() {
242 
243     setUILookAndFeel();
244 
245     statusBar = new JLabel();
246     tabbedPane = new javax.swing.JTabbedPane();
247 
248     showStatusBarMessage("No test suite loaded.");
249     getContainer().add(statusBar, java.awt.BorderLayout.SOUTH);
250 
251     mainTab = new MainTabWidget();
252     testSuiteTab = new TestSuiteTabWidget();
253     reportTab = new ReportTabWidget(statusBar);
254 
255     tabbedPane.addTab("Main", mainTab);
256     tabbedPane.addTab("TestSuite", testSuiteTab);
257     tabbedPane.addTab("Report", reportTab);
258 
259     getContainer().add(tabbedPane, java.awt.BorderLayout.CENTER);
260     this.core.setMainTab(mainTab);
261     this.core.setTable(testSuiteTab.getTable());
262     this.core.setReport(reportTab);
263     this.core.setStatusBar(statusBar);
264 
265     mainTab.setWorking(false);
266 
267     //setUILookAndFeel();
268   }
269 
270   //
271   // Constructors
272   //
273 
274   private CommonWindow() {
275 
276     setCommonWindow(this);
277     container = null;
278   }
279 
280   /***
281    * Public constructor for applet mode.
282    * @param applet The main applet
283    * @param testSuiteListURL URL of the testsuite list
284    */
285   public CommonWindow(final Applet applet, final URL testSuiteListURL) {
286     this(((JApplet) applet).getContentPane());
287 
288     Core.getCore().setApplet(applet);
289     Core.getCore().setTestSuiteListURL(testSuiteListURL);
290   }
291 
292   /***
293    * Public constructor for standalone mode.
294    * @param frame The main frame
295    * @param testSuiteListURL URL of the testsuite list
296    */
297   public CommonWindow(final JFrame frame, final URL testSuiteListURL) {
298     this(frame.getContentPane());
299 
300     DoelanRegistery.setAppletMode(false);
301     Core.getCore().setTestSuiteListURL(testSuiteListURL);
302   }
303 
304   private CommonWindow(final Container container) {
305     this();
306 
307     this.container = container;
308 
309     if (DoelanRegistery.isAppletMode()) {
310       log.info("Mode: Applet");
311       ToolTipManager.sharedInstance().setEnabled(false);
312     } else
313       log.info("Mode: Standalone application");
314 
315   }
316 
317 }