001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package com.artenum.so6.dataflow.util;
034:
035: import java.awt.BorderLayout;
036: import java.awt.Color;
037: import java.awt.Container;
038: import java.awt.Dimension;
039: import java.awt.Toolkit;
040: import java.awt.event.ActionEvent;
041: import java.awt.event.ActionListener;
042:
043: import java.io.File;
044:
045: import java.util.Enumeration;
046: import java.util.Hashtable;
047: import java.util.Iterator;
048: import java.util.Map;
049: import java.util.Set;
050: import java.util.Vector;
051:
052: import javax.swing.BorderFactory;
053: import javax.swing.Box;
054: import javax.swing.BoxLayout;
055: import javax.swing.JButton;
056: import javax.swing.JDialog;
057: import javax.swing.JFileChooser;
058: import javax.swing.JLabel;
059: import javax.swing.JList;
060: import javax.swing.JOptionPane;
061: import javax.swing.JPanel;
062: import javax.swing.JPasswordField;
063: import javax.swing.JScrollPane;
064: import javax.swing.JTextArea;
065: import javax.swing.JTextField;
066: import javax.swing.ListSelectionModel;
067: import javax.swing.text.JTextComponent;
068:
069: public abstract class LsClassicPropertiesEditor extends JDialog
070: implements ActionListener {
071: private JPanel propertiesPanel;
072: protected Hashtable propertiesEls = new Hashtable();
073: protected Hashtable properties = new Hashtable();
074:
075: public LsClassicPropertiesEditor(String name) {
076: setTitle(name);
077: setModal(true);
078:
079: // properties
080: propertiesPanel = new JPanel();
081: propertiesPanel.setLayout(new BoxLayout(propertiesPanel,
082: BoxLayout.Y_AXIS));
083:
084: //Lay out the label and scroll pane from top to bottom.
085: JPanel coolPane = new JPanel();
086: coolPane.setLayout(new BoxLayout(coolPane, BoxLayout.Y_AXIS));
087: coolPane.add(Box.createRigidArea(new Dimension(0, 2)));
088:
089: JPanel line = new JPanel();
090: line.setBackground(new Color(47, 80, 119));
091: line.setMaximumSize(new Dimension(1000, 1));
092: line.setMinimumSize(new Dimension(1, 1));
093: line.setPreferredSize(new Dimension(100, 1));
094: coolPane.add(line);
095: coolPane.add(Box.createRigidArea(new Dimension(0, 5)));
096: coolPane.add(propertiesPanel);
097: coolPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
098:
099: // buttons
100: JButton cancelButton = new JButton("Cancel");
101: JButton setButton = new JButton("Set");
102: cancelButton.setForeground(Color.DARK_GRAY);
103: cancelButton.setActionCommand("CANCEL");
104: cancelButton.addActionListener(this );
105: setButton.setActionCommand("SET");
106: setButton.addActionListener(this );
107:
108: //Lay out the buttons from left to right.
109: JPanel buttonPane = new JPanel();
110: buttonPane
111: .setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
112: buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 5,
113: 5));
114: buttonPane.add(Box.createHorizontalGlue());
115: buttonPane.add(setButton);
116: buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
117: buttonPane.add(cancelButton);
118:
119: //Put everything together, using the content pane's BorderLayout.
120: Container contentPane = getContentPane();
121: contentPane.add(coolPane, BorderLayout.CENTER);
122: contentPane.add(buttonPane, BorderLayout.SOUTH);
123:
124: setResizable(false);
125: }
126:
127: public Hashtable editProperties() {
128: properties = new Hashtable();
129: propertiesPanel.add(Box.createVerticalGlue());
130: pack();
131: setLocation(((int) Toolkit.getDefaultToolkit().getScreenSize()
132: .getWidth() - getWidth()) / 2,
133: ((int) Toolkit.getDefaultToolkit().getScreenSize()
134: .getHeight() - getHeight()) / 2);
135: setVisible(true);
136:
137: if (!properties.keys().hasMoreElements()) {
138: return null;
139: }
140:
141: return properties;
142: }
143:
144: public void addTextPropertie(String propertyKey,
145: String propertyName, String value) {
146: JPanel myPanel = new JPanel();
147: myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.X_AXIS));
148:
149: JLabel label = new JLabel(propertyName, JLabel.RIGHT);
150: label.setMaximumSize(new Dimension(160, 20));
151: label.setMinimumSize(new Dimension(160, 20));
152: label.setPreferredSize(new Dimension(160, 20));
153:
154: JTextField field = new JTextField(value);
155: field.setMaximumSize(new Dimension(800, 20));
156: field.setMinimumSize(new Dimension(10, 20));
157: field.setPreferredSize(new Dimension(250, 20));
158: myPanel.add(label);
159: myPanel.add(Box.createRigidArea(new Dimension(5, 0)));
160: myPanel.add(field);
161: propertiesPanel.add(myPanel);
162: propertiesPanel.add(Box.createRigidArea(new Dimension(0, 3)));
163:
164: //
165: propertiesEls.put(propertyKey, field);
166: }
167:
168: public void addPasswordPropertie(String propertyKey,
169: String propertyName, String value) {
170: JPanel myPanel = new JPanel();
171: myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.X_AXIS));
172:
173: JLabel label = new JLabel(propertyName, JLabel.RIGHT);
174: label.setMaximumSize(new Dimension(160, 20));
175: label.setMinimumSize(new Dimension(160, 20));
176: label.setPreferredSize(new Dimension(160, 20));
177:
178: JPasswordField field = new JPasswordField(value);
179: field.setMaximumSize(new Dimension(800, 20));
180: field.setMinimumSize(new Dimension(10, 20));
181: field.setPreferredSize(new Dimension(250, 20));
182: myPanel.add(label);
183: myPanel.add(Box.createRigidArea(new Dimension(5, 0)));
184: myPanel.add(field);
185: propertiesPanel.add(myPanel);
186: propertiesPanel.add(Box.createRigidArea(new Dimension(0, 3)));
187:
188: //
189: propertiesEls.put(propertyKey, field);
190: }
191:
192: public void addPathPropertie(String propertyKey,
193: String propertyName, String value) {
194: JPanel myPanel = new JPanel();
195: myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.X_AXIS));
196:
197: JLabel label = new JLabel(propertyName, JLabel.RIGHT);
198: label.setMaximumSize(new Dimension(160, 20));
199: label.setMinimumSize(new Dimension(160, 20));
200: label.setPreferredSize(new Dimension(160, 20));
201:
202: PathChooser pc = new PathChooser(value);
203: myPanel.add(label);
204: myPanel.add(Box.createRigidArea(new Dimension(5, 0)));
205: myPanel.add(pc);
206: propertiesPanel.add(myPanel);
207: propertiesPanel.add(Box.createRigidArea(new Dimension(0, 3)));
208:
209: //
210: propertiesEls.put(propertyKey, pc);
211: }
212:
213: public void addDisabledPropertie(String propertyName, String value) {
214: JPanel myPanel = new JPanel();
215: myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.X_AXIS));
216:
217: JLabel label = new JLabel(propertyName, JLabel.RIGHT);
218: label.setMaximumSize(new Dimension(80, 20));
219: label.setMinimumSize(new Dimension(80, 20));
220: label.setPreferredSize(new Dimension(80, 20));
221:
222: JTextField field = new JTextField(value);
223: field.setEnabled(false);
224: field.setMaximumSize(new Dimension(800, 20));
225: field.setMinimumSize(new Dimension(10, 20));
226: field.setPreferredSize(new Dimension(250, 20));
227: myPanel.add(label);
228: myPanel.add(Box.createRigidArea(new Dimension(5, 0)));
229: myPanel.add(field);
230: propertiesPanel.add(myPanel);
231: propertiesPanel.add(Box.createRigidArea(new Dimension(0, 3)));
232: }
233:
234: public void addTextAreaPropertie(String propertyKey,
235: String propertyName, String value) {
236: JPanel myPanel = new JPanel();
237: myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.X_AXIS));
238:
239: JLabel label = new JLabel(propertyName, JLabel.RIGHT);
240: label.setVerticalAlignment(JLabel.TOP);
241: label.setMaximumSize(new Dimension(80, 60));
242: label.setMinimumSize(new Dimension(80, 60));
243: label.setPreferredSize(new Dimension(80, 60));
244:
245: JTextArea field = new JTextArea(value);
246: field.setLineWrap(true);
247:
248: JScrollPane scroll = new JScrollPane(field);
249: scroll.setMaximumSize(new Dimension(800, 60));
250: scroll.setMinimumSize(new Dimension(10, 60));
251: scroll.setPreferredSize(new Dimension(250, 60));
252: myPanel.add(label);
253: myPanel.add(Box.createRigidArea(new Dimension(5, 0)));
254: myPanel.add(scroll);
255: propertiesPanel.add(myPanel);
256: propertiesPanel.add(Box.createRigidArea(new Dimension(0, 3)));
257:
258: //
259: propertiesEls.put(propertyKey, field);
260: }
261:
262: public void addListPropertie(String propertyKey,
263: String propertyName, Map choices) {
264: JPanel myPanel = new JPanel();
265: myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.X_AXIS));
266:
267: JLabel label = new JLabel(propertyName, JLabel.RIGHT);
268: label.setVerticalAlignment(JLabel.TOP);
269: label.setMaximumSize(new Dimension(80, 60));
270: label.setMinimumSize(new Dimension(80, 60));
271: label.setPreferredSize(new Dimension(80, 60));
272:
273: List list = new List(choices);
274: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
275:
276: JScrollPane scroll = new JScrollPane(list);
277: scroll.setMaximumSize(new Dimension(800, 60));
278: scroll.setMinimumSize(new Dimension(10, 60));
279: scroll.setPreferredSize(new Dimension(250, 60));
280: myPanel.add(label);
281: myPanel.add(Box.createRigidArea(new Dimension(5, 0)));
282: myPanel.add(scroll);
283: propertiesPanel.add(myPanel);
284: propertiesPanel.add(Box.createRigidArea(new Dimension(0, 3)));
285:
286: //
287: propertiesEls.put(propertyKey, list);
288: }
289:
290: public void addListPropertie(String propertyKey,
291: String propertyName, Vector values, Vector keys) {
292: JPanel myPanel = new JPanel();
293: myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.X_AXIS));
294:
295: JLabel label = new JLabel(propertyName, JLabel.RIGHT);
296: label.setVerticalAlignment(JLabel.TOP);
297: label.setMaximumSize(new Dimension(80, 60));
298: label.setMinimumSize(new Dimension(80, 60));
299: label.setPreferredSize(new Dimension(80, 60));
300:
301: List list = new List(values, keys);
302: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
303:
304: JScrollPane scroll = new JScrollPane(list);
305: scroll.setMaximumSize(new Dimension(800, 60));
306: scroll.setMinimumSize(new Dimension(10, 60));
307: scroll.setPreferredSize(new Dimension(250, 60));
308: myPanel.add(label);
309: myPanel.add(Box.createRigidArea(new Dimension(5, 0)));
310: myPanel.add(scroll);
311: propertiesPanel.add(myPanel);
312: propertiesPanel.add(Box.createRigidArea(new Dimension(0, 3)));
313:
314: //
315: propertiesEls.put(propertyKey, list);
316: }
317:
318: /**
319: * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
320: */
321: public void actionPerformed(ActionEvent e) {
322: if (e.getActionCommand().equals("CANCEL")) {
323: this .dispose();
324: }
325:
326: if (e.getActionCommand().equals("SET")) {
327: set();
328: }
329: }
330:
331: private void set() {
332: for (Enumeration e = propertiesEls.keys(); e.hasMoreElements();) {
333: String key = e.nextElement() + "";
334: Object c = propertiesEls.get(key);
335:
336: if (c instanceof JTextComponent) {
337: properties.put(key, ((JTextComponent) c).getText());
338: }
339:
340: if (c instanceof PathChooser) {
341: String path = c + "";
342:
343: if (!new File(path).exists()
344: || !new File(path).isDirectory()) {
345: properties = new Hashtable();
346: JOptionPane
347: .showMessageDialog(
348: this ,
349: "Path "
350: + c
351: + " does not exist or is not a directory",
352: "Invalid path",
353: JOptionPane.WARNING_MESSAGE);
354:
355: return;
356: }
357:
358: properties.put(key, path);
359: }
360:
361: if (c instanceof JList) {
362: String value = c.toString();
363:
364: if (value == null) {
365: properties = new Hashtable();
366: JOptionPane.showMessageDialog(this ,
367: "You should select a value in the list",
368: "Warning", JOptionPane.WARNING_MESSAGE);
369:
370: return;
371: }
372:
373: properties.put(key, value);
374: }
375: }
376:
377: dispose();
378: }
379:
380: class PathChooser extends JPanel implements ActionListener {
381: private JButton button;
382: private JTextField field;
383:
384: public PathChooser(String value) {
385: setLayout(new BoxLayout(this , BoxLayout.X_AXIS));
386: field = new JTextField(value);
387: field.setMaximumSize(new Dimension(800, 20));
388: field.setMinimumSize(new Dimension(10, 20));
389: field.setPreferredSize(new Dimension(200, 20));
390: button = new JButton("Browse");
391: button.setPreferredSize(new Dimension(80, 20));
392: add(field);
393: add(Box.createRigidArea(new Dimension(2, 0)));
394: add(button);
395: button.addActionListener(this );
396: }
397:
398: public String getValue() {
399: return field.getText();
400: }
401:
402: /**
403: * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
404: */
405: public void actionPerformed(ActionEvent e) {
406: JFileChooser chooser = new JFileChooser(
407: new File(getValue()).getParent());
408: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
409:
410: int returnVal = chooser.showOpenDialog(this );
411:
412: if (returnVal == JFileChooser.APPROVE_OPTION) {
413: field.setText(chooser.getSelectedFile()
414: .getAbsolutePath());
415: }
416: }
417:
418: public String toString() {
419: return getValue();
420: }
421: }
422:
423: class List extends JList {
424: public List(Map choices) {
425: super ();
426:
427: // elements
428: Set keys = choices.keySet();
429: Vector items = new Vector();
430:
431: for (Iterator i = keys.iterator(); i.hasNext();) {
432: String key = i.next() + "";
433: ListItem item = new ListItem(choices.get(key) + "", key);
434: items.add(item);
435: }
436:
437: setListData(items);
438: }
439:
440: public List(Vector values, Vector keys) {
441: super ();
442:
443: // elements
444: Vector items = new Vector();
445:
446: for (int i = 0; i < values.size(); i++) {
447: ListItem item = new ListItem(values.get(i) + "", keys
448: .get(i)
449: + "");
450: items.add(item);
451: }
452:
453: setListData(items);
454: }
455:
456: /**
457: * @see javax.swing.JList#getSelectedValue()
458: */
459: public Object getSelectedValue() {
460: ListItem item = (ListItem) super .getSelectedValue();
461:
462: if (item == null) {
463: return null;
464: }
465:
466: return item.getValue();
467: }
468:
469: public String toString() {
470: String sv = (String) getSelectedValue();
471:
472: return sv;
473: }
474:
475: class ListItem {
476: private String label;
477: private String value;
478:
479: public ListItem(String label, String value) {
480: setLabel(label);
481: setValue(value);
482: }
483:
484: /**
485: * Returns the label.
486: * @return String
487: */
488: public String getLabel() {
489: return label;
490: }
491:
492: /**
493: * Returns the value.
494: * @return String
495: */
496: public String getValue() {
497: return value;
498: }
499:
500: /**
501: * Sets the label.
502: * @param label The label to set
503: */
504: public void setLabel(String label) {
505: this .label = label;
506: }
507:
508: /**
509: * Sets the value.
510: * @param value The value to set
511: */
512: public void setValue(String value) {
513: this .value = value;
514: }
515:
516: /**
517: * @see java.lang.Object#toString()
518: */
519: public String toString() {
520: return getLabel();
521: }
522: }
523: }
524: }
|