KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > xmlc > XMLCOutputPanel


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common.xmlc;
24
25 // AddinCore
26
import org.enhydra.kelp.common.Constants;
27 import org.enhydra.kelp.common.event.WriteEvent;
28 import org.enhydra.kelp.common.event.WriteListener;
29 import org.enhydra.kelp.common.node.OtterProject;
30 import org.enhydra.kelp.common.swing.OutputPanel;
31
32 // ToolBox
33
import org.enhydra.tool.common.ExtensionFilter;
34 import org.enhydra.tool.common.PathHandle;
35
36 // JDK
37
import javax.swing.*;
38 import javax.swing.text.BadLocationException JavaDoc;
39 import java.awt.*;
40 import java.awt.event.*;
41 import java.beans.Beans JavaDoc;
42 import java.io.File JavaDoc;
43 import java.io.FileWriter JavaDoc;
44 import java.util.ResourceBundle JavaDoc;
45
46 //
47
public class XMLCOutputPanel extends JPanel implements WriteListener {
48
49     //
50
public static ResourceBundle JavaDoc res =
51         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
52
private LocalButtonListener buttonListener = null;
53     private LocalCheckListener checkListener = null;
54     private OtterProject project = null;
55     private JPanel panelFile;
56     private JCheckBox checkSave;
57     private OutputPanel outputPanel;
58     private JTextField textFile;
59     private JButton buttonBrowse;
60     private GridBagLayout layoutFile;
61     private GridBagLayout layoutMain;
62     private int waitCount = 0;
63
64     /**
65      * Constructor declaration
66      *
67      */

68     public XMLCOutputPanel() {
69         try {
70             jbInit();
71             pmInit();
72         } catch (Exception JavaDoc e) {
73             e.printStackTrace();
74         }
75     }
76
77     /**
78      * Method declaration
79      *
80      *
81      * @param event
82      */

83     public void onWrite(WriteEvent event) {
84         outputPanel.onWrite(event);
85     }
86
87     /**
88      * Method declaration
89      *
90      *
91      * @return
92      */

93     public OtterProject getProject() {
94         return project;
95     }
96
97     /**
98      * Method declaration
99      *
100      *
101      * @param p
102      */

103     public void setProject(OtterProject p) {
104         project = p;
105         String JavaDoc outPath = project.getOutputFilename();
106
107         if ((outPath == null) || (outPath.trim().length() == 0)) {
108             checkSave.setSelected(false);
109             textFile.setText(new String JavaDoc());
110         } else {
111             textFile.setText(PathHandle.createPathString(outPath));
112             checkSave.setSelected(true);
113         }
114         outputPanel.setProject(project);
115     }
116
117     public void clearOutput() {
118         outputPanel.clearOutput();
119     }
120
121     //
122
// PROTECTED
123
//
124

125     /**
126      * Method declaration
127      *
128      */

129     protected void scrollToBottom() {
130         outputPanel.scrollToBottom();
131     }
132
133     //
134
// PRIVATE
135
//
136
private void buttonFileAction() {
137         JFileChooser chooser = null;
138         ExtensionFilter filter = null;
139         PathHandle path = null;
140         File JavaDoc file = null;
141         String JavaDoc fileDir = new String JavaDoc();
142         int choice = -1;
143
144         //
145
filter = new ExtensionFilter();
146         filter.addExtension(Constants.TYPE_TXT);
147         filter.setDescriptionTitle(res.getString("Kelp_log"));
148
149         //
150
chooser = new JFileChooser();
151         if (textFile.getText().trim().length() == 0) {
152             file = new File JavaDoc(project.getRootPath() + File.separator
153                             + Constants.FILE_KELP_TXT);
154         } else {
155             file = new File JavaDoc(textFile.getText());
156         }
157         chooser.setSelectedFile(file);
158         chooser.setFileFilter(filter);
159         chooser.setDialogTitle(res.getString("Select_log"));
160         chooser.setApproveButtonText(res.getString("OK"));
161         choice = chooser.showOpenDialog(this);
162
163         //
164
this.requestFocus();
165         buttonBrowse.requestFocus();
166
167         //
168
if (choice == JFileChooser.APPROVE_OPTION
169                 && chooser.getSelectedFile() != null) {
170             path = PathHandle.createPathHandle(chooser.getSelectedFile());
171             textFile.setText(path.getPath());
172             project.setOutputFilename(path.getPath());
173         }
174
175         //
176
chooser.removeAll();
177         chooser = null;
178     }
179
180     /**
181      * Method declaration
182      *
183      */

184     private void pmInit() {
185         buttonListener = new LocalButtonListener();
186         buttonBrowse.addActionListener(buttonListener);
187         buttonBrowse.setEnabled(false);
188         checkListener = new LocalCheckListener();
189         checkSave.addItemListener(checkListener);
190         checkSave.setSelected(false);
191     }
192
193     /**
194      * Method declaration
195      *
196      *
197      * @throws Exception
198      */

199     private void jbInit() throws Exception JavaDoc {
200         panelFile = (JPanel) Beans.instantiate(getClass().getClassLoader(),
201                                                JPanel.class.getName());
202         outputPanel =
203             (OutputPanel) Beans.instantiate(getClass().getClassLoader(),
204                                             OutputPanel.class.getName());
205         checkSave = (JCheckBox) Beans.instantiate(getClass().getClassLoader(),
206                                                   JCheckBox.class.getName());
207         textFile = (JTextField) Beans.instantiate(getClass().getClassLoader(),
208                                                   JTextField.class.getName());
209         buttonBrowse =
210             (JButton) Beans.instantiate(getClass().getClassLoader(),
211                                         JButton.class.getName());
212         layoutFile =
213             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
214                                               GridBagLayout.class.getName());
215         layoutMain =
216             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
217                                               GridBagLayout.class.getName());
218         buttonBrowse.setText(res.getString("Browse"));
219         panelFile.setLayout(layoutFile);
220         checkSave.setText(res.getString("Output_to_log"));
221         textFile.setBackground(SystemColor.controlLtHighlight);
222         textFile.setEnabled(false);
223         textFile.setPreferredSize(new Dimension(150, 21));
224         panelFile.add(checkSave,
225                       new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
226                                              GridBagConstraints.WEST,
227                                              GridBagConstraints.BOTH,
228                                              new Insets(5, 5, 5, 5), 14, 0));
229         panelFile.add(textFile,
230                       new GridBagConstraints(1, 0, 1, 1, 0.5, 0.5,
231                                              GridBagConstraints.CENTER,
232                                              GridBagConstraints.HORIZONTAL,
233                                              new Insets(5, 5, 5, 5), 14, 0));
234         panelFile.add(buttonBrowse,
235                       new GridBagConstraints(2, 0, 1, 1, 0.5, 0.5,
236                                              GridBagConstraints.WEST,
237                                              GridBagConstraints.NONE,
238                                              new Insets(5, 5, 5, 5), 6, 0));
239         this.setLayout(layoutMain);
240         this.add(outputPanel,
241                  new GridBagConstraints(0, 0, 1, 1, 0.75, 0.75,
242                                         GridBagConstraints.WEST,
243                                         GridBagConstraints.BOTH,
244                                         new Insets(8, 12, 0, 0), 0, 0));
245         this.add(panelFile,
246                  new GridBagConstraints(0, 1, 1, 1, 0.25, 0.25,
247                                         GridBagConstraints.CENTER,
248                                         GridBagConstraints.BOTH,
249                                         new Insets(0, 12, 0, 0), 0, 0));
250     }
251
252     /**
253      */

254     private class LocalCheckListener implements ItemListener {
255         public void itemStateChanged(ItemEvent event) {
256             Object JavaDoc source = event.getSource();
257
258             if (source == checkSave) {
259                 if (checkSave.isSelected()) {
260                     buttonBrowse.setEnabled(true);
261                 } else {
262                     buttonBrowse.setEnabled(false);
263                     textFile.setText(new String JavaDoc());
264                     if (project == null) {
265
266                         // done
267
} else {
268                         project.setOutputFilename(new String JavaDoc());
269                     }
270                 }
271             }
272         }
273
274     }
275
276     /**
277      */

278     private class LocalButtonListener implements ActionListener {
279         public void actionPerformed(ActionEvent event) {
280             Object JavaDoc source = event.getSource();
281
282             if (source == buttonBrowse) {
283                 buttonFileAction();
284             }
285         }
286
287     }
288 }
289
Popular Tags