001    /*
002    Copyright (C) 2000 Chr. Clemens Lee <clemens@kclee.com>.
003    
004    This file is part of JavaNCSS
005    (http://www.kclee.com/clemens/java/javancss/).
006    
007    JavaNCSS is free software; you can redistribute it and/or modify it
008    under the terms of the GNU General Public License as published by the
009    Free Software Foundation; either version 2, or (at your option) any
010    later version.
011    
012    JavaNCSS is distributed in the hope that it will be useful, but WITHOUT
013    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
015    for more details.
016    
017    You should have received a copy of the GNU General Public License
018    along with JavaNCSS; see the file COPYING.  If not, write to
019    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
020    Boston, MA 02111-1307, USA.  */
021    
022    package javancss;
023    
024    import java.awt.*;
025    import java.awt.event.*;
026    import java.util.*;
027    import java.text.*;
028    import java.io.*;
029    
030    import javax.swing.*;
031    import javax.swing.border.*;
032    
033    import ccl.swing.AboutDialog;
034    import ccl.swing.AnimationPanel;
035    import ccl.swing.AutoGridBagLayout;
036    import ccl.swing.MainJFrame;
037    import ccl.swing.SwingUtil;
038    import ccl.util.FileUtil;
039    import ccl.util.Init;
040    import ccl.util.Util;
041    
042    /**
043     * Main class used to start JavaNCSS in GUI mode from other
044     * java applications. To start JavaNCSS from the command line,
045     * gui mode or not, class 'Main' is used.
046     *
047     * @author  <a href="http://www.kclee.com/clemens/">Chr. Clemens Lee</a> (<a href="mailto:clemens@kclee.com"><i>clemens@kclee.com</i></a>)
048     * @version $Id: JavancssFrame.java 121 2009-01-17 22:19:45Z hboutemy $
049     */
050    public class JavancssFrame extends MainJFrame {
051        public static final String S_PACKAGES = "Packages";
052        public static final String S_CLASSES = "Classes";
053        public static final String S_METHODS = "Methods";
054    
055        private static final String S_MN_F_SAVE = "Save";
056    
057        private int _oldThreadPriority = -1;
058    
059        private AnimationPanel _pAnimationPanel = null;
060    
061        private JTextArea _txtPackage;
062        private JTextArea _txtObject;
063        private JTextArea _txtFunction;
064        private JTextArea _txtError;
065    
066        private JTabbedPane _pTabbedPane = null;
067    
068        private Font pFont = new Font("Monospaced", Font.PLAIN, 12);
069    
070        private boolean _bNoError = true;
071    
072        private String _sProjectName = null;
073        private String _sProjectPath = null;
074    
075        private Init _pInit = null;
076    
077        public void save() {
078            String sFullProjectName = FileUtil.concatPath
079                   (_sProjectPath, _sProjectName.toLowerCase());
080            String sPackagesFullFileName = sFullProjectName +
081                   ".packages.txt";
082            String sClassesFullFileName = sFullProjectName +
083                   ".classes.txt";
084            String sMethodsFullFileName = sFullProjectName +
085                   ".methods.txt";
086    
087            String sSuccessMessage = "Data appended successfully to the following files:";
088    
089            try {
090                FileUtil.appendFile(sPackagesFullFileName,
091                                    _txtPackage.getText());
092                sSuccessMessage += "\n" + sPackagesFullFileName;
093            } catch(Exception ePackages) {
094                SwingUtil.showMessage(this, "Error: could not append to file '" +
095                                    sPackagesFullFileName + "'.\n" + ePackages);
096            }
097    
098            try {
099                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    }