View Javadoc

1   /*
2    * Copyright 2006 - 2007 Markku Saarela 
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, 
11   * software distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. 
14   * See the License for the specific language governing permissions and limitations under the License. 
15   */
16  package net.sourceforge.jivalo.editor;
17  
18  import java.awt.Dimension;
19  import java.beans.PropertyChangeEvent;
20  import java.beans.PropertyChangeListener;
21  import java.io.BufferedReader;
22  import java.io.File;
23  import java.io.FileNotFoundException;
24  import java.io.FileOutputStream;
25  import java.io.FileReader;
26  import java.io.IOException;
27  import java.io.InvalidClassException;
28  import java.io.InvalidObjectException;
29  import java.io.PrintStream;
30  
31  import javax.swing.JFrame;
32  import javax.swing.JMenu;
33  import javax.swing.JMenuBar;
34  import javax.swing.JMenuItem;
35  import javax.swing.JOptionPane;
36  import javax.swing.JPanel;
37  import javax.swing.SpringLayout;
38  
39  /**
40   * Main class for jIvaloEditor standalone application.
41   * @author <a href="mailto:ivalo@iki.fi">Markku Saarela</a>
42   *
43   */
44  public class JIvaloEdit extends JPanel implements PropertyChangeListener
45  {
46  
47      private static final long serialVersionUID = 1L;
48  
49      private JIvaloEditor editor;
50  
51      private JMenuBar menuBar;
52  
53      private JMenuItem closeMenuItem;
54  
55      private JMenuItem saveMenuItem;
56  
57      private boolean isModified;
58      
59      private File selectedFile;
60      
61      private JIvaloEditorProperties editorProperties;
62  
63      /**
64       * 
65       */
66      public JIvaloEdit()
67      {
68          super();
69          initialize();
70      }
71  
72      private void initialize()
73      {
74          
75          try
76          {
77              this.editorProperties = 
78                  PropertiesUtils.getInstance().getEditorProperties();
79              
80          } catch ( InvalidClassException e )
81          {
82              // TODO Auto-generated catch block
83              e.printStackTrace();
84          } catch ( InvalidObjectException e )
85          {
86              // TODO Auto-generated catch block
87              e.printStackTrace();
88          }
89          
90          SpringLayout layout = new SpringLayout();
91  
92          this.setLayout( layout );
93  
94          this.setPreferredSize( new Dimension( 1240, 900 ) );
95  
96          this.setMinimumSize( new Dimension( 600, 400 ) );
97  
98          this.setMaximumSize( new Dimension( 1600, 1200 ) );
99  
100         //Adjust constraints for the label so it's at (5,5).
101 
102         this.editor = new JIvaloEditor();
103 
104         this.add( this.editor );
105 
106         this.editor.addPropertyChangeListener( "text", this );
107 
108         layout.putConstraint( SpringLayout.WEST, this.editor, 5,
109                 SpringLayout.WEST, this );
110         layout.putConstraint( SpringLayout.NORTH, this.editor, 5,
111                 SpringLayout.NORTH, this );
112         layout.putConstraint( SpringLayout.EAST, this, 5, SpringLayout.EAST,
113                 this.editor );
114         layout.putConstraint( SpringLayout.SOUTH, this, 5, SpringLayout.SOUTH,
115                 this.editor );
116 
117         this.menuBar = new JMenuBar();
118 
119         //      Build the File menu.
120         JMenu menu = new JMenu( "File" );
121         menuBar.add( menu );
122 
123         menu.add( new MenuNewAction( this ) );
124 
125         this.closeMenuItem = new JMenuItem( new MenuCloseAction( this ) );
126         this.closeMenuItem.setEnabled( false );
127 
128         menu.add( this.closeMenuItem );
129 
130         this.saveMenuItem = new JMenuItem( new MenuSaveAsAction( this ) );
131         this.saveMenuItem.setEnabled( false );
132 
133         menu.add( this.saveMenuItem );
134 
135         menu.add( new MenuOpenAction( this ) );
136         menu.addSeparator();
137         menu.add( new MenuExitAction() );
138 
139         resetEditor();
140     }
141 
142     /**
143      * Get's menu bar for editor
144      * @return Menu bar for editor
145      */
146     private JMenuBar getMenuBar()
147     {
148         return this.menuBar;
149     }
150 
151     public void newFile()
152     {
153         isModifiedQuestion();
154         resetEditor();
155     }
156 
157     public void openFile( File file )
158     {
159 
160         this.selectedFile = file;
161         
162         BufferedReader inputStream = null;
163         try
164         {
165             inputStream = new BufferedReader( new FileReader( file ) );
166 
167             StringBuffer sb = new StringBuffer();
168 
169             String l;
170 
171             while ( ( l = inputStream.readLine() ) != null )
172             {
173                 sb.append( l ).append( "\n" );
174             }
175 
176             String fileSuffix = null;
177             
178             if ( file.getName().lastIndexOf( '.' ) > 0 )
179             {
180                 fileSuffix = 
181                     file.getName().substring( file.getName().lastIndexOf( '.' ) + 1 );
182             }
183             
184             this.editor.setText( sb.toString(), fileSuffix );
185 
186             this.closeMenuItem.setEnabled( true );
187             this.saveMenuItem.setEnabled( false );
188             this.isModified = false;
189         } catch ( FileNotFoundException e )
190         {
191             // TODO Auto-generated catch block
192             e.printStackTrace();
193         } catch ( IOException e )
194         {
195             // TODO Auto-generated catch block
196             e.printStackTrace();
197         } finally
198         {
199             if ( inputStream != null )
200             {
201                 try
202                 {
203                     inputStream.close();
204                 } catch ( IOException e )
205                 {
206                     // TODO Auto-generated catch block
207                     e.printStackTrace();
208                 }
209             }
210         }
211     }
212 
213     public void saveFile( File file )
214     {
215         this.selectedFile = file;
216         
217         saveText();
218         
219         this.saveMenuItem.setEnabled( false );
220         this.isModified = false;
221     }
222 
223     public void closeFile()
224     {
225 
226         isModifiedQuestion();
227         resetEditor();
228 
229     }
230 
231     private void isModifiedQuestion()
232     {
233 
234         if ( this.isModified )
235         {
236             int n = JOptionPane.showConfirmDialog( this,
237                     "Text has been modified. Save it?", "Save Text?",
238                     JOptionPane.YES_NO_OPTION );
239 
240             if ( n == JOptionPane.YES_OPTION )
241             {
242                 saveText();
243             }
244         }
245     }
246 
247     private void saveText()
248     {
249         if ( this.selectedFile != null )
250         {
251             FileOutputStream out; // declare a file output object
252             PrintStream p = null; // declare a print stream object
253             
254             try
255             {
256                 // Create a new file output stream
257                 // connected to selectedFile
258                 out = new FileOutputStream( this.selectedFile );
259                 
260                 // Connect print stream to the output stream
261                 p = new PrintStream( out );
262             
263                 p.print( this.editor.getText() );
264                 
265             } catch ( FileNotFoundException e )
266             {
267                 // TODO Auto-generated catch block
268                 e.printStackTrace();
269             } finally
270             {
271                 if ( p != null )
272                 {
273                     p.close();
274                 }
275             }
276 
277 
278         }
279 
280     }
281 
282     private void resetEditor()
283     {
284         if ( this.editorProperties != null )
285         {
286             this.editor.setText( ""
287                     ,  this.editorProperties.getSyntaxHighlightingDefault() );           
288         }
289         else
290         {
291             this.editor.setText( "",  null );           
292         }
293         this.closeMenuItem.setEnabled( false );
294         this.saveMenuItem.setEnabled( false );
295         this.isModified = false;
296     }
297 
298     public void propertyChange( PropertyChangeEvent evt )
299     {
300         if ( "text".equals( evt.getPropertyName() ) )
301         {
302             this.isModified = true;
303             this.closeMenuItem.setEnabled( true );
304             this.saveMenuItem.setEnabled( true );
305         }
306     }
307 
308     /**
309      * Create the GUI and show it.  For thread safety,
310      * this method should be invoked from the
311      * event-dispatching thread.
312      */
313     private static void createAndShowGUI()
314     {
315         //Make sure we have nice window decorations.
316         JFrame.setDefaultLookAndFeelDecorated( true );
317 
318         //Create and set up the window.
319         JFrame frame = new JFrame( "jIvaloEdit" );
320         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
321 
322         //Create and set up the content pane.
323         JIvaloEdit newContentPane = new JIvaloEdit();
324 
325         newContentPane.setOpaque( true ); //content panes must be opaque
326 
327         frame.setContentPane( newContentPane );
328 
329         frame.setJMenuBar( newContentPane.getMenuBar() );
330         //        newContentPane.enableContentSensitiveKeys(frame);
331 
332         //Display the window.
333         frame.pack();
334         // set's location to center
335 
336         java.awt.Dimension dim = frame.getToolkit().getScreenSize();
337 
338         frame.setLocation( dim.width / 2 - frame.getWidth() / 2, dim.height / 2
339                 - frame.getHeight() / 2 );
340 
341         frame.setVisible( true );
342     }
343 
344     public static void main( String[] args )
345     {
346         //Schedule a job for the event-dispatching thread:
347         //creating and showing this application's GUI.
348         javax.swing.SwingUtilities.invokeLater( new Runnable()
349         {
350             public void run()
351             {
352                 createAndShowGUI();
353             }
354         } );
355     }
356 
357 }