KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xsl > ui > TransformPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xsl.ui;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.awt.*;
29 import javax.swing.*;
30 import java.util.Vector JavaDoc;
31
32 import javax.xml.transform.*;
33
34 import org.openide.loaders.DataObject;
35 import org.openide.loaders.RepositoryNodeFactory;
36 import org.openide.filesystems.*;
37 import org.openide.util.Lookup;
38 import org.openide.DialogDisplayer;
39 import org.openide.NotifyDescriptor;
40
41 import org.netbeans.api.xml.cookies.TransformableCookie;
42
43 import org.netbeans.modules.xsl.settings.TransformHistory;
44 import org.netbeans.modules.xsl.utils.TransformUtil;
45
46 /**
47  *
48  * @author Libor Kramolis
49  * @version 0.1
50  */

51 public final class TransformPanel extends javax.swing.JPanel JavaDoc {
52     /** Serial Version UID */
53     private static final long serialVersionUID = -3449709794133206327L;
54     
55     public static final String JavaDoc DATA_XML_MODIFIED="DATA_XML_MODIFIED";
56     public static final String JavaDoc DATA_XSL_MODIFIED="DATA_XSL_MODIFIED";
57     public static final String JavaDoc DATA_OUTPUT_MODIFIED="DATA_OUTPUT_MODIFIED";
58     public static final String JavaDoc DATA_PROCESS_MODIFIED="DATA_PROCESS_MODIFIED";
59     public static final String JavaDoc DATA_OVERWRITE_MODIFIED="DATA_OVERWRITE_MODIFIED";
60     
61     private URL JavaDoc baseURL;
62     private Data data;
63     private boolean initialized = false;
64     
65     private DataObject xmlDataObject;
66     private String JavaDoc xml_stylesheet; // <?xsl-stylesheet ...
67
private DataObject xslDataObject;
68     private TransformHistory xmlHistory;
69     private TransformHistory xslHistory;
70     
71     private boolean userSetOutput = false;
72     private boolean userSetProcess = false;
73     private String JavaDoc lastOutputFileExt = TransformUtil.DEFAULT_OUTPUT_EXT;
74     private Object JavaDoc lastXSLObject = new Object JavaDoc();
75     
76     /** Hide transformation components if true. */
77     private boolean suppressXSL;
78     
79     /** Names of output actions. */
80     private static final String JavaDoc[] SHOW_NAMES = new String JavaDoc[] {
81         Util.THIS.getString("NAME_process_output_do_nothing"), // TransformHistory.DO_NOTHING
82
Util.THIS.getString("NAME_process_output_apply_default_action"), // TransformHistory.APPLY_DEFAULT_ACTION
83
Util.THIS.getString("NAME_process_output_open_in_browser"), // TransformHistory.OPEN_IN_BROWSER
84
};
85     
86     private static final Object JavaDoc JUST_PREVIEW = new Preview();
87     
88     /** Creates new form TransformPanel */
89     public TransformPanel (DataObject xml, String JavaDoc xml_ss, DataObject xsl, boolean suppressXSL) throws MalformedURLException JavaDoc, FileStateInvalidException {
90         initComponents();
91         
92         init(xml, xml_ss, xsl, suppressXSL);
93         initAccessibility();
94     }
95     
96     /** Creates new form TransformPanel */
97     public TransformPanel(DataObject xml, String JavaDoc xml_ss, DataObject xsl) throws MalformedURLException JavaDoc, FileStateInvalidException {
98         this(xml, xml_ss, xsl, false);
99     }
100     
101     private void init(DataObject xml, String JavaDoc xml_ss, DataObject xsl, boolean supXSL) throws MalformedURLException JavaDoc, FileStateInvalidException {
102         data = new Data();
103         xmlDataObject = xml;
104         xml_stylesheet = xml_ss;
105         xslDataObject = xsl;
106         suppressXSL = supXSL;
107         
108         if ( xmlDataObject != null ) {
109             setInput(TransformUtil.getURLName(xmlDataObject.getPrimaryFile()));
110             FileObject xmlFileObject = xmlDataObject.getPrimaryFile();
111             xmlHistory = (TransformHistory)xmlFileObject.getAttribute(TransformHistory.TRANSFORM_HISTORY_ATTRIBUTE);
112             if ( xmlHistory != null ) {
113                 setXSL(xmlHistory.getLastXSL());
114             }
115             if ( ( data.xsl == null ) &&
116             ( xml_stylesheet != null ) ) {
117                 setXSL(xml_stylesheet);
118             }
119             try {
120                 baseURL = xmlFileObject.getParent().getURL();
121             } catch (FileStateInvalidException exc) {
122                 // ignore it
123
}
124         }
125         if ( xslDataObject != null ) {
126             setXSL(TransformUtil.getURLName(xslDataObject.getPrimaryFile()));
127             FileObject xslFileObject = xslDataObject.getPrimaryFile();
128             xslHistory = (TransformHistory)xslFileObject.getAttribute(TransformHistory.TRANSFORM_HISTORY_ATTRIBUTE);
129             if ( ( data.xml == null ) && ( xslHistory != null ) ) {
130                 setInput(xslHistory.getLastXML());
131             }
132             if ( baseURL == null ) {
133                 try {
134                     baseURL = xslFileObject.getParent().getURL();
135                 } catch (FileStateInvalidException exc) {
136                     // ignore it
137
}
138             }
139         }
140         
141         if ( ( xmlHistory != null ) || ( xslHistory != null ) ) {
142             if ( xmlHistory != null ) {
143                 setOutput(xmlHistory.getLastXSLOutput());
144             }
145             if ( ( data.output == null || (data.output instanceof String JavaDoc && "".equals(data.output)) ) &&
146             ( xslHistory != null ) ) {
147                 setOutput(xslHistory.getLastXMLOutput());
148             }
149             if ( data.output == null ) {
150                 setOutput(JUST_PREVIEW);
151             }
152         }
153         
154         if ( xmlHistory != null ) {
155             setOverwriteOutput( xmlHistory.isOverwriteOutput());
156             setProcessOutput(new Integer JavaDoc(xmlHistory.getProcessOutput()));
157         } else if ( xslHistory != null ) {
158             setOverwriteOutput( xslHistory.isOverwriteOutput());
159             setProcessOutput(new Integer JavaDoc(xslHistory.getProcessOutput()));
160         }
161         
162         ownInitComponents();
163     }
164     
165     
166     private void ownInitComponents() {
167         // XML Input
168
updateXMLComboBoxModel(null);
169         // XSL Transformation
170
updateXSLComboBoxModel(null);
171         
172         updateComponents();
173         
174         setCaretPosition(inputComboBox);
175         setCaretPosition(transformComboBox);
176         setCaretPosition(outputComboBox);
177     }
178     
179     
180     private void setCaretPosition(JComboBox comboBox) {
181         ComboBoxEditor cbEditor = comboBox.getEditor();
182         final Component editorComponent = cbEditor.getEditorComponent();
183         
184         if ( Util.THIS.isLoggable() ) /* then */ {
185             Util.THIS.debug("TransformPanel.setCaretPosition: " + comboBox);
186             Util.THIS.debug(" editorComponent = " + editorComponent);
187         }
188         
189         if ( editorComponent instanceof JTextField ) {
190             SwingUtilities.invokeLater
191             (new Runnable JavaDoc() {
192                 public void run() {
193                     JTextField textField = (JTextField) editorComponent;
194                     int length = textField.getText().length();
195                     textField.setCaretPosition(length);
196                     
197                     if ( Util.THIS.isLoggable() ) /* then */ {
198                         Util.THIS.debug(" text[" + length + "]='" + textField.getText() + "' - " + textField.getCaretPosition());
199                     }
200                 }
201             }
202             );
203         }
204     }
205     
206     private void updateXMLComboBoxModel(Object JavaDoc prefItem) {
207         Object JavaDoc[] history = null;
208         if ( ( xmlDataObject == null ) &&
209         ( xslHistory != null ) ) {
210             history = xslHistory.getXMLs();
211         }
212         
213         Vector JavaDoc cbModel = new Vector JavaDoc();
214         
215         // Preferred Item
216
if ( prefItem != null ) {
217             cbModel.add(prefItem);
218         }
219         // History
220
if ( history != null ) {
221             for ( int i = 0; i < history.length; i++ ) {
222                 cbModel.add(history[i]);
223             }
224         }
225         
226         inputComboBox.setModel(new DefaultComboBoxModel(cbModel));
227     }
228     
229     private void updateXSLComboBoxModel(Object JavaDoc prefItem) {
230         Object JavaDoc[] history = null;
231         if ( ( xslDataObject == null ) &&
232         ( xmlHistory != null ) ) {
233             history = xmlHistory.getXSLs();
234         }
235         
236         Vector JavaDoc cbModel = new Vector JavaDoc();
237         
238         // Preferred Item
239
if ( prefItem != null ) {
240             cbModel.add(prefItem);
241         }
242         // <?xsl-stylesheet ...
243
if ( xml_stylesheet != null ) {
244             cbModel.add(xml_stylesheet);
245         }
246         // History
247
if ( history != null ) {
248             for ( int i = 0; i < history.length; i++ ) {
249                 if ( ( history[i] != null ) &&
250                 ( history[i].equals(xml_stylesheet) == false ) ) { // do not duplicate xml_stylesheet item
251
cbModel.add(history[i]);
252                 }
253             }
254         }
255         
256         transformComboBox.setModel(new DefaultComboBoxModel(cbModel));
257     }
258     
259     private boolean isInitialized() {
260         synchronized ( data ) {
261             return initialized;
262         }
263     }
264     
265     private void setInitialized(boolean init) {
266         synchronized ( data ) {
267             initialized = init;
268         }
269     }
270     
271     private static String JavaDoc guessFileName(String JavaDoc xml) {
272         String JavaDoc fileName = null;
273         
274         int slashIndex = xml.lastIndexOf('/');
275         if ( slashIndex != -1 ) {
276             fileName = xml.substring(slashIndex + 1);
277         } else {
278             fileName = xml;
279         }
280         
281         return fileName;
282     }
283     
284     private String JavaDoc guessOutputFileExt() {
285         String JavaDoc ext = lastOutputFileExt;
286         String JavaDoc xslObject = getXSL();
287         
288         if ( xslObject != lastXSLObject ) {
289             try {
290                 Source xslSource;
291                 xslSource = TransformUtil.createSource(baseURL, xslObject);
292                 ext = TransformUtil.guessOutputExt(xslSource);
293                 
294                 // cache last values
295
lastXSLObject = xslObject;
296                 lastOutputFileExt = ext;
297             } catch (Exception JavaDoc exc) {
298                 // ignore it
299

300                 if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("[TransformPanel] Cannot guess extension!", exc);
301             }
302         }
303         
304         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("[TransformPanel] I guess output has '" + ext + "' extension.");
305         
306         return ext;
307     }
308     
309     private Object JavaDoc guessOutputFile() {
310         Object JavaDoc output = data.output;
311         
312         if ( output == null || "".equals(output) || JUST_PREVIEW.equals(output)) {
313             String JavaDoc origName = guessFileName(data.xml);
314             String JavaDoc origExt = "";
315             int dotIndex = origName.lastIndexOf('.');
316             if ( dotIndex != -1 ) {
317                 origExt = origName.substring(dotIndex + 1);
318                 origName = origName.substring(0, dotIndex);
319             }
320             
321             String JavaDoc ext = guessOutputFileExt();
322             String JavaDoc plusName = "";
323             if ( ext.equals(origExt) ) {
324                 plusName = Util.THIS.getString("NAME_plusNameIfSameName");
325             }
326             
327             output = origName + plusName + "." + ext; // NOI18N
328
}
329         
330         return output;
331     }
332     
333     private void initOutputComboBox(Object JavaDoc defaultOutput) {
334         outputComboBox.setModel(new DefaultComboBoxModel(new Object JavaDoc[] { defaultOutput, JUST_PREVIEW }));
335     }
336     
337     
338     private void updateComponents() {
339         setInitialized(false);
340         
341         // XML Input
342
boolean notXML = ( xmlDataObject == null );
343         if ( data.xml != null ) {
344             inputComboBox.setSelectedItem(data.xml);
345             inputComboBox.setEditable(data.xml instanceof String JavaDoc);
346         }
347         inputComboBox.setEnabled(notXML);
348         browseInputButton.setEnabled(notXML);
349         
350         // XSL Transformation
351
if ( suppressXSL ) {
352             transformLabel.setVisible(false);
353             transformComboBox.setVisible(false);
354             browseXSLTButton.setVisible(false);
355         } else {
356             transformLabel.setVisible(true);
357             transformComboBox.setVisible(true);
358             browseXSLTButton.setVisible(true);
359             
360             boolean notXSL = ( xslDataObject == null );
361             transformComboBox.setEnabled(notXSL);
362             browseXSLTButton.setEnabled(notXSL);
363             if ( data.xsl != null ) {
364                 transformComboBox.setSelectedItem(data.xsl);
365                 transformComboBox.setEditable(data.xsl instanceof String JavaDoc);
366             }
367         }
368         
369         // test if XML and also XSL
370
boolean canOutput = true;
371         if ( ( data.xml == null ) ||
372         ( data.xsl == null ) ||
373         ( data.xml.length() == 0 ) ||
374         ( data.xsl.length() == 0 ) ) {
375             canOutput = false;
376         }
377         
378         // Output
379
outputComboBox.setEnabled(canOutput);
380         if ( canOutput ) {
381             Object JavaDoc output = guessOutputFile();
382             initOutputComboBox(output);
383             
384             outputComboBox.setSelectedItem(data.output != null ? output : JUST_PREVIEW);
385             outputComboBox.setEditable(data.output != null);
386         }
387         
388         // Overwrite Output
389
if ( data.overwrite != null ) {
390             overwriteCheckBox.setSelected(data.overwrite.booleanValue());
391         }
392         overwriteCheckBox.setEnabled(canOutput && data.output != null);
393         
394         // Process Output
395
if ( data.process != null ) {
396             showComboBox.setSelectedIndex(data.process.intValue());
397         } else {
398             String JavaDoc ext = guessOutputFileExt().toLowerCase();
399             if ( ext.equals("html") || ext.equals("htm") ) { // NOI18N
400
showComboBox.setSelectedIndex(TransformHistory.OPEN_IN_BROWSER);
401             } else {
402                 showComboBox.setSelectedIndex(TransformHistory.APPLY_DEFAULT_ACTION);
403             }
404         }
405         showComboBox.setEnabled(canOutput && data.output != null);
406         
407         setInitialized(true);
408     }
409     
410     
411     public Data getData() {
412         return new Data (getInput(), getXSL(), getOutput(), isOverwriteOutput(), getProcessOutput());
413     }
414     
415     public void setData(Data data) {
416         this.data = data;
417         updateComponents();
418     }
419     
420     /**
421      * @return selected XML input.
422      */

423     private String JavaDoc getInput() {
424         return (String JavaDoc) inputComboBox.getSelectedItem();
425     }
426     
427     /**
428      * @return selected XSLT script.
429      */

430     private String JavaDoc getXSL() {
431         return (String JavaDoc) transformComboBox.getSelectedItem();
432     }
433     
434     /**
435      * @return selected output.
436      */

437     private String JavaDoc getOutput() {
438         Object JavaDoc output = outputComboBox.getSelectedItem();
439         
440         if ( JUST_PREVIEW.equals(output) ) {
441             return null;
442         }
443         return (String JavaDoc) output;
444     }
445     
446     /**
447      * @return selected overwrite output option.
448      */

449     private boolean isOverwriteOutput() {
450         return overwriteCheckBox.isSelected();
451     }
452     
453     /**
454      * @return selected process output option.
455      */

456     private int getProcessOutput() {
457         return showComboBox.getSelectedIndex();
458     }
459     
460     /**
461      * Compute preffered dimension for combo with
462      * particulal number of columns
463      */

464     private Dimension comboSize(int columns) {
465         JTextField template = new JTextField();
466         template.setColumns(columns);
467         return template.getPreferredSize();
468     }
469     
470     /** This method is called from within the constructor to
471      * initialize the form.
472      * WARNING: Do NOT modify this code. The content of this method is
473      * always regenerated by the Form Editor.
474      */

475     private void initComponents() {//GEN-BEGIN:initComponents
476
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
477
478         inputLabel = new javax.swing.JLabel JavaDoc();
479         inputLabel.setDisplayedMnemonic (Util.THIS.getChar ("LBL_XML_input_mnemonic"));
480         inputComboBox = new javax.swing.JComboBox JavaDoc();
481         browseInputButton = new javax.swing.JButton JavaDoc();
482         transformLabel = new javax.swing.JLabel JavaDoc();
483         transformLabel.setDisplayedMnemonic (Util.THIS.getChar ("LBL_XSL_transform_mnemonic"));
484         transformComboBox = new javax.swing.JComboBox JavaDoc();
485         browseXSLTButton = new javax.swing.JButton JavaDoc();
486         outputLabel = new javax.swing.JLabel JavaDoc();
487         outputLabel.setDisplayedMnemonic (Util.THIS.getChar ("LBL_trans_output_mnemonic"));
488         outputComboBox = new javax.swing.JComboBox JavaDoc();
489         overwriteCheckBox = new javax.swing.JCheckBox JavaDoc();
490         overwriteCheckBox.setMnemonic (Util.THIS.getChar ("LBL_over_write_mnemonic"));
491         showLabel = new javax.swing.JLabel JavaDoc();
492         showLabel.setDisplayedMnemonic (Util.THIS.getChar ("LBL_show_output_mnemonic"));
493         showComboBox = new javax.swing.JComboBox JavaDoc();
494
495         FormListener formListener = new FormListener();
496
497         setLayout(new java.awt.GridBagLayout JavaDoc());
498
499         inputLabel.setLabelFor(inputComboBox);
500         inputLabel.setText(Util.THIS.getString ("LBL_XML_input"));
501         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
502         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
503         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 12, 0, 0);
504         add(inputLabel, gridBagConstraints);
505
506         inputComboBox.setEditable(true);
507         inputComboBox.setPreferredSize(comboSize(40));
508         inputComboBox.addActionListener(formListener);
509
510         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
511         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
512         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
513         gridBagConstraints.weightx = 1.0;
514         add(inputComboBox, gridBagConstraints);
515
516         browseInputButton.setMnemonic(Util.THIS.getChar ("LBL_browse_file_mnemonic"));
517         browseInputButton.setText(Util.THIS.getString ("LBL_browse_file"));
518         browseInputButton.addActionListener(formListener);
519
520         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
521         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 5, 0, 11);
522         add(browseInputButton, gridBagConstraints);
523
524         transformLabel.setLabelFor(transformComboBox);
525         transformLabel.setText(Util.THIS.getString ("LBL_XSL_transform"));
526         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
527         gridBagConstraints.gridx = 0;
528         gridBagConstraints.gridy = 1;
529         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
530         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 12, 0, 0);
531         add(transformLabel, gridBagConstraints);
532
533         transformComboBox.setEditable(true);
534         transformComboBox.setPreferredSize(comboSize(40));
535         transformComboBox.addActionListener(formListener);
536
537         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
538         gridBagConstraints.gridx = 1;
539         gridBagConstraints.gridy = 1;
540         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
541         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 0);
542         gridBagConstraints.weightx = 1.0;
543         add(transformComboBox, gridBagConstraints);
544
545         browseXSLTButton.setMnemonic(Util.THIS.getChar ("LBL_browse_xslt_mnemonic"));
546         browseXSLTButton.setText(Util.THIS.getString ("LBL_browse_xslt"));
547         browseXSLTButton.addActionListener(formListener);
548
549         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
550         gridBagConstraints.gridx = 2;
551         gridBagConstraints.gridy = 1;
552         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 0, 11);
553         add(browseXSLTButton, gridBagConstraints);
554
555         outputLabel.setLabelFor(outputComboBox);
556         outputLabel.setText(Util.THIS.getString ("LBL_trans_output"));
557         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
558         gridBagConstraints.gridx = 0;
559         gridBagConstraints.gridy = 2;
560         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
561         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 12, 0, 0);
562         add(outputLabel, gridBagConstraints);
563
564         outputComboBox.setEditable(true);
565         outputComboBox.setPreferredSize(comboSize(40));
566         outputComboBox.addActionListener(formListener);
567
568         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
569         gridBagConstraints.gridx = 1;
570         gridBagConstraints.gridy = 2;
571         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
572         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 0);
573         gridBagConstraints.weightx = 1.0;
574         add(outputComboBox, gridBagConstraints);
575
576         overwriteCheckBox.setSelected(true);
577         overwriteCheckBox.setText(Util.THIS.getString ("LBL_over_write"));
578         overwriteCheckBox.addActionListener(formListener);
579
580         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
581         gridBagConstraints.gridx = 1;
582         gridBagConstraints.gridy = 4;
583         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 0);
584         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
585         add(overwriteCheckBox, gridBagConstraints);
586
587         showLabel.setText(Util.THIS.getString ("LBL_show_output"));
588         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
589         gridBagConstraints.gridx = 0;
590         gridBagConstraints.gridy = 5;
591         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
592         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 12, 6, 0);
593         add(showLabel, gridBagConstraints);
594
595         showComboBox.setModel(new javax.swing.DefaultComboBoxModel JavaDoc (SHOW_NAMES));
596         showComboBox.addActionListener(formListener);
597
598         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
599         gridBagConstraints.gridx = 1;
600         gridBagConstraints.gridy = 5;
601         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
602         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 12, 6, 0);
603         gridBagConstraints.weightx = 1.0;
604         add(showComboBox, gridBagConstraints);
605
606     }
607
608     // Code for dispatching events from components to event handlers.
609

