KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > swing > FileNodeSelectionPanel


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.swing;
24
25 // Kelp imports
26
import org.enhydra.kelp.common.event.SelectionPanelEvent;
27 import org.enhydra.kelp.common.event.SelectionPanelListener;
28 import org.enhydra.kelp.common.node.OtterFileNode;
29
30 // Standard imports
31
import java.awt.event.ActionEvent JavaDoc;
32 import java.awt.event.ActionListener JavaDoc;
33 import java.io.File JavaDoc;
34 import java.util.ResourceBundle JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Arrays JavaDoc;
37 import javax.swing.JButton JavaDoc;
38 import javax.swing.JCheckBox JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JList JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42 import javax.swing.JScrollPane JavaDoc;
43 import javax.swing.DefaultListModel JavaDoc;
44 import java.awt.*;
45 import java.beans.*;
46
47 //
48
public class FileNodeSelectionPanel extends JPanel JavaDoc {
49
50     //
51
public static ResourceBundle JavaDoc res =
52         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
53

54     // strings not to be resourced
55
private final String JavaDoc SELECT = ">";
56     private final String JavaDoc SELECT_ALL = ">>";
57     private final String JavaDoc REMOVE = "<";
58     private final String JavaDoc REMOVE_ALL = "<<";
59
60     //
61
private JLabel JavaDoc labelAvailable;
62     private JLabel JavaDoc labelSelected;
63     private OtterFileNode[] nodes = new OtterFileNode[0];
64     private LocalButtonListener buttonListener = null;
65     private JScrollPane JavaDoc scrollAvailable;
66     private JScrollPane JavaDoc scrollSelected;
67     private JList JavaDoc listAvailable;
68     private JList JavaDoc listSelected;
69     private JButton JavaDoc buttonAdd;
70     private JButton JavaDoc buttonAddAll;
71     private JButton JavaDoc buttonRemove;
72     private JButton JavaDoc buttonRemoveAll;
73     private GridBagLayout layoutSelect;
74     private JPanel JavaDoc panelSelect;
75     private JPanel JavaDoc panelOption;
76     private DefaultListModel JavaDoc modelAvailable = new DefaultListModel JavaDoc();
77     private DefaultListModel JavaDoc modelSelected = new DefaultListModel JavaDoc();
78     private GridBagLayout layoutOption;
79     private JCheckBox JavaDoc checkShowFull;
80     private SelectionPanelListener[] selectionPanelListeners =
81         new SelectionPanelListener[0];
82     private GridBagLayout layoutMain;
83
84     /**
85      * Constructor declaration
86      *
87      */

88     public FileNodeSelectionPanel() {
89         try {
90             jbInit();
91             pmInit();
92         } catch (Exception JavaDoc e) {
93             e.printStackTrace();
94         }
95     }
96
97     // override JPanel
98
public void setEnabled(boolean enable) {
99         buttonAdd.setEnabled(enable);
100         buttonAddAll.setEnabled(enable);
101         buttonRemove.setEnabled(enable);
102         buttonRemoveAll.setEnabled(enable);
103         listAvailable.setEnabled(enable);
104         listSelected.setEnabled(enable);
105         checkShowFull.setEnabled(enable);
106         super.setEnabled(enable);
107     }
108
109     public void clearAll() {
110         buttonAdd.removeActionListener(buttonListener);
111         buttonAddAll.removeActionListener(buttonListener);
112         buttonRemove.removeActionListener(buttonListener);
113         buttonRemoveAll.removeActionListener(buttonListener);
114         checkShowFull.removeActionListener(buttonListener);
115         listAvailable.setModel(new DefaultListModel JavaDoc());
116         listSelected.setModel(new DefaultListModel JavaDoc());
117         removeAll();
118         modelAvailable = null;
119         modelSelected = null;
120         nodes = null;
121         selectionPanelListeners = null;
122         buttonListener = null;
123     }
124
125     // event methods
126
public synchronized void addSelectionPanelListener(SelectionPanelListener l) {
127         ArrayList JavaDoc list = null;
128
129         list = new ArrayList JavaDoc(Arrays.asList(selectionPanelListeners));
130         if (!list.contains(l)) {
131             list.add(l);
132             list.trimToSize();
133             selectionPanelListeners = new SelectionPanelListener[list.size()];
134             selectionPanelListeners =
135                 (SelectionPanelListener[]) list.toArray(selectionPanelListeners);
136         }
137         list.clear();
138     }
139
140     public synchronized void removeSelectionPanelListener(SelectionPanelListener l) {
141         ArrayList JavaDoc list = null;
142
143         list = new ArrayList JavaDoc(Arrays.asList(selectionPanelListeners));
144         if (list.contains(l)) {
145             list.remove(l);
146             list.trimToSize();
147             selectionPanelListeners = new SelectionPanelListener[list.size()];
148             selectionPanelListeners =
149                 (SelectionPanelListener[]) list.toArray(selectionPanelListeners);
150         }
151         list.clear();
152     }
153
154     /**
155      * Method declaration
156      *
157      *
158      * @return
159      */

160     public OtterFileNode[] getNodes() {
161         return nodes;
162     }
163
164     /**
165      * Method declaration
166      *
167      *
168      * @param source
169      */

170     public void setNodes(OtterFileNode[] n) {
171         nodes = n;
172         fillLists();
173         fireSelectionEvent();
174     }
175
176     public String JavaDoc[] getSelectText() {
177         String JavaDoc[] s = new String JavaDoc[2];
178
179         s[0] = labelAvailable.getText();
180         s[1] = labelSelected.getText();
181         return s;
182     }
183
184     public void setSelectText(String JavaDoc[] s) {
185         labelAvailable.setText(s[0]);
186         labelSelected.setText(s[1]);
187     }
188
189     //
190
// PROTECTED
191
//
192
protected void fillLists() {
193         int count = getNodes().length;
194         OtterFileNode source = null;
195
196         if (getModelSelected().getSize() > 0) {
197             getModelSelected().clear();
198         }
199         if (getModelAvailable().getSize() > 0) {
200             getModelAvailable().clear();
201         }
202         for (int i = 0; i < count; i++) {
203             LocalListNode listNode = null;
204
205             source = getNodes()[i];
206             listNode = new LocalListNode(source.getFilePath());
207             if (source.isSelected()) {
208                 getModelSelected().addElement(listNode);
209             } else {
210                 getModelAvailable().addElement(listNode);
211             }
212         }
213     }
214
215     protected DefaultListModel JavaDoc getModelSelected() {
216         return modelSelected;
217     }
218
219     protected DefaultListModel JavaDoc getModelAvailable() {
220         return modelAvailable;
221     }
222
223     //
224
// PRIVATE
225
//
226
private void pmInit() {
227         buttonListener = new LocalButtonListener();
228         buttonAdd.addActionListener(buttonListener);
229         buttonAddAll.addActionListener(buttonListener);
230         buttonRemove.addActionListener(buttonListener);
231         buttonRemoveAll.addActionListener(buttonListener);
232         checkShowFull.addActionListener(buttonListener);
233         checkShowFull.setSelected(false);
234     }
235
236     private void jbInit() throws Exception JavaDoc {
237         layoutSelect =
238             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
239                                               GridBagLayout.class.getName());
240         panelSelect = (JPanel JavaDoc) Beans.instantiate(getClass().getClassLoader(),
241                                                  JPanel JavaDoc.class.getName());
242         panelOption = (JPanel JavaDoc) Beans.instantiate(getClass().getClassLoader(),
243                                                  JPanel JavaDoc.class.getName());
244         scrollAvailable =
245             (JScrollPane JavaDoc) Beans.instantiate(getClass().getClassLoader(),
246                                             JScrollPane JavaDoc.class.getName());
247         scrollSelected =
248             (JScrollPane JavaDoc) Beans.instantiate(getClass().getClassLoader(),
249                                             JScrollPane JavaDoc.class.getName());
250         labelAvailable =
251             (JLabel JavaDoc) Beans.instantiate(getClass().getClassLoader(),
252                                        JLabel JavaDoc.class.getName());
253         labelSelected =
254             (JLabel JavaDoc) Beans.instantiate(getClass().getClassLoader(),
255                                        JLabel JavaDoc.class.getName());
256         listAvailable = (JList JavaDoc) Beans.instantiate(getClass().getClassLoader(),
257                                                   JList JavaDoc.class.getName());
258         listSelected = (JList JavaDoc) Beans.instantiate(getClass().getClassLoader(),
259                                                  JList JavaDoc.class.getName());
260         buttonAdd = (JButton JavaDoc) Beans.instantiate(getClass().getClassLoader(),
261                                                 JButton JavaDoc.class.getName());
262         buttonAddAll =
263             (JButton JavaDoc) Beans.instantiate(getClass().getClassLoader(),
264                                         JButton JavaDoc.class.getName());
265         buttonRemove =
266             (JButton JavaDoc) Beans.instantiate(getClass().getClassLoader(),
267                                         JButton JavaDoc.class.getName());
268         buttonRemoveAll =
269             (JButton JavaDoc) Beans.instantiate(getClass().getClassLoader(),
270                                         JButton JavaDoc.class.getName());
271         layoutOption =
272             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
273                                               GridBagLayout.class.getName());
274         checkShowFull =
275             (JCheckBox JavaDoc) Beans.instantiate(getClass().getClassLoader(),
276                                           JCheckBox JavaDoc.class.getName());
277         layoutMain =
278             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
279                                               GridBagLayout.class.getName());
280         listAvailable.setModel(modelAvailable);
281         listSelected.setModel(modelSelected);
282         labelAvailable.setText(res.getString("Available_Pages"));
283         labelSelected.setText(res.getString("Selected_Pages"));
284         buttonAdd.setMargin(new Insets(1, 10, 1, 10));
285         buttonAdd.setText(SELECT);
286         buttonAddAll.setMargin(new Insets(1, 10, 1, 10));
287         buttonAddAll.setText(SELECT_ALL);
288         buttonRemove.setMargin(new Insets(1, 10, 1, 10));
289         buttonRemove.setText(REMOVE);
290         buttonRemoveAll.setMargin(new Insets(1, 10, 1, 10));
291         buttonRemoveAll.setText(REMOVE_ALL);
292         checkShowFull.setText(res.getString("Show_full"));
293         scrollAvailable.getViewport().add(listAvailable);
294         scrollSelected.getViewport().add(listSelected);
295         panelSelect.setLayout(layoutSelect);
296         panelSelect.add(labelAvailable,
297                         new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
298                                                GridBagConstraints.WEST,
299                                                GridBagConstraints.HORIZONTAL,
300                                                new Insets(5, 5, 2, 5), 0, 0));
301         panelSelect.add(scrollAvailable,
302                         new GridBagConstraints(0, 1, 1, 4, 0.4, 0.0,
303                                                GridBagConstraints.WEST,
304                                                GridBagConstraints.BOTH,
305                                                new Insets(3, 3, 5, 3), 0, 0));
306         panelSelect.add(buttonAdd,
307                         new GridBagConstraints(1, 1, 1, 1, 0.1, 0.0,
308                                                GridBagConstraints.CENTER,
309                                                GridBagConstraints.NONE,
310                                                new Insets(8, 2, 3, 2), 14,
311                                                0));
312         panelSelect.add(buttonAddAll,
313                         new GridBagConstraints(1, 2, 1, 1, 0.1, 0.0,
314                                                GridBagConstraints.CENTER,
315                                                GridBagConstraints.NONE,
316                                                new Insets(3, 2, 3, 2), 6, 0));
317         panelSelect.add(buttonRemove,
318                         new GridBagConstraints(1, 3, 1, 1, 0.1, 0.0,
319                                                GridBagConstraints.CENTER,
320                                                GridBagConstraints.NONE,
321                                                new Insets(3, 2, 3, 2), 14,
322                                                0));
323         panelSelect.add(buttonRemoveAll,
324                         new GridBagConstraints(1, 4, 1, 1, 0.1, 0.0,
325                                                GridBagConstraints.CENTER,
326                                                GridBagConstraints.NONE,
327                                                new Insets(3, 2, 8, 2), 6, 0));
328         panelSelect.add(labelSelected,
329                         new GridBagConstraints(2, 0, 1, 1, 0.1, 0.0,
330                                                GridBagConstraints.WEST,
331                                                GridBagConstraints.HORIZONTAL,
332                                                new Insets(5, 5, 2, 5), 0, 0));
333         panelSelect.add(scrollSelected,
334                         new GridBagConstraints(2, 1, 1, 4, 0.4, 0.0,
335                                                GridBagConstraints.EAST,
336                                                GridBagConstraints.BOTH,
337                                                new Insets(3, 3, 5, 3), 0, 0));
338         panelOption.setLayout(layoutOption);
339         panelOption.add(checkShowFull,
340                         new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
341                                                GridBagConstraints.WEST,
342                                                GridBagConstraints.HORIZONTAL,
343                                                new Insets(5, 5, 0, 5), 0, 0));
344         this.setLayout(layoutMain);
345         this.add(panelSelect,
346                  new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0,
347                                         GridBagConstraints.CENTER,
348                                         GridBagConstraints.BOTH,
349                                         new Insets(0, 0, 0, 0), 0, 0));
350         this.add(panelOption,
351                  new GridBagConstraints(0, 1, 1, 1, 0.1, 0.0,
352                                         GridBagConstraints.CENTER,
353                                         GridBagConstraints.HORIZONTAL,
354                                         new Insets(0, 0, 0, 0), 0, 0));
355     }
356
357     private void addSelected() {
358         Object JavaDoc[] elements = listAvailable.getSelectedValues();
359
360         for (int i = 0; i < elements.length; i++) {
361             LocalListNode listNode = (LocalListNode) elements[i];
362
363             modelSelected.addElement(listNode);
364             modelAvailable.removeElement(listNode);
365             for (int j = 0; j < getNodes().length; j++) {
366                 if (getNodes()[j].getFilePath().equals(listNode.getFullname())) {
367                     getNodes()[j].setSelected(true);
368                 }
369             }
370         }
371         fireSelectionEvent();
372     }
373
374     private void addAllElements() {
375         for (int i = 0; i < modelAvailable.size(); i++) {
376             modelSelected.addElement(modelAvailable.elementAt(i));
377         }
378         modelAvailable.removeAllElements();
379         for (int i = 0; i < getNodes().length; i++) {
380             getNodes()[i].setSelected(true);
381         }
382         fireSelectionEvent();
383     }
384
385     private void removeSelected() {
386         Object JavaDoc[] elements = listSelected.getSelectedValues();
387
388         for (int i = 0; i < elements.length; i++) {
389             LocalListNode listNode = (LocalListNode) elements[i];
390
391             modelAvailable.addElement(listNode);
392             modelSelected.removeElement(listNode);
393             for (int j = 0; j < getNodes().length; j++) {
394                 if (getNodes()[j].getFilePath().equals(listNode.getFullname())) {
395                     getNodes()[j].setSelected(false);
396                 }
397             }
398         }
399         fireSelectionEvent();
400     }
401
402     private void removeAllElements() {
403         for (int i = 0; i < modelSelected.size(); i++) {
404             modelAvailable.addElement(modelSelected.elementAt(i));
405         }
406         modelSelected.removeAllElements();
407         for (int i = 0; i < getNodes().length; i++) {
408             getNodes()[i].setSelected(false);
409         }
410         fireSelectionEvent();
411     }
412
413     private void fireSelectionEvent() {
414         SelectionPanelEvent event = null;
415
416         event = new SelectionPanelEvent(this, modelAvailable, modelSelected);
417         for (int i = 0; i < selectionPanelListeners.length; i++) {
418             selectionPanelListeners[i].onSelection(event);
419         }
420     }
421
422     private class LocalButtonListener implements ActionListener JavaDoc {
423         public void actionPerformed(ActionEvent JavaDoc event) {
424             Object JavaDoc source = event.getSource();
425
426             if (source == buttonAdd) {
427                 addSelected();
428             } else if (source == buttonAddAll) {
429                 addAllElements();
430             } else if (source == buttonRemove) {
431                 removeSelected();
432             } else if (source == buttonRemoveAll) {
433                 removeAllElements();
434             } else if (source == checkShowFull) {
435                 checkShowFull.isSelected();
436                 fillLists();
437             }
438         }
439
440     }
441
442     //
443
private class LocalListNode {
444         private String JavaDoc full = new String JavaDoc();
445
446         public LocalListNode(String JavaDoc fullName) {
447             full = fullName;
448         }
449
450         public String JavaDoc toString() {
451             String JavaDoc s = full;
452
453             if (!checkShowFull.isSelected()) {
454                 File JavaDoc f = new File JavaDoc(full);
455
456                 s = f.getName();
457             }
458             return s;
459         }
460
461         public String JavaDoc getFullname() {
462             return full;
463         }
464
465     }
466 }
467
Popular Tags