KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 //
26
package org.enhydra.tool.archive.wizard;
27
28 //
29
import org.enhydra.tool.common.PathHandle;
30 import org.enhydra.tool.common.ExtensionFilter;
31 import org.enhydra.tool.common.SwingUtil;
32
33 //
34
import java.awt.*;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.ActionListener JavaDoc;
37 import java.beans.Beans JavaDoc;
38 import java.io.File JavaDoc;
39 import javax.swing.*;
40
41 //
42
public class OneColumnFileSelector extends RefPanel {
43     private JScrollPane scrollPane = null;
44     private JList list = null;
45     private JButton buttonAdd = null;
46     private JButton buttonRemove = null;
47     private GridBagLayout layoutMain = null;
48     private LocalButtonListener buttonListener = null;
49     private String JavaDoc[] extensions = {
50         "jar"
51     };
52     private String JavaDoc title = "Library";
53
54     //
55
public OneColumnFileSelector() {
56         try {
57             jbInit();
58             pmInit();
59         } catch (Exception JavaDoc ex) {
60             ex.printStackTrace();
61         }
62     }
63
64     public String JavaDoc[] getSelections() {
65         int size = list.getModel().getSize();
66         String JavaDoc[] selects = new String JavaDoc[size];
67
68         for (int i = 0; i < size; i++) {
69             selects[i] = list.getModel().getElementAt(i).toString();
70         }
71         return selects;
72     }
73
74     protected void setTitle(String JavaDoc t) {
75         title = t;
76     }
77
78     protected String JavaDoc getTitle() {
79         return title;
80     }
81
82     protected void setExtensions(String JavaDoc[] exts) {
83         extensions = exts;
84     }
85
86     protected String JavaDoc[] getExtensions() {
87         return extensions;
88     }
89
90     protected void setSelections(String JavaDoc[] files) {
91         getListModel().clear();
92         for (int i = 0; i < files.length; i++) {
93             getListModel().addElement(files[i]);
94         }
95     }
96
97     protected JScrollPane getScrollPane() {
98         return scrollPane;
99     }
100
101     //
102
private void pmInit() {
103         buttonListener = new LocalButtonListener();
104         buttonAdd.addActionListener(buttonListener);
105         buttonRemove.addActionListener(buttonListener);
106         list.setModel(new DefaultListModel());
107     }
108
109     private void jbInit() throws Exception JavaDoc {
110         scrollPane =
111             (JScrollPane) Beans.instantiate(getClass().getClassLoader(),
112                                             JScrollPane.class.getName());
113         list = (JList) Beans.instantiate(getClass().getClassLoader(),
114                                          JList.class.getName());
115         buttonAdd = (JButton) Beans.instantiate(getClass().getClassLoader(),
116                                                 JButton.class.getName());
117         buttonRemove =
118             (JButton) Beans.instantiate(getClass().getClassLoader(),
119                                         JButton.class.getName());
120         layoutMain =
121             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
122                                               GridBagLayout.class.getName());
123         buttonAdd.setPreferredSize(new Dimension(75, 27));
124         buttonAdd.setMargin(new Insets(2, 10, 2, 10));
125         buttonAdd.setText("Add...");
126         buttonRemove.setPreferredSize(new Dimension(75, 27));
127         buttonRemove.setMargin(new Insets(2, 10, 2, 10));
128         buttonRemove.setText("Remove");
129         scrollPane.getViewport().add(list, null);
130         this.setLayout(layoutMain);
131         this.add(scrollPane,
132                  new GridBagConstraints(0, 0, 1, 2, 0.8, 0.8,
133                                         GridBagConstraints.CENTER,
134                                         GridBagConstraints.BOTH,
135                                         new Insets(5, 5, 0, 5), 0, 0));
136         this.add(buttonAdd,
137                  new GridBagConstraints(1, 0, 1, 1, 0.1, 0.1,
138                                         GridBagConstraints.SOUTH,
139                                         GridBagConstraints.NONE,
140                                         new Insets(10, 5, 10, 5), 20, 0));
141         this.add(buttonRemove,
142                  new GridBagConstraints(1, 1, 1, 1, 0.1, 0.1,
143                                         GridBagConstraints.NORTH,
144                                         GridBagConstraints.NONE,
145                                         new Insets(10, 5, 10, 5), 20, 0));
146     }
147
148     private void addChoice() {
149         ExtensionFilter filter = null;
150         File JavaDoc choice = null;
151         PathHandle path = null;
152
153         filter = new ExtensionFilter();
154         filter.setDescriptionTitle(getTitle());
155         for (int i = 0; i < getExtensions().length; i++) {
156             filter.addExtension(getExtensions()[i]);
157         }
158         choice = SwingUtil.getFileChoice(this, getWorkingRoot(), filter,
159                                          "Select a "
160                                          + getTitle().toLowerCase()
161                                          + " to add");
162         path = PathHandle.createPathHandle(choice);
163         if (path.isFile()) {
164             addToModel(path);
165         }
166     }
167
168     protected void addToModel(PathHandle path) {
169         if (!getListModel().contains(path.getPath())) {
170             getListModel().addElement(path.getPath());
171         }
172     }
173
174     private DefaultListModel getListModel() {
175         return (DefaultListModel) list.getModel();
176     }
177
178     protected void removeSelection() {
179         int index = list.getSelectedIndex();
180
181         if ((index > -1) && (index < getListModel().size())) {
182             getListModel().remove(index);
183         }
184     }
185
186     private class LocalButtonListener implements ActionListener JavaDoc {
187         public void actionPerformed(ActionEvent JavaDoc event) {
188             Object JavaDoc source = event.getSource();
189
190             if (source == buttonAdd) {
191                 addChoice();
192             } else if (source == buttonRemove) {
193                 removeSelection();
194             }
195         }
196
197     }
198 }
199
Popular Tags