610     private class FormListener implements java.awt.event.ActionListener JavaDoc {
611         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
612             if (evt.getSource() == inputComboBox) {
613                 TransformPanel.this.inputComboBoxActionPerformed(evt);
614             }
615             else if (evt.getSource() == browseInputButton) {
616                 TransformPanel.this.browseInputButtonActionPerformed(evt);
617             }
618             else if (evt.getSource() == transformComboBox) {
619                 TransformPanel.this.transformComboBoxActionPerformed(evt);
620             }
621             else if (evt.getSource() == browseXSLTButton) {
622                 TransformPanel.this.browseXSLTButtonActionPerformed(evt);
623             }
624             else if (evt.getSource() == outputComboBox) {
625                 TransformPanel.this.outputComboBoxActionPerformed(evt);
626             }
627             else if (evt.getSource() == overwriteCheckBox) {
628                 TransformPanel.this.overwriteCheckBoxActionPerformed(evt);
629             }
630             else if (evt.getSource() == showComboBox) {
631                 TransformPanel.this.showComboBoxActionPerformed(evt);
632             }
633         }
634     }//GEN-END:initComponents
635

636     
637     /** Initialize accesibility
638      */

639     private void initAccessibility() {
640         this.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_TransformPanel"));
641         
642         overwriteCheckBox.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_overwriteCheckBox"));
643         outputComboBox.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_outputComboBox"));
644         inputComboBox.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_inputComboBox"));
645         browseXSLTButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_browseXSLTButton"));
646         showComboBox.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_showComboBox"));
647         browseInputButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_browseInputButton"));
648         transformComboBox.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_transformComboBox"));
649     }
650     
651     
652     private void browseXSLTButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseXSLTButtonActionPerformed
653

