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.deployer;
024:
025: // AddinCore
026: import org.enhydra.kelp.common.event.WriteListener;
027: import org.enhydra.kelp.common.node.OtterProject;
028: import org.enhydra.kelp.common.swing.FileNodeSelectionPanel;
029: import org.enhydra.kelp.common.DirectoryFilter;
030:
031: // ToolBox
032: import org.enhydra.tool.common.DataValidationException;
033: import org.enhydra.tool.common.PathHandle;
034: import org.enhydra.tool.common.SwingUtil;
035: import org.enhydra.tool.common.ToolException;
036:
037: // JDK
038: import java.awt.*;
039: import java.awt.event.ActionEvent;
040: import java.awt.event.ActionListener;
041: import java.io.File;
042: import java.lang.ref.WeakReference;
043: import javax.swing.*;
044: import javax.swing.event.ChangeEvent;
045: import javax.swing.event.ChangeListener;
046: import java.beans.*;
047:
048: // Standard imports
049: import java.util.ResourceBundle;
050:
051: //
052: public class InputPanel extends JPanel implements Instructor,
053: DeployStep {
054: static ResourceBundle res = ResourceBundle
055: .getBundle("org.enhydra.kelp.common.Res"); // nores
056: private JTabbedPane tab;
057: private GridBagLayout layoutMain;
058: private JPanel panelSelections;
059: private GridBagLayout layoutSelections;
060: private FileNodeSelectionPanel selectionPanel;
061: private JPanel panelOps;
062: private GridBagLayout layoutOps;
063: private JCheckBox checkInputFilter;
064: private JPanel panelRoot;
065: private GridBagLayout layoutRoot;
066: private JTextField textInputRoot;
067: private JButton buttonRoot;
068: private ReplacementTablePanel replacementPanel;
069: private JLabel labelRoot;
070: private JCheckBox checkEnable;
071: private LocalButtonListener buttonListener = null;
072: private LocalCheckListener checkListener = null;
073: private WeakReference projectRef = null;
074: private boolean ran = false;
075:
076: public InputPanel() {
077: try {
078: jbInit();
079: pmInit();
080: } catch (Exception ex) {
081: ex.printStackTrace();
082: }
083: }
084:
085: // implements Instructor
086: public String getTab() {
087: return "Input";
088: }
089:
090: // implements Instructor
091: public String getTitle() {
092: return "Input templates";
093: }
094:
095: // implements Instructor
096: public String getInstructions() {
097: return "The deployer copies files from the input root to the deployment root. "
098: + "The replacement table is used to search and replace tokens within the input templates during the copy.";
099: }
100:
101: // implements DeployStep
102: public void refresh() {
103: if (getProject() != null) {
104: checkEnable.setSelected(getProject().isDeployInput());
105: }
106: refreshEnable();
107: }
108:
109: // implements DeployStep
110: public void clearAll() {
111: selectionPanel.clearAll();
112: replacementPanel.clearAll();
113: buttonRoot.removeActionListener(buttonListener);
114: checkInputFilter.removeChangeListener(checkListener);
115: checkEnable.removeChangeListener(checkListener);
116: if (projectRef != null) {
117: projectRef.clear();
118: }
119: removeAll();
120: buttonListener = null;
121: checkListener = null;
122: projectRef = null;
123: }
124:
125: // implements DeployStep
126: public boolean isDataValid() {
127: return DeployStepUtil.isDataValid(this );
128: }
129:
130: // implements DeployStep
131: public boolean isDataEqual(OtterProject project) {
132: PathHandle projectPath = null;
133: PathHandle panelPath = null;
134: boolean equal = true;
135:
136: projectRef = new WeakReference(project);
137: if (project == null) {
138: System.err
139: .println("InnerPanel.isDataEqual() - project null");
140: return true;
141: } else if (checkInputFilter.isSelected() != project
142: .isDeployInputFilter()) {
143: equal = false;
144: }
145: if (checkEnable.isSelected() != project.isDeployInput()) {
146: equal = false;
147: }
148: if (equal) {
149: projectPath = PathHandle.createPathHandle(project
150: .getDeployInputPath());
151: panelPath = PathHandle.createPathHandle(textInputRoot
152: .getText());
153: if (!projectPath.equals(panelPath)) {
154: equal = false;
155: }
156: }
157: if (equal) {
158: equal = replacementPanel.isDataEqual();
159: }
160:
161: // selectionPanel - always equal - realtime update
162: return equal;
163: }
164:
165: // implements DeployStep
166: public void validateData() throws DataValidationException {
167: PathHandle path = null;
168:
169: if (checkEnable.isSelected()) {
170: path = PathHandle.createPathHandle(textInputRoot.getText());
171: if (!path.getPath().equalsIgnoreCase(
172: textInputRoot.getText())) {
173: throw new DataValidationException(
174: "Invalid input root: "
175: + textInputRoot.getText());
176: }
177: if (!path.isDirectory()) {
178: throw new DataValidationException(
179: "Invalid input root: "
180: + textInputRoot.getText());
181: }
182: }
183:
184: // selectionPanel - always valid
185: // replacementTable - always valid
186: }
187:
188: // implements DeployStep
189: public boolean isBuilt(OtterProject project) {
190: projectRef = new WeakReference(project);
191: ran = DeployStepUtil.isBuilt(this , ran, project);
192: return ran;
193: }
194:
195: // implements DeployStep
196: public boolean isSelectable(OtterProject project, DeployStep[] steps) {
197: boolean able = false;
198:
199: projectRef = new WeakReference(project);
200: for (int i = 0; i < steps.length; i++) {
201: if (steps[i] instanceof GeneralPanel) {
202: able = steps[i].isBuilt(project);
203: break;
204: }
205: }
206: return able;
207: }
208:
209: // implements DeployStep
210: public void build(OtterProject project, WriteListener listener)
211: throws DataValidationException, ToolException {
212: InputBuilder builder = null;
213:
214: projectRef = new WeakReference(project);
215: if (!isBuilt(project)) {
216: write(project);
217: builder = new InputBuilder(listener);
218: builder.setProject(project);
219: builder.setOwner(this );
220: builder.setEcho(true);
221: builder.buildInCurrentThread();
222: ran = true;
223: }
224: }
225:
226: // implements DeployStep
227: public void read(OtterProject project) {
228: PathHandle path = null;
229:
230: projectRef = new WeakReference(project);
231: path = PathHandle
232: .createPathHandle(project.getDeployInputPath());
233: if (path.isDirectory()) {
234: textInputRoot.setText(path.getPath());
235: } else {
236: textInputRoot.setText(new String());
237: }
238: textInputRoot.setToolTipText(textInputRoot.getText());
239: selectionPanel.setNodes(project.getAllInput());
240: replacementPanel.setNode(project);
241: replacementPanel.readProperties();
242: checkInputFilter.setSelected(project.isDeployInputFilter());
243: checkEnable.setSelected(project.isDeployInput());
244: refreshEnable();
245: }
246:
247: // implements DeployStep
248: public void write(OtterProject project)
249: throws DataValidationException {
250: projectRef = new WeakReference(project);
251: validateData();
252: if (!isDataEqual(project)) {
253: project.setDeployInput(checkEnable.isSelected());
254: project.setDeployInputFilter(checkInputFilter.isSelected());
255: project.setDeployInputPath(PathHandle
256: .createPathString(textInputRoot.getText()));
257: }
258: replacementPanel.writeProperties();
259:
260: // selection panel - uses realtime write
261: }
262:
263: //
264: //
265: private void refreshEnable() {
266: boolean enable = checkEnable.isSelected();
267:
268: buttonRoot.setEnabled(enable);
269: checkInputFilter.setEnabled(enable);
270: selectionPanel.setEnabled(enable);
271: replacementPanel.setEnabled(enable);
272: for (int i = 0; i < tab.getTabCount(); i++) {
273: tab.setEnabledAt(i, enable);
274: }
275: }
276:
277: private void refreshSelections() {
278: if (getProject() != null) {
279: getProject().setDeployInputFilter(
280: checkInputFilter.isSelected());
281: getProject().setDeployInputPath(textInputRoot.getText());
282: selectionPanel.setNodes(getProject().getAllInput());
283: }
284: }
285:
286: private void chooseRootDirectory() {
287: File choice = null;
288: PathHandle path = null;
289:
290: choice = SwingUtil.getDirectoryChoice(this , textInputRoot
291: .getText(), "Select input template directory");
292: path = PathHandle.createPathHandle(choice);
293: if (path.isDirectory()) {
294: textInputRoot.setText(path.getPath());
295: textInputRoot.setToolTipText(textInputRoot.getText());
296: refreshSelections();
297: }
298: }
299:
300: private OtterProject getProject() {
301: OtterProject project = null;
302:
303: if (projectRef != null) {
304: project = (OtterProject) projectRef.get();
305: }
306: return project;
307: }
308:
309: private void pmInit() {
310: String[] text = { "Input available", "Input selected" };
311:
312: buttonListener = new LocalButtonListener();
313: buttonRoot.addActionListener(buttonListener);
314: selectionPanel.setSelectText(text);
315: checkListener = new LocalCheckListener();
316: checkInputFilter.addChangeListener(checkListener);
317: checkEnable.addChangeListener(checkListener);
318: }
319:
320: private void jbInit() throws Exception {
321: tab = (JTabbedPane) Beans.instantiate(getClass()
322: .getClassLoader(), JTabbedPane.class.getName());
323: panelSelections = (JPanel) Beans.instantiate(getClass()
324: .getClassLoader(), JPanel.class.getName());
325: selectionPanel = (FileNodeSelectionPanel) Beans.instantiate(
326: getClass().getClassLoader(),
327: FileNodeSelectionPanel.class.getName());
328: panelOps = (JPanel) Beans.instantiate(getClass()
329: .getClassLoader(), JPanel.class.getName());
330: layoutOps = (GridBagLayout) Beans.instantiate(getClass()
331: .getClassLoader(), GridBagLayout.class.getName());
332: checkInputFilter = (JCheckBox) Beans.instantiate(getClass()
333: .getClassLoader(), JCheckBox.class.getName());
334: panelRoot = (JPanel) Beans.instantiate(getClass()
335: .getClassLoader(), JPanel.class.getName());
336: layoutRoot = (GridBagLayout) Beans.instantiate(getClass()
337: .getClassLoader(), GridBagLayout.class.getName());
338: textInputRoot = (JTextField) Beans.instantiate(getClass()
339: .getClassLoader(), JTextField.class.getName());
340: buttonRoot = (JButton) Beans.instantiate(getClass()
341: .getClassLoader(), JButton.class.getName());
342: layoutSelections = (GridBagLayout) Beans.instantiate(getClass()
343: .getClassLoader(), GridBagLayout.class.getName());
344: replacementPanel = (ReplacementTablePanel) Beans.instantiate(
345: getClass().getClassLoader(),
346: ReplacementTablePanel.class.getName());
347: labelRoot = (JLabel) Beans.instantiate(getClass()
348: .getClassLoader(), JLabel.class.getName());
349: checkEnable = (JCheckBox) Beans.instantiate(getClass()
350: .getClassLoader(), JCheckBox.class.getName());
351: layoutMain = (GridBagLayout) Beans.instantiate(getClass()
352: .getClassLoader(), GridBagLayout.class.getName());
353: panelOps.setLayout(layoutOps);
354: checkInputFilter.setText("Input templates (.in) only");
355: panelRoot.setLayout(layoutRoot);
356: textInputRoot.setEnabled(false);
357: textInputRoot.setEditable(false);
358: textInputRoot.setText("INTPUTROOT");
359: buttonRoot.setText("...");
360: labelRoot.setText("Input root:");
361: checkEnable.setSelected(true);
362: checkEnable.setText("Enable input deployment");
363: panelOps.add(checkInputFilter, new GridBagConstraints(0, 0, 1,
364: 1, 0.1, 0.0, GridBagConstraints.WEST,
365: GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 5),
366: 0, 0));
367: panelOps.add(panelRoot, new GridBagConstraints(0, 1, 2, 1, 0.1,
368: 0.0, GridBagConstraints.CENTER,
369: GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5),
370: 0, 0));
371: panelRoot.add(textInputRoot, new GridBagConstraints(1, 0, 1, 1,
372: 0.1, 0.0, GridBagConstraints.CENTER,
373: GridBagConstraints.HORIZONTAL, new Insets(2, 5, 2, 5),
374: 0, 0));
375: panelRoot.add(buttonRoot, new GridBagConstraints(2, 0, 1, 1,
376: 0.0, 0.0, GridBagConstraints.CENTER,
377: GridBagConstraints.NONE, new Insets(1, 5, 1, 5), 0, 0));
378: panelRoot.add(labelRoot, new GridBagConstraints(0, 0, 1, 1,
379: 0.0, 0.0, GridBagConstraints.WEST,
380: GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
381: panelSelections.setLayout(layoutSelections);
382: panelSelections.add(selectionPanel, new GridBagConstraints(0,
383: 0, 1, 1, 0.2, 0.0, GridBagConstraints.CENTER,
384: GridBagConstraints.BOTH, new Insets(0, 5, 0, 5), 0, 0));
385: panelSelections.add(panelOps, new GridBagConstraints(0, 1, 1,
386: 1, 0.1, 0.0, GridBagConstraints.CENTER,
387: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),
388: 0, 0));
389: tab.add(panelSelections, "Selections");
390: tab.add(replacementPanel, "Replacements");
391: this .setLayout(layoutMain);
392: this .add(checkEnable, new GridBagConstraints(0, 0, 1, 1, 0.1,
393: 0.0, GridBagConstraints.WEST,
394: GridBagConstraints.HORIZONTAL,
395: new Insets(2, 10, 2, 10), 0, 0));
396: this .add(tab, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
397: GridBagConstraints.CENTER, GridBagConstraints.BOTH,
398: new Insets(0, 0, 0, 0), -198, 15));
399: }
400:
401: private class LocalButtonListener implements ActionListener {
402: public void actionPerformed(ActionEvent event) {
403: Object source = event.getSource();
404:
405: if (source == buttonRoot) {
406: //Dusan 24.11.2002.
407: // chooseRootDirectory();
408: browseForDirectory();
409: }
410: }
411:
412: }
413:
414: private class LocalCheckListener implements ChangeListener {
415: public void stateChanged(ChangeEvent e) {
416: Object source = e.getSource();
417: if (source == checkInputFilter) {
418: refreshSelections();
419: } else if (source == checkEnable) {
420: if (getProject() != null) {
421: getProject().setDeployInput(
422: checkEnable.isSelected());
423: }
424: refreshEnable();
425: }
426: }
427:
428: }
429:
430: /**
431: * Method declaration
432: *
433: */
434: private void browseForDirectory() {
435: JFileChooser chooser;
436: String fileDir = new String();
437: String fileName = new String();
438:
439: chooser = new JFileChooser();
440: if (textInputRoot.getText().trim().length() > 0) {
441: chooser.setCurrentDirectory(new File(textInputRoot
442: .getText()));
443: } else {
444: chooser.setCurrentDirectory(new File(""));
445: }
446: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
447: chooser
448: .setFileFilter((javax.swing.filechooser.FileFilter) new DirectoryFilter());
449: chooser.setDialogTitle(res
450: .getString("chooser_InputRoot_DialogTitle"));
451: chooser.setApproveButtonText(res.getString("OK"));
452: int v = chooser.showOpenDialog(this );
453:
454: this .requestFocus();
455: buttonRoot.requestFocus();
456: if (v == JFileChooser.APPROVE_OPTION) {
457: if (chooser.getSelectedFile() == null) {
458: textInputRoot.setText(new String());
459: } else {
460: fileDir = chooser.getCurrentDirectory().toString();
461: fileDir = stripSlash(fileDir);
462: fileName = chooser.getSelectedFile().getName();
463: String strDeployRoot = fileDir + File.separator
464: + fileName;
465: textInputRoot.setText(strDeployRoot.replace('\\', '/'));
466: }
467: }
468: chooser.removeAll();
469: chooser = null;
470: }
471:
472: /**
473: * Method declaration
474: *
475: *
476: * @param dir
477: *
478: * @return
479: */
480: private String stripSlash(String dir) {
481: String stripped = new String(dir);
482:
483: if (dir != null) {
484: if (dir.length() > 0) {
485: if (dir.endsWith(File.separator)) {
486: stripped = dir.substring(0, dir.length() - 1);
487: }
488: }
489: }
490: return stripped;
491: }
492: }
|