--- /dev/null
+// SelectFileEditor.java\r
+// $Id: SelectFileEditor.java,v 1.1 2010/06/15 12:20:44 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1997.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigadmin.attributes ;\r
+\r
+import java.awt.Component;\r
+import java.awt.BorderLayout;\r
+\r
+import javax.swing.JFileChooser;\r
+import javax.swing.JPanel;\r
+import javax.swing.JButton;\r
+import javax.swing.JTextField;\r
+import javax.swing.BorderFactory;\r
+\r
+import java.io.File;\r
+\r
+import java.awt.event.ActionListener;\r
+import java.awt.event.ActionEvent;\r
+\r
+import java.util.Properties;\r
+\r
+import org.w3c.tools.resources.Attribute;\r
+\r
+import org.w3c.jigsaw.admin.RemoteAccessException;\r
+import org.w3c.jigsaw.admin.RemoteResource;\r
+\r
+import org.w3c.jigadm.RemoteResourceWrapper;\r
+import org.w3c.jigadm.editors.AttributeEditor;\r
+\r
+public class SelectFileEditor extends AttributeEditor {\r
+\r
+ class FComponent extends JPanel implements ActionListener {\r
+ JTextField text = null;\r
+ JButton select = null;\r
+\r
+ public String getFile() {\r
+ return text.getText();\r
+ }\r
+\r
+ public void setFile(String file) {\r
+ text.setText(file);\r
+ }\r
+\r
+ public void actionPerformed(ActionEvent e) {\r
+ String command = e.getActionCommand();\r
+ if (command != null) {\r
+ if (command.equals("select")) {\r
+ JFileChooser chooser = null;\r
+ String file = getFile();\r
+ String dir = null;\r
+ if (file != null) {\r
+ dir = new File(file).getParent();\r
+ chooser = new JFileChooser(dir);\r
+ } else {\r
+ chooser = new JFileChooser();\r
+ }\r
+ chooser.setFileSelectionMode(\r
+ JFileChooser.FILES_AND_DIRECTORIES);\r
+ int returnVal = chooser.showOpenDialog(this);\r
+ if (returnVal == JFileChooser.APPROVE_OPTION) {\r
+ File selected = chooser.getSelectedFile();\r
+ setFile(selected.getAbsolutePath());\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ FComponent() {\r
+ this.text = new JTextField();\r
+ text.setBorder(BorderFactory.createLoweredBevelBorder());\r
+ this.select = new JButton("Select");\r
+ select.setActionCommand("select");\r
+ select.addActionListener(this);\r
+ setLayout( new BorderLayout());\r
+ add(text,"Center");\r
+ add(select,"East");\r
+ }\r
+ }\r
+\r
+ private String origs;\r
+ protected FComponent widget;\r
+\r
+ /**\r
+ * Tells if the edited value has changed\r
+ * @return true if the value changed.\r
+ */\r
+\r
+ public boolean hasChanged() {\r
+ return !origs.equals(widget.getFile());\r
+ }\r
+\r
+ /**\r
+ * set the current value to be the original value, ie: changed\r
+ * must return <strong>false</strong> after a reset.\r
+ */\r
+\r
+ public void clearChanged() {\r
+ origs = widget.getFile();\r
+ }\r
+\r
+ /**\r
+ * reset the changes (if any)\r
+ */\r
+\r
+ public void resetChanges() {\r
+ widget.setFile(origs);\r
+ }\r
+\r
+ /**\r
+ * Get the current value of the edited value\r
+ * @return an object or <strong>null</strong> if the object was not\r
+ * initialized\r
+ */\r
+\r
+ public Object getValue() {\r
+ return widget.getFile();\r
+ }\r
+\r
+ /**\r
+ * Set the value of the edited value\r
+ * @param o the new value.\r
+ */\r
+\r
+ public void setValue(Object o) {\r
+ widget.setFile(o.toString());\r
+ }\r
+\r
+ /**\r
+ * get the Component created by the editor.\r
+ * @return a Component\r
+ */\r
+\r
+ public Component getComponent() {\r
+ return widget;\r
+ }\r
+\r
+ /**\r
+ * Initialize the editor\r
+ * @param w the ResourceWrapper father of the attribute\r
+ * @param a the Attribute we are editing\r
+ * @param o the value of the above attribute\r
+ * @param p some Properties, used to fine-tune the editor\r
+ * @exception RemoteAccessException if a remote access error occurs.\r
+ */\r
+\r
+ public void initialize(RemoteResourceWrapper w, Attribute a, Object o,\r
+ Properties p)\r
+ throws RemoteAccessException\r
+ {\r
+ RemoteResource r = w.getResource();\r
+ if(o == null) {\r
+ String v = null;\r
+ // FIXME\r
+ v = (String) r.getValue(a.getName());\r
+ \r
+ if(v == null)\r
+ if(a.getDefault() != null)\r
+ v = a.getDefault().toString();\r
+ if ( v != null ) {\r
+ origs = v;\r
+ widget.setFile(origs);\r
+ } \r
+ } else {\r
+ origs = o.toString();\r
+ }\r
+ widget.setFile(origs);\r
+ }\r
+\r
+ public void createComponent() {\r
+ widget = new FComponent();\r
+ }\r
+\r
+ public SelectFileEditor() {\r
+ createComponent();\r
+ origs = "";\r
+ }\r
+\r
+}\r