654         try {
655             File JavaDoc selectedFile=getFileFromChooser(getXSL());
656             if (selectedFile==null) return;
657             FileObject fo = FileUtil.toFileObject(selectedFile);
658             DataObject dObj = DataObject.find(fo);
659             if (dObj==null || !TransformUtil.isXSLTransformation(dObj)) {
660                 NotifyDescriptor desc = new NotifyDescriptor.Message(
661                     Util.THIS.getString("MSG_notXslFile", //NOI18N
662
selectedFile.getName()),NotifyDescriptor.ERROR_MESSAGE);
663                 DialogDisplayer.getDefault().notify(desc);
664                 return;
665             }
666             setXSL(TransformUtil.getURLName(fo));
667
668             if ( ( userSetOutput == false ) && ( xmlHistory != null ) ) {
669                 setOutput(xmlHistory.getXSLOutput(data.xsl));
670             }
671             if ( userSetProcess == false ) {
672                 setProcessOutput(null);
673             }
674             updateXSLComboBoxModel(data.xsl);
675
676             updateComponents();
677
678             setCaretPosition(transformComboBox);
679             
680         } catch (IOException JavaDoc exc) { // TransformUtil.getURLName (...)
681
// ignore it
682
Util.THIS.debug(exc);
683         } finally {
684             Util.icons = null;
685         }
686     }//GEN-LAST:event_browseXSLTButtonActionPerformed
687

