KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > deployer > InputPanel


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.deployer;
24
25 // AddinCore
26
import org.enhydra.kelp.common.event.WriteListener;
27 import org.enhydra.kelp.common.node.OtterProject;
28 import org.enhydra.kelp.common.node.OtterNode;
29 import org.enhydra.kelp.common.swing.FileNodeSelectionPanel;
30 import org.enhydra.kelp.common.Constants;
31 import org.enhydra.kelp.common.DirectoryFilter;
32
33 // ToolBox
34
import org.enhydra.tool.common.DataValidationException;
35 import org.enhydra.tool.common.PathHandle;
36 import org.enhydra.tool.common.SwingUtil;
37 import org.enhydra.tool.common.ToolException;
38 import org.enhydra.tool.common.ExtensionFilter;
39
40 // JDK
41
import java.awt.*;
42 import java.awt.event.ActionEvent JavaDoc;
43 import java.awt.event.ActionListener JavaDoc;
44 import java.io.File JavaDoc;
45 import java.io.PrintWriter JavaDoc;
46 import java.lang.ref.WeakReference JavaDoc;
47 import javax.swing.*;
48 import javax.swing.border.*;
49 import javax.swing.event.ChangeEvent JavaDoc;
50 import javax.swing.event.ChangeListener JavaDoc;
51 import java.beans.*;
52
53 // Standard imports
54
import java.awt.event.*;
55 import java.io.File JavaDoc;
56 import java.util.ResourceBundle JavaDoc;
57
58 //
59
public class InputPanel extends JPanel implements Instructor, DeployStep {
60     static ResourceBundle JavaDoc res =
61         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
62
private JTabbedPane tab;
63     private GridBagLayout layoutMain;
64     private JPanel panelSelections;
65     private GridBagLayout layoutSelections;
66     private FileNodeSelectionPanel selectionPanel;
67     private JPanel panelOps;
68     private GridBagLayout layoutOps;
69     private JCheckBox checkInputFilter;
70     private JPanel panelRoot;
71     private GridBagLayout layoutRoot;
72     private JTextField textInputRoot;
73     private JButton buttonRoot;
74     private ReplacementTablePanel replacementPanel;
75     private JLabel labelRoot;
76     private JCheckBox checkEnable;
77     private LocalButtonListener buttonListener = null;
78     private LocalCheckListener checkListener = null;
79     private WeakReference JavaDoc projectRef = null;
80     private boolean ran = false;
81
82     public InputPanel() {
83         try {
84             jbInit();
85             pmInit();
86         } catch (Exception JavaDoc ex) {
87             ex.printStackTrace();
88         }
89     }
90
91     // implements Instructor
92
public String JavaDoc getTab() {
93         return "Input";
94     }
95
96     // implements Instructor
97
public String JavaDoc getTitle() {
98         return "Input templates";
99     }
100
101     // implements Instructor
102
public String JavaDoc getInstructions() {
103         return "The deployer copies files from the input root to the deployment root. "
104                + "The replacement table is used to search and replace tokens within the input templates during the copy.";
105     }
106
107     // implements DeployStep
108
public void refresh() {
109         if (getProject() != null) {
110             checkEnable.setSelected(getProject().isDeployInput());
111         }
112         refreshEnable();
113     }
114
115     // implements DeployStep
116
public void clearAll() {
117         selectionPanel.clearAll();
118         replacementPanel.clearAll();
119         buttonRoot.removeActionListener(buttonListener);
120         checkInputFilter.removeChangeListener(checkListener);
121         checkEnable.removeChangeListener(checkListener);
122         if (projectRef != null) {
123             projectRef.clear();
124         }
125         removeAll();
126         buttonListener = null;
127         checkListener = null;
128         projectRef = null;
129     }
130
131     // implements DeployStep
132
public boolean isDataValid() {
133         return DeployStepUtil.isDataValid(this);
134     }
135
136     // implements DeployStep
137
public boolean isDataEqual(OtterProject project) {
138         PathHandle projectPath = null;
139         PathHandle panelPath = null;
140         boolean equal = true;
141
142         projectRef = new WeakReference JavaDoc(project);
143         if (project == null) {
144             System.err.println("InnerPanel.isDataEqual() - project null");
145             return true;
146         } else if (checkInputFilter.isSelected()
147                    != project.isDeployInputFilter()) {
148             equal = false;
149         }
150         if (checkEnable.isSelected() != project.isDeployInput()) {
151             equal = false;
152         }
153         if (equal) {
154             projectPath =
155                 PathHandle.createPathHandle(project.getDeployInputPath());
156             panelPath = PathHandle.createPathHandle(textInputRoot.getText());
157             if (!projectPath.equals(panelPath)) {
158                 equal = false;
159             }
160         }
161         if (equal) {
162             equal = replacementPanel.isDataEqual();
163         }
164
165         // selectionPanel - always equal - realtime update
166
return equal;
167     }
168
169     // implements DeployStep
170
public void validateData() throws DataValidationException {
171         PathHandle path = null;
172
173         if (checkEnable.isSelected()) {
174             path = PathHandle.createPathHandle(textInputRoot.getText());
175             if (!path.getPath().equalsIgnoreCase(textInputRoot.getText())) {
176                 throw new DataValidationException("Invalid input root: "
177                                                   + textInputRoot.getText());
178             }
179             if (!path.isDirectory()) {
180                 throw new DataValidationException("Invalid input root: "
181                                                   + textInputRoot.getText());
182             }
183         }
184
185         // selectionPanel - always valid
186
// replacementTable - always valid
187
}
188
189     // implements DeployStep
190
public boolean isBuilt(OtterProject project) {
191         projectRef = new WeakReference JavaDoc(project);
192         ran = DeployStepUtil.isBuilt(this, ran, project);
193         return ran;
194     }
195
196     // implements DeployStep
197
public boolean isSelectable(OtterProject project, DeployStep[] steps) {
198         boolean able = false;
199
200         projectRef = new WeakReference JavaDoc(project);
201         for (int i = 0; i < steps.length; i++) {
202             if (steps[i] instanceof GeneralPanel) {
203                 able = steps[i].isBuilt(project);
204                 break;
205             }
206         }
207         return able;
208     }
209
210     // implements DeployStep
211
public void build(OtterProject project, WriteListener listener)
212             throws DataValidationException, ToolException {
213         InputBuilder builder = null;
214
215         projectRef = new WeakReference JavaDoc(project);
216         if (!isBuilt(project)) {
217             write(project);
218             builder = new InputBuilder(listener);
219             builder.setProject(project);
220             builder.setOwner(this);
221             builder.setEcho(true);
222             builder.buildInCurrentThread();
223             ran = true;
224         }
225     }
226
227     // implements DeployStep
228
public void read(OtterProject project) {
229         PathHandle path = null;
230
231         projectRef = new WeakReference JavaDoc(project);
232         path = PathHandle.createPathHandle(project.getDeployInputPath());
233         if (path.isDirectory()) {
234             textInputRoot.setText(path.getPath());
235         } else {
236             textInputRoot.setText(new String JavaDoc());
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) throws DataValidationException {
249         projectRef = new WeakReference JavaDoc(project);
250         validateData();
251         if (!isDataEqual(project)) {
252             project.setDeployInput(checkEnable.isSelected());
253             project.setDeployInputFilter(checkInputFilter.isSelected());
254             project.setDeployInputPath(PathHandle.createPathString(textInputRoot.getText()));
255         }
256         replacementPanel.writeProperties();
257
258         // selection panel - uses realtime write
259
}
260
261     //
262
//
263
private void refreshEnable() {
264         boolean enable = checkEnable.isSelected();
265
266         buttonRoot.setEnabled(enable);
267         checkInputFilter.setEnabled(enable);
268         selectionPanel.setEnabled(enable);
269         replacementPanel.setEnabled(enable);
270         for (int i = 0; i < tab.getTabCount(); i++) {
271             tab.setEnabledAt(i, enable);
272         }
273     }
274
275     private void refreshSelections() {
276         if (getProject() != null) {
277             getProject().setDeployInputFilter(checkInputFilter.isSelected());
278             getProject().setDeployInputPath(textInputRoot.getText());
279             selectionPanel.setNodes(getProject().getAllInput());
280         }
281     }
282
283     private void chooseRootDirectory() {
284         File JavaDoc choice = null;
285         PathHandle path = null;
286
287         choice =
288             SwingUtil.getDirectoryChoice(this, textInputRoot.getText(),
289                                          "Select input template directory");
290         path = PathHandle.createPathHandle(choice);
291         if (path.isDirectory()) {
292             textInputRoot.setText(path.getPath());
293             textInputRoot.setToolTipText(textInputRoot.getText());
294             refreshSelections();
295         }
296     }
297
298     private OtterProject getProject() {
299         OtterProject project = null;
300
301         if (projectRef != null) {
302             project = (OtterProject) projectRef.get();
303         }
304         return project;
305     }
306
307     private void pmInit() {
308         String JavaDoc[] text = {
309             "Input available", "Input selected"
310         };
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 JavaDoc {
321         tab = (JTabbedPane) Beans.instantiate(getClass().getClassLoader(),
322                                               JTabbedPane.class.getName());
323         panelSelections =
324             (JPanel) Beans.instantiate(getClass().getClassLoader(),
325                                        JPanel.class.getName());
326         selectionPanel =
327             (FileNodeSelectionPanel) Beans.instantiate(getClass().getClassLoader(),
328                 FileNodeSelectionPanel.class.getName());
329         panelOps = (JPanel) Beans.instantiate(getClass().getClassLoader(),
330                                               JPanel.class.getName());
331         layoutOps =
332             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
333                                               GridBagLayout.class.getName());
334         checkInputFilter =
335             (JCheckBox) Beans.instantiate(getClass().getClassLoader(),
336                                           JCheckBox.class.getName());
337         panelRoot = (JPanel) Beans.instantiate(getClass().getClassLoader(),
338                                                JPanel.class.getName());
339         layoutRoot =
340             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
341                                               GridBagLayout.class.getName());
342         textInputRoot =
343             (JTextField) Beans.instantiate(getClass().getClassLoader(),
344                                            JTextField.class.getName());
345         buttonRoot = (JButton) Beans.instantiate(getClass().getClassLoader(),
346                                                  JButton.class.getName());
347         layoutSelections =
348             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
349                                               GridBagLayout.class.getName());
350         replacementPanel =
351             (ReplacementTablePanel) Beans.instantiate(getClass().getClassLoader(),
352                 ReplacementTablePanel.class.getName());
353         labelRoot = (JLabel) Beans.instantiate(getClass().getClassLoader(),
354                                                JLabel.class.getName());
355         checkEnable =
356             (JCheckBox) Beans.instantiate(getClass().getClassLoader(),
357                                           JCheckBox.class.getName());
358         layoutMain =
359             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
360                                               GridBagLayout.class.getName());
361         panelOps.setLayout(layoutOps);
362         checkInputFilter.setText("Input templates (.in) only");
363         panelRoot.setLayout(layoutRoot);
364         textInputRoot.setEnabled(false);
365         textInputRoot.setEditable(false);
366         textInputRoot.setText("INTPUTROOT");
367         buttonRoot.setText("...");
368         labelRoot.setText("Input root:");
369         checkEnable.setSelected(true);
370         checkEnable.setText("Enable input deployment");
371         panelOps.add(checkInputFilter,
372                      new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
373                                             GridBagConstraints.WEST,
374                                             GridBagConstraints.HORIZONTAL,
375                                             new Insets(0, 10, 0, 5), 0, 0));
376         panelOps.add(panelRoot,
377                      new GridBagConstraints(0, 1, 2, 1, 0.1, 0.0,
378                                             GridBagConstraints.CENTER,
379                                             GridBagConstraints.HORIZONTAL,
380                                             new Insets(0, 5, 0, 5), 0, 0));
381         panelRoot.add(textInputRoot,
382                       new GridBagConstraints(1, 0, 1, 1, 0.1, 0.0,
383                                              GridBagConstraints.CENTER,
384                                              GridBagConstraints.HORIZONTAL,
385                                              new Insets(2, 5, 2, 5), 0, 0));
386         panelRoot.add(buttonRoot,
387                       new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
388                                              GridBagConstraints.CENTER,
389                                              GridBagConstraints.NONE,
390                                              new Insets(1, 5, 1, 5), 0, 0));
391         panelRoot.add(labelRoot,
392                       new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
393                                              GridBagConstraints.WEST,
394                                              GridBagConstraints.NONE,
395                                              new Insets(2, 5, 2, 5), 0, 0));
396         panelSelections.setLayout(layoutSelections);
397         panelSelections.add(selectionPanel,
398                             new GridBagConstraints(0, 0, 1, 1, 0.2, 0.0,
399                                                    GridBagConstraints.CENTER,
400                                                    GridBagConstraints.BOTH,
401                                                    new Insets(0, 5, 0, 5), 0,
402                                                    0));
403         panelSelections.add(panelOps,
404                             new GridBagConstraints(0, 1, 1, 1, 0.1, 0.0,
405                                                    GridBagConstraints.CENTER,
406                                                    GridBagConstraints.HORIZONTAL,
407                                                    new Insets(0, 0, 0, 0), 0,
408                                                    0));
409         tab.add(panelSelections, "Selections");
410         tab.add(replacementPanel, "Replacements");
411         this.setLayout(layoutMain);
412         this.add(checkEnable,
413                  new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
414                                         GridBagConstraints.WEST,
415                                         GridBagConstraints.HORIZONTAL,
416                                         new Insets(2, 10, 2, 10), 0, 0));
417         this.add(tab,
418                  new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
419                                         GridBagConstraints.CENTER,
420                                         GridBagConstraints.BOTH,
421                                         new Insets(0, 0, 0, 0), -198, 15));
422     }
423
424     private class LocalButtonListener implements ActionListener JavaDoc {
425         public void actionPerformed(ActionEvent JavaDoc event) {
426             Object JavaDoc source = event.getSource();
427
428             if (source == buttonRoot) {
429 //Dusan 24.11.2002.
430
// chooseRootDirectory();
431
browseForDirectory();
432             }
433         }
434
435     }
436     private class LocalCheckListener implements ChangeListener JavaDoc {
437         public void stateChanged(ChangeEvent JavaDoc e) {
438             Object JavaDoc source = e.getSource();
439             if (source == checkInputFilter) {
440                 refreshSelections();
441             } else if (source == checkEnable) {
442                if (getProject() != null) {
443                   getProject().setDeployInput(checkEnable.isSelected());
444                }
445                 refreshEnable();
446             }
447         }
448
449     }
450
451     /**
452      * Method declaration
453      *
454      */

