KickJava   Java API By Example, From Geeks To Geeks.

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


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.ExtensionChangeEvent;
27 import org.enhydra.kelp.common.event.ExtensionChangeListener;
28 import org.enhydra.kelp.common.Constants;
29
30 // Standard imports
31
import java.awt.*;
32 import java.beans.*;
33 import javax.swing.*;
34 import javax.swing.event.*;
35 import java.awt.event.*;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Arrays JavaDoc;
38
39 //
40
public class ExtensionPanel extends JPanel {
41     private GridBagLayout layoutRoot;
42     private JPanel panelList;
43     private JPanel panelButtons;
44     private GridBagLayout layoutList;
45     private GridBagLayout layoutButtons;
46     private JLabel labelList;
47     private JList listExt;
48     private JButton buttonAdd;
49     private JButton buttonRemove;
50     private JButton buttonReset;
51     private String JavaDoc[] defaults = null;
52     private LocalButtonListener buttonListener = null;
53     private LocalListListener listListener = null;
54     private JScrollPane scrollPane;
55     private String JavaDoc[] readOnly = new String JavaDoc[0];
56     private ExtensionChangeListener[] listeners =
57         new ExtensionChangeListener[0];
58
59     public ExtensionPanel() {
60         try {
61             jbInit();
62             pmInit();
63         } catch (Exception JavaDoc e) {
64             e.printStackTrace();
65         }
66     }
67
68     // Just for testing
69
public static void main(String JavaDoc[] args) {
70         String JavaDoc[] exts = {
71             Constants.TYPE_HTM, Constants.TYPE_HTML, Constants.TYPE_WML
72         };
73         String JavaDoc[] ro = {
74             Constants.TYPE_HTML
75         };
76         JFrame frame = new JFrame();
77         ExtensionPanel panel = new ExtensionPanel();
78
79         panel.setExtensions(exts);
80         panel.setReadOnly(ro);
81         frame.getContentPane().setLayout(new BorderLayout());
82         frame.getContentPane().add(panel, BorderLayout.CENTER);
83         frame.pack();
84         frame.show();
85     }
86
87     public void clearAll() {
88         buttonRemove.removeActionListener(buttonListener);
89         buttonReset.removeActionListener(buttonListener);
90         buttonAdd.removeActionListener(buttonListener);
91         listExt.removeListSelectionListener(listListener);
92         removeAll();
93         buttonListener = null;
94         listListener = null;
95         listeners = null;
96     }
97
98     public void addListener(ExtensionChangeListener l) {
99         ArrayList JavaDoc list = null;
100
101         list = new ArrayList JavaDoc(Arrays.asList(listeners));
102         if (list.contains(l)) {}
103         else {
104             list.add(l);
105             list.trimToSize();
106             listeners = new ExtensionChangeListener[list.size()];
107             listeners = (ExtensionChangeListener[]) list.toArray(listeners);
108         }
109         list.clear();
110     }
111
112     public void removeListener(ExtensionChangeListener l) {
113         ArrayList JavaDoc list = null;
114
115         list = new ArrayList JavaDoc(Arrays.asList(listeners));
116         if (list.contains(l)) {
117             list.remove(l);
118             list.trimToSize();
119             listeners = new ExtensionChangeListener[list.size()];
120             listeners = (ExtensionChangeListener[]) list.toArray(listeners);
121         }
122         list.clear();
123     }
124
125     public ExtensionChangeListener[] getListeners() {
126         return listeners;
127     }
128
129     public String JavaDoc[] getReadOnly() {
130         return readOnly;
131     }
132
133     public void setReadOnly(String JavaDoc[] r) {
134         readOnly = r;
135     }
136
137     public String JavaDoc[] getExtensions() {
138         String JavaDoc[] exts = new String JavaDoc[0];
139         ListModel model = listExt.getModel();
140
141         exts = new String JavaDoc[model.getSize()];
142         for (int i = 0; i < model.getSize(); i++) {
143             exts[i] = model.getElementAt(i).toString();
144         }
145         return exts;
146     }
147
148     public void setExtensions(String JavaDoc[] exts) {
149         if (exts == null) {
150             exts = new String JavaDoc[0];
151         }
152         listExt.setListData(exts);
153         listExt.updateUI();
154         if (defaults == null) {
155             defaults = new String JavaDoc[exts.length];
156             for (int i = 0; i < exts.length; i++) {
157                 defaults[i] = new String JavaDoc(exts[i]);
158             }
159         }
160     }
161
162     public void setDefaults(String JavaDoc[] defs) {
163         defaults = defs;
164     }
165
166     public String JavaDoc[] getDefaults() {
167         return defaults;
168     }
169
170     //
171
// PRIVATE
172
//
173
private void notifyListeners() {
174         ExtensionChangeEvent event = null;
175
176         event = new ExtensionChangeEvent(this, getExtensions());
177         for (int i = 0; i < listeners.length; i++) {
178             listeners[i].onChange(event);
179         }
180     }
181
182     private void pmInit() {
183         buttonListener = new LocalButtonListener();
184         listListener = new LocalListListener();
185         buttonRemove.addActionListener(buttonListener);
186         buttonReset.addActionListener(buttonListener);
187         buttonAdd.addActionListener(buttonListener);
188         listExt.addListSelectionListener(listListener);
189         buttonRemove.setEnabled(false);
190     }
191
192     private void jbInit() throws Exception JavaDoc {
193         panelList = (JPanel) Beans.instantiate(getClass().getClassLoader(),
194                                                JPanel.class.getName());
195         panelButtons = (JPanel) Beans.instantiate(getClass().getClassLoader(),
196                                                   JPanel.class.getName());
197         layoutList =
198             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
199                                               GridBagLayout.class.getName());
200         layoutRoot =
201             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
202                                               GridBagLayout.class.getName());
203         layoutButtons =
204             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
205                                               GridBagLayout.class.getName());
206         labelList = (JLabel) Beans.instantiate(getClass().getClassLoader(),
207                                                JLabel.class.getName());
208         listExt = (JList) Beans.instantiate(getClass().getClassLoader(),
209                                             JList.class.getName());
210         buttonAdd = (JButton) Beans.instantiate(getClass().getClassLoader(),
211                                                 JButton.class.getName());
212         buttonRemove =
213             (JButton) Beans.instantiate(getClass().getClassLoader(),
214                                         JButton.class.getName());
215         buttonReset = (JButton) Beans.instantiate(getClass().getClassLoader(),
216                                                   JButton.class.getName());
217         labelList.setDisplayedMnemonic('X');
218         labelList.setLabelFor(listExt);
219         labelList.setText("Associated extensions:");
220         buttonAdd.setPreferredSize(new Dimension(85, 27));
221         buttonAdd.setMnemonic('A');
222         buttonAdd.setText("Add...");
223         buttonRemove.setPreferredSize(new Dimension(85, 27));
224         buttonRemove.setMnemonic('V');
225         buttonRemove.setText("Remove");
226         buttonReset.setPreferredSize(new Dimension(85, 27));
227         buttonReset.setText("Reset");
228         scrollPane = new JScrollPane(listExt);
229         scrollPane.setMinimumSize(new Dimension(100, 100));
230         scrollPane.setPreferredSize(new Dimension(100, 100));
231         panelList.setLayout(layoutList);
232         panelList.add(labelList,
233                       new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
234                                              GridBagConstraints.CENTER,
235                                              GridBagConstraints.NONE,
236                                              new Insets(10, 10, 2, 10), 0,
237                                              0));
238         panelList.add(scrollPane,
239                       new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
240                                              GridBagConstraints.CENTER,
241                                              GridBagConstraints.BOTH,
242                                              new Insets(2, 10, 10, 10), 0,
243                                              0));
244         panelButtons.setLayout(layoutButtons);
245         panelButtons.add(buttonAdd,
246                          new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
247                                                 GridBagConstraints.CENTER,
248                                                 GridBagConstraints.NONE,
249                                                 new Insets(5, 5, 5, 5), 0,
250                                                 0));
251         panelButtons.add(buttonRemove,
252                          new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
253                                                 GridBagConstraints.CENTER,
254                                                 GridBagConstraints.NONE,
255                                                 new Insets(5, 5, 5, 5), 0,
256                                                 0));
257         panelButtons.add(buttonReset,
258                          new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
259                                                 GridBagConstraints.CENTER,
260                                                 GridBagConstraints.NONE,
261                                                 new Insets(5, 5, 5, 5), 0,
262                                                 0));
263         this.setLayout(layoutRoot);
264         this.add(panelList,
265                  new GridBagConstraints(0, 0, 1, 1, 0.2, 0.1,
266                                         GridBagConstraints.WEST,
267                                         GridBagConstraints.BOTH,
268                                         new Insets(0, 0, 0, 0), 0, 0));
269         this.add(panelButtons,
270                  new GridBagConstraints(1, 0, 1, 1, 0.1, 0.1,
271                                         GridBagConstraints.EAST,
272                                         GridBagConstraints.BOTH,
273                                         new Insets(0, 0, 0, 0), 0, 0));
274     }
275
276     private void reset() {
277         setExtensions(defaults);
278     }
279
280     private String JavaDoc[] getSelections() {
281         Object JavaDoc[] values = new Object JavaDoc[0];
282         String JavaDoc[] selections = new String JavaDoc[0];
283
284         values = listExt.getSelectedValues();
285         selections = new String JavaDoc[values.length];
286         for (int i = 0; i < selections.length; i++) {
287             selections[i] = values[i].toString();
288         }
289         values = new Object JavaDoc[0];
290         return selections;
291     }
292
293     private void removeSelection() {
294         ArrayList JavaDoc list = null;
295         String JavaDoc[] exts = getSelections();
296
297         if (exts.length >= 1) {
298             list = new ArrayList JavaDoc(Arrays.asList(getExtensions()));
299             for (int i = 0; i < exts.length; i++) {
300                 list.remove(exts[i]);
301             }
302             list.trimToSize();
303             exts = new String JavaDoc[list.size()];
304             exts = (String JavaDoc[]) list.toArray(exts);
305             setExtensions(exts);
306             list.clear();
307         }
308     }
309
310     private boolean promptForAdd() {
311         boolean change = false;
312         Object JavaDoc input = null;
313
314         input = JOptionPane.showInputDialog(this, "Enter new extension",
315                                             "Add Custom Extension",
316                                             JOptionPane.QUESTION_MESSAGE);
317         if (input == null) {}
318         else {
319             String JavaDoc[] exts = new String JavaDoc[0];
320             String JavaDoc newExt = new String JavaDoc();
321             ArrayList JavaDoc list = null;
322
323             newExt = input.toString();
324             list = new ArrayList JavaDoc(Arrays.asList(getExtensions()));
325             if (list.contains(newExt)) {}
326             else if (extensionValid(newExt)) {
327                 list.add(newExt);
328             }
329             list.trimToSize();
330             exts = new String JavaDoc[list.size()];
331             exts = (String JavaDoc[]) list.toArray(exts);
332             setExtensions(exts);
333             list.clear();
334             change = true;
335         }
336         return change;
337     }
338
339     private boolean extensionValid(String JavaDoc testExt) {
340         boolean valid = true;
341
342         testExt = testExt.trim();
343         if (testExt.length() == 0) {
344             valid = false;
345         } else if (testExt.indexOf(' ') > -1) {
346             valid = false;
347         } else {
348             for (int i = 0; i < testExt.length(); i++) {
349                 char c = testExt.charAt(i);
350
351                 if (!Character.isLetterOrDigit(c)) {
352                     valid = false;
353                     break;
354                 }
355             }
356         }
357         return valid;
358     }
359
360     private class LocalButtonListener implements ActionListener {
361         public void actionPerformed(ActionEvent e) {
362             Object JavaDoc source = e.getSource();
363
364             if (source == buttonRemove) {
365                 removeSelection();
366                 notifyListeners();
367             } else if (source == buttonReset) {
368                 reset();
369                 notifyListeners();
370             } else if (source == buttonAdd) {
371                 if (promptForAdd()) {
372                     notifyListeners();
373                 }
374             }
375         }
376
377     }
378
379     //
380
private class LocalListListener implements ListSelectionListener {
381         public void valueChanged(ListSelectionEvent e) {
382             if (e.getValueIsAdjusting()) {
383
384                 // do nothing
385
} else {
386                 boolean enable = true;
387                 String JavaDoc[] selections = getSelections();
388
389                 if (selections.length >= 1) {
390                     ArrayList JavaDoc list = null;
391
392                     list = new ArrayList JavaDoc(Arrays.asList(getReadOnly()));
393                     for (int i = 0; i < selections.length; i++) {
394                         if (list.contains(selections[i])) {
395                             enable = false;
396                             break;
397                         }
398                     }
399                 }
400                 buttonRemove.setEnabled(enable);
401             }
402         }
403
404     }
405 }
406
Popular Tags