KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jahia > opentools > advancedapprunner > ClassPathListPanel


1 package com.jahia.opentools.advancedapprunner;
2
3 import java.io.File JavaDoc;
4
5 import javax.swing.event.ListSelectionEvent JavaDoc;
6 import javax.swing.event.ListSelectionListener JavaDoc;
7
8 import com.borland.primetime.vfs.FileFilesystem;
9 import com.borland.primetime.vfs.Url;
10 import com.borland.primetime.vfs.ZipFilesystem;
11 import com.borland.primetime.vfs.ui.UrlChooser;
12 import java.awt.*;
13 import javax.swing.*;
14 import java.awt.event.*;
15
16 /**
17  * <p>Title: List of class paths</p>
18  * <p>Description: </p>
19  * <p>Copyright: Copyright (c) 2001</p>
20  * <p>Company: </p>
21  * @author Serge Huber
22  * @version 1.0
23  */

24 public class ClassPathListPanel extends JPanel implements ListSelectionListener JavaDoc {
25
26     private static final String JavaDoc TITLE = "Class path directory";
27     GridBagLayout gridBagLayout = new GridBagLayout();
28     DefaultListModel listModel = new DefaultListModel();
29     JButton addURLButton = new JButton();
30     JButton addCustomButton = new JButton();
31     JButton removeButton = new JButton();
32     JButton moveUpButton = new JButton();
33     JButton moveDownButton = new JButton();
34     JScrollPane scrollPane = new JScrollPane();
35     JList list = new JList();
36     JButton editButton = new JButton();
37
38     /**
39      * Make adding an element public.
40      *
41      * @param obj is the new element to add
42      */

43     public void addListElement(Object JavaDoc obj) {
44         if (obj == null) {
45             return;
46         }
47         int index = list.getSelectedIndex();
48         int size = listModel.getSize();
49
50         //If no selection or if item in last position is selected,
51
//add the new one to end of list, and select new one.
52
if (index == -1 || (index + 1 >= size)) {
53             listModel.addElement(obj);
54             list.setSelectedIndex(size);
55
56             //Otherwise insert the new one after the current selection,
57
//and select new one.
58
} else {
59             listModel.insertElementAt(obj, index + 1);
60             list.setSelectedIndex(index + 1);
61         }
62     }
63
64     public DefaultListModel getList() {
65         return listModel;
66     }
67
68     /**
69      * Delete all entries in the list.
70      */

71     public void clear() {
72         listModel.clear();
73     }
74
75     /**
76      * Update the given value in the list.
77      *
78      * @param value the element to amend
79      * @return the updated element, or null if cancelled
80      */

81     protected Object JavaDoc editElement(Object JavaDoc value) {
82         Url url = null;
83         if (value != null) {
84             url = new Url(new File JavaDoc( (String JavaDoc) value));
85         }
86         url = UrlChooser.promptForDir(this, url, TITLE);
87         if (url == null) {
88             return null;
89         }
90         if ( (url.getProtocol() != FileFilesystem.PROTOCOL) &&
91             (url.getProtocol() != ZipFilesystem.PROTOCOL)) {
92             JOptionPane.showMessageDialog(this,
93                                           "Only normal file system directories can be selected",
94                                           "Run - OpenTool",
95                                           JOptionPane.WARNING_MESSAGE);
96             return null;
97         }
98         return url.getFileObject().toString();
99     }
100
101     /**
102      * Ask for a new element to add.
103      *
104      * @return the new element, or null if cancelled
105      */

106     protected Object JavaDoc promptForElement() {
107         return editElement(null);
108     }
109
110     public ClassPathListPanel() {
111         try {
112             jbInit();
113         } catch (Exception JavaDoc e) {
114             e.printStackTrace();
115         }
116     }
117
118     private void jbInit() throws Exception JavaDoc {
119         this.setLayout(gridBagLayout);
120         list.setSelectionMode(
121             ListSelectionModel.SINGLE_INTERVAL_SELECTION);
122         list.setModel(listModel);
123         list.setSelectedIndex(0);
124         list.addListSelectionListener(this);
125         addURLButton.setMaximumSize(new Dimension(103, 25));
126         addURLButton.setMinimumSize(new Dimension(103, 25));
127         addURLButton.setPreferredSize(new Dimension(103, 25));
128         addURLButton.setText("Add path...");
129         addURLButton.addActionListener(new
130             ClassPathListPanel_addURLButton_actionAdapter(this));
131         addCustomButton.setText("Add Custom...");
132         addCustomButton.addActionListener(new
133             ClassPathListPanel_addCustomButton_actionAdapter(this));
134         removeButton.setMaximumSize(new Dimension(103, 25));
135         removeButton.setMinimumSize(new Dimension(103, 25));
136         removeButton.setOpaque(true);
137         removeButton.setPreferredSize(new Dimension(103, 25));
138         removeButton.setText("Remove");
139         removeButton.addActionListener(new
140             ClassPathListPanel_removeButton_actionAdapter(this));
141         moveUpButton.setMaximumSize(new Dimension(103, 25));
142         moveUpButton.setMinimumSize(new Dimension(103, 25));
143         moveUpButton.setPreferredSize(new Dimension(103, 25));
144         moveUpButton.setText("Move Up");
145         moveUpButton.addActionListener(new
146             ClassPathListPanel_moveUpButton_actionAdapter(this));
147         moveDownButton.setMaximumSize(new Dimension(103, 25));
148         moveDownButton.setMinimumSize(new Dimension(103, 25));
149         moveDownButton.setPreferredSize(new Dimension(103, 25));
150         moveDownButton.setText("Move Down");
151         moveDownButton.addActionListener(new
152             ClassPathListPanel_moveDownButton_actionAdapter(this));
153         scrollPane.setMinimumSize(new Dimension(200, 150));
154         this.setMinimumSize(new Dimension(323, 160));
155         editButton.setMaximumSize(new Dimension(103, 25));
156         editButton.setMinimumSize(new Dimension(103, 25));
157         editButton.setPreferredSize(new Dimension(103, 25));
158         editButton.setText("Edit...");
159         editButton.addActionListener(new ClassPathListPanel_editButton_actionAdapter(this));
160         this.add(addCustomButton, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
161             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
162         this.add(addURLButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
163             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
164         this.add(removeButton, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0
165             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 5, 2, 5), 0, 0));
166         this.add(moveUpButton, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0
167             ,GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
168         this.add(moveDownButton, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0
169             ,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
170         this.add(scrollPane, new GridBagConstraints(0, 0, 1, 6, 1.0, 1.0
171             ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
172         this.add(editButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
173             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2, 5, 2, 5), 0, 0));
174         scrollPane.getViewport().add(list, null);
175     }
176
177     //Swap two elements in the list.
178
private void swap(int a, int b) {
179         Object JavaDoc aObject = listModel.getElementAt(a);
180         Object JavaDoc bObject = listModel.getElementAt(b);
181         listModel.set(a, bObject);
182         listModel.set(b, aObject);
183     }
184
185     //Listener method for list selection changes.
186
public void valueChanged(ListSelectionEvent JavaDoc e) {
187         if (e.getValueIsAdjusting() == false) {
188
189             if (list.getSelectedIndex() == -1) {
190                 //No selection: disable delete, up, and down buttons.
191
removeButton.setEnabled(false);
192                 moveUpButton.setEnabled(false);
193                 moveDownButton.setEnabled(false);
194                 editButton.setEnabled(false);
195
196             } else if (list.getSelectedIndices().length > 1) {
197                 //Multiple selection: disable up and down buttons.
198
removeButton.setEnabled(true);
199                 moveUpButton.setEnabled(false);
200                 moveDownButton.setEnabled(false);
201                 editButton.setEnabled(false);
202
203             } else {
204                 //Single selection: permit all operations.
205
removeButton.setEnabled(true);
206                 moveUpButton.setEnabled(true);
207                 moveDownButton.setEnabled(true);
208                 editButton.setEnabled(true);
209             }
210         }
211     }
212
213     void addURLButton_actionPerformed(ActionEvent e) {
214         Object JavaDoc newURL = promptForElement();
215         if (newURL != null) {
216             addListElement(newURL);
217         }
218     }
219
220     void addCustomButton_actionPerformed(ActionEvent e) {
221         String JavaDoc inputValue = JOptionPane.showInputDialog("Enter custom path :");
222         if ((inputValue != null) && (!"".equals(inputValue))) {
223             addListElement(inputValue);
224         }
225     }
226
227     void removeButton_actionPerformed(ActionEvent e) {
228         /*
229          * This method can be called only if
230          * there's a valid selection,
231          * so go ahead and remove whatever's selected.
232          */

233
234         ListSelectionModel lsm = list.getSelectionModel();
235         int firstSelected = lsm.getMinSelectionIndex();
236         int lastSelected = lsm.getMaxSelectionIndex();
237         listModel.removeRange(firstSelected, lastSelected);
238
239         int size = listModel.size();
240
241         if (size == 0) {
242             //List is empty: disable delete, up, and down buttons.
243
removeButton.setEnabled(false);
244             moveUpButton.setEnabled(false);
245             moveDownButton.setEnabled(false);
246
247         } else {
248             //Adjust the selection.
249
if (firstSelected == listModel.getSize()) {
250                 //Removed item in last position.
251
firstSelected--;
252             }
253             list.setSelectedIndex(firstSelected);
254         }
255     }
256
257     void moveUpButton_actionPerformed(ActionEvent e) {
258         //This method can be called only when
259
//there's a valid selection,
260
//so go ahead and move the list item.
261
int moveMe = list.getSelectedIndex();
262
263         //UP ARROW BUTTON
264
if (moveMe != 0) {
265             //not already at top
266
swap(moveMe, moveMe - 1);
267             list.setSelectedIndex(moveMe - 1);
268             list.ensureIndexIsVisible(moveMe - 1);
269         }
270     }
271
272     void moveDownButton_actionPerformed(ActionEvent e) {
273         //This method can be called only when
274
//there's a valid selection,
275
//so go ahead and move the list item.
276
int moveMe = list.getSelectedIndex();
277
278         //DOWN ARROW BUTTON
279
if (moveMe != listModel.getSize() - 1) {
280             //not already at bottom
281
swap(moveMe, moveMe + 1);
282             list.setSelectedIndex(moveMe + 1);
283             list.ensureIndexIsVisible(moveMe + 1);
284         }
285     }
286
287     void editButton_actionPerformed(ActionEvent e) {
288         int editIndex = list.getSelectedIndex();
289         String JavaDoc curPathValue = (String JavaDoc) listModel.getElementAt(editIndex);
290         String JavaDoc inputValue = JOptionPane.showInputDialog("Edit path :", curPathValue);
291         if ((inputValue != null) && (!"".equals(inputValue))) {
292             listModel.setElementAt(inputValue, editIndex);
293             list.ensureIndexIsVisible(editIndex);
294         }
295     }
296 }
297
298 class ClassPathListPanel_addURLButton_actionAdapter implements java.awt.event.
299
JavaDoc    ActionListener {
300     ClassPathListPanel adaptee;
301
302     ClassPathListPanel_addURLButton_actionAdapter(ClassPathListPanel adaptee) {
303         this.adaptee = adaptee;
304     }
305
306     public void actionPerformed(ActionEvent e) {
307         adaptee.addURLButton_actionPerformed(e);
308     }
309 }
310
311 class ClassPathListPanel_addCustomButton_actionAdapter implements java.awt.
312
JavaDoc    event.ActionListener {
313     ClassPathListPanel adaptee;
314
315     ClassPathListPanel_addCustomButton_actionAdapter(ClassPathListPanel adaptee) {
316         this.adaptee = adaptee;
317     }
318
319     public void actionPerformed(ActionEvent e) {
320         adaptee.addCustomButton_actionPerformed(e);
321     }
322 }
323
324 class ClassPathListPanel_removeButton_actionAdapter implements java.awt.event.
325
JavaDoc    ActionListener {
326     ClassPathListPanel adaptee;
327
328     ClassPathListPanel_removeButton_actionAdapter(ClassPathListPanel adaptee) {
329         this.adaptee = adaptee;
330     }
331
332     public void actionPerformed(ActionEvent e) {
333         adaptee.removeButton_actionPerformed(e);
334     }
335 }
336
337 class ClassPathListPanel_moveUpButton_actionAdapter implements java.awt.event.
338
JavaDoc    ActionListener {
339     ClassPathListPanel adaptee;
340
341     ClassPathListPanel_moveUpButton_actionAdapter(ClassPathListPanel adaptee) {
342         this.adaptee = adaptee;
343     }
344
345     public void actionPerformed(ActionEvent e) {
346         adaptee.moveUpButton_actionPerformed(e);
347     }
348 }
349
350 class ClassPathListPanel_moveDownButton_actionAdapter implements java.awt.event.
351
JavaDoc    ActionListener {
352     ClassPathListPanel adaptee;
353
354     ClassPathListPanel_moveDownButton_actionAdapter(ClassPathListPanel adaptee) {
355         this.adaptee = adaptee;
356     }
357
358     public void actionPerformed(ActionEvent e) {
359         adaptee.moveDownButton_actionPerformed(e);
360     }
361 }
362
363 class ClassPathListPanel_editButton_actionAdapter implements java.awt.event.ActionListener JavaDoc {
364     ClassPathListPanel adaptee;
365
366     ClassPathListPanel_editButton_actionAdapter(ClassPathListPanel adaptee) {
367         this.adaptee = adaptee;
368     }
369     public void actionPerformed(ActionEvent e) {
370         adaptee.editButton_actionPerformed(e);
371     }
372 }
373
Popular Tags