001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp.common.xmlc;
024:
025: // ToolBox imports
026: import org.enhydra.tool.common.DialogPanel;
027: import org.enhydra.tool.common.ExtensionFilter;
028: import org.enhydra.tool.common.PathHandle;
029:
030: // Kelp imports
031: import org.enhydra.kelp.common.Constants;
032: import org.enhydra.kelp.common.node.OtterNode;
033: import org.enhydra.kelp.common.node.OtterFileNode;
034: import org.enhydra.kelp.common.properties.XMLCParameterViewer;
035:
036: // Standard imports
037: import javax.swing.*;
038: import javax.swing.border.TitledBorder;
039: import java.awt.event.*;
040: import java.io.File;
041: import java.awt.*;
042: import java.beans.*;
043: import java.util.ResourceBundle;
044:
045: /**
046: * Class declaration
047: *
048: *
049: * @author Paul Mahar
050: */
051: public class XMLCOptionPanel extends JPanel {
052: static ResourceBundle res = ResourceBundle
053: .getBundle("org.enhydra.kelp.common.Res"); // nores
054: private GridBagLayout layoutMain = new GridBagLayout();
055: private OtterNode node = null;
056: private JTextField textFile = null;
057: private JButton buttonBrowse = null;
058: private LocalButtonListener buttonListener = null;
059: private TitledBorder border = null;
060: private JTextField textCommandLine = null;
061: private JLabel labelCommandLine = null;
062: private JLabel labelFile = null;
063: private JButton buttonClear = null;
064: private JButton buttonTool = null;
065:
066: /**
067: * Constructor declaration
068: *
069: */
070: public XMLCOptionPanel() {
071: try {
072: jbInit();
073: pmInit();
074: } catch (Exception e) {
075: e.printStackTrace();
076: }
077: }
078:
079: /**
080: * Method declaration
081: *
082: *
083: * @param e
084: */
085: public void setEnabled(boolean b) {
086: super .setEnabled(b);
087: textCommandLine.setEnabled(b);
088: textFile.setEnabled(b);
089: buttonTool.setEnabled(b);
090: buttonBrowse.setEnabled(b);
091: buttonClear.setEnabled(b);
092: if (b) {
093: textCommandLine.setBackground(SystemColor.info);
094: textFile.setBackground(SystemColor.info);
095: } else {
096: textCommandLine.setBackground(SystemColor.text);
097: textFile.setBackground(SystemColor.text);
098: }
099: }
100:
101: /**
102: * Method declaration
103: *
104: *
105: * @return
106: */
107: public OtterNode getNode() {
108: return node;
109: }
110:
111: /**
112: * Method declaration
113: *
114: *
115: * @param n
116: */
117: public void setNode(OtterNode n) {
118: node = n;
119: }
120:
121: /**
122: * Method declaration
123: *
124: */
125: public void readProperties() {
126: String filename = null;
127: String parameters = null;
128:
129: if (node == null) {
130: buttonBrowse.setEnabled(false);
131: } else {
132: buttonBrowse.setEnabled(true);
133: filename = node.getXMLCOptionFilePath();
134: parameters = node.getXMLCParameters();
135: }
136: if (filename != null) {
137: File file = new File(filename);
138:
139: if (file.isFile()) {
140: textFile.setText(filename);
141: }
142: }
143: if (parameters != null) {
144: textCommandLine.setText(parameters);
145: }
146: }
147:
148: /**
149: * Method declaration
150: *
151: */
152: public void writeProperties() {
153: String parameters = textCommandLine.getText().trim();
154:
155: if (node != null) {
156: node.setXMLCParameters(parameters);
157: node.setXMLCOptionFilePath(getValidFilename());
158: }
159: }
160:
161: /**
162: * Method declaration
163: *
164: *
165: * @return
166: */
167: private String getValidFilename() {
168: PathHandle path = null;
169: String returnPath = new String();
170:
171: path = PathHandle.createPathHandle(textFile.getText());
172: if (path.isFile() && path.hasExtension(Constants.TYPE_XMLC)) {
173: returnPath = path.getPath();
174: textFile.setText(path.getPath());
175: } else {
176: textFile.setText(new String());
177: }
178: return returnPath;
179: }
180:
181: /**
182: * Method declaration
183: *
184: *
185: * @return
186: */
187: private boolean isValidFilename() {
188: return (getValidFilename().length() > 0);
189: }
190:
191: /**
192: * Method declaration
193: *
194: */
195: private void browseForFile() {
196: JFileChooser chooser;
197: ExtensionFilter filter;
198: File file;
199: String fileDir = new String();
200: String fileName = new String();
201:
202: filter = new ExtensionFilter();
203: filter.addExtension(Constants.TYPE_XMLC);
204: filter.setDescriptionTitle(res
205: .getString("filter_DescriptionTitle"));
206: chooser = new JFileChooser();
207: if (textFile.getText().trim().length() > 0) {
208: chooser.setCurrentDirectory(new File(textFile.getText()));
209: } else {
210: PathHandle ph = null;
211:
212: if (node instanceof OtterFileNode) {
213: OtterFileNode fileNode = (OtterFileNode) node;
214: ph = PathHandle
215: .createPathHandle(fileNode.getFilePath());
216: if (ph.isEmpty()) {
217: ph = null;
218: } else {
219: ph = ph.getParent();
220: }
221: }
222: if (ph == null) {
223: ph = PathHandle.createPathHandle(node.getProject()
224: .getRootPath());
225: }
226: chooser.setCurrentDirectory(ph.getFile());
227: }
228: chooser.setFileFilter(filter);
229: chooser.setDialogTitle(res.getString("chooser_DialogTitle"));
230: chooser.setApproveButtonText(res.getString("OK"));
231: int v = chooser.showOpenDialog(this );
232:
233: this .requestFocus();
234: buttonBrowse.requestFocus();
235: if (v == JFileChooser.APPROVE_OPTION) {
236: if (chooser.getSelectedFile() == null
237: || (!chooser.getSelectedFile().isFile())) {
238: textFile.setText(new String());
239: } else {
240: fileDir = chooser.getCurrentDirectory().toString();
241: fileDir = stripSlash(fileDir);
242: fileName = chooser.getSelectedFile().getName();
243: textFile.setText(fileDir + File.separator + fileName);
244: }
245: }
246: chooser.removeAll();
247: chooser = null;
248: }
249:
250: /**
251: * Method declaration
252: *
253: *
254: * @param dir
255: *
256: * @return
257: */
258: private String stripSlash(String dir) {
259: String stripped = new String(dir);
260:
261: if (dir != null) {
262: if (dir.length() > 0) {
263: if (dir.endsWith(File.separator)) {
264: stripped = dir.substring(0, dir.length() - 1);
265: }
266: }
267: }
268: return stripped;
269: }
270:
271: private void editParameters() {
272: String params = new String();
273: XMLCParameterViewer viewer = null;
274:
275: params = textCommandLine.getText();
276: viewer = new XMLCParameterViewer();
277: viewer.setParameters(params);
278: viewer.setOwner((Dialog) getTopLevelAncestor());
279: if (viewer.showDialog() == DialogPanel.OK_OPTION) {
280: params = viewer.getParameters();
281: textCommandLine.setText(params);
282: }
283: }
284:
285: /**
286: * Method declaration
287: *
288: */
289: private void pmInit() {
290: textCommandLine.setText(new String());
291: buttonListener = new LocalButtonListener();
292: buttonBrowse.addActionListener(buttonListener);
293: buttonClear.addActionListener(buttonListener);
294: buttonTool.addActionListener(buttonListener);
295: setEnabled(true);
296: }
297:
298: /**
299: * Method declaration
300: *
301: *
302: * @exception Exception
303: */
304: private void jbInit() throws Exception {
305: buttonBrowse = (JButton) Beans.instantiate(getClass()
306: .getClassLoader(), JButton.class.getName());
307: textFile = (JTextField) Beans.instantiate(getClass()
308: .getClassLoader(), JTextField.class.getName());
309: border = BorderFactory.createTitledBorder(res
310: .getString("XMLC_Options"));
311: textCommandLine = (JTextField) Beans.instantiate(getClass()
312: .getClassLoader(), JTextField.class.getName());
313: labelCommandLine = (JLabel) Beans.instantiate(getClass()
314: .getClassLoader(), JLabel.class.getName());
315: labelFile = (JLabel) Beans.instantiate(getClass()
316: .getClassLoader(), JLabel.class.getName());
317: buttonClear = (JButton) Beans.instantiate(getClass()
318: .getClassLoader(), JButton.class.getName());
319: buttonTool = (JButton) Beans.instantiate(getClass()
320: .getClassLoader(), JButton.class.getName());
321: textFile.setMinimumSize(new Dimension(100, 21));
322: textFile.setPreferredSize(new Dimension(100, 21));
323: textFile.setEditable(false);
324: buttonBrowse.setText(res.getString("Browse"));
325: labelCommandLine
326: .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, new GridBagConstraints(0, 4, 1, 1, 1.0, 0.0,
333: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
334: new Insets(1, 2, 2, 2), 100, 0));
335: this .add(buttonBrowse, new GridBagConstraints(1, 4, 1, 1, 0.0,
336: 0.0, GridBagConstraints.CENTER,
337: GridBagConstraints.NONE, new Insets(1, 2, 2, 2), 0, 0));
338: this .add(labelCommandLine, new GridBagConstraints(0, 1, 4, 1,
339: 0.0, 0.0, GridBagConstraints.WEST,
340: GridBagConstraints.HORIZONTAL, new Insets(2, 2, 1, 2),
341: 0, 0));
342: this .add(textCommandLine, new GridBagConstraints(0, 2, 3, 1,
343: 0.0, 0.0, GridBagConstraints.WEST,
344: GridBagConstraints.HORIZONTAL, new Insets(1, 2, 2, 2),
345: 0, 0));
346: this .add(labelFile, new GridBagConstraints(0, 3, 4, 1, 0.0,
347: 0.0, GridBagConstraints.WEST,
348: GridBagConstraints.HORIZONTAL, new Insets(2, 2, 1, 2),
349: 0, 0));
350: this .add(buttonClear, new GridBagConstraints(2, 4, 2, 1, 0.0,
351: 0.0, GridBagConstraints.CENTER,
352: GridBagConstraints.NONE, new Insets(1, 2, 2, 2), 0, 0));
353: this .add(buttonTool, new GridBagConstraints(3, 2, 1, 1, 0.0,
354: 0.0, GridBagConstraints.CENTER,
355: GridBagConstraints.NONE, new Insets(1, 2, 2, 2), 0, 0));
356: }
357:
358: //
359: private class LocalButtonListener implements ActionListener {
360: public void actionPerformed(ActionEvent e) {
361: Object source = e.getSource();
362:
363: if (source == buttonBrowse) {
364: browseForFile();
365: } else if (source == buttonClear) {
366: textFile.setText(new String());
367: } else if (source == buttonTool) {
368: editParameters();
369: }
370: }
371:
372: }
373: }
|