View Javadoc
1   /*
2   Copyright (C) 2014 Chr. Clemens Lee <clemens@kclee.com>.
3   
4   This file is part of JavaNCSS
5   (http://javancss.codehaus.org/).
6   
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public
9   License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11  
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  Lesser General Public License for more details.
16  
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA*/
20  
21  package javancss;
22  
23  import java.awt.*;
24  import java.awt.event.*;
25  import java.net.URI;
26  import java.util.*;
27  import java.text.*;
28  import java.io.*;
29  import java.util.List;
30  
31  import javax.swing.*;
32  
33  /**
34   * Main class used to start JavaNCSS in GUI mode from other
35   * java applications. To start JavaNCSS from the command line,
36   * gui mode or not, class 'Main' is used.
37   *
38   * @author  <a href="http://www.kclee.de/clemens/">Chr. Clemens Lee</a> (<a href="mailto:clemens@kclee.com"><i>clemens@kclee.com</i></a>)
39   * @version $Id$
40   */
41  public class JavancssFrame extends JFrame {
42      public static final String S_PACKAGES = "Packages";
43      public static final String S_CLASSES = "Classes";
44      public static final String S_METHODS = "Methods";
45  
46      private JTextArea _txtPackage;
47      private JTextArea _txtObject;
48      private JTextArea _txtFunction;
49      private JTextArea _txtError;
50  
51      private JTabbedPane _pTabbedPane = null;
52  
53      private Font pFont = new Font("Monospaced", Font.PLAIN, 12);
54  
55      private boolean _bNoError = true;
56  
57      public void save() {
58          File targetDirectory = new File(".");
59          File packagesFile = new File( targetDirectory, "javancss-packages.txt" );
60          File classesFile  = new File( targetDirectory, "javancss-classes.txt" );
61          File methodsFile  = new File( targetDirectory, "javancss-methods.txt" );
62  
63          String sSuccessMessage = "Data appended successfully to the following files:";
64  
65          try {
66              appendFile( packagesFile, _txtPackage.getText() );
67              sSuccessMessage += "\n" + packagesFile;
68          } catch(Exception e) {
69              JOptionPane.showMessageDialog( this, "Could not append to file '" + classesFile + "'.\n" + e, "Error", JOptionPane.ERROR_MESSAGE );
70          }
71  
72          try {
73              appendFile( classesFile, _txtObject.getText() );
74              sSuccessMessage += "\n" + classesFile;
75          } catch(Exception e) {
76              JOptionPane.showMessageDialog( this, "Could not append to file '" + classesFile + "'.\n" + e, "Error", JOptionPane.ERROR_MESSAGE );
77          }
78  
79          try {
80              appendFile( methodsFile, _txtFunction.getText() );
81              sSuccessMessage += "\n" + methodsFile;
82          } catch(Exception e) {
83              JOptionPane.showMessageDialog( this, "Could not append to file '" + methodsFile + "'.\n" + e, "Error", JOptionPane.ERROR_MESSAGE );
84          }
85  
86          JOptionPane.showMessageDialog( this, sSuccessMessage, "Message", JOptionPane.INFORMATION_MESSAGE );
87      }
88  
89      public static void appendFile( File file, String content ) throws IOException
90      {
91          FileWriter writer = new FileWriter( file, true );
92          writer.write( content );
93          writer.close();
94      }
95  
96      public JavancssFrame( List<String> files )
97      {
98          super( "JavaNCSS: " + files );
99  
100         setDefaultCloseOperation( DISPOSE_ON_CLOSE );
101         setIconImage( new ImageIcon( getClass().getClassLoader().getResource( "javancss/javancssframe.gif" ) ).getImage() );
102 
103         createMenuBar();
104 
105         GridBagLayout layout = new GridBagLayout();
106 
107         getContentPane().setLayout(layout);
108         
109         JPanel busyPanel = new JPanel();
110         busyPanel.setLayout( new BorderLayout() );
111         busyPanel.add( new JLabel( new ImageIcon( getClass().getResource("/javancss/busy-squares.gif") ) ) );
112         
113         getContentPane().add(busyPanel);
114 
115         pack();
116         setSize(800, 600);
117         
118         // center the frame on the screen
119         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
120         setLocation( ( screenSize.width - getWidth() ) / 2, ( screenSize.height - getHeight() ) / 2 );
121     }
122 
123     private void createMenuBar()
124     {
125         JMenuBar menubar = new JMenuBar();
126         menubar.add( createFileMenu() );
127         menubar.add( createHelpMenu() );
128 
129         setJMenuBar( menubar );
130     }
131 
132     private JMenu createFileMenu()
133     {
134         JMenu menu = new JMenu( "File" );
135 
136         JMenuItem item = new JMenuItem( "Save" );
137         item.setAccelerator( KeyStroke.getKeyStroke( 'S', InputEvent.CTRL_MASK ) );
138         item.addActionListener( new ActionListener()
139         {
140             public void actionPerformed( ActionEvent event )
141             {
142                 save();
143             }
144         } );
145         menu.add( item );
146 
147         item = new JMenuItem( "Exit" );
148         item.addActionListener( new ActionListener()
149         {
150             public void actionPerformed( ActionEvent event )
151             {
152                 dispose();
153             }
154         } );
155 
156         menu.add( item );
157 
158         return menu;
159     }
160 
161     private JMenu createHelpMenu()
162     {
163         JMenu menu = new JMenu( "Help" );
164 
165         JMenuItem item = new JMenuItem( "Project page...", 'P' );
166         item.addActionListener( new ActionListener()
167         {
168             public void actionPerformed( ActionEvent event )
169             {
170                 try
171                 {
172                     Desktop.getDesktop().browse( new URI( "https://github.com/JavaNCSS/javancss" ) );
173                 }
174                 catch ( Exception e )
175                 {
176                     e.printStackTrace();
177                 }
178             }
179         } );
180 
181         menu.add( item );
182         menu.addSeparator();
183         
184         item = new JMenuItem( "About..." );
185         item.addActionListener( new ActionListener()
186         {
187             public void actionPerformed( ActionEvent e )
188             {
189                 AboutDialog about = new AboutDialog( JavancssFrame.this, "JavaNCSS", getClass().getPackage().getSpecificationVersion(), "Chr. Clemens Lee & co" );
190                 about.dispose();
191             }
192         } );
193         
194         menu.add( item );
195         
196         return menu;
197     }
198 
199     public void showJavancss(Javancss pJavancss_) throws IOException {
200         getContentPane().removeAll();
201         getContentPane().setLayout(new BorderLayout());
202         _bNoError = true;
203         if (pJavancss_.getLastErrorMessage() != null && pJavancss_.getNcss() <= 0) {
204             _bNoError = false;
205             JTextArea txtError = new JTextArea();
206             String sError = "Error in Javancss: " +
207                    pJavancss_.getLastErrorMessage();
208             txtError.setText(sError);
209             JScrollPane jspError = new JScrollPane(txtError);
210             getContentPane().add(jspError, BorderLayout.CENTER);
211         } else {
212             JPanel pPanel = new JPanel(true);
213             pPanel.setLayout(new BorderLayout());
214             _pTabbedPane = new JTabbedPane();
215             _pTabbedPane.setDoubleBuffered(true);
216 
217             _txtPackage = new JTextArea();
218             _txtPackage.setFont(pFont);
219             JScrollPane jspPackage = new JScrollPane(_txtPackage);
220             int inset = 5;
221             jspPackage.setBorder( BorderFactory.
222                                   createEmptyBorder
223                                   ( inset, inset, inset, inset ) );
224             _pTabbedPane.addTab("Packages", null, jspPackage);
225 
226             _txtObject = new JTextArea();
227             _txtObject.setFont(pFont);
228             JScrollPane jspObject = new JScrollPane(_txtObject);
229             jspObject.setBorder( BorderFactory.
230                                   createEmptyBorder
231                                   ( inset, inset, inset, inset ) );
232             _pTabbedPane.addTab("Classes", null, jspObject);
233 
234             _txtFunction = new JTextArea();
235             _txtFunction.setFont(pFont);
236             JScrollPane jspFunction = new JScrollPane(_txtFunction);
237             jspFunction.setBorder( BorderFactory.
238                                   createEmptyBorder
239                                   ( inset, inset, inset, inset ) );
240             _pTabbedPane.addTab("Methods", null, jspFunction);
241 
242             // date and time
243             String sTimeZoneID = System.getProperty("user.timezone");
244             if (sTimeZoneID.equals("CET")) {
245                 sTimeZoneID = "ECT";
246             }
247             TimeZone pTimeZone = TimeZone.getTimeZone(sTimeZoneID);
248 
249             SimpleDateFormat pSimpleDateFormat
250                    = new SimpleDateFormat("EEE, MMM dd, yyyy  HH:mm:ss");//"yyyy.mm.dd e 'at' hh:mm:ss a z");
251             pSimpleDateFormat.setTimeZone(pTimeZone);
252             String sDate = pSimpleDateFormat.format(new Date()) + " " + pTimeZone.getID();
253 
254             StringWriter sw=new StringWriter();
255             pJavancss_.printPackageNcss(sw);
256 
257             _txtPackage.setText(sDate + "\n\n" + sw.toString());
258             
259             sw=new StringWriter();
260             pJavancss_.printObjectNcss(sw);
261             
262             _txtObject.setText(sDate + "\n\n" + sw.toString());
263 
264             sw=new StringWriter();
265             pJavancss_.printFunctionNcss(sw);
266             _txtFunction.setText(sDate + "\n\n" + sw.toString());
267 
268             if (pJavancss_.getLastErrorMessage() != null) {
269                 _txtError = new JTextArea();
270                 String sError = "Errors in Javancss:\n\n" +
271                        pJavancss_.getLastErrorMessage();
272                 _txtError.setText(sError);
273                 JScrollPane jspError = new JScrollPane(_txtError);
274                 jspError.setBorder( BorderFactory.
275                                   createEmptyBorder
276                                   ( inset, inset, inset, inset ) );
277                 getContentPane().add(jspError, BorderLayout.CENTER);
278                 _pTabbedPane.addTab("Errors", null, jspError);
279             }
280 
281             pPanel.add(_pTabbedPane, BorderLayout.CENTER);
282             getContentPane().add(pPanel, BorderLayout.CENTER);
283         }
284 
285         validate();
286         repaint();
287     }
288 
289     public void setSelectedTab(String sTab_) {
290         if (sTab_ == null || sTab_.trim().length() == 0) {
291             throw new IllegalArgumentException();
292         }
293 
294         if (!_bNoError) {
295             return;
296         }
297         if (sTab_.equals(S_METHODS)) {
298             /*_pTabbedPane.setSelectedComponent(_txtFunction);*/
299             _pTabbedPane.setSelectedIndex(2);
300         } else if (sTab_.equals(S_CLASSES)) {
301             /*_pTabbedPane.setSelectedComponent(_txtObject);*/
302             _pTabbedPane.setSelectedIndex(1);
303         } else {
304             /*_pTabbedPane.setSelectedComponent(_txtPackage);*/
305             _pTabbedPane.setSelectedIndex(0);
306         }
307     }
308 
309     private static class AboutDialog extends JDialog
310     {
311         private JLabel labelName = new JLabel();
312         private JLabel labelVersion = new JLabel();
313         private JLabel labelAuthor = new JLabel( "Author:" );
314         private JLabel labelRealAuthor = new JLabel();
315         private JLabel labelDate = new JLabel( "Last change:" );
316         private JLabel labelRealDate = new JLabel();
317 
318         public AboutDialog( Frame parent, String programName, String version, String author)
319         {
320             super( parent, "About", true );
321 
322             labelName.setText( programName );
323             labelName.setFont( labelName.getFont().deriveFont( Font.BOLD, 14 ) );
324             
325             labelVersion.setText( version );
326             labelVersion.setFont( labelVersion.getFont().deriveFont( Font.PLAIN ) );
327             labelRealDate.setText( getDate() );
328             labelRealDate.setFont( labelRealDate.getFont().deriveFont( Font.PLAIN ) );
329             labelRealAuthor.setText( author );
330             labelRealAuthor.setFont( labelRealAuthor.getFont().deriveFont( Font.PLAIN ) );
331 
332             ( ( JPanel ) getContentPane() ).setBorder( BorderFactory.createEmptyBorder( 25, 40, 25, 40 ) );
333 
334             getContentPane().setLayout( new GridLayout( 3, 2, 0, 10 ) );
335 
336             getContentPane().add( labelName );
337             getContentPane().add( labelVersion );
338             getContentPane().add( labelAuthor );
339             getContentPane().add( labelRealAuthor );
340             getContentPane().add( labelDate );
341             getContentPane().add( labelRealDate );
342 
343             pack();
344             
345             // center the dialog relatively to the parent frame
346             Dimension parentSize = parent.getSize();
347             setLocation(parent.getLocation().x + ( parentSize.width - getWidth() ) / 2,
348                         parent.getLocation().y + ( parentSize.height - getHeight() ) / 2 );
349             
350             setVisible( true );
351         }
352 
353         private String getDate() {
354             try
355             {
356                 String classfile = "/" + getClass().getName().replaceAll( "\\.", "/" ) + ".class";
357                 long timestamp = getClass().getResource( classfile ).openConnection().getLastModified();
358                 return DateFormat.getDateTimeInstance().format( new Date(timestamp) );
359             }
360             catch ( IOException e )
361             {
362                 e.printStackTrace();
363             }
364             
365             return null;
366         }
367     }
368 }