001: // DateAttributeEditor.java
002: // $Id: DateAttributeEditor.java,v 1.18 2000/08/17 10:03:50 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.BorderLayout;
009: import java.awt.Component;
010: import java.awt.Container;
011: import java.awt.GridLayout;
012: import java.awt.Image;
013: import java.awt.Label;
014: import java.awt.Panel;
015: import java.awt.Toolkit;
016:
017: import java.awt.event.ActionEvent;
018: import java.awt.event.ActionListener;
019:
020: import java.util.Calendar;
021: import java.util.Date;
022: import java.util.Properties;
023:
024: import org.w3c.tools.resources.Attribute;
025:
026: import org.w3c.tools.widgets.BorderPanel;
027: import org.w3c.tools.widgets.ImageButton;
028:
029: import org.w3c.jigsaw.admin.RemoteAccessException;
030: import org.w3c.jigsaw.admin.RemoteResource;
031:
032: import org.w3c.jigadm.PropertyManager;
033: import org.w3c.jigadm.RemoteResourceWrapper;
034:
035: public class DateAttributeEditor extends AttributeEditor {
036:
037: /**
038: * an inner ActionListener for the '+' and '-' Buttons
039: */
040:
041: class DateActionListener implements ActionListener {
042: int field = 0;
043:
044: public void actionPerformed(ActionEvent ae) {
045: if (ae.getActionCommand().equals("+"))
046: updateValue(field, true);
047: else
048: updateValue(field, false);
049: }
050:
051: DateActionListener(int f) {
052: field = f;
053: }
054: }
055:
056: private Calendar c;
057: private Date origd;
058: Date currd;
059: Panel widget;
060: private Label h, min, s, d, m, y;
061: private static final String smonth[] = { "Jan", "Feb", "Mar",
062: "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
063: "Dec" };
064:
065: /**
066: * reset the strings in the Textfields according to the new date
067: */
068:
069: private void updateFields() {
070: h
071: .setText((new Integer(c.get(Calendar.HOUR_OF_DAY)))
072: .toString());
073: min.setText((new Integer(c.get(Calendar.MINUTE))).toString());
074: s.setText((new Integer(c.get(Calendar.SECOND))).toString());
075: d.setText((new Integer(c.get(Calendar.DAY_OF_MONTH)))
076: .toString());
077: m.setText(smonth[c.get(Calendar.MONTH)]);
078: y.setText((new Integer(c.get(Calendar.YEAR))).toString());
079: }
080:
081: /**
082: * update the new date value, according to the field defined in
083: * the Calendar
084: * @see Calendar
085: * @param field the field of the Calendar to be modified
086: * @param plus a boolean which determine the change sign
087: */
088:
089: protected void updateValue(int field, boolean plus) {
090: c.setTime(currd);
091: c.roll(field, plus);
092: currd = c.getTime();
093: // jdk 1.1 kludge
094: c.setTime(currd);
095: updateFields();
096: }
097:
098: /**
099: * Tells if the edited value has changed
100: * @return true if the value changed.
101: */
102:
103: public boolean hasChanged() {
104: return (!origd.equals(currd));
105: }
106:
107: /**
108: * set the current value to be the original value, ie: changed
109: * must return <strong>false</strong> after a reset.
110: */
111:
112: public void clearChanged() {
113: origd = currd;
114: }
115:
116: /**
117: * reset the changes (if any)
118: */
119:
120: public void resetChanges() {
121: currd = origd;
122: c.setTime(currd);
123: updateFields();
124: }
125:
126: /**
127: * Get the current value of the edited value
128: * @return an object or <strong>null</strong> if the object was not
129: * initialized
130: */
131:
132: public Object getValue() {
133: return new Long(currd.getTime());
134: }
135:
136: /**
137: * Add a Listener to this editor.
138: * @param el a listener
139: */
140:
141: public void setValue(Object o) {
142: if (o instanceof Date) {
143: currd = (Date) o;
144: c.setTime(currd);
145: updateFields();
146: }
147: }
148:
149: /**
150: * get the Component created by the editor.
151: * @return a Component
152: */
153:
154: public Component getComponent() {
155: return widget;
156: }
157:
158: public DateAttributeEditor() {
159: ImageButton pl, mi;
160: Panel p, arrows;
161: DateActionListener dae;
162: PropertyManager pm = PropertyManager.getPropertyManager();
163:
164: Image up = Toolkit.getDefaultToolkit().getImage(
165: pm.getIconLocation("pup"));
166: Image down = Toolkit.getDefaultToolkit().getImage(
167: pm.getIconLocation("pdown"));
168:
169: widget = new Panel(new GridLayout(2, 1, 1, 1));
170:
171: Panel time = new BorderPanel(BorderPanel.IN);
172: time.setLayout(new GridLayout(1, 3));
173:
174: Panel date = new BorderPanel(BorderPanel.IN);
175: date.setLayout(new GridLayout(1, 3));
176:
177: // add the "hour" panel
178: h = new Label();
179: h.setAlignment(Label.CENTER);
180: dae = new DateActionListener(Calendar.HOUR_OF_DAY);
181:
182: pl = new ImageButton(up);
183: pl.addActionListener(dae);
184: pl.setActionCommand("+");
185:
186: mi = new ImageButton(down);
187: mi.addActionListener(dae);
188: mi.setActionCommand("-");
189:
190: arrows = new Panel(new GridLayout(2, 1));
191: arrows.add(pl);
192: arrows.add(mi);
193: p = new Panel(new BorderLayout());
194: p.add(h, "Center");
195: p.add(arrows, "East");
196: time.add(p);
197:
198: // add the "min" panel
199: min = new Label();
200: min.setAlignment(Label.CENTER);
201: dae = new DateActionListener(Calendar.MINUTE);
202:
203: pl = new ImageButton(up);
204: pl.addActionListener(dae);
205: pl.setActionCommand("+");
206:
207: mi = new ImageButton(down);
208: mi.addActionListener(dae);
209: mi.setActionCommand("-");
210:
211: arrows = new Panel(new GridLayout(2, 1));
212: arrows.add(pl);
213: arrows.add(mi);
214: p = new Panel(new BorderLayout());
215: p.add(min, "Center");
216: p.add(arrows, "East");
217: time.add(p);
218:
219: s = new Label();
220: s.setAlignment(Label.CENTER);
221: dae = new DateActionListener(Calendar.SECOND);
222:
223: pl = new ImageButton(up);
224: pl.addActionListener(dae);
225: pl.setActionCommand("+");
226:
227: mi = new ImageButton(down);
228: mi.addActionListener(dae);
229: mi.setActionCommand("-");
230:
231: arrows = new Panel(new GridLayout(2, 1));
232: arrows.add(pl);
233: arrows.add(mi);
234: p = new Panel(new BorderLayout());
235: p.add(s, "Center");
236: p.add(arrows, "East");
237: time.add(p);
238:
239: // add the "day" panel
240: d = new Label();
241: d.setAlignment(Label.CENTER);
242: dae = new DateActionListener(Calendar.DAY_OF_MONTH);
243:
244: pl = new ImageButton(up);
245: pl.addActionListener(dae);
246: pl.setActionCommand("+");
247:
248: mi = new ImageButton(down);
249: mi.addActionListener(dae);
250: mi.setActionCommand("-");
251:
252: arrows = new Panel(new GridLayout(2, 1));
253: arrows.add(pl);
254: arrows.add(mi);
255: p = new Panel(new BorderLayout());
256: p.add(d, "Center");
257: p.add(arrows, "East");
258: date.add(p);
259:
260: // then the "Month" panel
261: m = new Label();
262: m.setAlignment(Label.CENTER);
263: dae = new DateActionListener(Calendar.MONTH);
264:
265: pl = new ImageButton(up);
266: pl.addActionListener(dae);
267: pl.setActionCommand("+");
268:
269: mi = new ImageButton(down);
270: mi.addActionListener(dae);
271: mi.setActionCommand("-");
272:
273: arrows = new Panel(new GridLayout(2, 1));
274: arrows.add(pl);
275: arrows.add(mi);
276: p = new Panel(new BorderLayout());
277: p.add(m, "Center");
278: p.add(arrows, "East");
279: date.add(p);
280:
281: // then the "Year" panel
282: y = new Label();
283: y.setAlignment(Label.CENTER);
284: dae = new DateActionListener(Calendar.YEAR);
285:
286: pl = new ImageButton(up);
287: pl.addActionListener(dae);
288: pl.setActionCommand("+");
289:
290: mi = new ImageButton(down);
291: mi.addActionListener(dae);
292: mi.setActionCommand("-");
293:
294: arrows = new Panel(new GridLayout(2, 1));
295: arrows.add(pl);
296: arrows.add(mi);
297: p = new Panel(new BorderLayout());
298: p.add(y, "Center");
299: p.add(arrows, "East");
300: date.add(p);
301:
302: widget.add(time);
303: widget.add(date);
304:
305: c = Calendar.getInstance();
306: }
307:
308: /**
309: * Initialize the editor
310: * @param w the ResourceWrapper father of the attribute
311: * @param a the Attribute we are editing
312: * @param o the value of the above attribute
313: * @param p some Properties, used to fine-tune the editor
314: * @exception RemoteAccessException if a remote access error occurs.
315: */
316:
317: public void initialize(RemoteResourceWrapper w, Attribute a,
318: Object o, Properties p) throws RemoteAccessException {
319: RemoteResource r = w.getResource();
320: if (o == null) {
321: Date d = null;
322: try {
323: d = new Date(((Long) r.getValue(a.getName()))
324: .longValue());
325:
326: if (d == null)
327: if (a.getDefault() != null)
328: d = new Date(((Long) a.getDefault())
329: .longValue());
330: } catch (Exception ex) {
331: // a fancy error?
332: }
333: if (d != null) {
334: origd = d;
335: c.setTime(d);
336: updateFields();
337: } else {
338: origd = new Date();
339: }
340: currd = origd;
341: } else {
342: if (o instanceof Long) {
343: origd = new Date(((Long) o).longValue());
344: c.setTime(origd);
345: }
346: }
347: updateFields();
348: currd = origd;
349: }
350: }
|