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