688     private void browseInputButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseInputButtonActionPerformed
689

690         try {
691             File JavaDoc selectedFile=getFileFromChooser(getInput());
692             if (selectedFile==null) return;
693             FileObject fo = FileUtil.toFileObject(selectedFile);
694             DataObject dObj = DataObject.find(fo);
695             if (dObj==null || dObj.getCookie(TransformableCookie.class)==null) {
696                 NotifyDescriptor desc = new NotifyDescriptor.Message(
697                     Util.THIS.getString("MSG_notXmlFile", //NOI18N
698
selectedFile.getName()),NotifyDescriptor.ERROR_MESSAGE);
699                 DialogDisplayer.getDefault().notify(desc);
700                 return;
701             }
702             setInput(TransformUtil.getURLName(fo));
703             
704             if ( ( userSetOutput == false ) && ( xslHistory != null ) ) {
705                 setOutput(xslHistory.getXMLOutput(data.xml));
706             }
707             if ( userSetProcess == false ) {
708                 setProcessOutput(null);
709             }
710             updateXMLComboBoxModel(data.xml);
711             
712             updateComponents();
713             
714             setCaretPosition(inputComboBox);
715             
716         } catch (IOException JavaDoc exc) { // TransformUtil.getURLName (...)
717
// ignore it
718
Util.THIS.debug(exc);
719         } finally {
720             Util.icons = null;
721         }
722     }//GEN-LAST:event_browseInputButtonActionPerformed
723

