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.SelectionPanelEvent;
027: import org.enhydra.kelp.common.event.SelectionPanelListener;
028: import org.enhydra.kelp.common.node.OtterFileNode;
029:
030: // Standard imports
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.io.File;
034: import java.util.ResourceBundle;
035: import java.util.ArrayList;
036: import java.util.Arrays;
037: import javax.swing.JButton;
038: import javax.swing.JCheckBox;
039: import javax.swing.JLabel;
040: import javax.swing.JList;
041: import javax.swing.JPanel;
042: import javax.swing.JScrollPane;
043: import javax.swing.DefaultListModel;
044: import java.awt.*;
045: import java.beans.*;
046:
047: //
048: public class FileNodeSelectionPanel extends JPanel {
049:
050: //
051: public static ResourceBundle res = ResourceBundle
052: .getBundle("org.enhydra.kelp.common.Res"); // nores
053:
054: // strings not to be resourced
055: private final String SELECT = ">";
056: private final String SELECT_ALL = ">>";
057: private final String REMOVE = "<";
058: private final String REMOVE_ALL = "<<";
059:
060: //
061: private JLabel labelAvailable;
062: private JLabel labelSelected;
063: private OtterFileNode[] nodes = new OtterFileNode[0];
064: private LocalButtonListener buttonListener = null;
065: private JScrollPane scrollAvailable;
066: private JScrollPane scrollSelected;
067: private JList listAvailable;
068: private JList listSelected;
069: private JButton buttonAdd;
070: private JButton buttonAddAll;
071: private JButton buttonRemove;
072: private JButton buttonRemoveAll;
073: private GridBagLayout layoutSelect;
074: private JPanel panelSelect;
075: private JPanel panelOption;
076: private DefaultListModel modelAvailable = new DefaultListModel();
077: private DefaultListModel modelSelected = new DefaultListModel();
078: private GridBagLayout layoutOption;
079: private JCheckBox checkShowFull;
080: private SelectionPanelListener[] selectionPanelListeners = new SelectionPanelListener[0];
081: private GridBagLayout layoutMain;
082:
083: /**
084: * Constructor declaration
085: *
086: */
087: public FileNodeSelectionPanel() {
088: try {
089: jbInit();
090: pmInit();
091: } catch (Exception e) {
092: e.printStackTrace();
093: }
094: }
095:
096: // override JPanel
097: public void setEnabled(boolean enable) {
098: buttonAdd.setEnabled(enable);
099: buttonAddAll.setEnabled(enable);
100: buttonRemove.setEnabled(enable);
101: buttonRemoveAll.setEnabled(enable);
102: listAvailable.setEnabled(enable);
103: listSelected.setEnabled(enable);
104: checkShowFull.setEnabled(enable);
105: super .setEnabled(enable);
106: }
107:
108: public void clearAll() {
109: buttonAdd.removeActionListener(buttonListener);
110: buttonAddAll.removeActionListener(buttonListener);
111: buttonRemove.removeActionListener(buttonListener);
112: buttonRemoveAll.removeActionListener(buttonListener);
113: checkShowFull.removeActionListener(buttonListener);
114: listAvailable.setModel(new DefaultListModel());
115: listSelected.setModel(new DefaultListModel());
116: removeAll();
117: modelAvailable = null;
118: modelSelected = null;
119: nodes = null;
120: selectionPanelListeners = null;
121: buttonListener = null;
122: }
123:
124: // event methods
125: public synchronized void addSelectionPanelListener(
126: SelectionPanelListener l) {
127: ArrayList list = null;
128:
129: list = new ArrayList(Arrays.asList(selectionPanelListeners));
130: if (!list.contains(l)) {
131: list.add(l);
132: list.trimToSize();
133: selectionPanelListeners = new SelectionPanelListener[list
134: .size()];
135: selectionPanelListeners = (SelectionPanelListener[]) list
136: .toArray(selectionPanelListeners);
137: }
138: list.clear();
139: }
140:
141: public synchronized void removeSelectionPanelListener(
142: SelectionPanelListener l) {
143: ArrayList list = null;
144:
145: list = new ArrayList(Arrays.asList(selectionPanelListeners));
146: if (list.contains(l)) {
147: list.remove(l);
148: list.trimToSize();
149: selectionPanelListeners = new SelectionPanelListener[list
150: .size()];
151: selectionPanelListeners = (SelectionPanelListener[]) list
152: .toArray(selectionPanelListeners);
153: }
154: list.clear();
155: }
156:
157: /**
158: * Method declaration
159: *
160: *
161: * @return
162: */
163: public OtterFileNode[] getNodes() {
164: return nodes;
165: }
166:
167: /**
168: * Method declaration
169: *
170: *
171: * @param source
172: */
173: public void setNodes(OtterFileNode[] n) {
174: nodes = n;
175: fillLists();
176: fireSelectionEvent();
177: }
178:
179: public String[] getSelectText() {
180: String[] s = new String[2];
181:
182: s[0] = labelAvailable.getText();
183: s[1] = labelSelected.getText();
184: return s;
185: }
186:
187: public void setSelectText(String[] s) {
188: labelAvailable.setText(s[0]);
189: labelSelected.setText(s[1]);
190: }
191:
192: //
193: // PROTECTED
194: //
195: protected void fillLists() {
196: int count = getNodes().length;
197: OtterFileNode source = null;
198:
199: if (getModelSelected().getSize() > 0) {
200: getModelSelected().clear();
201: }
202: if (getModelAvailable().getSize() > 0) {
203: getModelAvailable().clear();
204: }
205: for (int i = 0; i < count; i++) {
206: LocalListNode listNode = null;
207:
208: source = getNodes()[i];
209: listNode = new LocalListNode(source.getFilePath());
210: if (source.isSelected()) {
211: getModelSelected().addElement(listNode);
212: } else {
213: getModelAvailable().addElement(listNode);
214: }
215: }
216: }
217:
218: protected DefaultListModel getModelSelected() {
219: return modelSelected;
220: }
221:
222: protected DefaultListModel getModelAvailable() {
223: return modelAvailable;
224: }
225:
226: //
227: // PRIVATE
228: //
229: private void pmInit() {
230: buttonListener = new LocalButtonListener();
231: buttonAdd.addActionListener(buttonListener);
232: buttonAddAll.addActionListener(buttonListener);
233: buttonRemove.addActionListener(buttonListener);
234: buttonRemoveAll.addActionListener(buttonListener);
235: checkShowFull.addActionListener(buttonListener);
236: checkShowFull.setSelected(false);
237: }
238:
239: private void jbInit() throws Exception {
240: layoutSelect = (GridBagLayout) Beans.instantiate(getClass()
241: .getClassLoader(), GridBagLayout.class.getName());
242: panelSelect = (JPanel) Beans.instantiate(getClass()
243: .getClassLoader(), JPanel.class.getName());
244: panelOption = (JPanel) Beans.instantiate(getClass()
245: .getClassLoader(), JPanel.class.getName());
246: scrollAvailable = (JScrollPane) Beans.instantiate(getClass()
247: .getClassLoader(), JScrollPane.class.getName());
248: scrollSelected = (JScrollPane) Beans.instantiate(getClass()
249: .getClassLoader(), JScrollPane.class.getName());
250: labelAvailable = (JLabel) Beans.instantiate(getClass()
251: .getClassLoader(), JLabel.class.getName());
252: labelSelected = (JLabel) Beans.instantiate(getClass()
253: .getClassLoader(), JLabel.class.getName());
254: listAvailable = (JList) Beans.instantiate(getClass()
255: .getClassLoader(), JList.class.getName());
256: listSelected = (JList) Beans.instantiate(getClass()
257: .getClassLoader(), JList.class.getName());
258: buttonAdd = (JButton) Beans.instantiate(getClass()
259: .getClassLoader(), JButton.class.getName());
260: buttonAddAll = (JButton) Beans.instantiate(getClass()
261: .getClassLoader(), JButton.class.getName());
262: buttonRemove = (JButton) Beans.instantiate(getClass()
263: .getClassLoader(), JButton.class.getName());
264: buttonRemoveAll = (JButton) Beans.instantiate(getClass()
265: .getClassLoader(), JButton.class.getName());
266: layoutOption = (GridBagLayout) Beans.instantiate(getClass()
267: .getClassLoader(), GridBagLayout.class.getName());
268: checkShowFull = (JCheckBox) Beans.instantiate(getClass()
269: .getClassLoader(), JCheckBox.class.getName());
270: layoutMain = (GridBagLayout) Beans.instantiate(getClass()
271: .getClassLoader(), GridBagLayout.class.getName());
272: listAvailable.setModel(modelAvailable);
273: listSelected.setModel(modelSelected);
274: labelAvailable.setText(res.getString("Available_Pages"));
275: labelSelected.setText(res.getString("Selected_Pages"));
276: buttonAdd.setMargin(new Insets(1, 10, 1, 10));
277: buttonAdd.setText(SELECT);
278: buttonAddAll.setMargin(new Insets(1, 10, 1, 10));
279: buttonAddAll.setText(SELECT_ALL);
280: buttonRemove.setMargin(new Insets(1, 10, 1, 10));
281: buttonRemove.setText(REMOVE);
282: buttonRemoveAll.setMargin(new Insets(1, 10, 1, 10));
283: buttonRemoveAll.setText(REMOVE_ALL);
284: checkShowFull.setText(res.getString("Show_full"));
285: scrollAvailable.getViewport().add(listAvailable);
286: scrollSelected.getViewport().add(listSelected);
287: panelSelect.setLayout(layoutSelect);
288: panelSelect.add(labelAvailable, new GridBagConstraints(0, 0, 1,
289: 1, 0.1, 0.0, GridBagConstraints.WEST,
290: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 2, 5),
291: 0, 0));
292: panelSelect.add(scrollAvailable, new GridBagConstraints(0, 1,
293: 1, 4, 0.4, 0.0, GridBagConstraints.WEST,
294: GridBagConstraints.BOTH, new Insets(3, 3, 5, 3), 0, 0));
295: panelSelect
296: .add(buttonAdd, new GridBagConstraints(1, 1, 1, 1, 0.1,
297: 0.0, GridBagConstraints.CENTER,
298: GridBagConstraints.NONE,
299: new Insets(8, 2, 3, 2), 14, 0));
300: panelSelect.add(buttonAddAll, new GridBagConstraints(1, 2, 1,
301: 1, 0.1, 0.0, GridBagConstraints.CENTER,
302: GridBagConstraints.NONE, new Insets(3, 2, 3, 2), 6, 0));
303: panelSelect
304: .add(buttonRemove, new GridBagConstraints(1, 3, 1, 1,
305: 0.1, 0.0, GridBagConstraints.CENTER,
306: GridBagConstraints.NONE,
307: new Insets(3, 2, 3, 2), 14, 0));
308: panelSelect.add(buttonRemoveAll, new GridBagConstraints(1, 4,
309: 1, 1, 0.1, 0.0, GridBagConstraints.CENTER,
310: GridBagConstraints.NONE, new Insets(3, 2, 8, 2), 6, 0));
311: panelSelect.add(labelSelected, new GridBagConstraints(2, 0, 1,
312: 1, 0.1, 0.0, GridBagConstraints.WEST,
313: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 2, 5),
314: 0, 0));
315: panelSelect.add(scrollSelected, new GridBagConstraints(2, 1, 1,
316: 4, 0.4, 0.0, GridBagConstraints.EAST,
317: GridBagConstraints.BOTH, new Insets(3, 3, 5, 3), 0, 0));
318: panelOption.setLayout(layoutOption);
319: panelOption.add(checkShowFull, new GridBagConstraints(0, 0, 1,
320: 1, 0.1, 0.0, GridBagConstraints.WEST,
321: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5),
322: 0, 0));
323: this .setLayout(layoutMain);
324: this .add(panelSelect, new GridBagConstraints(0, 0, 1, 1, 0.1,
325: 0.0, GridBagConstraints.CENTER,
326: GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
327: this .add(panelOption, new GridBagConstraints(0, 1, 1, 1, 0.1,
328: 0.0, GridBagConstraints.CENTER,
329: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),
330: 0, 0));
331: }
332:
333: private void addSelected() {
334: Object[] elements = listAvailable.getSelectedValues();
335:
336: for (int i = 0; i < elements.length; i++) {
337: LocalListNode listNode = (LocalListNode) elements[i];
338:
339: modelSelected.addElement(listNode);
340: modelAvailable.removeElement(listNode);
341: for (int j = 0; j < getNodes().length; j++) {
342: if (getNodes()[j].getFilePath().equals(
343: listNode.getFullname())) {
344: getNodes()[j].setSelected(true);
345: }
346: }
347: }
348: fireSelectionEvent();
349: }
350:
351: private void addAllElements() {
352: for (int i = 0; i < modelAvailable.size(); i++) {
353: modelSelected.addElement(modelAvailable.elementAt(i));
354: }
355: modelAvailable.removeAllElements();
356: for (int i = 0; i < getNodes().length; i++) {
357: getNodes()[i].setSelected(true);
358: }
359: fireSelectionEvent();
360: }
361:
362: private void removeSelected() {
363: Object[] elements = listSelected.getSelectedValues();
364:
365: for (int i = 0; i < elements.length; i++) {
366: LocalListNode listNode = (LocalListNode) elements[i];
367:
368: modelAvailable.addElement(listNode);
369: modelSelected.removeElement(listNode);
370: for (int j = 0; j < getNodes().length; j++) {
371: if (getNodes()[j].getFilePath().equals(
372: listNode.getFullname())) {
373: getNodes()[j].setSelected(false);
374: }
375: }
376: }
377: fireSelectionEvent();
378: }
379:
380: private void removeAllElements() {
381: for (int i = 0; i < modelSelected.size(); i++) {
382: modelAvailable.addElement(modelSelected.elementAt(i));
383: }
384: modelSelected.removeAllElements();
385: for (int i = 0; i < getNodes().length; i++) {
386: getNodes()[i].setSelected(false);
387: }
388: fireSelectionEvent();
389: }
390:
391: private void fireSelectionEvent() {
392: SelectionPanelEvent event = null;
393:
394: event = new SelectionPanelEvent(this , modelAvailable,
395: modelSelected);
396: for (int i = 0; i < selectionPanelListeners.length; i++) {
397: selectionPanelListeners[i].onSelection(event);
398: }
399: }
400:
401: private class LocalButtonListener implements ActionListener {
402: public void actionPerformed(ActionEvent event) {
403: Object source = event.getSource();
404:
405: if (source == buttonAdd) {
406: addSelected();
407: } else if (source == buttonAddAll) {
408: addAllElements();
409: } else if (source == buttonRemove) {
410: removeSelected();
411: } else if (source == buttonRemoveAll) {
412: removeAllElements();
413: } else if (source == checkShowFull) {
414: checkShowFull.isSelected();
415: fillLists();
416: }
417: }
418:
419: }
420:
421: //
422: private class LocalListNode {
423: private String full = new String();
424:
425: public LocalListNode(String fullName) {
426: full = fullName;
427: }
428:
429: public String toString() {
430: String s = full;
431:
432: if (!checkShowFull.isSelected()) {
433: File f = new File(full);
434:
435: s = f.getName();
436: }
437: return s;
438: }
439:
440: public String getFullname() {
441: return full;
442: }
443:
444: }
445: }
|