View Javadoc

1   package org.xrn.gui;
2   
3   import java.awt.Color;
4   import java.awt.Font;
5   import java.awt.event.ActionEvent;
6   import java.awt.event.ActionListener;
7   import java.io.File;
8   import java.io.FileNotFoundException;
9   import java.io.FileOutputStream;
10  import java.io.FileWriter;
11  import java.io.IOException;
12  import java.io.PrintWriter;
13  
14  import javax.swing.JButton;
15  import javax.swing.JComponent;
16  import javax.swing.JFileChooser;
17  import javax.swing.JLabel;
18  import javax.swing.JPanel;
19  import javax.swing.JTabbedPane;
20  import javax.swing.JTextField;
21  import javax.swing.border.TitledBorder;
22  import javax.xml.bind.JAXBContext;
23  import javax.xml.bind.JAXBException;
24  import javax.xml.bind.MarshalException;
25  import javax.xml.bind.Marshaller;
26  import javax.xml.transform.Result;
27  import javax.xml.transform.Source;
28  import javax.xml.transform.Transformer;
29  import javax.xml.transform.TransformerConfigurationException;
30  import javax.xml.transform.TransformerException;
31  import javax.xml.transform.TransformerFactory;
32  import javax.xml.transform.sax.SAXResult;
33  import javax.xml.transform.stream.StreamResult;
34  import javax.xml.transform.stream.StreamSource;
35  
36  import org.apache.avalon.framework.ExceptionUtil;
37  import org.apache.avalon.framework.logger.ConsoleLogger;
38  import org.apache.avalon.framework.logger.Logger;
39  import org.apache.fop.apps.Driver;
40  import org.apache.fop.messaging.MessageHandler;
41  import org.xrn.xsd2java.ObjectFactory;
42  import org.xrn.xsd2java.ReleaseNotes;
43  import org.xrn.xsd2java.impl.ReleaseNotesImpl;
44  
45  /***
46   * 
47   * The window for the working area to enter the release notes
48   * @author Carsten Maneg
49   * Date Nov 8, 2004
50   * Time 10:49:07 PM
51   */
52  public class ReleaseNotesWindow extends JPanel implements ActionListener{
53      private int x0 = 40;
54      private int y0 = 40;
55      private int width = 900;
56      private int height  = 550;
57      
58      private int mode = XRNConstants.NEW_NOTES;
59    
60      private XRNMainWindow mainWindow;
61      private ComponentsPanel componentsPanel;
62      private DeliveriesPanel deliveriesPanel;
63      private ActorsPanel actorsPanel;
64      private SummaryPanel summaryPanel;
65      private ReleasesPanel releasesPanel;
66  
67      private JLabel idLabel = new JLabel( "Name (ID)" );
68      private JTextField idField = new JTextField();
69  //    private JLabel versionLabel = new JLabel( "Version" );
70  //    private JTextField versionField = new JTextField();  
71    
72      private JFileChooser fileChooser;
73      private JTabbedPane releaseNotesTabbedPane;
74      
75      private String filePath = "";
76      private String fileName = "";
77      private File currentFile = null;
78    
79      private JButton saveButton = new JButton( "Save" );
80      private JButton createHTMLButton = new JButton( "Create HTML page" );
81      private JButton createPDFButton = new JButton( "Create PDF file" );
82      private JButton cancelButton = new JButton( "Cancel" );
83      private JButton resetButton = new JButton( "Reset" );
84      private TitledBorder titledBorder = new TitledBorder( "" );
85      
86      private ReleaseNotesImpl releaseNotes = new ReleaseNotesImpl();
87  
88      /***
89       * Constructor - The window for the working area
90       * @param mainWindow
91       */
92      public ReleaseNotesWindow( XRNMainWindow mainWindow, int mode, File choosenFile ){
93    	
94          this.mode = mode;
95          if( mode == XRNConstants.NEW_NOTES )
96              titledBorder.setTitle( " New Release Notes " );
97          else
98              titledBorder.setTitle( " Release Notes " );
99              
100         titledBorder.setTitleFont( new Font( "Dialog", Font.BOLD, 12 ) );
101         setBorder( titledBorder );
102         setLayout( null );
103        
104     
105         this.mainWindow = mainWindow;
106         this.currentFile = choosenFile;
107         
108 //        dlgObjHinzufuegen( versionLabel, 25, 20, 40, 20 ); 
109 //        dlgObjHinzufuegen( versionField, 70, 20, 80, 20 ); 
110         dlgObjHinzufuegen( idLabel, 25, 20, 50, 20 ); 
111         dlgObjHinzufuegen( idField, 80, 20, 150, 20 );
112         
113 //        versionField.setEnabled( false );
114 //        idField.setEnabled( false );
115        
116         saveButton.setMnemonic( 'S' );
117         
118         saveButton.setEnabled( true );
119         saveButton.addActionListener( this );
120         createHTMLButton.setEnabled( false );
121         createHTMLButton.addActionListener( this );
122         createPDFButton.setEnabled( false );
123         createPDFButton.addActionListener( this );
124         resetButton.setEnabled( false );
125         resetButton.addActionListener( this );
126         cancelButton.setEnabled( false );
127         cancelButton.addActionListener( this );
128         dlgObjHinzufuegen( saveButton, 420, 20, 80, 20 ); 
129         dlgObjHinzufuegen( createHTMLButton, 520, 20, 130, 20 ); 
130         dlgObjHinzufuegen( createPDFButton, 660, 20, 120, 20 ); 
131         dlgObjHinzufuegen( resetButton, 800, 20, 80, 20 ); 
132         dlgObjHinzufuegen( cancelButton, 900, 20, 80, 20 );
133         
134         componentsPanel = new ComponentsPanel( this );
135         releasesPanel = new ReleasesPanel( this );
136         deliveriesPanel = new DeliveriesPanel( this, " Deliveries " );
137         actorsPanel = new ActorsPanel( this );
138         summaryPanel = new SummaryPanel( this, "big" );
139         
140         releaseNotesTabbedPane = new JTabbedPane();
141         releaseNotesTabbedPane.insertTab( "Components", null, componentsPanel, "Components", 0 );
142         releaseNotesTabbedPane.insertTab( "Releases", null, releasesPanel, "Releases", 1 );
143         releaseNotesTabbedPane.insertTab( "Deliveries", null, deliveriesPanel, "Deliveries", 2 );
144         releaseNotesTabbedPane.insertTab( "Actors", null, actorsPanel, "Actors", 3 );
145         releaseNotesTabbedPane.insertTab( "Summary", null, summaryPanel, "Summary", 4 );
146         dlgObjHinzufuegen( releaseNotesTabbedPane, 10, 50, 990, 530 );
147         
148         releaseNotesTabbedPane.setEnabledAt( 3, false );
149     }
150 
151     /***
152      * Return the main window
153      * @return
154      */
155     public XRNMainWindow getMainWindow(){
156         return mainWindow;
157     }
158     
159     /***
160      * The button to create the HTML file
161      * @return
162      */
163     public JButton getCreateHTMLButton(){
164         return createHTMLButton;
165     }
166     
167     /***
168      * Reset all input fields in the window
169      *
170      */
171     public void resetAllInput(){
172         componentsPanel.resetFields();
173         deliveriesPanel.resetFields();
174         actorsPanel.resetFields();
175         summaryPanel.resetFields();
176         releasesPanel.resetFields();
177     }
178 
179     /***
180      * Add the component to the window
181      * @param c
182      * @param x0
183      * @param y0
184      * @param breite
185      * @param hoehe
186      */
187     public void dlgObjHinzufuegen( JComponent c, int x0, int y0, int breite, int hoehe ){
188         c.setBounds( x0, y0, breite, hoehe );
189         c.setForeground( Color.black );
190         add( c );
191     }
192 
193     /***
194      * Handle the button actions.
195      */
196     public void actionPerformed( ActionEvent e ){
197         
198         if ( e.getSource( ) == saveButton ){
199             save();
200             createHTMLButton.setEnabled( true );
201         }
202         if( e.getSource() == createHTMLButton ){
203             createHTML();            
204         }
205         if( e.getSource() == createPDFButton ){
206             createPDF();
207         }
208         if ( e.getSource( ) == resetButton ){
209             resetAllInput();
210         }
211         if ( e.getSource( ) == cancelButton ){
212             // close the new-Window
213         }
214     }
215     
216     /***
217      * 
218      *
219      */
220     private void save(){
221 
222         try{
223           JAXBContext jContext = JAXBContext.newInstance( "org.xrn.xsd2java" );
224           ObjectFactory factory = new ObjectFactory();
225           ReleaseNotes releaseNotes = factory.createReleaseNotes();
226           releaseNotes = getReleaseNotes();
227           Marshaller marshaller = jContext.createMarshaller();
228           marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
229 //          marshaller.setProperty( Marshaller.JAXB_SCHEMA_LOCATION, new String( XRNConstants.XSD_URL ) );
230 //          marshaller.setProperty( Marshaller.JAXB_ENCODING, new String("ISO-8859-1") );
231           marshaller.setProperty( "com.sun.xml.bind.xmlDeclaration", Boolean.TRUE );
232 //          marshaller.setProperty( Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, new String("CMTEST NAMESPACE") );
233           
234           PrintWriter fileOutput = null;
235           if( mode == XRNConstants.NEW_NOTES ){
236               fileChooser = new JFileChooser( XRNConstants.DEFAULT_PROJECT_DIR );
237               fileChooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
238               XMLFileFilter fileFilter = new XMLFileFilter();
239               fileFilter.addExtension( "xml" );
240               fileChooser.setFileFilter( fileFilter );
241               int state = fileChooser.showSaveDialog( this );
242               File selectedFile = fileChooser.getSelectedFile();
243               if( (selectedFile != null) && (state == JFileChooser.APPROVE_OPTION) ){
244                   filePath = selectedFile.getPath();
245                   fileName = selectedFile.getName();
246               }
247               currentFile = new File( filePath + XRNConstants.XML_FILE_SUFFIX );
248               currentFile.createNewFile();            
249               fileOutput = new PrintWriter( new FileWriter( currentFile ), true );
250 //              fileOutput.print( XRNConstants.XML_DECLARATION );
251 //              fileOutput.print( XRNConstants.STYLESHEET );
252               marshaller.marshal( releaseNotes, fileOutput );
253               fileOutput.close();
254           }
255           if( mode == XRNConstants.EDIT_NOTES ){
256               FileOutputStream outputStream = new FileOutputStream( currentFile.getPath() );
257               
258               outputStream.write( XRNConstants.XML_DECLARATION.getBytes() );
259               outputStream.write ( XRNConstants.STYLESHEET.getBytes() );
260               marshaller.marshal( releaseNotes, outputStream );
261               outputStream.close();
262           }
263       }
264       catch( MarshalException unEx ) {
265           MessageWindow.mitteilung( "Error while validating xml file ! Details: " + unEx.getMessage() );
266      }
267      catch( JAXBException jaxEx ) {
268          MessageWindow.mitteilung( "JAXB Error ! Details: " + jaxEx.getMessage() );
269          jaxEx.printStackTrace();
270      }
271      catch( FileNotFoundException ioEx ) {
272          MessageWindow.mitteilung( "IO Error ! Details: " + ioEx.getMessage() );
273      }
274      catch( IOException io ){
275          MessageWindow.mitteilung( "Error ! File not found. " + io.getMessage() );
276      }          
277         
278     }
279     
280     /***
281      * Creates the HTML file from the input xml file using the xslt file
282      *
283      */
284     private void createHTML(){
285         try{
286             TransformerFactory tFactory = TransformerFactory.newInstance();
287             
288             Transformer transformer = tFactory.newTransformer( 
289                     new javax.xml.transform.stream.StreamSource("C:/xmlreleasenotes/xslt/XMLReleaseNotes2HTML.xsl"));
290             
291             String htmlName = currentFile.getPath().replaceFirst( ".xml", ".html" );
292             transformer.transform( new javax.xml.transform.stream.StreamSource( currentFile.getPath() ),
293                     new javax.xml.transform.stream.StreamResult( new FileOutputStream( htmlName ) ) );
294         }
295         catch( TransformerConfigurationException transConfEx){
296             transConfEx.printStackTrace( );
297         }
298         catch( TransformerException transEx ){
299             transEx.printStackTrace( );
300         }
301         catch (FileNotFoundException fileEx){
302             fileEx.printStackTrace( );
303         }    
304         
305     }
306     
307     /***
308      * 
309      *
310      */
311     private void createPDF(){
312         File pdfFile = new File( "C:/xmlreleaseNotes/projects/testneu.pdf" );
313 //        File xsltFile = new File( "C:/xmlreleasenotes/xslt/testXML2FO.xsl" );        
314         File xsltFile = new File( "C:/xmlreleasenotes/xslt/test07122fo.xsl" );        
315 //        File xmlFile = new File( "C:/xmlreleasenotes/projects/projectteam.xml" );        
316         File xmlFile = new File( "C:/xmlreleasenotes/projects/cmtest1012.xml" );        
317         
318         Driver driver = new Driver();
319         
320         //Setup logger
321         Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
322         driver.setLogger(logger);
323         MessageHandler.setScreenLogger(logger);
324 
325         //Setup Renderer (output format)        
326         driver.setRenderer(Driver.RENDER_PDF);
327         
328         //Setup output
329         FileOutputStream out = null;
330         try {
331             out = new FileOutputStream( pdfFile );
332 //            out = new BufferedOutputStream(out);
333             driver.setOutputStream( out );
334 
335             //Setup XSLT
336             TransformerFactory factory = TransformerFactory.newInstance();
337             Transformer transformer = factory.newTransformer( new StreamSource( xsltFile ) );        
338             
339             // Set the value of a <param> in the stylesheet
340 //            transformer.setParameter("versionParam", "2.0");
341             
342             //Setup input for XSLT transformation
343             StreamSource src = new StreamSource( xmlFile );
344 //            StreamSource src = new StreamSource( currentFile );
345         
346             //Resulting SAX events (the generated FO) must be piped through to FOP
347             SAXResult res = new SAXResult( driver.getContentHandler() );
348 
349             //Start XSLT transformation and FOP processing
350             transformer.transform( src, res );
351 //            transformer.transform( src, new javax.xml.transform.stream.StreamResult( new FileOutputStream( "C:/xmlreleaseNotes/projects/test0712.txt" ) ) );
352             
353             out.close();                      
354         }
355        catch( FileNotFoundException fileEx ){
356             MessageWindow.mitteilung( " Can't find pdf file !" );
357         }
358         catch( TransformerConfigurationException transEx ){
359             MessageWindow.mitteilung( "Error with Transformer Factory !" );
360             System.err.println(ExceptionUtil.printStackTrace(transEx));
361         }
362         catch( TransformerException transEx ){
363             MessageWindow.mitteilung( "Error while transforming file !" + transEx.getMessage() );
364 //            System.out.println( transEx.getLocalizedMessage() );
365             System.err.println(ExceptionUtil.printStackTrace(transEx));
366         }
367         catch( IOException ioEx ){
368             MessageWindow.mitteilung( "Error while closing output stream !" );
369         }
370     }
371     /***
372      * 
373      *
374      */
375     private void createFO(){
376         File foFile = new File( "C:/xmlreleaseNotes/projects/test.pdf" );
377         File xsltFile = new File( "C:/xmlreleasenotes/xslt/test07122fo.xsl" );        
378 //        File xsltFile = new File( "C:/xmlreleasenotes/xslt/projectteam2fo.xsl" );        
379 //        File xmlFile = new File( "C:/xmlreleasenotes/projects/projectteam.xml" );        
380         File xmlFile = new File( "C:/xmlreleasenotes/projects/test0912.xml" );        
381         
382         Driver driver = new Driver();
383         
384         //Setup logger
385         Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
386         driver.setLogger(logger);
387         MessageHandler.setScreenLogger(logger);
388 
389         //Setup Renderer (output format)        
390         driver.setRenderer(Driver.RENDER_PDF);
391         
392         //Setup output
393         FileOutputStream out = null;
394         try {
395             out = new java.io.FileOutputStream( foFile );
396             //Setup XSLT
397             TransformerFactory factory = TransformerFactory.newInstance();
398             Transformer transformer = factory.newTransformer(new StreamSource(xsltFile));
399         
400             //Setup input for XSLT transformation
401             Source src = new StreamSource(xmlFile);
402         
403             //Resulting SAX events (the generated FO) must be piped through to FOP
404             Result res = new StreamResult(out);
405 
406             //Start XSLT transformation and FOP processing
407             transformer.transform(src, res);
408             
409             out.close();
410         }
411        catch( FileNotFoundException fileEx ){
412             MessageWindow.mitteilung( " Can't find pdf file !" );
413         }
414         catch( TransformerConfigurationException transEx ){
415             MessageWindow.mitteilung( "Error with Transformer Factory !" );
416         }
417         catch( TransformerException transEx ){
418             MessageWindow.mitteilung( "Error while transforming file !" + transEx.getMessage() );
419 //            System.out.println( transEx.getLocalizedMessage() );
420             System.err.println(ExceptionUtil.printStackTrace(transEx));
421         }
422         catch( IOException ioEx ){
423             MessageWindow.mitteilung( "Error while closing output stream !" );
424         }
425     }
426     
427     /***
428      * Read the input and save it to a xml-file
429      *
430      */
431     public ReleaseNotesImpl getReleaseNotes(){
432         releaseNotes.setID( idField.getText() );
433         releaseNotes.setVersion( "0.17.1" );
434         releaseNotes.setComponents( componentsPanel.getComponentsList() );
435         releaseNotes.setActors( actorsPanel.getActors() );
436         releaseNotes.setDeliveries( deliveriesPanel.getDeliveries() );
437         releaseNotes.setSummary( summaryPanel.getSummary() );
438         releaseNotes.setReleases( releasesPanel.getReleases() );
439         return releaseNotes;
440     }
441     
442     /***
443      * Write the data to the window fields.
444      * @param notes
445      */
446     public void setReleaseNotes( ReleaseNotesImpl notes ){
447         this.releaseNotes = notes;
448         if( notes != null ){
449             idField.setText( notes.getID() );
450 //            versionField.setText( notes.getVersion() );
451             componentsPanel.setComponentsList( notes.getComponents() );
452             actorsPanel.setActors( notes.getActors() );
453             deliveriesPanel.setDeliveries( notes.getDeliveries() );
454             summaryPanel.setSummary( notes.getSummary() );
455             releasesPanel.setReleases( notes.getReleases() );
456         }
457     }
458 }