KickJava   Java API By Example, From Geeks To Geeks.

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


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 imports
27
import org.enhydra.tool.archive.ArchiveException;
28 import org.enhydra.tool.archive.ClassFilter;
29 import org.enhydra.tool.common.PathHandle;
30 import org.enhydra.tool.common.SwingUtil;
31
32 // Standard imports
33
import java.awt.event.ActionEvent JavaDoc;
34 import java.awt.event.ActionListener JavaDoc;
35 import java.io.File JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37 import java.util.Enumeration JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Vector JavaDoc;
40 import java.awt.*;
41 import javax.swing.*;
42 import java.beans.*;
43
44 /**
45  * Class declaration
46  *
47  * @author Paul Mahar
48  */

49 public class TwoColumnFileSelector extends JPanel {
50
51     // strings not to be resourced
52
private final String JavaDoc SELECT = ">";
53     private final String JavaDoc SELECT_ALL = ">>";
54     private final String JavaDoc REMOVE = "<";
55     private final String JavaDoc REMOVE_ALL = "<<";
56
57     //
58
private boolean defaultSelect = false;
59     private JLabel labelAvailable = null;
60     private JLabel labelSelected = null;
61     private JScrollPane scrollAvailable = null;
62     private JScrollPane scrollSelected = null;
63     private JList listAvailable = null;
64     private JList listSelected = null;
65     private JButton buttonAdd = null;
66     private JButton buttonAddAll = null;
67     private JButton buttonRemove = null;
68     private JButton buttonRemoveAll = null;
69     private GridBagLayout layoutSelect = null;
70     private JPanel panelSelect = null;
71     private JPanel panelOption = null;
72     private BorderLayout layoutMain = null;
73     private GridBagLayout layoutOption = null;
74     private JCheckBox checkShowFull = null;
75     private JLabel labelRoot = null;
76     private JTextField textRoot = null;
77     private JButton buttonRoot = null;
78     private LocalButtonListener buttonListener = null;
79     private DefaultListModel modelAvailable = new DefaultListModel();
80     private DefaultListModel modelSelected = new DefaultListModel();
81     private ClassFilter filter = null;
82
83     /**
84      * Constructor declaration
85      *
86      */

87     public TwoColumnFileSelector() {
88         try {
89             jbInit();
90             pmInit();
91         } catch (Exception JavaDoc e) {
92             e.printStackTrace();
93         }
94     }
95
96     protected void validatePlan() throws ArchiveException {
97         String JavaDoc[] selects = new String JavaDoc[0];
98         File JavaDoc file = null;
99
100         if (getRootFile() == null || (!getRootFile().isDirectory())) {
101             throw new ArchiveException("Invalid root");
102         }
103         selects = getSelections();
104         for (int i = 0; i < selects.length; i++) {
105             file = new File JavaDoc(selects[i]);
106             if (!file.isFile()) {
107                 throw new ArchiveException("Invalid selection: "
108                                            + file.getAbsolutePath());
109             }
110         }
111     }
112
113     protected void setFilter(ClassFilter f) {
114         filter = f;
115     }
116
117     protected ClassFilter getFilter() {
118         return filter;
119     }
120
121     protected void setDefaultSelect(boolean b) {
122         defaultSelect = b;
123     }
124
125     protected boolean isDefaultSelect() {
126         return defaultSelect;
127     }
128
129     private void clearLists() {
130         if (getModelSelected().getSize() > 0) {
131             getModelSelected().clear();
132         }
133         if (getModelAvailable().getSize() > 0) {
134             getModelAvailable().clear();
135         }
136     }
137
138     /**
139      * Method declaration
140      *
141      */

142     protected void fillFrom(File JavaDoc cursor) throws ListOverflow {
143         File JavaDoc[] children = new File JavaDoc[0];
144
145         if (getModelAvailable().size() + getModelSelected().size() > 2500) {
146             throw new ListOverflow();
147         } else if (cursor == null) {
148             children = null;
149         } else {
150             if (getFilter() == null) {
151                 children = cursor.listFiles();
152             } else {
153                 getFilter().setRoot(getRoot());
154                 children = cursor.listFiles(getFilter());
155             }
156         }
157         if (children == null) {
158
159             // done
160
} else {
161             for (int i = 0; i < children.length; i++) {
162                 if (children[i].isDirectory()) {
163                     fillFrom(children[i]);
164                 } else if (children[i].isFile()) {
165                     LocalListNode listNode = null;
166
167                     listNode =
168                         new LocalListNode(children[i].getAbsolutePath());
169                     if (isDefaultSelect()) {
170                         getModelSelected().addElement(listNode);
171                     } else {
172                         getModelAvailable().addElement(listNode);
173                     }
174                 }
175             }
176         }
177     }
178
179     // /
180
// /
181

182     /**
183      * Method declaration
184      *
185      *
186      * @return
187      */

188     protected DefaultListModel getModelSelected() {
189         return modelSelected;
190     }
191
192     /**
193      * Method declaration
194      *
195      *
196      * @return
197      */

198     protected DefaultListModel getModelAvailable() {
199         return modelAvailable;
200     }
201
202     /**
203      * Method declaration
204      *
205      *
206      * @return
207      */

208     public String JavaDoc[] getSelections() {
209         String JavaDoc[] selects = new String JavaDoc[0];
210         Enumeration JavaDoc enumeration = null;
211         ArrayList JavaDoc list = new ArrayList JavaDoc();
212
213         enumeration = getModelSelected().elements();
214         while (enumeration.hasMoreElements()) {
215             LocalListNode next = (LocalListNode) enumeration.nextElement();
216
217             if (next.getFullname().trim().length() > 0) {
218                 list.add(next.getFullname());
219             }
220         }
221         list.trimToSize();
222         selects = new String JavaDoc[list.size()];
223         selects = (String JavaDoc[]) list.toArray(selects);
224         return selects;
225     }
226
227     public void setSelections(File JavaDoc[] selectFiles) {
228         String JavaDoc[] selects = new String JavaDoc[0];
229
230         if (selectFiles != null) {
231             selects = new String JavaDoc[selectFiles.length];
232             for (int i = 0; i < selects.length; i++) {
233                 selects[i] = selectFiles[i].getAbsolutePath();
234             }
235         }
236         setSelections(selects);
237     }
238
239     public void setSelections(String JavaDoc[] selects) {
240         setDefaultSelect(false);
241         clearLists();
242         fillFromRoot();
243         for (int i = 0; i < selects.length; i++) {
244             LocalListNode node = null;
245
246             node = new LocalListNode(selects[i]);
247             if (!getModelSelected().contains(node)) {
248                 getModelSelected().addElement(node);
249             }
250             if (getModelAvailable().contains(node)) {
251                 getModelAvailable().removeElement(node);
252             }
253         }
254     }
255
256     public void setRoot(String JavaDoc path) {
257         PathHandle root = null;
258
259         root = PathHandle.createPathHandle(path);
260         if (root.isDirectory()) {
261             textRoot.setText(root.getPath());
262         } else {
263             textRoot.setText(new String JavaDoc());
264         }
265         textRoot.setToolTipText(textRoot.getText());
266         clearLists();
267         fillFromRoot();
268     }
269
270     public String JavaDoc getRoot() {
271         String JavaDoc root = null;
272         PathHandle path = null;
273
274         path = PathHandle.createPathHandle(textRoot.getText());
275         if (path.isDirectory()) {
276             root = path.getPath();
277         }
278         return root;
279     }
280
281     public File JavaDoc getRootFile() {
282         String JavaDoc root = getRoot();
283         File JavaDoc file = null;
284
285         if (root == null) {
286
287             //
288
} else {
289             file = new File JavaDoc(root);
290         }
291         return file;
292     }
293
294     private void fillFromRoot() {
295         try {
296             fillFrom(getRootFile());
297         } catch (ListOverflow e) {
298             JOptionPane.showMessageDialog(this,
299                                           "Reached maximum files for root: 2500",
300                                           "Archive Wizard Warning",
301                                           JOptionPane.WARNING_MESSAGE);
302         }
303     }
304
305     private void pmInit() {
306         buttonListener = new LocalButtonListener();
307         buttonRoot.addActionListener(buttonListener);
308         buttonAdd.addActionListener(buttonListener);
309         buttonAddAll.addActionListener(buttonListener);
310         buttonRemove.addActionListener(buttonListener);
311         buttonRemoveAll.addActionListener(buttonListener);
312         checkShowFull.addActionListener(buttonListener);
313         checkShowFull.setSelected(false);
314         textRoot.setText(new String JavaDoc());
315     }
316
317     private void jbInit() throws Exception JavaDoc {
318         layoutSelect =
319             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
320                                               GridBagLayout.class.getName());
321         panelSelect = (JPanel) Beans.instantiate(getClass().getClassLoader(),
322                                                  JPanel.class.getName());
323         panelOption = (JPanel) Beans.instantiate(getClass().getClassLoader(),
324                                                  JPanel.class.getName());
325         scrollAvailable =
326             (JScrollPane) Beans.instantiate(getClass().getClassLoader(),
327                                             JScrollPane.class.getName());
328         scrollSelected =
329             (JScrollPane) Beans.instantiate(getClass().getClassLoader(),
330                                             JScrollPane.class.getName());
331         labelAvailable =
332             (JLabel) Beans.instantiate(getClass().getClassLoader(),
333                                        JLabel.class.getName());
334         labelSelected =
335             (JLabel) Beans.instantiate(getClass().getClassLoader(),
336                                        JLabel.class.getName());
337         listAvailable = (JList) Beans.instantiate(getClass().getClassLoader(),
338                                                   JList.class.getName());
339         listSelected = (JList) Beans.instantiate(getClass().getClassLoader(),
340                                                  JList.class.getName());
341         buttonAdd = (JButton) Beans.instantiate(getClass().getClassLoader(),
342                                                 JButton.class.getName());
343         buttonAddAll =
344             (JButton) Beans.instantiate(getClass().getClassLoader(),
345                                         JButton.class.getName());
346         buttonRemove =
347             (JButton) Beans.instantiate(getClass().getClassLoader(),
348                                         JButton.class.getName());
349         buttonRemoveAll =
350             (JButton) Beans.instantiate(getClass().getClassLoader(),
351                                         JButton.class.getName());
352         layoutMain =
353             (BorderLayout) Beans.instantiate(getClass().getClassLoader(),
354                                              BorderLayout.class.getName());
355         layoutOption =
356             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
357                                               GridBagLayout.class.getName());
358         checkShowFull =
359             (JCheckBox) Beans.instantiate(getClass().getClassLoader(),
360                                           JCheckBox.class.getName());
361         labelRoot = (JLabel) Beans.instantiate(getClass().getClassLoader(),
362                                                JLabel.class.getName());
363         textRoot = (JTextField) Beans.instantiate(getClass().getClassLoader(),
364                                                   JTextField.class.getName());
365         buttonRoot = (JButton) Beans.instantiate(getClass().getClassLoader(),
366                                                  JButton.class.getName());
367         listAvailable.setModel(modelAvailable);
368         listSelected.setModel(modelSelected);
369         labelAvailable.setText("Available");
370         labelSelected.setText("Selected");
371         buttonAdd.setMaximumSize(new Dimension(40, 27));
372         buttonAdd.setMinimumSize(new Dimension(40, 27));
373         buttonAdd.setPreferredSize(new Dimension(40, 27));
374         buttonAdd.setToolTipText("Select");
375         buttonAdd.setMargin(new Insets(2, 2, 2, 2));
376         buttonAdd.setText(SELECT);
377         buttonAddAll.setMaximumSize(new Dimension(40, 27));
378         buttonAddAll.setMinimumSize(new Dimension(40, 27));
379         buttonAddAll.setPreferredSize(new Dimension(40, 27));
380         buttonAddAll.setToolTipText("Select All");
381         buttonAddAll.setMargin(new Insets(2, 2, 2, 2));
382         buttonAddAll.setText(SELECT_ALL);
383         buttonRemove.setMaximumSize(new Dimension(40, 27));
384         buttonRemove.setMinimumSize(new Dimension(40, 27));
385         buttonRemove.setPreferredSize(new Dimension(40, 27));
386         buttonRemove.setToolTipText("Deselect");
387         buttonRemove.setMargin(new Insets(2, 2, 2, 2));
388         buttonRemove.setText(REMOVE);
389         buttonRemoveAll.setMaximumSize(new Dimension(40, 27));
390         buttonRemoveAll.setMinimumSize(new Dimension(40, 27));
391         buttonRemoveAll.setPreferredSize(new Dimension(40, 27));
392         buttonRemoveAll.setToolTipText("Deselect All");
393         buttonRemoveAll.setMargin(new Insets(2, 2, 2, 2));
394         buttonRemoveAll.setText(REMOVE_ALL);
395         scrollAvailable.setMaximumSize(new Dimension(300, 300));
396         scrollAvailable.setPreferredSize(new Dimension(250, 250));
397         scrollSelected.setMaximumSize(new Dimension(300, 300));
398         scrollSelected.setPreferredSize(new Dimension(250, 250));
399         checkShowFull.setText("Show paths");
400         labelRoot.setText("Root:");
401         textRoot.setEnabled(false);
402         textRoot.setEditable(false);
403         textRoot.setText("jTextField1");
404         buttonRoot.setMargin(new Insets(2, 6, 2, 6));
405         buttonRoot.setText("...");
406         scrollAvailable.getViewport().add(listAvailable);
407         scrollSelected.getViewport().add(listSelected);
408         panelSelect.setLayout(layoutSelect);
409         panelSelect.add(labelAvailable,
410                         new GridBagConstraints(0, 0, 1, 1, 0.1, 0.1,
411                                                GridBagConstraints.WEST,
412                                                GridBagConstraints.HORIZONTAL,
413                                                new Insets(5, 5, 2, 5), 0, 0));
414         panelSelect.add(scrollAvailable,
415                         new GridBagConstraints(0, 1, 1, 4, 0.2, 0.2,
416                                                GridBagConstraints.WEST,
417                                                GridBagConstraints.BOTH,
418                                                new Insets(2, 0, 2, 2), 100,
419                                                150));
420         panelSelect.add(buttonAdd,
421                         new GridBagConstraints(1, 1, 1, 1, 0.1, 0.1,
422                                                GridBagConstraints.CENTER,
423                                                GridBagConstraints.NONE,
424                                                new Insets(8, 0, 4, 0), 10,
425                                                0));
426         panelSelect.add(buttonAddAll,
427                         new GridBagConstraints(1, 2, 1, 1, 0.1, 0.1,
428                                                GridBagConstraints.CENTER,
429                                                GridBagConstraints.NONE,
430                                                new Insets(4, 0, 4, 0), 10,
431                                                0));
432         panelSelect.add(buttonRemove,
433                         new GridBagConstraints(1, 3, 1, 1, 0.1, 0.1,
434                                                GridBagConstraints.SOUTH,
435                                                GridBagConstraints.NONE,
436                                                new Insets(4, 0, 4, 0), 10,
437                                                0));
438         panelSelect.add(buttonRemoveAll,
439                         new GridBagConstraints(1, 4, 1, 1, 0.1, 0.1,
440                                                GridBagConstraints.CENTER,
441                                                GridBagConstraints.NONE,
442                                                new Insets(4, 0, 8, 0), 10,
443                                                0));
444         panelSelect.add(labelSelected,
445                         new GridBagConstraints(2, 0, 1, 1, 0.1, 0.1,
446                                                GridBagConstraints.WEST,
447                                                GridBagConstraints.HORIZONTAL,
448                                                new Insets(5, 5, 2, 5), 0, 0));
449         panelSelect.add(scrollSelected,
450                         new GridBagConstraints(2, 1, 1, 4, 0.2, 0.2,
451                                                GridBagConstraints.EAST,
452                                                GridBagConstraints.BOTH,
453                                                new Insets(2, 2, 2, 0), 100,
454                                                150));
455         panelOption.setLayout(layoutOption);
456         panelOption.add(labelRoot,
457                         new GridBagConstraints(0, 1, 1, 1, 0.1, 0.1,
458                                                GridBagConstraints.WEST,
459                                                GridBagConstraints.NONE,
460                                                new Insets(5, 15, 5, 0), 0,
461                                                0));
462         panelOption.add(checkShowFull,
463                         new GridBagConstraints(0, 0, 3, 1, 0.5, 0.5,
464                                                GridBagConstraints.WEST,
465                                                GridBagConstraints.HORIZONTAL,
466                                                new Insets(5, 15, 5, 5), 0,
467                                                0));
468         panelOption.add(textRoot,
469                         new GridBagConstraints(1, 1, 1, 1, 0.8, 0.8,
470                                                GridBagConstraints.CENTER,
471                                                GridBagConstraints.HORIZONTAL,
472                                                new Insets(5, 1, 5, 1), 0, 0));
473         panelOption.add(buttonRoot,
474                         new GridBagConstraints(2, 1, 1, 1, 0.1, 0.1,
475                                                GridBagConstraints.CENTER,
476                                                GridBagConstraints.NONE,
477                                                new Insets(5, 1, 5, 1), 0, 0));
478         this.setLayout(layoutMain);
479         this.setMinimumSize(new Dimension(400, 200));
480         this.setPreferredSize(new Dimension(620, 300));
481         this.add(panelSelect, BorderLayout.CENTER);
482         this.add(panelOption, BorderLayout.SOUTH);
483     }
484
485     private void addSelected() {
486         Object JavaDoc[] elements = listAvailable.getSelectedValues();
487
488         for (int i = 0; i < elements.length; i++) {
489             LocalListNode listNode = (LocalListNode) elements[i];
490
491             modelSelected.addElement(listNode);
492             modelAvailable.removeElement(listNode);
493         }
494     }
495
496     private void refreshLists() {
497         int size = modelAvailable.getSize();
498
499         for (int i = 0; i < size; i++) {
500             modelAvailable.setElementAt(modelAvailable.getElementAt(i), i);
501         }
502         size = modelSelected.getSize();
503         for (int i = 0; i < size; i++) {
504             modelSelected.setElementAt(modelSelected.getElementAt(i), i);
505         }
506     }
507
508     private void addAllElements() {
509         for (int i = 0; i < modelAvailable.size(); i++) {
510             modelSelected.addElement(modelAvailable.elementAt(i));
511         }
512         modelAvailable.removeAllElements();
513     }
514
515     private void removeSelected() {
516         Object JavaDoc[] elements = listSelected.getSelectedValues();
517
518         for (int i = 0; i < elements.length; i++) {
519             LocalListNode listNode = (LocalListNode) elements[i];
520
521             modelAvailable.addElement(listNode);
522             modelSelected.removeElement(listNode);
523         }
524     }
525
526     private void removeAllElements() {
527         for (int i = 0; i < modelSelected.size(); i++) {
528             modelAvailable.addElement(modelSelected.elementAt(i));
529         }
530         modelSelected.removeAllElements();
531     }
532
533     private void selectRoot() {
534         File JavaDoc choice = null;
535
536         choice = SwingUtil.getDirectoryChoice(this, textRoot.getText(),
537                                               "Select root directory");
538         if (choice == null) {
539
540             // no change
541
} else {
542             setRoot(choice.getAbsolutePath());
543         }
544     }
545
546     //
547
private class LocalButtonListener implements ActionListener JavaDoc {
548         public void actionPerformed(ActionEvent JavaDoc event) {
549             Object JavaDoc source = event.getSource();
550
551             if (source == buttonAdd) {
552                 addSelected();
553             } else if (source == buttonAddAll) {
554                 addAllElements();
555             } else if (source == buttonRemove) {
556                 removeSelected();
557             } else if (source == buttonRemoveAll) {
558                 removeAllElements();
559             } else if (source == checkShowFull) {
560                 refreshLists();
561             } else if (source == buttonRoot) {
562                 selectRoot();
563             }
564         }
565
566     }
567
568     //
569
private class LocalListNode {
570         private String JavaDoc full = new String JavaDoc();
571
572         public LocalListNode(String JavaDoc fullName) {
573             full = PathHandle.createPathString(fullName);
574         }
575
576         public String JavaDoc toString() {
577             String JavaDoc s = full;
578
579             if (!checkShowFull.isSelected()) {
580                 File JavaDoc f = new File JavaDoc(full);
581
582                 s = f.getName();
583             }
584             return s;
585         }
586
587         public String JavaDoc getFullname() {
588             return full;
589         }
590
591         public boolean equals(Object JavaDoc in) {
592             boolean eq = false;
593
594             if (in instanceof LocalListNode) {
595                 LocalListNode comp = (LocalListNode) in;
596
597                 eq = comp.getFullname().equals(getFullname());
598             }
599             return eq;
600         }
601
602     }
603     private class ListOverflow extends Exception JavaDoc {}
604 }
605
Popular Tags