001: // SelectFileEditor.java
002: // $Id: SelectFileEditor.java,v 1.3 2000/08/16 21:37:29 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadmin.attributes;
007:
008: import java.awt.Component;
009: import java.awt.BorderLayout;
010:
011: import javax.swing.JFileChooser;
012: import javax.swing.JPanel;
013: import javax.swing.JButton;
014: import javax.swing.JTextField;
015: import javax.swing.BorderFactory;
016:
017: import java.io.File;
018:
019: import java.awt.event.ActionListener;
020: import java.awt.event.ActionEvent;
021:
022: import java.util.Properties;
023:
024: import org.w3c.tools.resources.Attribute;
025:
026: import org.w3c.jigsaw.admin.RemoteAccessException;
027: import org.w3c.jigsaw.admin.RemoteResource;
028:
029: import org.w3c.jigadm.RemoteResourceWrapper;
030: import org.w3c.jigadm.editors.AttributeEditor;
031:
032: public class SelectFileEditor extends AttributeEditor {
033:
034: class FComponent extends JPanel implements ActionListener {
035: JTextField text = null;
036: JButton select = null;
037:
038: public String getFile() {
039: return text.getText();
040: }
041:
042: public void setFile(String file) {
043: text.setText(file);
044: }
045:
046: public void actionPerformed(ActionEvent e) {
047: String command = e.getActionCommand();
048: if (command != null) {
049: if (command.equals("select")) {
050: JFileChooser chooser = null;
051: String file = getFile();
052: String dir = null;
053: if (file != null) {
054: dir = new File(file).getParent();
055: chooser = new JFileChooser(dir);
056: } else {
057: chooser = new JFileChooser();
058: }
059: chooser
060: .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
061: int returnVal = chooser.showOpenDialog(this );
062: if (returnVal == JFileChooser.APPROVE_OPTION) {
063: File selected = chooser.getSelectedFile();
064: setFile(selected.getAbsolutePath());
065: }
066: }
067: }
068: }
069:
070: FComponent() {
071: this .text = new JTextField();
072: text.setBorder(BorderFactory.createLoweredBevelBorder());
073: this .select = new JButton("Select");
074: select.setActionCommand("select");
075: select.addActionListener(this );
076: setLayout(new BorderLayout());
077: add(text, "Center");
078: add(select, "East");
079: }
080: }
081:
082: private String origs;
083: protected FComponent widget;
084:
085: /**
086: * Tells if the edited value has changed
087: * @return true if the value changed.
088: */
089:
090: public boolean hasChanged() {
091: return !origs.equals(widget.getFile());
092: }
093:
094: /**
095: * set the current value to be the original value, ie: changed
096: * must return <strong>false</strong> after a reset.
097: */
098:
099: public void clearChanged() {
100: origs = widget.getFile();
101: }
102:
103: /**
104: * reset the changes (if any)
105: */
106:
107: public void resetChanges() {
108: widget.setFile(origs);
109: }
110:
111: /**
112: * Get the current value of the edited value
113: * @return an object or <strong>null</strong> if the object was not
114: * initialized
115: */
116:
117: public Object getValue() {
118: return widget.getFile();
119: }
120:
121: /**
122: * Set the value of the edited value
123: * @param o the new value.
124: */
125:
126: public void setValue(Object o) {
127: widget.setFile(o.toString());
128: }
129:
130: /**
131: * get the Component created by the editor.
132: * @return a Component
133: */
134:
135: public Component getComponent() {
136: return widget;
137: }
138:
139: /**
140: * Initialize the editor
141: * @param w the ResourceWrapper father of the attribute
142: * @param a the Attribute we are editing
143: * @param o the value of the above attribute
144: * @param p some Properties, used to fine-tune the editor
145: * @exception RemoteAccessException if a remote access error occurs.
146: */
147:
148: public void initialize(RemoteResourceWrapper w, Attribute a,
149: Object o, Properties p) throws RemoteAccessException {
150: RemoteResource r = w.getResource();
151: if (o == null) {
152: String v = null;
153: // FIXME
154: v = (String) r.getValue(a.getName());
155:
156: if (v == null)
157: if (a.getDefault() != null)
158: v = a.getDefault().toString();
159: if (v != null) {
160: origs = v;
161: widget.setFile(origs);
162: }
163: } else {
164: origs = o.toString();
165: }
166: widget.setFile(origs);
167: }
168:
169: public void createComponent() {
170: widget = new FComponent();
171: }
172:
173: public SelectFileEditor() {
174: createComponent();
175: origs = "";
176: }
177:
178: }
|