KickJava   Java API By Example, From Geeks To Geeks.

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


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.PathUtil;
27 import org.enhydra.kelp.common.PropUtil;
28 import org.enhydra.kelp.common.event.WriteListener;
29 import org.enhydra.kelp.common.node.OtterProject;
30 import org.enhydra.kelp.common.swing.ExtensionPanel;
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 //Dusan 24.11.2002.
38
import org.enhydra.tool.common.ExtensionFilter;
39 import org.enhydra.tool.common.PathHandle;
40
41 // JDK
42
import java.awt.*;
43 import java.awt.event.ActionEvent JavaDoc;
44 import java.awt.event.ActionListener JavaDoc;
45 import java.beans.*;
46 import java.io.File JavaDoc;
47 import java.io.PrintWriter JavaDoc;
48 import java.lang.ref.WeakReference JavaDoc;
49 import java.util.Arrays JavaDoc;
50 import javax.swing.*;
51 import javax.swing.border.TitledBorder JavaDoc;
52 import javax.swing.event.ChangeEvent JavaDoc;
53 import javax.swing.event.ChangeListener JavaDoc;
54
55 //Dusan 24.11.2002.
56
// Standard imports
57
import java.awt.event.*;
58 import java.io.File JavaDoc;
59 import java.util.ResourceBundle JavaDoc;
60 // Kelp imports
61
import org.enhydra.kelp.common.Constants;
62 import org.enhydra.kelp.common.node.OtterNode;
63 import org.enhydra.kelp.common.node.OtterFileNode;
64
65 //
66
//
67
public class ContentPanel extends JPanel implements Instructor, DeployStep {
68     static ResourceBundle JavaDoc res =
69         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
70
private GridBagLayout layoutMain;
71     private JTabbedPane tab;
72     private JPanel panelFiller;
73     private JPanel panelPaths;
74     private ExtensionPanel panelTypes;
75     private GridBagLayout layoutPaths;
76     private GridBagLayout layoutTypes;
77     private JPanel panelSource;
78     private JTextField textSource;
79     private GridBagLayout layoutSource;
80     private JButton buttonSource;
81     private JPanel panelDoc;
82     private GridBagLayout layoutDoc;
83     private JTextField textDoc;
84     private JPanel panelPathsFiller;
85     private JCheckBox checkEnable;
86     private LocalButtonListener buttonListener = null;
87     private LocalCheckListener checkListener = null;
88     private WeakReference JavaDoc projectRef = null;
89     private boolean ran = false;
90
91     public ContentPanel() {
92         try {
93             jbInit();
94             pmInit();
95         } catch (Exception JavaDoc e) {
96             e.printStackTrace();
97         }
98     }
99
100     // implements Instructor
101
public String JavaDoc getTab() {
102         return "Content";
103     }
104
105     // implements Instructor
106
public String JavaDoc getTitle() {
107         return "Web content";
108     }
109
110     // implements Instructor
111
public String JavaDoc getInstructions() {
112         return "The deployer will copy content types from the content source"
113                + " directory to the archive document root. Use the archive document "
114                + " root to select content when building web application archives.";
115     }
116
117     // implements DeployStep
118
public void refresh() {
119         if (getProject() != null) {
120             checkEnable.setSelected(getProject().isDeployContent());
121         }
122         privateRefresh();
123     }
124
125     private void privateRefresh() {
126         boolean enable = checkEnable.isSelected();
127
128         buttonSource.setEnabled(enable);
129         panelTypes.setEnabled(false);
130         for (int i = 0; i < tab.getTabCount(); i++) {
131             tab.setEnabledAt(i, enable);
132         }
133         if (getProject() != null) {
134             textDoc.setText(PathUtil.getDeployContentPath(getProject(),
135                     getProject().getDeployRootPath()));
136             textDoc.setToolTipText(textDoc.getText());
137         }
138     }
139
140     // implements DeployStep
141
public void clearAll() {
142         checkEnable.removeChangeListener(checkListener);
143         buttonSource.removeActionListener(buttonListener);
144         panelTypes.clearAll();
145         if (projectRef != null) {
146             projectRef.clear();
147         }
148         removeAll();
149         projectRef = null;
150         checkListener = null;
151         buttonListener = null;
152     }
153
154     // implements DeployStep
155
public boolean isDataValid() {
156         return DeployStepUtil.isDataValid(this);
157     }
158
159     // implements DeployStep
160
public boolean isDataEqual(OtterProject project) {
161         PathHandle projectPath = null;
162         PathHandle panelPath = null;
163         String JavaDoc[] panelList = null;
164         String JavaDoc[] projectList = null;
165         boolean equal = true;
166
167         projectRef = new WeakReference JavaDoc(project);
168         if (project == null) {
169             System.err.println("ContentPanel.isDataEqual() - project null");
170             return true;
171         } else if (checkEnable.isSelected()
172                    == project.isDeployContent()) {
173             equal = false;
174         }
175         if (equal) {
176             projectPath =
177                 PathHandle.createPathHandle(project.getDeployResourcePath());
178             panelPath = PathHandle.createPathHandle(textSource.getText());
179             if (!projectPath.equals(panelPath)) {
180                 equal = false;
181             }
182         }
183         if (equal) {
184             panelList = panelTypes.getExtensions();
185             Arrays.sort(panelList);
186             projectList = project.getContentTypes();
187             Arrays.sort(projectList);
188             equal = Arrays.equals(panelList, projectList);
189         }
190         return equal;
191     }
192
193     // implements DeployStep
194
public void validateData() throws DataValidationException {
195         PathHandle path = null;
196
197         path = PathHandle.createPathHandle(textSource.getText());
198         if (checkEnable.isSelected()) {
199             if (!path.getPath().equalsIgnoreCase(textSource.getText())) {
200                 throw new DataValidationException("Invalid content source directory: "
201                                                   + textSource.getText());
202             }
203             if (!path.isDirectory()) {
204                 throw new DataValidationException("Invalid content source directory: "
205                                                   + path.getPath());
206             }
207             if (panelTypes.getExtensions().length == 0) {
208                 throw new DataValidationException("No content types defined");
209             }
210         }
211     }
212
213     // implements DeployStep
214
public boolean isBuilt(OtterProject project) {
215         projectRef = new WeakReference JavaDoc(project);
216         ran = DeployStepUtil.isBuilt(this, ran, project);
217         return ran;
218     }
219
220     // implements DeployStep
221
public boolean isSelectable(OtterProject project, DeployStep[] steps) {
222         boolean able = false;
223
224         projectRef = new WeakReference JavaDoc(project);
225         for (int i = 0; i < steps.length; i++) {
226             if (steps[i] instanceof GeneralPanel) {
227                 able = steps[i].isBuilt(project);
228                 break;
229             }
230         }
231         return able;
232     }
233
234     // implements DeployStep
235
public void build(OtterProject project,
236                       WriteListener listener) throws DataValidationException {
237         ContentBuilder builder = null;
238
239         projectRef = new WeakReference JavaDoc(project);
240         if (!isBuilt(project)) {
241             write(project);
242             builder = new ContentBuilder(listener);
243             builder.setEcho(true);
244             builder.setProject(project);
245             builder.buildInCurrentThread();
246             ran = true;
247         }
248     }
249
250     // implements DeployStep
251
public void read(OtterProject project) {
252         PathHandle path = null;
253
254         projectRef = new WeakReference JavaDoc(project);
255         checkEnable.setSelected(project.isDeployContent());
256         path = PathHandle.createPathHandle(project.getDeployResourcePath());
257         if (path.isDirectory()) {
258             textSource.setText(path.getPath());
259         } else {
260             textSource.setText(new String JavaDoc());
261         }
262         textSource.setToolTipText(textSource.getText());
263         textDoc.setText(PathUtil.getDeployContentPath(project,
264                 textSource.getText()));
265         textDoc.setToolTipText(textDoc.getText());
266         panelTypes.setExtensions(project.getContentTypes());
267         panelTypes.setDefaults(PropUtil.getDefaultContentTypes());
268         privateRefresh();
269     }
270
271     // implements DeployStep
272
public void write(OtterProject project) throws DataValidationException {
273         projectRef = new WeakReference JavaDoc(project);
274         validateData();
275         if (!isDataEqual(project)) {
276             project.setDeployContent(checkEnable.isSelected());
277             project.setDeployResourcePath(PathHandle.createPathString(textSource.getText()));
278         }
279         if (project == null) {
280             System.err.println("ContentPanel.write() - project null");
281         } else {
282             project.setContentTypes(panelTypes.getExtensions());
283         }
284     }
285
286     //
287
private void chooseSourceDirectory() {
288         File JavaDoc choice = null;
289         PathHandle path = null;
290
291         choice =
292             SwingUtil.getDirectoryChoice(this, textSource.getText(),
293                                          "Select content source directory");
294         path = PathHandle.createPathHandle(choice);
295         if (path.isDirectory()) {
296             textSource.setText(path.getPath());
297             textSource.setToolTipText(textSource.getText());
298             privateRefresh();
299         }
300     }
301
302     private OtterProject getProject() {
303         OtterProject project = null;
304
305         if (projectRef != null) {
306             project = (OtterProject) projectRef.get();
307         }
308         return project;
309     }
310
311     private void pmInit() {
312         buttonListener = new LocalButtonListener();
313         buttonSource.addActionListener(buttonListener);
314         checkListener = new LocalCheckListener();
315         checkEnable.addChangeListener(checkListener);
316     }
317
318     private void jbInit() throws Exception JavaDoc {
319         tab = (JTabbedPane) Beans.instantiate(getClass().getClassLoader(),
320                                               JTabbedPane.class.getName());
321         panelFiller = (JPanel) Beans.instantiate(getClass().getClassLoader(),
322                                                  JPanel.class.getName());
323         panelPaths = (JPanel) Beans.instantiate(getClass().getClassLoader(),
324                                                 JPanel.class.getName());
325         panelTypes =
326             (ExtensionPanel) Beans.instantiate(getClass().getClassLoader(),
327                                                ExtensionPanel.class.getName());
328         layoutPaths =
329             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
330                                               GridBagLayout.class.getName());
331         layoutTypes =
332             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
333                                               GridBagLayout.class.getName());
334         panelSource = (JPanel) Beans.instantiate(getClass().getClassLoader(),
335                                                  JPanel.class.getName());
336         textSource =
337             (JTextField) Beans.instantiate(getClass().getClassLoader(),
338                                            JTextField.class.getName());
339         layoutSource =
340             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
341                                               GridBagLayout.class.getName());
342         buttonSource =
343             (JButton) Beans.instantiate(getClass().getClassLoader(),
344                                         JButton.class.getName());
345         panelDoc = (JPanel) Beans.instantiate(getClass().getClassLoader(),
346                                               JPanel.class.getName());
347         layoutDoc =
348             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
349                                               GridBagLayout.class.getName());
350         layoutMain =
351             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
352                                               GridBagLayout.class.getName());
353         textDoc = (JTextField) Beans.instantiate(getClass().getClassLoader(),
354                                                  JTextField.class.getName());
355         panelPathsFiller =
356             (JPanel) Beans.instantiate(getClass().getClassLoader(),
357                                        JPanel.class.getName());
358         checkEnable =
359             (JCheckBox) Beans.instantiate(getClass().getClassLoader(),
360                                           JCheckBox.class.getName());
361         panelPaths.setLayout(layoutPaths);
362         panelTypes.setLayout(layoutTypes);
363         panelSource.setBorder(new TitledBorder JavaDoc(BorderFactory.createEtchedBorder(new Color(187, 218, 252), new Color(91, 107, 123)),
364                                                "Content source"));
365         panelSource.setLayout(layoutSource);
366         textSource.setEnabled(false);
367         textSource.setEditable(false);
368         textSource.setText("Source ROOT");
369         buttonSource.setText("...");
370         panelDoc.setLayout(layoutDoc);
371         panelDoc.setBorder(new TitledBorder JavaDoc(BorderFactory.createEtchedBorder(new Color(187, 218, 252), new Color(91, 107, 123)),
372                                             "Archive document root"));
373         textDoc.setEnabled(false);
374         textDoc.setEditable(false);
375         textDoc.setText("DOCUMENT ROOT");
376         checkEnable.setSelected(true);
377         checkEnable.setText("Enable content deployment");
378         panelPaths.add(panelSource,
379                        new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
380                                               GridBagConstraints.CENTER,
381                                               GridBagConstraints.HORIZONTAL,
382                                               new Insets(5, 5, 5, 5), 0, 0));
383         panelSource.add(textSource,
384                         new GridBagConstraints(0, 0, 1, 1, 0.8, 0.0,
385                                                GridBagConstraints.WEST,
386                                                GridBagConstraints.HORIZONTAL,
387                                                new Insets(5, 5, 5, 0), 0, 0));
388         panelSource.add(buttonSource,
389                         new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
390                                                GridBagConstraints.CENTER,
391                                                GridBagConstraints.NONE,
392                                                new Insets(5, 5, 5, 5), 0, 0));
393         panelPaths.add(panelDoc,
394                        new GridBagConstraints(0, 1, 1, 1, 0.1, 0.0,
395                                               GridBagConstraints.CENTER,
396                                               GridBagConstraints.HORIZONTAL,
397                                               new Insets(5, 5, 5, 5), 0, 0));
398         panelDoc.add(textDoc,
399                      new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
400                                             GridBagConstraints.WEST,
401                                             GridBagConstraints.HORIZONTAL,
402                                             new Insets(5, 5, 5, 0), 0, 0));
403         panelPaths.add(panelPathsFiller,
404                        new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
405                                               GridBagConstraints.CENTER,
406                                               GridBagConstraints.BOTH,
407                                               new Insets(0, 0, 0, 0), 0, 0));
408         tab.add(panelPaths, "Paths");
409         tab.add(panelTypes, "Types");
410         this.setLayout(layoutMain);
411         this.add(tab,
412                  new GridBagConstraints(0, 1, 3, 1, 0.1, 0.1,
413                                         GridBagConstraints.CENTER,
414                                         GridBagConstraints.BOTH,
415                                         new Insets(0, 0, 0, 0), 0, 0));
416         this.add(panelFiller,
417                  new GridBagConstraints(0, 2, 1, 1, 0.0, 0.2,
418                                         GridBagConstraints.CENTER,
419                                         GridBagConstraints.VERTICAL,
420                                         new Insets(0, 0, 0, 0), 0, 0));
421         this.add(checkEnable,
422                  new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
423                                         GridBagConstraints.WEST,
424                                         GridBagConstraints.HORIZONTAL,
425                                         new Insets(2, 10, 2, 10), 0, 0));
426     }
427
428     //
429
private class LocalButtonListener implements ActionListener JavaDoc {
430         public void actionPerformed(ActionEvent JavaDoc event) {
431             Object JavaDoc source = event.getSource();
432
433             if (source == buttonSource) {
434 //Dusan 24.11.2002.
435
// chooseSourceDirectory();
436
browseForDirectory();
437             }
438         }
439
440     }
441     private class LocalCheckListener implements ChangeListener JavaDoc {
442         public void stateChanged(ChangeEvent JavaDoc e) {
443             Object JavaDoc source = e.getSource();
444
445             if (source == checkEnable) {
446                 if (getProject() != null) {
447                   getProject().setDeployContent(checkEnable.isSelected());
448                 }
449                 privateRefresh();
450             }
451         }
452
453     }
454
455     /**
456      * Method declaration
457      *
458      */

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

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