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