--- /dev/null
+// MimeTypeAttributeEditor.java\r
+// $Id: MimeTypeAttributeEditor.java,v 1.1 2010/06/15 12:22:49 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.jigadm.editors ;\r
+\r
+import java.awt.BorderLayout;\r
+import java.awt.Button;\r
+import java.awt.Component;\r
+import java.awt.Container;\r
+import java.awt.Frame;\r
+import java.awt.GridBagConstraints;\r
+import java.awt.GridBagLayout;\r
+import java.awt.Insets;\r
+import java.awt.Label;\r
+import java.awt.Menu;\r
+import java.awt.MenuBar;\r
+import java.awt.MenuItem;\r
+import java.awt.Panel;\r
+import java.awt.TextComponent;\r
+import java.awt.TextField;\r
+import java.awt.Window;\r
+\r
+import java.awt.event.ActionEvent;\r
+import java.awt.event.ActionListener;\r
+import java.awt.event.TextEvent;\r
+import java.awt.event.TextListener;\r
+\r
+import java.util.Enumeration;\r
+import java.util.Hashtable;\r
+import java.util.Properties;\r
+\r
+import org.w3c.tools.resources.Attribute;\r
+\r
+import org.w3c.tools.widgets.ClosableFrame;\r
+\r
+import org.w3c.www.mime.MimeType;\r
+\r
+import org.w3c.jigadm.PropertyManager;\r
+import org.w3c.jigadm.RemoteResourceWrapper;\r
+\r
+import org.w3c.jigsaw.admin.RemoteAccessException;\r
+\r
+/**\r
+ * MimeTypeAttributeEditor :\r
+ * @author Benoit Mahe <bmahe@sophia.inria.fr>\r
+ */\r
+\r
+public class MimeTypeAttributeEditor extends AttributeEditor {\r
+\r
+ class MimeTypeAttributePopup extends ClosableFrame \r
+ implements ActionListener \r
+ {\r
+\r
+ protected TextField mimetype = null;\r
+ protected MimeTypeAttributeComponent parent = null;\r
+\r
+ public void actionPerformed(ActionEvent e) {\r
+ String command = e.getActionCommand();\r
+ if (command != null) {\r
+ if (command.equals("update")) {\r
+ parent.setText(mimetype.getText());\r
+ setVisible(false);\r
+ } else if (command.equals("cancel")) {\r
+ close();\r
+ } else \r
+ mimetype.setText(command);\r
+ }\r
+ }\r
+\r
+ protected void close() {\r
+ setVisible(false);\r
+ mimetype.setText("");\r
+ }\r
+\r
+ private void addMenuListener(MenuItem item, String action) {\r
+ item.addActionListener(this);\r
+ item.setActionCommand(action);\r
+ }\r
+\r
+ MimeTypeAttributePopup(MimeTypeAttributeComponent parent) {\r
+ this.parent = parent;\r
+\r
+ GridBagLayout layout = new GridBagLayout();\r
+ GridBagConstraints c = new GridBagConstraints();\r
+ c.fill = GridBagConstraints.BOTH;\r
+ c.insets = new Insets(5,5,5,5);\r
+ setLayout(layout);\r
+\r
+ // MENU\r
+ MenuBar menubar = new MenuBar();\r
+ MenuItem item;\r
+ Menu menu = new Menu("MimeTypes");\r
+ Hashtable mimeTypes;\r
+ Enumeration e;\r
+ String minor[];\r
+\r
+ mimeTypes = PropertyManager.getPropertyManager().getMimeTypes();\r
+ e = mimeTypes.keys();\r
+\r
+ while(e.hasMoreElements()) {\r
+ String major = (String)e.nextElement();\r
+ Menu imenu = new Menu(major);\r
+ imenu.addActionListener(this);\r
+ minor = (String[]) mimeTypes.get(major);\r
+ for(int i=0; i<minor.length; i++) {\r
+ item = new MenuItem(minor[i]);\r
+ addMenuListener(item, major + "/" + minor[i]);\r
+ imenu.add(item);\r
+ }\r
+ menu.add(imenu);\r
+ }\r
+ menubar.add(menu);\r
+\r
+ c.gridwidth = GridBagConstraints.RELATIVE;\r
+ Label label = new Label("Mime Type : ");\r
+ layout.setConstraints(label,c);\r
+ add(label);\r
+\r
+ c.gridwidth = GridBagConstraints.REMAINDER; \r
+ mimetype = new TextField(20);\r
+ layout.setConstraints(mimetype,c);\r
+ add(mimetype);\r
+\r
+ Button okB = new Button("Ok");\r
+ okB.setActionCommand("update");\r
+ okB.addActionListener(this);\r
+ Button cancelB = new Button("Cancel");\r
+ cancelB.setActionCommand("cancel");\r
+ cancelB.addActionListener(this);\r
+\r
+ Panel p = new Panel();\r
+ GridBagLayout playout = new GridBagLayout();\r
+ p.setLayout( playout);\r
+\r
+ c.fill = GridBagConstraints.NONE;\r
+ c.anchor=GridBagConstraints.EAST;\r
+ c.gridwidth = GridBagConstraints.RELATIVE;\r
+ playout.setConstraints(okB,c);\r
+ p.add(okB);\r
+\r
+ c.gridwidth = GridBagConstraints.REMAINDER;\r
+ c.anchor=GridBagConstraints.WEST;\r
+ playout.setConstraints(cancelB,c);\r
+ p.add(cancelB);\r
+\r
+ c.fill = GridBagConstraints.NONE;\r
+ c.anchor=GridBagConstraints.CENTER;\r
+ c.gridwidth = 2;\r
+ layout.setConstraints(p,c);\r
+ add(p);\r
+\r
+ setMenuBar(menubar);\r
+\r
+ setSize(300,150);\r
+ }\r
+\r
+ }\r
+\r
+ class MimeTypeAttributeComponent extends Panel \r
+ implements ActionListener,\r
+ TextListener \r
+ {\r
+\r
+ protected TextField type = null;\r
+ protected MimeTypeAttributePopup popup = null;\r
+ protected MimeTypeAttributeEditor editor = null;\r
+\r
+ public void textValueChanged(TextEvent e) {\r
+ setModified();\r
+ }\r
+\r
+ public void actionPerformed(ActionEvent e) {\r
+ String command = e.getActionCommand();\r
+ if (command != null) {\r
+ if (command.equals("edit")) {\r
+ if (popup == null)\r
+ popup = new MimeTypeAttributePopup(this);\r
+ popup.show();\r
+ }\r
+ }\r
+ }\r
+\r
+ public String getText() {\r
+ return type.getText();\r
+ }\r
+\r
+ public void setText(String text) {\r
+ type.setText(text);\r
+ editor.setModified();\r
+ }\r
+\r
+ MimeTypeAttributeComponent (MimeTypeAttributeEditor editor,\r
+ String type)\r
+ {\r
+ super();\r
+ this.editor = editor;\r
+ this.type = new TextField(20);\r
+ this.type.setText(type);\r
+ this.type.addTextListener(this);\r
+ Button editB = new Button("Change");\r
+ editB.setActionCommand("edit");\r
+ editB.addActionListener(this);\r
+ setLayout( new BorderLayout());\r
+ add(this.type,"West");\r
+ add(editB,"Center");\r
+ }\r
+\r
+ }\r
+\r
+ // The MimeTypeAttributeEditor itself\r
+\r
+ protected MimeTypeAttributeComponent comp = null;\r
+ protected boolean hasChanged = false;\r
+ protected String oldvalue = null;\r
+\r
+ protected void createComponent(String type) {\r
+ if ( comp == null ) \r
+ comp = new MimeTypeAttributeComponent(this,type);\r
+ }\r
+\r
+ protected void setModified() {\r
+ hasChanged = true;\r
+ }\r
+\r
+ /**\r
+ * Tells if the edited value has changed\r
+ * @return true if the value changed.\r
+ */\r
+ public boolean hasChanged() {\r
+ return hasChanged;\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
+ public void clearChanged() {\r
+ hasChanged = false;\r
+ }\r
+\r
+ /**\r
+ * reset the changes (if any)\r
+ */\r
+ public void resetChanges() {\r
+ hasChanged = false;\r
+ comp.setText(oldvalue);\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
+ public Object getValue() {\r
+ try {\r
+ return new MimeType(comp.getText());\r
+ } catch (Exception ex) {\r
+ ex.printStackTrace();\r
+ }\r
+ // not reached\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Set the value of the edited value\r
+ * @param o the new value.\r
+ */ \r
+ public void setValue(Object o) {\r
+ this.oldvalue = (String) o;\r
+ comp.setText(oldvalue);\r
+ }\r
+\r
+ /**\r
+ * get the Component created by the editor.\r
+ * @return a Component\r
+ */\r
+ public Component getComponent() {\r
+ return comp;\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
+ public void initialize(RemoteResourceWrapper w\r
+ , Attribute a\r
+ , Object o\r
+ , Properties p) \r
+ throws RemoteAccessException\r
+ {\r
+ MimeType type = (MimeType)o;\r
+ if (o == null) {\r
+ oldvalue = "*none*";\r
+ createComponent(oldvalue);;\r
+ } else {\r
+ createComponent(type.toString());\r
+ oldvalue = type.toString();\r
+ }\r
+ }\r
+\r
+}\r