001: // SlideIntegerAttributeEditor.java
002: // $Id: SlideIntegerAttributeEditor.java,v 1.5 2000/10/31 15:08:45 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:
010: import java.util.Properties;
011:
012: import org.w3c.tools.resources.Attribute;
013:
014: import org.w3c.jigadmin.widgets.EditableSlider;
015:
016: import org.w3c.jigsaw.admin.RemoteAccessException;
017: import org.w3c.jigsaw.admin.RemoteResource;
018:
019: import org.w3c.jigadm.RemoteResourceWrapper;
020: import org.w3c.jigadm.editors.AttributeEditor;
021:
022: /**
023: * SlideIntegerAttributeEditor :
024: * @author Benoit Mahe <bmahe@sophia.inria.fr>
025: */
026:
027: public class SlideIntegerAttributeEditor extends AttributeEditor {
028:
029: /**
030: * The slider's max bound property.
031: */
032: public static final String MAX_P = "slider.max";
033:
034: /**
035: * The slider's min bound property.
036: */
037: public static final String MIN_P = "slider.min";
038:
039: /**
040: * The slider's step property.
041: */
042: public static final String STEP_P = "slider.step";
043:
044: /**
045: * The slider border property
046: */
047: public static final String BORDER_P = "slider.border";
048:
049: private int origs;
050: EditableSlider widget;
051:
052: /**
053: * Tells if the edited value has changed
054: * @return true if the value changed.
055: */
056: public boolean hasChanged() {
057: return (origs != widget.getValue());
058: }
059:
060: /**
061: * set the current value to be the original value, ie: changed
062: * must return <strong>false</strong> after a reset.
063: */
064: public void clearChanged() {
065: origs = widget.getValue();
066: }
067:
068: /**
069: * reset the changes (if any)
070: */
071: public void resetChanges() {
072: widget.setValue(origs);
073: }
074:
075: /**
076: * Get the current value of the edited value
077: * @return an object or <strong>null</strong> if the object was not
078: * initialized
079: */
080: public Object getValue() {
081: return new Integer(widget.getValue());
082: }
083:
084: /**
085: * Set the value of the edited value
086: * @param o the new value.
087: */
088: public void setValue(Object o) {
089: widget.setValue(((Integer) o).intValue());
090: }
091:
092: /**
093: * get the Component created by the editor.
094: * @return a Component
095: */
096: public Component getComponent() {
097: return widget;
098: }
099:
100: public EditableSlider getSlider(int min, int max, int step,
101: int value) {
102: //negative value not accepted
103: min = (min < 0 ? 0 : min);
104: max = (max < 0 ? 0 : max);
105: step = (step < 0 ? 0 : step);
106: value = (value < 0 ? 0 : value);
107: if (min > max) {
108: min = max;
109: }
110: if (value <= min) {
111: min = Math.max(0, value - (max - min) / 5);
112: }
113: if (value >= max) {
114: max = value + (max - min) / 5;
115: }
116: EditableSlider slider = new EditableSlider(min, max, step,
117: value);
118: return slider;
119: }
120:
121: /**
122: * Initialize the editor
123: * @param w the ResourceWrapper father of the attribute
124: * @param a the Attribute we are editing
125: * @param o the value of the above attribute
126: * @param p some Properties, used to fine-tune the editor
127: * @exception RemoteAccessException if a remote access error occurs.
128: */
129: public void initialize(RemoteResourceWrapper w, Attribute a,
130: Object o, Properties p) throws RemoteAccessException {
131: int min = -1;
132: int max = -1;
133: int step = -1;
134: if (p != null) {
135:
136: try {
137: min = Integer.parseInt(p.getProperty(MIN_P, "0"));
138: } catch (NumberFormatException ex) {
139: min = 0;
140: }
141:
142: try {
143: max = Integer.parseInt(p.getProperty(MAX_P, "100"));
144: } catch (NumberFormatException ex) {
145: max = 100;
146: }
147:
148: try {
149: step = Integer.parseInt(p.getProperty(STEP_P, "5"));
150: } catch (NumberFormatException ex) {
151: step = 5;
152: }
153:
154: } else {
155: min = 0;
156: max = 100;
157: step = 5;
158: }
159: RemoteResource r = w.getResource();
160: if (o == null) {
161: Integer v = (Integer) r.getValue(a.getName());
162: if (v == null)
163: if (a.getDefault() != null)
164: v = (Integer) a.getDefault();
165: if (v != null) {
166: origs = v.intValue();
167: }
168: } else {
169: origs = ((Integer) o).intValue();
170: }
171: widget = getSlider(min, max, step, origs);
172: }
173:
174: public SlideIntegerAttributeEditor() {
175: }
176:
177: }
|