View Javadoc

1   /*
2   Copyright (C) 2000 Chr. Clemens Lee <clemens@kclee.com>.
3   
4   This file is part of JavaNCSS
5   (http://www.kclee.com/clemens/java/javancss/).
6   
7   JavaNCSS is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2, or (at your option) any
10  later version.
11  
12  JavaNCSS is distributed in the hope that it will be useful, but WITHOUT
13  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  for more details.
16  
17  You should have received a copy of the GNU General Public License
18  along with JavaNCSS; see the file COPYING.  If not, write to
19  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  Boston, MA 02111-1307, USA.  */
21  
22  package javancss;
23  
24  import java.awt.*;
25  import java.awt.event.*;
26  import java.util.*;
27  import java.text.*;
28  import java.io.*;
29  
30  import javax.swing.*;
31  import javax.swing.border.*;
32  
33  import ccl.swing.AboutDialog;
34  import ccl.swing.AnimationPanel;
35  import ccl.swing.AutoGridBagLayout;
36  import ccl.swing.MainJFrame;
37  import ccl.swing.SwingUtil;
38  import ccl.util.FileUtil;
39  import ccl.util.Init;
40  import ccl.util.Util;
41  
42  /**
43   * Main class used to start JavaNCSS in GUI mode from other
44   * java applications. To start JavaNCSS from the command line,
45   * gui mode or not, class 'Main' is used.
46   *
47   * @author  <a href="http://www.kclee.com/clemens/">Chr. Clemens Lee</a> (<a href="mailto:clemens@kclee.com"><i>clemens@kclee.com</i></a>)
48   * @version $Id: JavancssFrame.java 121 2009-01-17 22:19:45Z hboutemy $
49   */
50  public class JavancssFrame extends MainJFrame {
51      public static final String S_PACKAGES = "Packages";
52      public static final String S_CLASSES = "Classes";
53      public static final String S_METHODS = "Methods";
54  
55      private static final String S_MN_F_SAVE = "Save";
56  
57      private int _oldThreadPriority = -1;
58  
59      private AnimationPanel _pAnimationPanel = null;
60  
61      private JTextArea _txtPackage;
62      private JTextArea _txtObject;
63      private JTextArea _txtFunction;
64      private JTextArea _txtError;
65  
66      private JTabbedPane _pTabbedPane = null;
67  
68      private Font pFont = new Font("Monospaced", Font.PLAIN, 12);
69  
70      private boolean _bNoError = true;
71  
72      private String _sProjectName = null;
73      private String _sProjectPath = null;
74  
75      private Init _pInit = null;
76  
77      public void save() {
78          String sFullProjectName = FileUtil.concatPath
79                 (_sProjectPath, _sProjectName.toLowerCase());
80          String sPackagesFullFileName = sFullProjectName +
81                 ".packages.txt";
82          String sClassesFullFileName = sFullProjectName +
83                 ".classes.txt";
84          String sMethodsFullFileName = sFullProjectName +
85                 ".methods.txt";
86  
87          String sSuccessMessage = "Data appended successfully to the following files:";
88  
89          try {
90              FileUtil.appendFile(sPackagesFullFileName,
91                                  _txtPackage.getText());
92              sSuccessMessage += "\n" + sPackagesFullFileName;
93          } catch(Exception ePackages) {
94              SwingUtil.showMessage(this, "Error: could not append to file '" +
95                                  sPackagesFullFileName + "'.\n" + ePackages);
96          }
97  
98          try {
99              FileUtil.appendFile(sClassesFullFileName,
100                                 _txtObject.getText());
101                         sSuccessMessage += "\n" + sClassesFullFileName;
102         } catch(Exception eClasses) {
103             SwingUtil.showMessage(this, "Error: could not append to file '" +
104                                 sClassesFullFileName + "'.\n" + eClasses);
105         }
106 
107         try {
108             FileUtil.appendFile(sMethodsFullFileName,
109                                 _txtFunction.getText());
110             sSuccessMessage += "\n" + sMethodsFullFileName;
111         } catch(Exception eMethods) {
112             SwingUtil.showMessage(this, "Error: could not append to file '" +
113                                 sMethodsFullFileName + "'.\n" + eMethods);
114         }
115 
116         SwingUtil.showMessage(this, sSuccessMessage);
117     }
118 
119     private void _setMenuBar() {
120         Vector vMenus = new Vector();
121 
122         Vector vFileMenu = new Vector();
123         Vector vHelpMenu = new Vector();
124 
125         vFileMenu.addElement("File");
126         vFileMenu.addElement(S_MN_F_SAVE);
127         vFileMenu.addElement("Exit");
128 
129         vHelpMenu.addElement("Help");
130         vHelpMenu.addElement("&Contents...");
131         vHelpMenu.addElement("---");
132         vHelpMenu.addElement("About...");
133 
134         vMenus.addElement(vFileMenu);
135         vMenus.addElement(vHelpMenu);
136 
137         setMenuBar(vMenus);
138     }
139 
140     /**
141      * Returns init object provided with constructor.
142      */
143     public Init getInit() {
144         return _pInit;
145     }
146 
147     public JavancssFrame(Init pInit_) {
148         super( "JavaNCSS: " + pInit_.getFileName() );
149 
150         _pInit = pInit_;
151         getInit().setAuthor( "Chr. Clemens Lee" );
152 
153         super.setBackground( _pInit.getBackground() );
154 
155         _sProjectName = pInit_.getFileName();
156         _sProjectPath = pInit_.getFilePath();
157         if (Util.isEmpty(_sProjectName)) {
158             _sProjectName = pInit_.getApplicationName();
159             _sProjectPath = pInit_.getApplicationPath();
160         }
161 
162         _setMenuBar();
163 
164         _bAboutSelected = false;
165 
166         AutoGridBagLayout pAutoGridBagLayout = new AutoGridBagLayout();
167 
168         getContentPane().setLayout(pAutoGridBagLayout);
169 
170         Image pImage = Toolkit.getDefaultToolkit().
171                getImage( SwingUtil.createCCLBorder().getClass().getResource
172                          ( "anim_recycle_brown.gif" ) );
173         _pAnimationPanel = new AnimationPanel( pImage, 350 );
174 
175         JPanel pPanel = new JPanel();
176         pPanel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
177         pPanel.add(_pAnimationPanel, BorderLayout.CENTER);
178 
179         getContentPane().add(pPanel);
180 
181 
182         pack();
183         setSize(640, 480);
184         SwingUtil.centerComponent(this);
185     }
186 
187     public void showJavancss(Javancss pJavancss_) {
188         _bStop = false;
189         _bSave = false;
190         if (_oldThreadPriority != -1) {
191             Thread.currentThread().setPriority(_oldThreadPriority);
192             _pAnimationPanel.stop();
193         }
194         getContentPane().removeAll();
195         getContentPane().setLayout(new BorderLayout());
196         _bNoError = true;
197         if (pJavancss_.getLastErrorMessage() != null && pJavancss_.getNcss() <= 0) {
198             _bNoError = false;
199             JTextArea txtError = new JTextArea();
200             String sError = "Error in Javancss: " +
201                    pJavancss_.getLastErrorMessage();
202             txtError.setText(sError);
203             JScrollPane jspError = new JScrollPane(txtError);
204             getContentPane().add(jspError, BorderLayout.CENTER);
205         } else {
206             Util.debug("JavancssFrame.showJavancss(..).NOERROR");
207             JPanel pPanel = new JPanel(true);
208             pPanel.setLayout(new BorderLayout());
209             _pTabbedPane = new JTabbedPane();
210             _pTabbedPane.setDoubleBuffered(true);
211 
212             _txtPackage = new JTextArea();
213             _txtPackage.setFont(pFont);
214             JScrollPane jspPackage = new JScrollPane(_txtPackage);
215             int inset = 5;
216             jspPackage.setBorder( BorderFactory.
217                                   createEmptyBorder
218                                   ( inset, inset, inset, inset ) );
219             _pTabbedPane.addTab("Packages", null, jspPackage);
220 
221             _txtObject = new JTextArea();
222             _txtObject.setFont(pFont);
223             JScrollPane jspObject = new JScrollPane(_txtObject);
224             jspObject.setBorder( BorderFactory.
225                                   createEmptyBorder
226                                   ( inset, inset, inset, inset ) );
227             _pTabbedPane.addTab("Classes", null, jspObject);
228 
229             _txtFunction = new JTextArea();
230             _txtFunction.setFont(pFont);
231             JScrollPane jspFunction = new JScrollPane(_txtFunction);
232             jspFunction.setBorder( BorderFactory.
233                                   createEmptyBorder
234                                   ( inset, inset, inset, inset ) );
235             _pTabbedPane.addTab("Methods", null, jspFunction);
236 
237             // date and time
238             String sTimeZoneID = System.getProperty("user.timezone");
239             if (sTimeZoneID.equals("CET")) {
240                 sTimeZoneID = "ECT";
241             }
242             TimeZone pTimeZone = TimeZone.getTimeZone(sTimeZoneID);
243             Util.debug("JavancssFrame.showJavancss(..).pTimeZone.getID(): " + pTimeZone.getID());
244 
245             SimpleDateFormat pSimpleDateFormat
246                    = new SimpleDateFormat("EEE, MMM dd, yyyy  HH:mm:ss");//"yyyy.mm.dd e 'at' hh:mm:ss a z");
247             pSimpleDateFormat.setTimeZone(pTimeZone);
248             String sDate = pSimpleDateFormat.format(new Date()) + " " + pTimeZone.getID();
249 
250             _txtPackage.setText(sDate + "\n\n" + pJavancss_.printPackageNcss());
251             _txtObject.setText(sDate + "\n\n" + pJavancss_.printObjectNcss());
252             _txtFunction.setText(sDate + "\n\n" + pJavancss_.printFunctionNcss());
253 
254             if (pJavancss_.getLastErrorMessage() != null) {
255                 _txtError = new JTextArea();
256                 String sError = "Errors in Javancss:\n\n" +
257                        pJavancss_.getLastErrorMessage();
258                 _txtError.setText(sError);
259                 JScrollPane jspError = new JScrollPane(_txtError);
260                 jspError.setBorder( BorderFactory.
261                                   createEmptyBorder
262                                   ( inset, inset, inset, inset ) );
263                 getContentPane().add(jspError, BorderLayout.CENTER);
264                 _pTabbedPane.addTab("Errors", null, jspError);
265             }
266 
267             pPanel.add(_pTabbedPane, BorderLayout.CENTER);
268             getContentPane().add(pPanel, BorderLayout.CENTER);
269         }
270 
271         validate();
272         repaint();
273     }
274 
275     private boolean _bStop = false;
276     private boolean _bSave = false;
277 
278     public void run() {
279         _bSave = false;
280         while(!_bStop) {
281             if (_bSave) {
282                 save();
283                 _bSave = false;
284             }
285 
286             if (isExitSet()) {
287                 exit();
288                 _bStop = true;
289                 break;
290             }
291 
292             if (_bAboutSelected) {
293                 _bAboutSelected = false;
294                 AboutDialog dlgAbout = new AboutDialog
295                     ( this,
296                       getInit().getAuthor(),
297                       javancss.Main.S_RCS_HEADER );
298                 dlgAbout.dispose();
299                 requestFocus();
300             }
301 
302             try {
303                 Thread.sleep(500);
304             } catch (InterruptedException e) {
305             }
306         }
307     }
308 
309     public void setVisible(boolean bVisible_) {
310         if (bVisible_) {
311             _oldThreadPriority = Thread.currentThread().getPriority();
312             _pAnimationPanel.start();
313             Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
314         } else {
315             _pAnimationPanel.stop();
316         }
317 
318         super.setVisible(bVisible_);
319     }
320 
321     public void setSelectedTab(String sTab_) {
322         Util.panicIf(Util.isEmpty(sTab_));
323 
324         if (!_bNoError) {
325             return;
326         }
327         if (sTab_.equals(S_METHODS)) {
328             /*_pTabbedPane.setSelectedComponent(_txtFunction);*/
329             _pTabbedPane.setSelectedIndex(2);
330         } else if (sTab_.equals(S_CLASSES)) {
331             /*_pTabbedPane.setSelectedComponent(_txtObject);*/
332             _pTabbedPane.setSelectedIndex(1);
333         } else {
334             /*_pTabbedPane.setSelectedComponent(_txtPackage);*/
335             _pTabbedPane.setSelectedIndex(0);
336         }
337     }
338 
339     private boolean _bAboutSelected = false;
340 
341     public void actionPerformed(ActionEvent pActionEvent_) {
342         Util.debug("JavancssFrame.actionPerformed(..).1");
343         Object oSource = pActionEvent_.getSource();
344         if (oSource instanceof JMenuItem) {
345             String sMenuItem = ((JMenuItem)oSource).getText();
346             if (sMenuItem.equals("Beenden") || sMenuItem.equals("Exit")) {
347                 processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
348             } else if (sMenuItem.equals(S_MN_F_SAVE)) {
349                 _bSave = true;
350             } else if (sMenuItem.equals("Info...") || sMenuItem.equals("About...") ||
351                        sMenuItem.equals("Info") || sMenuItem.equals("About"))
352             {
353                 _bAboutSelected = true;
354             } else if (sMenuItem.equals("Inhalt...") || sMenuItem.equals("Contents...") ||
355                        sMenuItem.equals("Inhalt") || sMenuItem.equals("Contents"))
356             {
357                 String sStartURL = FileUtil.concatPath(FileUtil.getPackagePath("javancss"),
358                                                        S_DOC_DIR) + File.separator +
359                        "index.html";
360                 if (Util.isEmpty(sStartURL)) {
361                     return;
362                 }
363                 sStartURL = sStartURL.replace('\\', '/');
364                 if (sStartURL.charAt(0) != '/') {
365                     sStartURL = "/" + sStartURL;
366                 }
367                 sStartURL = "file:" + sStartURL;
368                 Util.debug("JavancssFrame.actionPerformed(): sStartURL: " + sStartURL);
369                 /*try {
370                     URL urlHelpDocument = new URL(sStartURL);
371                     //HtmlViewer pHtmlViewer = new HtmlViewer(urlHelpDocument);
372                 } catch(Exception pException) {
373                     Util.debug("JavancssFrame.actionPerformed(..).pException: " + pException);
374                 }*/
375             }
376         }
377     }
378 }