KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > ant > swing > AntOutputPanel


1 package org.enhydra.kelp.ant.swing;
2
3
4 // AddinCore
5
import org.enhydra.kelp.common.Constants;
6 import org.enhydra.kelp.common.event.WriteEvent;
7 import org.enhydra.kelp.common.event.WriteListener;
8 import org.enhydra.kelp.common.node.OtterProject;
9 import org.enhydra.kelp.common.swing.OutputPanel;
10
11 // ToolBox
12
import org.enhydra.tool.common.ExtensionFilter;
13 import org.enhydra.tool.common.PathHandle;
14
15 // JDK
16
import javax.swing.*;
17 import javax.swing.text.BadLocationException JavaDoc;
18 import java.awt.*;
19 import java.awt.event.*;
20 import java.beans.Beans JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileWriter JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24 import org.enhydra.kelp.ant.node.AntProject;
25
26 //
27
public class AntOutputPanel extends JPanel implements WriteListener {
28
29     //
30
public static ResourceBundle JavaDoc res =
31         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
32
private LocalButtonListener buttonListener = null;
33     private LocalCheckListener checkListener = null;
34     private OtterProject project = null;
35     private JPanel panelFile;
36     private JCheckBox checkSave;
37     private OutputPanel outputPanel;
38     private JTextField textFile;
39     private JButton buttonBrowse;
40     private GridBagLayout layoutFile;
41     private GridBagLayout layoutMain;
42     private int waitCount = 0;
43
44     /**
45      * Constructor declaration
46      *
47      */

48     public AntOutputPanel() {
49         try {
50             jbInit();
51             pmInit();
52         } catch (Exception JavaDoc e) {
53             e.printStackTrace();
54         }
55     }
56
57     /**
58      * Method declaration
59      *
60      *
61      * @param event
62      */

63     public void onWrite(WriteEvent event) {
64         outputPanel.onWrite(event);
65     }
66
67     /**
68      * Method declaration
69      *
70      *
71      * @return
72      */

73     public OtterProject getProject() {
74         return project;
75     }
76
77     /**
78      * Method declaration
79      *
80      *
81      * @param p
82      */

83     public void setProject(OtterProject p) {
84         project = p;
85         String JavaDoc outPath = project.getOutputFilename();
86
87         if ((outPath == null) || (outPath.trim().length() == 0)) {
88             checkSave.setSelected(false);
89             textFile.setText(new String JavaDoc());
90         } else {
91             textFile.setText(PathHandle.createPathString(outPath));
92             checkSave.setSelected(true);
93         }
94         outputPanel.setProject(project);
95     }
96
97     public void clearOutput() {
98         outputPanel.clearOutput();
99     }
100
101     //
102
// PROTECTED
103
//
104

105     /**
106      * Method declaration
107      *
108      */

109     protected void scrollToBottom() {
110         outputPanel.scrollToBottom();
111     }
112
113     //
114
// PRIVATE
115
//
116
private void buttonFileAction() {
117         JFileChooser chooser = null;
118         ExtensionFilter filter = null;
119         PathHandle path = null;
120         File JavaDoc file = null;
121         String JavaDoc fileDir = new String JavaDoc();
122         int choice = -1;
123
124         //
125
filter = new ExtensionFilter();
126         filter.addExtension(Constants.TYPE_TXT);
127         filter.setDescriptionTitle(res.getString("Kelp_log"));
128
129         //
130
chooser = new JFileChooser();
131         if (textFile.getText().trim().length() == 0) {
132             file = new File JavaDoc(project.getRootPath() + File.separator
133                             + Constants.FILE_KELP_TXT);
134         } else {
135             file = new File JavaDoc(textFile.getText());
136         }
137         chooser.setSelectedFile(file);
138         chooser.setFileFilter(filter);
139         chooser.setDialogTitle(res.getString("Select_log"));
140         chooser.setApproveButtonText(res.getString("OK"));
141         choice = chooser.showOpenDialog(this);
142
143         //
144
this.requestFocus();
145         buttonBrowse.requestFocus();
146
147         //
148
if (choice == JFileChooser.APPROVE_OPTION
149                 && chooser.getSelectedFile() != null) {
150             path = PathHandle.createPathHandle(chooser.getSelectedFile());
151             textFile.setText(path.getPath());
152             project.setOutputFilename(path.getPath());
153         }
154
155         //
156
chooser.removeAll();
157         chooser = null;
158     }
159
160     /**
161      * Method declaration
162      *
163      */

164     private void pmInit() {
165         buttonListener = new LocalButtonListener();
166         buttonBrowse.addActionListener(buttonListener);
167         buttonBrowse.setEnabled(false);
168         checkListener = new LocalCheckListener();
169         checkSave.addItemListener(checkListener);
170         checkSave.setSelected(false);
171     }
172
173     /**
174      * Method declaration
175      *
176      *
177      * @throws Exception
178      */

179     private void jbInit() throws Exception JavaDoc {
180         panelFile = (JPanel) Beans.instantiate(getClass().getClassLoader(),
181                                                JPanel.class.getName());
182         outputPanel =
183             (OutputPanel) Beans.instantiate(getClass().getClassLoader(),
184                                             OutputPanel.class.getName());
185         checkSave = (JCheckBox) Beans.instantiate(getClass().getClassLoader(),
186                                                   JCheckBox.class.getName());
187         textFile = (JTextField) Beans.instantiate(getClass().getClassLoader(),
188                                                   JTextField.class.getName());
189         buttonBrowse =
190             (JButton) Beans.instantiate(getClass().getClassLoader(),
191                                         JButton.class.getName());
192         layoutFile =
193             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
194                                               GridBagLayout.class.getName());
195         layoutMain =
196             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
197                                               GridBagLayout.class.getName());
198         buttonBrowse.setText(res.getString("Browse"));
199         panelFile.setLayout(layoutFile);
200         checkSave.setText(res.getString("Output_to_log"));
201         textFile.setBackground(SystemColor.controlLtHighlight);
202         textFile.setEnabled(false);
203         textFile.setPreferredSize(new Dimension(150, 21));
204         panelFile.add(checkSave,
205                       new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
206                                              GridBagConstraints.WEST,
207                                              GridBagConstraints.BOTH,
208                                              new Insets(5, 5, 5, 5), 14, 0));
209         panelFile.add(textFile,
210                       new GridBagConstraints(1, 0, 1, 1, 0.5, 0.5,
211                                              GridBagConstraints.CENTER,
212                                              GridBagConstraints.HORIZONTAL,
213                                              new Insets(5, 5, 5, 5), 14, 0));
214         panelFile.add(buttonBrowse,
215                       new GridBagConstraints(2, 0, 1, 1, 0.5, 0.5,
216                                              GridBagConstraints.WEST,
217                                              GridBagConstraints.NONE,
218                                              new Insets(5, 5, 5, 5), 6, 0));
219         this.setLayout(layoutMain);
220         this.add(outputPanel,
221                  new GridBagConstraints(0, 0, 1, 1, 0.75, 0.75,
222                                         GridBagConstraints.WEST,
223                                         GridBagConstraints.BOTH,
224                                         new Insets(8, 12, 0, 0), 0, 0));
225         this.add(panelFile,
226                  new GridBagConstraints(0, 1, 1, 1, 0.25, 0.25,
227                                         GridBagConstraints.CENTER,
228                                         GridBagConstraints.BOTH,
229                                         new Insets(0, 12, 0, 0), 0, 0));
230     }
231
232     /**
233      */

234     private class LocalCheckListener implements ItemListener {
235         public void itemStateChanged(ItemEvent event) {
236             Object JavaDoc source = event.getSource();
237
238             if (source == checkSave) {
239                 if (checkSave.isSelected()) {
240                     buttonBrowse.setEnabled(true);
241                     ((AntProject)project).setOutputFileEnabled(true);
242                 } else {
243                     buttonBrowse.setEnabled(false);
244                     textFile.setText(new String JavaDoc());
245                     if (project == null) {
246
247                         // done
248
} else {
249                         project.setOutputFilename(new String JavaDoc());
250                         ((AntProject)project).setOutputFileEnabled(false);
251                     }
252                 }
253             }
254         }
255
256     }
257
258     /**
259      */

260     private class LocalButtonListener implements ActionListener {
261         public void actionPerformed(ActionEvent event) {
262             Object JavaDoc source = event.getSource();
263
264             if (source == buttonBrowse) {
265                 buttonFileAction();
266             }
267         }
268
269     }
270
271 }
272
Popular Tags