001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp.common.swing;
024:
025: // Kelp imports
026: import org.enhydra.kelp.common.event.ExtensionChangeEvent;
027: import org.enhydra.kelp.common.event.ExtensionChangeListener;
028: import org.enhydra.kelp.common.Constants;
029:
030: // Standard imports
031: import java.awt.*;
032: import java.beans.*;
033: import javax.swing.*;
034: import javax.swing.event.*;
035: import java.awt.event.*;
036: import java.util.ArrayList;
037: import java.util.Arrays;
038:
039: //
040: public class ExtensionPanel extends JPanel {
041: private GridBagLayout layoutRoot;
042: private JPanel panelList;
043: private JPanel panelButtons;
044: private GridBagLayout layoutList;
045: private GridBagLayout layoutButtons;
046: private JLabel labelList;
047: private JList listExt;
048: private JButton buttonAdd;
049: private JButton buttonRemove;
050: private JButton buttonReset;
051: private String[] defaults = null;
052: private LocalButtonListener buttonListener = null;
053: private LocalListListener listListener = null;
054: private JScrollPane scrollPane;
055: private String[] readOnly = new String[0];
056: private ExtensionChangeListener[] listeners = new ExtensionChangeListener[0];
057:
058: public ExtensionPanel() {
059: try {
060: jbInit();
061: pmInit();
062: } catch (Exception e) {
063: e.printStackTrace();
064: }
065: }
066:
067: // Just for testing
068: public static void main(String[] args) {
069: String[] exts = { Constants.TYPE_HTM, Constants.TYPE_HTML,
070: Constants.TYPE_WML };
071: String[] ro = { Constants.TYPE_HTML };
072: JFrame frame = new JFrame();
073: ExtensionPanel panel = new ExtensionPanel();
074:
075: panel.setExtensions(exts);
076: panel.setReadOnly(ro);
077: frame.getContentPane().setLayout(new BorderLayout());
078: frame.getContentPane().add(panel, BorderLayout.CENTER);
079: frame.pack();
080: frame.show();
081: }
082:
083: public void clearAll() {
084: buttonRemove.removeActionListener(buttonListener);
085: buttonReset.removeActionListener(buttonListener);
086: buttonAdd.removeActionListener(buttonListener);
087: listExt.removeListSelectionListener(listListener);
088: removeAll();
089: buttonListener = null;
090: listListener = null;
091: listeners = null;
092: }
093:
094: public void addListener(ExtensionChangeListener l) {
095: ArrayList list = null;
096:
097: list = new ArrayList(Arrays.asList(listeners));
098: if (list.contains(l)) {
099: } else {
100: list.add(l);
101: list.trimToSize();
102: listeners = new ExtensionChangeListener[list.size()];
103: listeners = (ExtensionChangeListener[]) list
104: .toArray(listeners);
105: }
106: list.clear();
107: }
108:
109: public void removeListener(ExtensionChangeListener l) {
110: ArrayList list = null;
111:
112: list = new ArrayList(Arrays.asList(listeners));
113: if (list.contains(l)) {
114: list.remove(l);
115: list.trimToSize();
116: listeners = new ExtensionChangeListener[list.size()];
117: listeners = (ExtensionChangeListener[]) list
118: .toArray(listeners);
119: }
120: list.clear();
121: }
122:
123: public ExtensionChangeListener[] getListeners() {
124: return listeners;
125: }
126:
127: public String[] getReadOnly() {
128: return readOnly;
129: }
130:
131: public void setReadOnly(String[] r) {
132: readOnly = r;
133: }
134:
135: public String[] getExtensions() {
136: String[] exts = new String[0];
137: ListModel model = listExt.getModel();
138:
139: exts = new String[model.getSize()];
140: for (int i = 0; i < model.getSize(); i++) {
141: exts[i] = model.getElementAt(i).toString();
142: }
143: return exts;
144: }
145:
146: public void setExtensions(String[] exts) {
147: if (exts == null) {
148: exts = new String[0];
149: }
150: listExt.setListData(exts);
151: listExt.updateUI();
152: if (defaults == null) {
153: defaults = new String[exts.length];
154: for (int i = 0; i < exts.length; i++) {
155: defaults[i] = new String(exts[i]);
156: }
157: }
158: }
159:
160: public void setDefaults(String[] defs) {
161: defaults = defs;
162: }
163:
164: public String[] getDefaults() {
165: return defaults;
166: }
167:
168: //
169: // PRIVATE
170: //
171: private void notifyListeners() {
172: ExtensionChangeEvent event = null;
173:
174: event = new ExtensionChangeEvent(this , getExtensions());
175: for (int i = 0; i < listeners.length; i++) {
176: listeners[i].onChange(event);
177: }
178: }
179:
180: private void pmInit() {
181: buttonListener = new LocalButtonListener();
182: listListener = new LocalListListener();
183: buttonRemove.addActionListener(buttonListener);
184: buttonReset.addActionListener(buttonListener);
185: buttonAdd.addActionListener(buttonListener);
186: listExt.addListSelectionListener(listListener);
187: buttonRemove.setEnabled(false);
188: }
189:
190: private void jbInit() throws Exception {
191: panelList = (JPanel) Beans.instantiate(getClass()
192: .getClassLoader(), JPanel.class.getName());
193: panelButtons = (JPanel) Beans.instantiate(getClass()
194: .getClassLoader(), JPanel.class.getName());
195: layoutList = (GridBagLayout) Beans.instantiate(getClass()
196: .getClassLoader(), GridBagLayout.class.getName());
197: layoutRoot = (GridBagLayout) Beans.instantiate(getClass()
198: .getClassLoader(), GridBagLayout.class.getName());
199: layoutButtons = (GridBagLayout) Beans.instantiate(getClass()
200: .getClassLoader(), GridBagLayout.class.getName());
201: labelList = (JLabel) Beans.instantiate(getClass()
202: .getClassLoader(), JLabel.class.getName());
203: listExt = (JList) Beans.instantiate(
204: getClass().getClassLoader(), JList.class.getName());
205: buttonAdd = (JButton) Beans.instantiate(getClass()
206: .getClassLoader(), JButton.class.getName());
207: buttonRemove = (JButton) Beans.instantiate(getClass()
208: .getClassLoader(), JButton.class.getName());
209: buttonReset = (JButton) Beans.instantiate(getClass()
210: .getClassLoader(), JButton.class.getName());
211: labelList.setDisplayedMnemonic('X');
212: labelList.setLabelFor(listExt);
213: labelList.setText("Associated extensions:");
214: buttonAdd.setPreferredSize(new Dimension(85, 27));
215: buttonAdd.setMnemonic('A');
216: buttonAdd.setText("Add...");
217: buttonRemove.setPreferredSize(new Dimension(85, 27));
218: buttonRemove.setMnemonic('V');
219: buttonRemove.setText("Remove");
220: buttonReset.setPreferredSize(new Dimension(85, 27));
221: buttonReset.setText("Reset");
222: scrollPane = new JScrollPane(listExt);
223: scrollPane.setMinimumSize(new Dimension(100, 100));
224: scrollPane.setPreferredSize(new Dimension(100, 100));
225: panelList.setLayout(layoutList);
226: panelList.add(labelList, new GridBagConstraints(0, 0, 1, 1,
227: 0.0, 0.0, GridBagConstraints.CENTER,
228: GridBagConstraints.NONE, new Insets(10, 10, 2, 10), 0,
229: 0));
230: panelList.add(scrollPane, new GridBagConstraints(0, 1, 1, 1,
231: 0.0, 0.0, GridBagConstraints.CENTER,
232: GridBagConstraints.BOTH, new Insets(2, 10, 10, 10), 0,
233: 0));
234: panelButtons.setLayout(layoutButtons);
235: panelButtons.add(buttonAdd, new GridBagConstraints(0, 0, 1, 1,
236: 0.0, 0.0, GridBagConstraints.CENTER,
237: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
238: panelButtons.add(buttonRemove, new GridBagConstraints(0, 1, 1,
239: 1, 0.0, 0.0, GridBagConstraints.CENTER,
240: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
241: panelButtons.add(buttonReset, new GridBagConstraints(0, 2, 1,
242: 1, 0.0, 0.0, GridBagConstraints.CENTER,
243: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
244: this .setLayout(layoutRoot);
245: this .add(panelList, new GridBagConstraints(0, 0, 1, 1, 0.2,
246: 0.1, GridBagConstraints.WEST, GridBagConstraints.BOTH,
247: new Insets(0, 0, 0, 0), 0, 0));
248: this .add(panelButtons, new GridBagConstraints(1, 0, 1, 1, 0.1,
249: 0.1, GridBagConstraints.EAST, GridBagConstraints.BOTH,
250: new Insets(0, 0, 0, 0), 0, 0));
251: }
252:
253: private void reset() {
254: setExtensions(defaults);
255: }
256:
257: private String[] getSelections() {
258: Object[] values = new Object[0];
259: String[] selections = new String[0];
260:
261: values = listExt.getSelectedValues();
262: selections = new String[values.length];
263: for (int i = 0; i < selections.length; i++) {
264: selections[i] = values[i].toString();
265: }
266: values = new Object[0];
267: return selections;
268: }
269:
270: private void removeSelection() {
271: ArrayList list = null;
272: String[] exts = getSelections();
273:
274: if (exts.length >= 1) {
275: list = new ArrayList(Arrays.asList(getExtensions()));
276: for (int i = 0; i < exts.length; i++) {
277: list.remove(exts[i]);
278: }
279: list.trimToSize();
280: exts = new String[list.size()];
281: exts = (String[]) list.toArray(exts);
282: setExtensions(exts);
283: list.clear();
284: }
285: }
286:
287: private boolean promptForAdd() {
288: boolean change = false;
289: Object input = null;
290:
291: input = JOptionPane.showInputDialog(this ,
292: "Enter new extension", "Add Custom Extension",
293: JOptionPane.QUESTION_MESSAGE);
294: if (input == null) {
295: } else {
296: String[] exts = new String[0];
297: String newExt = new String();
298: ArrayList list = null;
299:
300: newExt = input.toString();
301: list = new ArrayList(Arrays.asList(getExtensions()));
302: if (list.contains(newExt)) {
303: } else if (extensionValid(newExt)) {
304: list.add(newExt);
305: }
306: list.trimToSize();
307: exts = new String[list.size()];
308: exts = (String[]) list.toArray(exts);
309: setExtensions(exts);
310: list.clear();
311: change = true;
312: }
313: return change;
314: }
315:
316: private boolean extensionValid(String testExt) {
317: boolean valid = true;
318:
319: testExt = testExt.trim();
320: if (testExt.length() == 0) {
321: valid = false;
322: } else if (testExt.indexOf(' ') > -1) {
323: valid = false;
324: } else {
325: for (int i = 0; i < testExt.length(); i++) {
326: char c = testExt.charAt(i);
327:
328: if (!Character.isLetterOrDigit(c)) {
329: valid = false;
330: break;
331: }
332: }
333: }
334: return valid;
335: }
336:
337: private class LocalButtonListener implements ActionListener {
338: public void actionPerformed(ActionEvent e) {
339: Object source = e.getSource();
340:
341: if (source == buttonRemove) {
342: removeSelection();
343: notifyListeners();
344: } else if (source == buttonReset) {
345: reset();
346: notifyListeners();
347: } else if (source == buttonAdd) {
348: if (promptForAdd()) {
349: notifyListeners();
350: }
351: }
352: }
353:
354: }
355:
356: //
357: private class LocalListListener implements ListSelectionListener {
358: public void valueChanged(ListSelectionEvent e) {
359: if (e.getValueIsAdjusting()) {
360:
361: // do nothing
362: } else {
363: boolean enable = true;
364: String[] selections = getSelections();
365:
366: if (selections.length >= 1) {
367: ArrayList list = null;
368:
369: list = new ArrayList(Arrays.asList(getReadOnly()));
370: for (int i = 0; i < selections.length; i++) {
371: if (list.contains(selections[i])) {
372: enable = false;
373: break;
374: }
375: }
376: }
377: buttonRemove.setEnabled(enable);
378: }
379: }
380:
381: }
382: }
|