001: // IPTemplateArrayEditor.java
002: // $Id: IPTemplateArrayEditor.java,v 1.13 2000/08/16 21:37:27 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.jigadm.editors;
007:
008: import java.awt.Dimension;
009:
010: import java.util.Hashtable;
011: import java.util.Properties;
012: import java.util.StringTokenizer;
013:
014: import org.w3c.tools.resources.Attribute;
015:
016: import org.w3c.jigadm.RemoteResourceWrapper;
017:
018: import org.w3c.jigsaw.admin.RemoteAccessException;
019:
020: import org.w3c.tools.widgets.IPTextField;
021: import org.w3c.tools.widgets.TextEditable;
022:
023: /**
024: * IPTemplateArrayEditor :
025: * @author Benoit Mahe <bmahe@sophia.inria.fr>
026: */
027:
028: public class IPTemplateArrayEditor extends StringArrayEditor {
029:
030: protected short oldshortarray[][] = null;
031:
032: private String shortsToIPString(short one, short two, short three,
033: short four) {
034: return (((one == 256) ? "*" : String.valueOf((int) one)) + "."
035: + ((two == 256) ? "*" : String.valueOf((int) two))
036: + "."
037: + ((three == 256) ? "*" : String.valueOf((int) three))
038: + "." + ((four == 256) ? "*" : String
039: .valueOf((int) four)));
040: }
041:
042: protected String[] toStringArray(short selectedItems[][]) {
043: if (selectedItems == null)
044: return null;
045: String selected[] = new String[selectedItems.length];
046: for (int i = 0; i < selectedItems.length; i++)
047: selected[i] = shortsToIPString(selectedItems[i][0],
048: selectedItems[i][1], selectedItems[i][2],
049: selectedItems[i][3]);
050: return selected;
051: }
052:
053: protected void setSelectedItems(short selectedItems[][]) {
054: comp.setSelectedItems(toStringArray(selectedItems));
055: }
056:
057: /**
058: * reset the changes (if any)
059: */
060: public void resetChanges() {
061: hasChanged = false;
062: setSelectedItems(oldshortarray);
063: }
064:
065: /**
066: * Get the current value of the edited value
067: * @return an object or <strong>null</strong> if the object was not
068: * initialized
069: */
070: public Object getValue() {
071: StringTokenizer st = null;
072: String stvalue[] = comp.getSelectedItems();
073: short shvalue[][] = new short[stvalue.length][4];
074: for (int i = 0; i < stvalue.length; i++) {
075: st = new StringTokenizer(stvalue[i], ".");
076: int j = 0;
077: while (j < 4 && st.hasMoreTokens()) {
078: String tok = st.nextToken();
079: if (tok.equals("*"))
080: shvalue[i][j] = (short) 256;
081: else {
082: try {
083: shvalue[i][j] = Short.parseShort(tok);
084: } catch (NumberFormatException ex) {
085: ex.printStackTrace();
086: shvalue[i][j] = (short) 0;
087: }
088: }
089: j++;
090: }
091: }
092: return shvalue;
093: }
094:
095: /**
096: * Set the value of the edited value
097: * @param o the new value.
098: */
099: public void setValue(Object o) {
100: this .oldshortarray = (short[][]) o;
101: setSelectedItems(oldshortarray);
102: }
103:
104: protected TextEditable getTextEditor() {
105: return new IPTextField();
106: }
107:
108: protected Dimension getPopupSize() {
109: return new Dimension(350, 250);
110: }
111:
112: /**
113: * Initialize the editor
114: * @param w the ResourceWrapper father of the attribute
115: * @param a the Attribute we are editing
116: * @param o the value of the above attribute
117: * @param p some Properties, used to fine-tune the editor
118: * @exception RemoteAccessException if a remote access error occurs.
119: */
120: public void initialize(RemoteResourceWrapper w, Attribute a,
121: Object o, Properties p) throws RemoteAccessException {
122: // Get the feeder class fromproperties:
123: EditorFeeder feeder = null;
124: String feederClass = null;
125:
126: feederClass = (String) p.get(FEEDER_CLASS_P);
127: if (feederClass == null)
128: throw new RuntimeException(
129: "StringArrayEditor mis-configuration: "
130: + FEEDER_CLASS_P + " property undefined.");
131: try {
132: Class c = Class.forName(feederClass);
133: feeder = (EditorFeeder) c.newInstance();
134: feeder.initialize(w, p);
135: } catch (Exception ex) {
136: ex.printStackTrace();
137: throw new RuntimeException(
138: "StringArrayEditor mis-configured: "
139: + " unable to instantiate " + feederClass
140: + ".");
141: }
142: oldshortarray = (short[][]) o;
143: createComponent(feeder, toStringArray(oldshortarray));
144: }
145:
146: public IPTemplateArrayEditor() {
147: super();
148: }
149: }
|