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