View Javadoc

1   package org.xrn.gui;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.ActionListener;
5   import java.io.File;
6   import java.io.FileInputStream;
7   import java.io.IOException;
8   
9   import javax.swing.JFileChooser;
10  import javax.swing.JMenu;
11  import javax.swing.JMenuItem;
12  import javax.xml.bind.JAXBContext;
13  import javax.xml.bind.JAXBException;
14  import javax.xml.bind.UnmarshalException;
15  import javax.xml.bind.Unmarshaller;
16  
17  import org.xrn.xsd2java.impl.ReleaseNotesImpl;
18  
19  /***
20   * 
21   * Menu item "Release Notes" of the menu bar
22   * @author Carsten Maneg
23   * Date Nov 7, 2004
24   * Time 5:52:25 PM
25   */
26  public class XRNMenu extends JMenu implements ActionListener{
27      
28      private static final String MENUNAME = "Release Notes";
29      private static final String NEWNOTES = "New Release Notes";
30  
31      private JMenuItem openMenuItem;
32      private JMenuItem newMenuItem;
33    
34      private ReleaseNotesWindow releaseNotesWindow;
35      private JFileChooser fileChooserDialog;
36      private XRNMainWindow mainWindow;
37      private String filename;
38      private String filePath;
39      private File choosenFile;
40  
41      /***
42       * Constructor - The main menu
43       * @param mainWindow
44       */
45      public XRNMenu( XRNMainWindow mainWindow ){
46          super( MENUNAME );
47          this.mainWindow = mainWindow;
48  
49          newMenuItem = new JMenuItem( " New " );
50          newMenuItem.addActionListener( this );
51          newMenuItem.setEnabled( true );
52          add( newMenuItem );
53  
54          openMenuItem = new JMenuItem( " Open " );
55          openMenuItem.addActionListener( this );
56          add( openMenuItem );
57      }
58      
59      /***
60       * 
61       */
62      public void actionPerformed( ActionEvent e ){
63        
64        int status = -1;
65        ReleaseNotesImpl releaseNotes = null;
66        
67        if ( e.getSource( ) == openMenuItem ){
68            
69  //    	openDialog = new ReleaseNotesWindow( mainWindow );
70  //    	mainWindow.setBereich( openDialog, true );
71            fileChooserDialog = new JFileChooser( XRNConstants.DEFAULT_PROJECT_DIR );
72            fileChooserDialog.setFileSelectionMode( JFileChooser.FILES_ONLY );
73            XMLFileFilter fileFilter = new XMLFileFilter();
74            fileFilter.addExtension( "xml" );
75            fileChooserDialog.setFileFilter( fileFilter );
76            status = fileChooserDialog.showOpenDialog( this );
77            choosenFile = fileChooserDialog.getSelectedFile();
78            if( (choosenFile != null) && (status == JFileChooser.APPROVE_OPTION) ){
79                
80                filePath = choosenFile.getPath();
81                releaseNotes = readReleaseNotes( filePath );
82                if( releaseNotes != null ){
83                    mainWindow.getInformationBar().setStatus( filePath );
84                    releaseNotesWindow = new ReleaseNotesWindow( mainWindow, XRNConstants.EDIT_NOTES, choosenFile );
85                    mainWindow.setBereich( releaseNotesWindow, true );
86                    releaseNotesWindow.setReleaseNotes( releaseNotes );
87                    releaseNotesWindow.getCreateHTMLButton().setEnabled( true );
88                }
89            }
90        }
91        if( e.getSource() == newMenuItem ){
92           	releaseNotesWindow = new ReleaseNotesWindow( mainWindow, XRNConstants.NEW_NOTES, null );
93              mainWindow.getInformationBar().setStatus( NEWNOTES );
94          	mainWindow.setBereich( releaseNotesWindow, true );
95        }
96      }
97      
98      /***
99       * Read the xml content into the java classes with jaxb unmarshalling
100      * @param filename
101      */    
102     private ReleaseNotesImpl readReleaseNotes( String filename ){
103         
104         ReleaseNotesImpl releaseNotes = null;
105         try{
106             JAXBContext jContext = JAXBContext.newInstance( "org.xrn.xsd2java" );
107             Unmarshaller unmarshaller = jContext.createUnmarshaller();
108 //            unmarshaller.setValidating( true );
109             releaseNotes = (ReleaseNotesImpl)unmarshaller.unmarshal( new FileInputStream( filename ) );
110         }
111         catch( UnmarshalException unEx ) {
112              MessageWindow.mitteilung( "Error while validating xml file ! Details: " + unEx.getMessage() );
113         }
114         catch( JAXBException jaxEx ) {
115             MessageWindow.mitteilung( "JAXB Error ! Details: " + jaxEx.getMessage() );
116             jaxEx.printStackTrace();
117         }
118         catch( IOException ioEx ) {
119             MessageWindow.mitteilung( "IO Error ! Details: " + ioEx.getMessage() );
120         }        
121         return releaseNotes;
122         
123     }
124 }