724     public void setInput(String JavaDoc input) {
725         if(data==null) {
726             return;
727         }
728         String JavaDoc oldInput=data.getInput();
729         data.setInput(input);
730         firePropertyChange(DATA_XML_MODIFIED,oldInput,input);
731     }
732     
733     public void setXSL(String JavaDoc xslValue) {
734         if(data==null) {
735             return;
736         }
737         String JavaDoc oldXSL=data.getXSL();
738         data.setXSL(xslValue);
739         firePropertyChange(DATA_XSL_MODIFIED,oldXSL,xslValue);
740     }
741     
742     public void setOutput(Object JavaDoc outputValue) {
743         if(data==null) {
744             return;
745         }
746         Object JavaDoc oldOutput=data.getOutput();
747         data.setOutput(outputValue);
748         firePropertyChange(DATA_OUTPUT_MODIFIED,oldOutput,outputValue);
749     }
750     
751     public void setOverwriteOutput(Boolean JavaDoc overwriteObject) {
752         if(data==null || overwriteObject==null) {
753             return;
754         }
755         setOverwriteOutput(overwriteObject.booleanValue());
756     }
757     
758     public void setOverwriteOutput(boolean overwriteValue) {
759         if(data==null) {
760             return;
761         }
762         boolean oldOverwrite=data.isOverwriteOutput();
763         data.setOverwriteOutput(overwriteValue);
764         firePropertyChange(DATA_OVERWRITE_MODIFIED,oldOverwrite,overwriteValue);
765     }
766     
767     public void setProcessOutput(Integer JavaDoc processObject) {
768         if(data==null || processObject==null) {
769             return;
770         }
771         setProcessOutput(processObject.intValue());
772     }
773     
774     public void setProcessOutput(int processValue) {
775         if(data==null) {
776             return;
777         }
778         int oldProcess=data.getProcessOutput();
779         data.setProcessOutput(processValue);
780         firePropertyChange(DATA_PROCESS_MODIFIED,oldProcess,processValue);
781     }
782     
783     private void showComboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_showComboBoxActionPerformed
784
// Add your handling code here:
785
if ( isInitialized() ) {
786             setProcessOutput(new Integer JavaDoc(showComboBox.getSelectedIndex()));
787             userSetProcess = true;
788             updateComponents();
789         }
790     }//GEN-LAST:event_showComboBoxActionPerformed
791

