KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > archive > wizard > FilePanel


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.archive.wizard;
25
26 // ToolBox
27
import org.enhydra.tool.archive.ArchiveException;
28 import org.enhydra.tool.archive.Descriptor;
29 import org.enhydra.tool.common.PathHandle;
30 import org.enhydra.tool.common.ExtensionFilter;
31 import org.enhydra.tool.common.FilenameFilter;
32 import org.enhydra.tool.common.SwingUtil;
33
34 // JDK
35
import java.awt.*;
36 import java.awt.event.*;
37 import java.io.File JavaDoc;
38 import javax.swing.*;
39 import javax.swing.event.ChangeEvent JavaDoc;
40 import javax.swing.event.ChangeListener JavaDoc;
41 import javax.swing.border.TitledBorder JavaDoc;
42 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
43 import java.beans.Beans JavaDoc;
44
45 //
46
public class FilePanel extends RefPanel {
47     private JPanel panelDescript = null;
48     private GridBagLayout layoutMain = null;
49     private JPanel panelFile = null;
50     private JTextField textFile = null;
51     private JButton buttonFile = null;
52     private GridBagLayout layoutFile = null;
53     private GridBagLayout layoutDescript = null;
54     private JButton buttonDD = null;
55     private LocalButtonListener buttonListener = null;
56     private JScrollPane scroll = null;
57     private JTable tableDD = null;
58     private String JavaDoc extension = "jar";
59     private String JavaDoc title = "Archive";
60     private String JavaDoc[] defaultDD = new String JavaDoc[0];
61
62     public FilePanel() {
63         try {
64             jbInit();
65             pmInit();
66         } catch (Exception JavaDoc ex) {
67             ex.printStackTrace();
68         }
69     }
70
71     protected void setExtension(String JavaDoc s) {
72         extension = s;
73     }
74
75     protected String JavaDoc getExtension() {
76         return extension;
77     }
78
79     protected void setTitle(String JavaDoc s) {
80         title = s;
81     }
82
83     protected String JavaDoc getTitle() {
84         return title;
85     }
86
87     protected String JavaDoc[] getDefaultDD() {
88         return defaultDD;
89     }
90
91     protected void setDefaultDD(String JavaDoc[] dd) {
92         defaultDD = dd;
93     }
94
95     protected void validatePlan() throws ArchiveException {
96         int count = -1;
97
98         if (getArchivePath().trim().length() == 0) {
99             throw new ArchiveException("Archive file name required");
100         } else {
101             PathHandle path = null;
102
103             path = PathHandle.createPathHandle(getArchivePath());
104             if (!path.hasExtension(getExtension())) {
105                 throw new ArchiveException("Invalid archive extension: "
106                                            + path.getExtension()
107                                            + "\n Expecting: "
108                                            + getExtension());
109             }
110         }
111         count = getDDTableModel().getRowCount();
112         for (int i = 0; i < count; i++) {
113             Descriptor row = null;
114
115             row = getDDTableModel().getDescriptor(i);
116             if (row.isRequired()) {
117                 if (row.getPath().trim().length() == 0) {
118                     throw new ArchiveException(row.getType()
119                                                + " descriptor required");
120                 } else {
121                     File JavaDoc file = null;
122
123                     file = new File JavaDoc(row.getPath());
124                     if (!file.isFile()) {
125                         throw new ArchiveException("Invalid descriptor: "
126                                                    + file.getAbsolutePath());
127                     }
128                 }
129             }
130         }
131     }
132
133     protected String JavaDoc getArchivePath() {
134         return textFile.getText();
135     }
136
137     protected void setArchivePath(String JavaDoc path) {
138         PathHandle handle = PathHandle.createPathHandle(path);
139
140         textFile.setText(handle.getPath());
141         if (getWizard() != null) {
142             for (int i = 0; i < 3; i++) {
143                 if (handle.getParent() == null) {
144                     break;
145                 } else {
146                     handle = handle.getParent();
147                 }
148             }
149             if (handle.isDirectory()) {
150                 setWorkingRoot(handle.getPath());
151             }
152         }
153     }
154
155     protected DDTableModel getDDTableModel() {
156         return (DDTableModel) tableDD.getModel();
157     }
158
159     protected JTable getDDTable() {
160         return tableDD;
161     }
162
163     protected void preparePanel() {
164         panelFile.setBorder(new TitledBorder JavaDoc(BorderFactory.createEtchedBorder(new Color(187, 218, 252), new Color(91, 107, 123)),
165                                              getTitle()));
166         setArchivePath(getArchivePath());
167         if (!getDDTable().isVisible()) {
168             remove(panelDescript);
169             doLayout();
170         }
171     }
172
173     private void pmInit() {
174         CheckCellRenderer checkRenderer = null;
175         TextCellRenderer textRenderer = null;
176
177         checkRenderer = new CheckCellRenderer();
178         textRenderer = new TextCellRenderer();
179         buttonListener = new LocalButtonListener();
180         buttonFile.addActionListener(buttonListener);
181         buttonDD.addActionListener(buttonListener);
182
183         //
184
tableDD.setModel(new DDTableModel());
185         tableDD.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
186         tableDD.getColumnModel().getColumn(0).setCellRenderer(checkRenderer);
187         tableDD.getColumnModel().getColumn(1).setCellRenderer(textRenderer);
188         tableDD.getColumnModel().getColumn(2).setCellRenderer(textRenderer);
189         tableDD.getColumnModel().getColumn(0).setPreferredWidth(50);
190         tableDD.getColumnModel().getColumn(1).setPreferredWidth(50);
191         tableDD.getColumnModel().getColumn(2).setPreferredWidth(100);
192     }
193
194     //
195
private void jbInit() throws Exception JavaDoc {
196         panelDescript =
197             (JPanel) Beans.instantiate(getClass().getClassLoader(),
198                                        JPanel.class.getName());
199         layoutMain =
200             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
201                                               GridBagLayout.class.getName());
202         panelFile = (JPanel) Beans.instantiate(getClass().getClassLoader(),
203                                                JPanel.class.getName());
204         textFile = (JTextField) Beans.instantiate(getClass().getClassLoader(),
205                                                   JTextField.class.getName());
206         buttonFile = (JButton) Beans.instantiate(getClass().getClassLoader(),
207                                                  JButton.class.getName());
208         layoutFile =
209             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
210                                               GridBagLayout.class.getName());
211         layoutDescript =
212             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
213                                               GridBagLayout.class.getName());
214         buttonDD = (JButton) Beans.instantiate(getClass().getClassLoader(),
215                                                JButton.class.getName());
216         scroll = (JScrollPane) Beans.instantiate(getClass().getClassLoader(),
217                                                  JScrollPane.class.getName());
218         tableDD = (JTable) Beans.instantiate(getClass().getClassLoader(),
219                                              JTable.class.getName());
220         panelFile.setBorder(new TitledBorder JavaDoc(BorderFactory.createEtchedBorder(new Color(187, 218, 252), new Color(91, 107, 123)),
221                                              getTitle()));
222         panelFile.setLayout(layoutFile);
223         panelDescript.setBorder(new TitledBorder JavaDoc(BorderFactory.createEtchedBorder(new Color(187, 218, 252), new Color(91, 107, 123)),
224                                                  "Deployment Descriptors"));
225         panelDescript.setLayout(layoutDescript);
226         textFile.setEnabled(false);
227         textFile.setText("ARCHIVE FILE");
228         buttonFile.setMargin(new Insets(2, 6, 2, 6));
229         buttonFile.setText("...");
230         buttonDD.setMargin(new Insets(2, 6, 2, 6));
231         buttonDD.setText("...");
232         panelDescript.add(buttonDD,
233                           new GridBagConstraints(3, 0, 1, 1, 0.1, 0.1,
234                                                  GridBagConstraints.CENTER,
235                                                  GridBagConstraints.NONE,
236                                                  new Insets(5, 0, 5, 1), 0,
237                                                  0));
238         panelDescript.add(scroll,
239                           new GridBagConstraints(2, 0, 1, 1, 0.6, 0.6,
240                                                  GridBagConstraints.CENTER,
241                                                  GridBagConstraints.BOTH,
242                                                  new Insets(5, 5, 5, 5), 100,
243                                                  60));
244         scroll.getViewport().add(tableDD, null);
245         panelFile.add(textFile,
246                       new GridBagConstraints(0, 0, 1, 1, 0.6, 0.6,
247                                              GridBagConstraints.WEST,
248                                              GridBagConstraints.HORIZONTAL,
249                                              new Insets(2, 10, 2, 2), 0, 0));
250         panelFile.add(buttonFile,
251                       new GridBagConstraints(1, 0, 1, 1, 0.1, 0.1,
252                                              GridBagConstraints.CENTER,
253                                              GridBagConstraints.NONE,
254                                              new Insets(1, 1, 1, 1), 0, 0));
255         this.setLayout(layoutMain);
256         this.add(panelDescript,
257                  new GridBagConstraints(0, 1, 1, 1, 0.1, 0.1,
258                                         GridBagConstraints.CENTER,
259                                         GridBagConstraints.BOTH,
260                                         new Insets(4, 5, 4, 5), 0, 0));
261         this.add(panelFile,
262                  new GridBagConstraints(0, 0, 1, 1, 0.1, 0.1,
263                                         GridBagConstraints.CENTER,
264                                         GridBagConstraints.BOTH,
265                                         new Insets(4, 5, 4, 5), 0, 0));
266     }
267
268     private void chooseArchive() {
269         ExtensionFilter filter = null;
270         File JavaDoc choice = null;
271         PathHandle path = null;
272
273         filter = new ExtensionFilter();
274         filter.setDescriptionTitle(getTitle());
275         filter.addExtension(getExtension());
276         choice = SwingUtil.getFileChoice(this, getArchivePath(), filter,
277                                          "Select or enter new "
278                                          + getExtension() + " file name");
279         if (choice == null) {
280           // cancelled
281
} else {
282           path = PathHandle.createPathHandle(choice);
283           if (path.getExtension().length() == 0) {
284               path.setExtension(getExtension());
285           }
286           setArchivePath(path.getPath());
287           if ((getWizard() != null) && path.isFile()) {
288               try {
289                   getWizard().readArchive(path.getPath());
290               } catch (ArchiveException e) {
291                   e.printStackTrace(System.err);
292             }
293           }
294         }
295     }
296
297     private void chooseDD() {
298         int index = -1;
299         DDTableModel model = null;
300         Descriptor row = null;
301         File JavaDoc choice = null;
302         FilenameFilter filter = null;
303         PathHandle path = null;
304
305         //
306
index = tableDD.getSelectedRow();
307         if (index > -1) {
308             model = (DDTableModel) tableDD.getModel();
309             row = model.getDescriptor(index);
310             filter = new FilenameFilter();
311             filter.setIncludeName(row.getType() + ".xml");
312             filter.setDescriptionTitle("Deployment Descriptor");
313             if (row.getPath().trim().length() == 0
314                     && getDefaultDD().length > 0) {
315                 path =
316                     PathHandle.createPathHandle(getWorkingRoot()
317                                                 + File.separator
318                                                 + getDefaultDD()[0]);
319                 if (!path.isFile()) {
320                     path =
321                         PathHandle.createPathHandle(getWorkingRoot());
322                 }
323             } else {
324                 path = PathHandle.createPathHandle(row.getPath());
325             }
326             choice = SwingUtil.getFileChoice(this, path.getPath(), filter,
327                                              "Select deployment descriptor");
328         }
329         if (choice == null) {
330
331             // no change
332
} else if (choice.isFile()) {
333             row.setPath(choice.getAbsolutePath());
334             model.setDescriptor(index, row);
335         }
336     }
337
338     private class LocalButtonListener implements ActionListener {
339         public void actionPerformed(ActionEvent e) {
340             Object JavaDoc source = e.getSource();
341
342             if (source == buttonFile) {
343                 chooseArchive();
344             } else if (source == buttonDD) {
345                 chooseDD();
346             }
347         }
348
349     }
350     private class CheckCellRenderer extends DefaultTableCellRenderer JavaDoc {
351         public Component getTableCellRendererComponent(JTable table,
352                 Object JavaDoc value, boolean isSelected, boolean hasFocus, int row,
353                 int column) {
354             Component comp = null;
355             JCheckBox check = null;
356
357             comp = super.getTableCellRendererComponent(table, value,
358                                                        isSelected, hasFocus,
359                                                        row, column);
360             check = new JCheckBox();
361             check.setBackground(comp.getBackground());
362             check.setForeground(comp.getForeground());
363             if (value instanceof Boolean JavaDoc) {
364                 Boolean JavaDoc b = (Boolean JavaDoc) value;
365
366                 check.setSelected(b.booleanValue());
367             }
368             check.setHorizontalAlignment(SwingConstants.CENTER);
369             return check;
370         }
371
372     }
373     private class TextCellRenderer extends DefaultTableCellRenderer JavaDoc {
374         public Component getTableCellRendererComponent(JTable table,
375                 Object JavaDoc value, boolean isSelected, boolean hasFocus, int row,
376                 int column) {
377             Component comp = null;
378
379             comp = super.getTableCellRendererComponent(table, value,
380                                                        isSelected, false,
381                                                        row, column);
382             if (comp instanceof JLabel) {
383                 JLabel jlab = (JLabel) comp;
384
385                 if (column == 1) {
386                     jlab.setHorizontalAlignment(SwingConstants.CENTER);
387                 } else {
388                     jlab.setToolTipText(jlab.getText());
389                 }
390             }
391             return comp;
392         }
393
394     }
395
396
397
398 }
399
Popular Tags