KickJava   Java API By Example, From Geeks To Geeks.

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


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 // ToolBox imports
26
import org.enhydra.tool.common.DialogPanel;
27 import org.enhydra.tool.common.ExtensionFilter;
28 import org.enhydra.tool.common.PathHandle;
29
30 // Kelp imports
31
import org.enhydra.kelp.common.Constants;
32 import org.enhydra.kelp.common.node.OtterNode;
33 import org.enhydra.kelp.common.node.OtterFileNode;
34 import org.enhydra.kelp.common.properties.XMLCParameterViewer;
35
36 // Standard imports
37
import javax.swing.*;
38 import javax.swing.border.TitledBorder JavaDoc;
39 import java.awt.event.*;
40 import java.io.File JavaDoc;
41 import java.awt.*;
42 import java.beans.*;
43 import java.util.ResourceBundle JavaDoc;
44
45 /**
46  * Class declaration
47  *
48  *
49  * @author Paul Mahar
50  */

51 public class XMLCOptionPanel extends JPanel {
52     static ResourceBundle JavaDoc res =
53         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
54
private GridBagLayout layoutMain = new GridBagLayout();
55     private OtterNode node = null;
56     private JTextField textFile = null;
57     private JButton buttonBrowse = null;
58     private LocalButtonListener buttonListener = null;
59     private TitledBorder JavaDoc border = null;
60     private JTextField textCommandLine = null;
61     private JLabel labelCommandLine = null;
62     private JLabel labelFile = null;
63     private JButton buttonClear = null;
64     private JButton buttonTool = null;
65
66     /**
67      * Constructor declaration
68      *
69      */

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

86     public void setEnabled(boolean b) {
87         super.setEnabled(b);
88         textCommandLine.setEnabled(b);
89         textFile.setEnabled(b);
90         buttonTool.setEnabled(b);
91         buttonBrowse.setEnabled(b);
92         buttonClear.setEnabled(b);
93         if (b) {
94             textCommandLine.setBackground(SystemColor.info);
95             textFile.setBackground(SystemColor.info);
96         } else {
97             textCommandLine.setBackground(SystemColor.text);
98             textFile.setBackground(SystemColor.text);
99         }
100     }
101
102     /**
103      * Method declaration
104      *
105      *
106      * @return
107      */

108     public OtterNode getNode() {
109         return node;
110     }
111
112     /**
113      * Method declaration
114      *
115      *
116      * @param n
117      */

118     public void setNode(OtterNode n) {
119         node = n;
120     }
121
122     /**
123      * Method declaration
124      *
125      */

126     public void readProperties() {
127         String JavaDoc filename = null;
128         String JavaDoc parameters = null;
129
130         if (node == null) {
131             buttonBrowse.setEnabled(false);
132         } else {
133             buttonBrowse.setEnabled(true);
134             filename = node.getXMLCOptionFilePath();
135             parameters = node.getXMLCParameters();
136         }
137         if (filename != null) {
138             File JavaDoc file = new File JavaDoc(filename);
139
140             if (file.isFile()) {
141                 textFile.setText(filename);
142             }
143         }
144         if (parameters != null) {
145             textCommandLine.setText(parameters);
146         }
147     }
148
149     /**
150      * Method declaration
151      *
152      */

153     public void writeProperties() {
154         String JavaDoc parameters = textCommandLine.getText().trim();
155
156         if (node != null) {
157             node.setXMLCParameters(parameters);
158             node.setXMLCOptionFilePath(getValidFilename());
159         }
160     }
161
162     /**
163      * Method declaration
164      *
165      *
166      * @return
167      */

168     private String JavaDoc getValidFilename() {
169         PathHandle path = null;
170         String JavaDoc returnPath = new String JavaDoc();
171
172         path = PathHandle.createPathHandle(textFile.getText());
173         if (path.isFile() && path.hasExtension(Constants.TYPE_XMLC)) {
174             returnPath = path.getPath();
175             textFile.setText(path.getPath());
176         } else {
177             textFile.setText(new String JavaDoc());
178         }
179         return returnPath;
180     }
181
182     /**
183      * Method declaration
184      *
185      *
186      * @return
187      */

188     private boolean isValidFilename() {
189         return (getValidFilename().length() > 0);
190     }
191
192     /**
193      * Method declaration
194      *
195      */

196     private void browseForFile() {
197         JFileChooser chooser;
198         ExtensionFilter filter;
199         File JavaDoc file;
200         String JavaDoc fileDir = new String JavaDoc();
201         String JavaDoc fileName = new String JavaDoc();
202
203         filter = new ExtensionFilter();
204         filter.addExtension(Constants.TYPE_XMLC);
205         filter.setDescriptionTitle(res.getString("filter_DescriptionTitle"));
206         chooser = new JFileChooser();
207         if (textFile.getText().trim().length() > 0) {
208             chooser.setCurrentDirectory(new File JavaDoc(textFile.getText()));
209         } else {
210             PathHandle ph = null;
211
212             if (node instanceof OtterFileNode) {
213                OtterFileNode fileNode = (OtterFileNode) node;
214                 ph = PathHandle.createPathHandle(fileNode.getFilePath());
215                 if (ph.isEmpty()) {
216                   ph = null;
217                 } else {
218                   ph = ph.getParent();
219                 }
220             }
221             if (ph == null) {
222                 ph = PathHandle.createPathHandle(node.getProject().getRootPath());
223             }
224             chooser.setCurrentDirectory(ph.getFile());
225         }
226         chooser.setFileFilter(filter);
227         chooser.setDialogTitle(res.getString("chooser_DialogTitle"));
228         chooser.setApproveButtonText(res.getString("OK"));
229         int v = chooser.showOpenDialog(this);
230
231         this.requestFocus();
232         buttonBrowse.requestFocus();
233         if (v == JFileChooser.APPROVE_OPTION) {
234             if (chooser.getSelectedFile() == null
235                     || (!chooser.getSelectedFile().isFile())) {
236                 textFile.setText(new String JavaDoc());
237             } else {
238                 fileDir = chooser.getCurrentDirectory().toString();
239                 fileDir = stripSlash(fileDir);
240                 fileName = chooser.getSelectedFile().getName();
241                 textFile.setText(fileDir + File.separator + fileName);
242             }
243         }
244         chooser.removeAll();
245         chooser = null;
246     }
247
248     /**
249      * Method declaration
250      *
251      *
252      * @param dir
253      *
254      * @return
255      */

256     private String JavaDoc stripSlash(String JavaDoc dir) {
257         String JavaDoc stripped = new String JavaDoc(dir);
258
259         if (dir != null) {
260             if (dir.length() > 0) {
261                 if (dir.endsWith(File.separator)) {
262                     stripped = dir.substring(0, dir.length() - 1);
263                 }
264             }
265         }
266         return stripped;
267     }
268
269     private void editParameters() {
270         String JavaDoc params = new String JavaDoc();
271         XMLCParameterViewer viewer = null;
272
273         params = textCommandLine.getText();
274         viewer = new XMLCParameterViewer();
275         viewer.setParameters(params);
276         viewer.setOwner((Dialog) getTopLevelAncestor());
277         if (viewer.showDialog() == DialogPanel.OK_OPTION) {
278             params = viewer.getParameters();
279             textCommandLine.setText(params);
280         }
281     }
282
283     /**
284      * Method declaration
285      *
286      */

287     private void pmInit() {
288         textCommandLine.setText(new String JavaDoc());
289         buttonListener = new LocalButtonListener();
290         buttonBrowse.addActionListener(buttonListener);
291         buttonClear.addActionListener(buttonListener);
292         buttonTool.addActionListener(buttonListener);
293         setEnabled(true);
294     }
295
296     /**
297      * Method declaration
298      *
299      *
300      * @exception Exception
301      */

302     private void jbInit() throws Exception JavaDoc {
303         buttonBrowse =
304             (JButton) Beans.instantiate(getClass().getClassLoader(),
305                                         JButton.class.getName());
306         textFile = (JTextField) Beans.instantiate(getClass().getClassLoader(),
307                                                   JTextField.class.getName());
308         border =
309             BorderFactory.createTitledBorder(res.getString("XMLC_Options"));
310         textCommandLine =
311             (JTextField) Beans.instantiate(getClass().getClassLoader(),
312                                            JTextField.class.getName());
313         labelCommandLine =
314             (JLabel) Beans.instantiate(getClass().getClassLoader(),
315                                        JLabel.class.getName());
316         labelFile = (JLabel) Beans.instantiate(getClass().getClassLoader(),
317                                                JLabel.class.getName());
318         buttonClear = (JButton) Beans.instantiate(getClass().getClassLoader(),
319                                                   JButton.class.getName());
320         buttonTool = (JButton) Beans.instantiate(getClass().getClassLoader(),
321                                                  JButton.class.getName());
322         textFile.setMinimumSize(new Dimension(100, 21));
323         textFile.setPreferredSize(new Dimension(100, 21));
324         textFile.setEditable(false);
325         buttonBrowse.setText(res.getString("Browse"));
326         labelCommandLine.setText(res.getString("labelCommandLine_Text"));
327         labelFile.setText(res.getString("labelFile_Text"));
328         buttonClear.setText(res.getString("Clear"));
329         buttonTool.setText("..."); // nores
330
this.setLayout(layoutMain);
331         this.setBorder(border);
332         this.add(textFile,
333                  new GridBagConstraints(0, 4, 1, 1, 1.0, 0.0,
334                                         GridBagConstraints.WEST,
335                                         GridBagConstraints.HORIZONTAL,
336                                         new Insets(1, 2, 2, 2), 100, 0));
337         this.add(buttonBrowse,
338                  new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
339                                         GridBagConstraints.CENTER,
340                                         GridBagConstraints.NONE,
341                                         new Insets(1, 2, 2, 2), 0, 0));
342         this.add(labelCommandLine,
343                  new GridBagConstraints(0, 1, 4, 1, 0.0, 0.0,
344                                         GridBagConstraints.WEST,
345                                         GridBagConstraints.HORIZONTAL,
346                                         new Insets(2, 2, 1, 2), 0, 0));
347         this.add(textCommandLine,
348                  new GridBagConstraints(0, 2, 3, 1, 0.0, 0.0,
349                                         GridBagConstraints.WEST,
350                                         GridBagConstraints.HORIZONTAL,
351                                         new Insets(1, 2, 2, 2), 0, 0));
352         this.add(labelFile,
353                  new GridBagConstraints(0, 3, 4, 1, 0.0, 0.0,
354                                         GridBagConstraints.WEST,
355                                         GridBagConstraints.HORIZONTAL,
356                                         new Insets(2, 2, 1, 2), 0, 0));
357         this.add(buttonClear,
358                  new GridBagConstraints(2, 4, 2, 1, 0.0, 0.0,
359                                         GridBagConstraints.CENTER,
360                                         GridBagConstraints.NONE,
361                                         new Insets(1, 2, 2, 2), 0, 0));
362         this.add(buttonTool,
363                  new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0,
364                                         GridBagConstraints.CENTER,
365                                         GridBagConstraints.NONE,
366                                         new Insets(1, 2, 2, 2), 0, 0));
367     }
368
369     //
370
private class LocalButtonListener implements ActionListener {
371         public void actionPerformed(ActionEvent e) {
372             Object JavaDoc source = e.getSource();
373
374             if (source == buttonBrowse) {
375                 browseForFile();
376             } else if (source == buttonClear) {
377                 textFile.setText(new String JavaDoc());
378             } else if (source == buttonTool) {
379                 editParameters();
380             }
381         }
382
383     }
384 }
385
Popular Tags