792     private void overwriteCheckBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_overwriteCheckBoxActionPerformed
793
// Add your handling code here:
794
if ( isInitialized() ) {
795             setOverwriteOutput( overwriteCheckBox.isSelected() );
796             updateComponents();
797         }
798     }//GEN-LAST:event_overwriteCheckBoxActionPerformed
799

800     private void transformComboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_transformComboBoxActionPerformed
801
// Add your handling code here:
802
if ( isInitialized() ) {
803             String JavaDoc item = (String JavaDoc) transformComboBox.getSelectedItem();
804             
805             if ( Util.THIS.isLoggable() ) /* then */ {
806                 Util.THIS.debug("TransformPanel.transformComboBoxActionPerformed: getSelectedItem = " + item);
807             }
808             
809             if ( item == null ) {
810                 return;
811             }
812             
813             setXSL(item.trim());
814             
815             if ( ( userSetOutput == false ) && ( xmlHistory != null ) ) {
816                 setOutput(xmlHistory.getXSLOutput(data.xsl));
817             }
818             if ( userSetProcess == false ) {
819                 setProcessOutput(null);
820             }
821             
822             updateComponents();
823             
824             // setCaretPosition (transformComboBox);
825
}
826     }//GEN-LAST:event_transformComboBoxActionPerformed
827