455     private void browseForDirectory() {
456         JFileChooser chooser;
457         File JavaDoc file;
458         String JavaDoc fileDir = new String JavaDoc();
459         String JavaDoc fileName = new String JavaDoc();
460
461         chooser = new JFileChooser();
462         if (textInputRoot.getText().trim().length() > 0) {
463             chooser.setCurrentDirectory(new File JavaDoc(textInputRoot.getText()));
464         } else {
465             PathHandle ph = null;
466
467             chooser.setCurrentDirectory(new File JavaDoc(""));
468         }
469         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
470         chooser.setFileFilter((javax.swing.filechooser.FileFilter JavaDoc) new DirectoryFilter());
471         chooser.setDialogTitle(res.getString("chooser_InputRoot_DialogTitle"));
472         chooser.setApproveButtonText(res.getString("OK"));
473         int v = chooser.showOpenDialog(this);
474
475         this.requestFocus();
476         buttonRoot.requestFocus();
477         if (v == JFileChooser.APPROVE_OPTION) {
478             if (chooser.getSelectedFile() == null) {
479                 textInputRoot.setText(new String JavaDoc());
480             } else {
481                 fileDir = chooser.getCurrentDirectory().toString();
482                 fileDir = stripSlash(fileDir);
483                 fileName = chooser.getSelectedFile().getName();
484                 String JavaDoc strDeployRoot = fileDir + File.separator + fileName;
485                 textInputRoot.setText(strDeployRoot.replace('\\','/'));
486             }
487         }
488         chooser.removeAll();
489         chooser = null;
490     }
491
492     /**
493      * Method declaration
494      *
495      *
496      * @param dir
497      *
498      * @return
499      */

500     private String JavaDoc stripSlash(String JavaDoc dir) {
501         String JavaDoc stripped = new String JavaDoc(dir);
502
503         if (dir != null) {
504             if (dir.length() > 0) {
505                 if (dir.endsWith(File.separator)) {
506                     stripped = dir.substring(0, dir.length() - 1);
507                 }
508             }
509         }
510         return stripped;
511     }
512 }
513
Popular Tags