KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > ant > deployer > AntDeployContentTypesPanel


1 package org.enhydra.kelp.ant.deployer;
2
3 /*
4  * Enhydra Java Application Server Project
5  *
6  * The contents of this file are subject to the Enhydra Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License on
9  * the Enhydra web site ( http://www.enhydra.org/ ).
10  *
11  * Software distributed under the License is distributed on an "AS IS"
12  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
13  * the License for the specific terms governing rights and limitations
14  * under the License.
15  *
16  * The Initial Developer of the Enhydra Application Server is Lutris
17  * Technologies, Inc. The Enhydra Application Server and portions created
18  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  * Paul Mahar
23  *
24  */

25
26 // Kelp imports
27
import org.enhydra.kelp.common.event.ExtensionChangeEvent;
28 import org.enhydra.kelp.common.event.ExtensionChangeListener;
29 import org.enhydra.kelp.common.Constants;
30
31 // Standard imports
32
import java.awt.*;
33 import java.beans.*;
34 import javax.swing.*;
35 import javax.swing.event.*;
36 import java.awt.event.*;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Arrays JavaDoc;
39 import org.enhydra.kelp.ant.node.AntProject;
40 import org.enhydra.kelp.common.node.OtterProject;
41
42 //
43
public class AntDeployContentTypesPanel extends JPanel {
44     private GridBagLayout layoutRoot;
45     private JPanel panelList;
46     private JPanel panelButtons;
47     private GridBagLayout layoutList;
48     private GridBagLayout layoutButtons;
49     private JLabel labelList;
50     private JList listExt;
51     private JButton buttonAdd;
52     private JButton buttonRemove;
53     private JButton buttonReset;
54     private String JavaDoc[] defaults = null;
55     private LocalButtonListener buttonListener = null;
56     private LocalListListener listListener = null;
57     private JScrollPane scrollPane;
58     private String JavaDoc[] readOnly = new String JavaDoc[0];
59     private ExtensionChangeListener[] listeners =
60         new ExtensionChangeListener[0];
61     private AntProject project = null;
62
63     public AntDeployContentTypesPanel() {
64         try {
65             jbInit();
66             pmInit();
67         } catch (Exception JavaDoc e) {
68             e.printStackTrace();
69         }
70     }
71
72     // Just for testing
73
// public static void main(String[] args) {
74
// String[] exts = {
75
// Constants.TYPE_HTM, Constants.TYPE_HTML, Constants.TYPE_WML
76
// };
77
// String[] ro = {
78
// Constants.TYPE_HTML
79
// };
80
// JFrame frame = new JFrame();
81
// AntDeployContentTypesPanel panel = new AntDeployContentTypesPanel();
82
//
83
// panel.setExtensions(exts);
84
// panel.setReadOnly(ro);
85
// frame.getContentPane().setLayout(new BorderLayout());
86
// frame.getContentPane().add(panel, BorderLayout.CENTER);
87
// frame.pack();
88
// frame.show();
89
// }
90

91     public void clearAll() {
92         buttonRemove.removeActionListener(buttonListener);
93         buttonReset.removeActionListener(buttonListener);
94         buttonAdd.removeActionListener(buttonListener);
95         listExt.removeListSelectionListener(listListener);
96         removeAll();
97         buttonListener = null;
98         listListener = null;
99         listeners = null;
100     }
101
102     public void addListener(ExtensionChangeListener l) {
103         ArrayList JavaDoc list = null;
104
105         list = new ArrayList JavaDoc(Arrays.asList(listeners));
106         if (list.contains(l)) {}
107         else {
108             list.add(l);
109             list.trimToSize();
110             listeners = new ExtensionChangeListener[list.size()];
111             listeners = (ExtensionChangeListener[]) list.toArray(listeners);
112         }
113         list.clear();
114     }
115
116     public void removeListener(ExtensionChangeListener l) {
117         ArrayList JavaDoc list = null;
118
119         list = new ArrayList JavaDoc(Arrays.asList(listeners));
120         if (list.contains(l)) {
121             list.remove(l);
122             list.trimToSize();
123             listeners = new ExtensionChangeListener[list.size()];
124             listeners = (ExtensionChangeListener[]) list.toArray(listeners);
125         }
126         list.clear();
127     }
128
129     public ExtensionChangeListener[] getListeners() {
130         return listeners;
131     }
132
133     public String JavaDoc[] getReadOnly() {
134         return readOnly;
135     }
136
137     public void setReadOnly(String JavaDoc[] r) {
138         readOnly = r;
139     }
140
141     public String JavaDoc[] getExtensions() {
142         String JavaDoc[] exts = new String JavaDoc[0];
143         ListModel model = listExt.getModel();
144
145         exts = new String JavaDoc[model.getSize()];
146         for (int i = 0; i < model.getSize(); i++) {
147             exts[i] = model.getElementAt(i).toString();
148         }
149         return exts;
150     }
151
152     public void setExtensions(String JavaDoc[] exts) {
153         if (exts == null) {
154             exts = new String JavaDoc[0];
155         }
156         listExt.setListData(exts);
157         listExt.updateUI();
158         if (defaults == null) {
159             defaults = new String JavaDoc[exts.length];
160             for (int i = 0; i < exts.length; i++) {
161                 defaults[i] = new String JavaDoc(exts[i]);
162             }
163         }
164     }
165
166     public void setDefaults(String JavaDoc[] defs) {
167         defaults = defs;
168     }
169
170     public String JavaDoc[] getDefaults() {
171         return defaults;
172     }
173
174     //
175
// PRIVATE
176
//
177
private void notifyListeners() {
178         ExtensionChangeEvent event = null;
179
180         event = new ExtensionChangeEvent(this, getExtensions());
181         for (int i = 0; i < listeners.length; i++) {
182             listeners[i].onChange(event);
183         }
184     }
185
186     private void pmInit() {
187         buttonListener = new LocalButtonListener();
188         listListener = new LocalListListener();
189         buttonRemove.addActionListener(buttonListener);
190         buttonReset.addActionListener(buttonListener);
191         buttonAdd.addActionListener(buttonListener);
192         listExt.addListSelectionListener(listListener);
193         buttonRemove.setEnabled(false);
194     }
195
196     private void jbInit() throws Exception JavaDoc {
197         panelList = (JPanel) Beans.instantiate(getClass().getClassLoader(),
198                                                JPanel.class.getName());
199         panelButtons = (JPanel) Beans.instantiate(getClass().getClassLoader(),
200                                                   JPanel.class.getName());
201         layoutList =
202             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
203                                               GridBagLayout.class.getName());
204         layoutRoot =
205             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
206                                               GridBagLayout.class.getName());
207         layoutButtons =
208             (GridBagLayout) Beans.instantiate(getClass().getClassLoader(),
209                                               GridBagLayout.class.getName());
210         labelList = (JLabel) Beans.instantiate(getClass().getClassLoader(),
211                                                JLabel.class.getName());
212         listExt = (JList) Beans.instantiate(getClass().getClassLoader(),
213                                             JList.class.getName());
214         buttonAdd = (JButton) Beans.instantiate(getClass().getClassLoader(),
215                                                 JButton.class.getName());
216         buttonRemove =
217             (JButton) Beans.instantiate(getClass().getClassLoader(),
218                                         JButton.class.getName());
219         buttonReset = (JButton) Beans.instantiate(getClass().getClassLoader(),
220                                                   JButton.class.getName());
221         labelList.setDisplayedMnemonic('X');
222         labelList.setLabelFor(listExt);
223         labelList.setText("Associated extensions:");
224         buttonAdd.setPreferredSize(new Dimension(85, 27));
225         buttonAdd.setMnemonic('A');
226         buttonAdd.setText("Add...");
227         buttonRemove.setPreferredSize(new Dimension(85, 27));
228         buttonRemove.setMnemonic('V');
229         buttonRemove.setText("Remove");
230         buttonReset.setPreferredSize(new Dimension(85, 27));
231         buttonReset.setText("Reset");
232         scrollPane = new JScrollPane(listExt);
233         scrollPane.setMinimumSize(new Dimension(100, 100));
234         scrollPane.setPreferredSize(new Dimension(100, 100));
235         panelList.setLayout(layoutList);
236         panelList.add(labelList,
237                       new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
238                                              GridBagConstraints.CENTER,
239                                              GridBagConstraints.NONE,
240                                              new Insets(10, 10, 2, 10), 0,
241                                              0));
242         panelList.add(scrollPane,
243                       new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
244                                              GridBagConstraints.CENTER,
245                                              GridBagConstraints.BOTH,
246                                              new Insets(2, 10, 10, 10), 0,
247                                              0));
248         panelButtons.setLayout(layoutButtons);
249         panelButtons.add(buttonAdd,
250                          new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
251                                                 GridBagConstraints.CENTER,
252                                                 GridBagConstraints.NONE,
253                                                 new Insets(5, 5, 5, 5), 0,
254                                                 0));
255         panelButtons.add(buttonRemove,
256                          new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
257                                                 GridBagConstraints.CENTER,
258                                                 GridBagConstraints.NONE,
259                                                 new Insets(5, 5, 5, 5), 0,
260                                                 0));
261         panelButtons.add(buttonReset,
262                          new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
263                                                 GridBagConstraints.CENTER,
264                                                 GridBagConstraints.NONE,
265                                                 new Insets(5, 5, 5, 5), 0,
266                                                 0));
267         this.setLayout(layoutRoot);
268         this.add(panelList,
269                        new GridBagConstraints(0, 0, 1, 1, 0.2, 0.1
270             ,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 136, 0, 0), 0, 0));
271         this.add(panelButtons,
272                          new GridBagConstraints(1, 0, 1, 1, 0.1, 0.1
273             ,GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(13, 0, 0, 106), 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     public void setProject(OtterProject otterProject){
361         if(otterProject instanceof AntProject) {
362             project = (AntProject) otterProject;
363             initOptions();
364         }
365         else
366             System.err.println("DEBUG project must be AntProject");//FIXME throw Exception
367
}
368
369     private void initOptions(){
370         setExtensions(project.getExtensions(AntProject.CONTENT_TYPE_EXTENSIONS));
371     }
372
373     private class LocalButtonListener implements ActionListener {
374         public void actionPerformed(ActionEvent e) {
375             Object JavaDoc source = e.getSource();
376
377             if (source == buttonRemove) {
378                 removeSelection();
379                 notifyListeners();
380             } else if (source == buttonReset) {
381                 reset();
382                 notifyListeners();
383             } else if (source == buttonAdd) {
384                 if (promptForAdd()) {
385                     notifyListeners();
386                 }
387             }
388             project.setExtensions(AntProject.CONTENT_TYPE_EXTENSIONS, getExtensions());
389         }
390
391     }
392
393
394     //
395
private class LocalListListener implements ListSelectionListener {
396         public void valueChanged(ListSelectionEvent e) {
397             if (e.getValueIsAdjusting()) {
398
399                 // do nothing
400
} else {
401                 boolean enable = true;
402                 String JavaDoc[] selections = getSelections();
403
404                 if (selections.length >= 1) {
405                     ArrayList JavaDoc list = null;
406
407                     list = new ArrayList JavaDoc(Arrays.asList(getReadOnly()));
408                     for (int i = 0; i < selections.length; i++) {
409                         if (list.contains(selections[i])) {
410                             enable = false;
411                             break;
412                         }
413                     }
414                 }
415                 buttonRemove.setEnabled(enable);
416             }
417         }
418
419     }
420 }
421
Popular Tags