828     private void inputComboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_inputComboBoxActionPerformed
829
// Add your handling code here:
830
if ( isInitialized() ) {
831             String JavaDoc item = (String JavaDoc) inputComboBox.getSelectedItem();
832             
833             if ( Util.THIS.isLoggable() ) /* then */ {
834                 Util.THIS.debug("TransformPanel.inputComboBoxActionPerformed: getSelectedItem = " + item);
835             }
836             
837             if ( item == null ) {
838                 return;
839             }
840             
841             setInput(item.trim());
842             
843             if ( ( userSetOutput == false ) && ( xslHistory != null ) ) {
844                 setOutput(xslHistory.getXMLOutput(data.xml));
845             }
846             if ( userSetProcess == false ) {
847                 setProcessOutput(null);
848             }
849             
850             updateComponents();
851             
852             // setCaretPosition (inputComboBox);
853
}
854     }//GEN-LAST:event_inputComboBoxActionPerformed
855

856     private void outputComboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_outputComboBoxActionPerformed
857
// Add your handling code here:
858
if ( isInitialized() ) {
859             Object JavaDoc item = outputComboBox.getSelectedItem();
860             if ( item instanceof String JavaDoc ) {
861                 String JavaDoc str = ((String JavaDoc) item).trim();
862                 if ( str.length() == 0 ) {
863                     str = null;
864                 }
865                 item = str;
866             }
867             setOutput(item);
868             
869             
870             userSetOutput = true;
871             updateComponents();
872             
873             
874             // setCaretPosition (outputComboBox);
875
}
876     }//GEN-LAST:event_outputComboBoxActionPerformed
877

878     // Variables declaration - do not modify//GEN-BEGIN:variables
879
private javax.swing.JButton JavaDoc browseInputButton;
880     private javax.swing.JButton JavaDoc browseXSLTButton;
881     private javax.swing.JComboBox JavaDoc inputComboBox;
882     private javax.swing.JLabel JavaDoc inputLabel;
883     private javax.swing.JComboBox JavaDoc outputComboBox;
884     private javax.swing.JLabel JavaDoc outputLabel;
885     private javax.swing.JCheckBox JavaDoc overwriteCheckBox;
886     private javax.swing.JComboBox JavaDoc showComboBox;
887     private javax.swing.JLabel JavaDoc showLabel;
888     private javax.swing.JComboBox JavaDoc transformComboBox;
889     private javax.swing.JLabel JavaDoc transformLabel;
890     // End of variables declaration//GEN-END:variables
891

892     
893     //
894
// class Data
895
//
896

897     public static final class Data {
898         private String JavaDoc xml;
899         private String JavaDoc xsl;
900         private Object JavaDoc output;
901         private Boolean JavaDoc overwrite;
902         private Integer JavaDoc process;
903         
904         public Data() {
905             this.xml = null;
906             this.xsl = null;
907             this.output = "";
908             this.overwrite = null;
909             this.process = null;
910         }
911         
912         public Data(String JavaDoc xml, String JavaDoc xsl, Object JavaDoc output, boolean overwrite, int process) {
913             this.xml = xml;
914             this.xsl = xsl;
915             this.output = output;
916             this.overwrite = overwrite ? Boolean.TRUE : Boolean.FALSE;
917             this.process = process == -1 ? null : new Integer JavaDoc(process);
918         }
919         
920         /**
921          * @return selected XML input.
922          */

923         public String JavaDoc getInput() {
924             return xml;
925         }
926         
927         /**
928          * @return selected XSLT script.
929          */

930         public String JavaDoc getXSL() {
931             return xsl;
932         }
933         
934         /**
935          * @return selected output.
936          */

937         public Object JavaDoc getOutput() {
938             return output;
939         }
940         
941         /**
942          * @return selected overwrite output option.
943          */

944         public boolean isOverwriteOutput() {
945             if(overwrite==null) {
946                 return false;
947             }
948             return overwrite.booleanValue();
949         }
950         
951         /**
952          * @return selected process output option.
953          */

954         public int getProcessOutput() {
955             if(process==null) {
956                 return 0;
957             }
958             return process.intValue();
959         }
960         
961         public void setInput(String JavaDoc input) {
962             xml=input;
963         }
964         
965         public void setXSL(String JavaDoc xslValue) {
966             xsl=xslValue;
967         }
968         
969         public void setOutput(Object JavaDoc outputValue) {
970             if (JUST_PREVIEW.equals(outputValue)) {
971                 output = null;
972             } else {
973                 output=outputValue;
974             }
975         }
976         
977         public void setOverwriteOutput(boolean overwriteValue) {
978             overwrite = overwriteValue ? Boolean.TRUE : Boolean.FALSE;
979         }
980         
981         public void setProcessOutput(Integer JavaDoc processObject) {
982             process=processObject;
983         }
984         
985         public void setProcessOutput(int processValue) {
986             setProcessOutput(new Integer JavaDoc(processValue));
987         }
988         
989         public String JavaDoc toString() {
990             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
991             
992             sb.append("[input='").append(xml).append("'; ");
993             sb.append("xsl='").append(xsl).append("'; ");
994             sb.append("output='").append(output).append("'; ");
995             sb.append("overwrite='").append(overwrite).append("'; ");
996             sb.append("process='").append(process).append("]");
997             
998             return sb.toString();
999         }
1000        
1001    } // class Data
1002

1003    
1004    //
1005
// class Preview
1006
//
1007

1008    private static class Preview {
1009        public String JavaDoc toString() {
1010            return Util.THIS.getString("NAME_output_just_preview");
1011        }
1012    } // class Preview
1013

1014    
1015    /** Open the file chooser and return the file.
1016     *@param oldUrl url where to start browsing
1017     */

1018    private File JavaDoc getFileFromChooser(String JavaDoc oldUrl) {
1019        javax.swing.JFileChooser JavaDoc chooser = new javax.swing.JFileChooser JavaDoc();
1020        if (oldUrl!=null) {
1021            try {
1022                File JavaDoc file=null;
1023                java.net.URI JavaDoc url = new java.net.URI JavaDoc(oldUrl);
1024                if (url!=null) {
1025                    file = new File JavaDoc(url);
1026                }
1027                if (file!=null) {
1028                    File JavaDoc parentDir = file.getParentFile();
1029                    if (parentDir!=null && parentDir.exists() )
1030                        chooser.setCurrentDirectory(parentDir);
1031                }
1032            } catch (java.net.URISyntaxException JavaDoc ex) {}
1033            catch (java.lang.IllegalArgumentException JavaDoc x) {}
1034        }
1035        File JavaDoc selectedFile=null;
1036        if ( chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ) {
1037            selectedFile = chooser.getSelectedFile();
1038        }
1039        return selectedFile;
1040    }
1041
1042}
1043
Popular Tags