001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.editor;
025:
026: import jacareto.record.CalendarRecordable;
027: import jacareto.struct.StructureElement;
028: import jacareto.system.Environment;
029: import jacareto.system.Language;
030: import jacareto.toolkit.event.TextValueListener;
031: import jacareto.toolkit.swing.IntegerTextField;
032:
033: import java.awt.Component;
034: import java.awt.GridBagConstraints;
035: import java.awt.GridBagLayout;
036: import java.awt.Insets;
037: import java.awt.event.ActionEvent;
038: import java.awt.event.ActionListener;
039:
040: import java.util.GregorianCalendar;
041:
042: import javax.swing.JButton;
043: import javax.swing.JLabel;
044: import javax.swing.JPanel;
045: import javax.swing.event.DocumentEvent;
046:
047: /**
048: * An editor for calendar recordables.
049: *
050: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
051: * @version 1.01
052: */
053: public class CalendarRecordableEditor extends Editor {
054: /** The element to edit. */
055: private StructureElement element;
056:
057: /** The editor's component. */
058: private JPanel editorPanel;
059:
060: /** The text field for the year. */
061: private IntegerTextField yearField;
062:
063: /** The text field for the month. */
064: private IntegerTextField monthField;
065:
066: /** The text field for the date. */
067: private IntegerTextField dateField;
068:
069: /** The text field for the "hour of day". */
070: private IntegerTextField hourOfDayField;
071:
072: /** The text field for the minute. */
073: private IntegerTextField minuteField;
074:
075: /** The text field for the second. */
076: private IntegerTextField secondField;
077:
078: /** Button for setting to actual time and date. */
079: private JButton actualTimeAndDateButton;
080:
081: /**
082: * Create a new calendar recordable editor.
083: *
084: * @param env the environment
085: */
086: public CalendarRecordableEditor(Environment env) {
087: super (env);
088:
089: Language language = getLanguage();
090:
091: editorPanel = new JPanel();
092: editorPanel.setLayout(new GridBagLayout());
093:
094: // The year field label
095: GridBagConstraints c = new GridBagConstraints();
096: c.gridx = 0;
097: c.gridy = 0;
098: c.weightx = 30;
099:
100: //c.ipady = 3;
101: //c.ipadx = 5;
102: c.insets = new Insets(5, 5, 5, 5);
103: c.anchor = GridBagConstraints.WEST;
104:
105: JLabel yearFieldLabel = new JLabel(language
106: .getString("General.Calendar.Year")
107: + ":");
108: editorPanel.add(yearFieldLabel, c);
109:
110: // The year field
111: c.gridx = 1;
112: c.weightx = 60;
113: yearField = new IntegerTextField(4);
114: yearField.getDocument().addDocumentListener(
115: new TextValueListener() {
116: public void textValueChanged(DocumentEvent e) {
117: if (isUpdateOnChange && (getElement() != null)) {
118: ((CalendarRecordable) getElement())
119: .setYear(yearField.getValue());
120: }
121: }
122: });
123: editorPanel.add(yearField, c);
124: yearFieldLabel.setLabelFor(yearField);
125:
126: // The month field label
127: c.gridx = 0;
128: c.gridy = 1;
129:
130: JLabel monthFieldLabel = new JLabel(language
131: .getString("General.Calendar.Month")
132: + ":");
133: editorPanel.add(monthFieldLabel, c);
134:
135: // The month field
136: c.gridx = 1;
137: monthField = new IntegerTextField(2);
138: monthField.getDocument().addDocumentListener(
139: new TextValueListener() {
140: public void textValueChanged(DocumentEvent e) {
141: if (isUpdateOnChange && (getElement() != null)) {
142: ((CalendarRecordable) getElement())
143: .setMonth(monthField.getValue() - 1);
144: }
145: }
146: });
147: editorPanel.add(monthField, c);
148: monthFieldLabel.setLabelFor(monthField);
149:
150: // The date field label
151: c.gridx = 0;
152: c.gridy = 2;
153:
154: JLabel dateFieldLabel = new JLabel(language
155: .getString("General.Calendar.Day")
156: + ":");
157: editorPanel.add(dateFieldLabel, c);
158:
159: // The date field
160: c.gridx = 1;
161: dateField = new IntegerTextField(2);
162: dateField.getDocument().addDocumentListener(
163: new TextValueListener() {
164: public void textValueChanged(DocumentEvent e) {
165: if (isUpdateOnChange && (getElement() != null)) {
166: ((CalendarRecordable) getElement())
167: .setDate(dateField.getValue());
168: }
169: }
170: });
171: editorPanel.add(dateField, c);
172: dateFieldLabel.setLabelFor(dateField);
173:
174: // The hour of day field label
175: c.gridx = 0;
176: c.gridy = 3;
177:
178: JLabel hourOfDayFieldLabel = new JLabel(language
179: .getString("General.Calendar.Hour")
180: + ":");
181: editorPanel.add(hourOfDayFieldLabel, c);
182:
183: // The hour of day field
184: c.gridx = 1;
185: hourOfDayField = new IntegerTextField(2);
186: hourOfDayField.getDocument().addDocumentListener(
187: new TextValueListener() {
188: public void textValueChanged(DocumentEvent e) {
189: if (isUpdateOnChange && (getElement() != null)) {
190: ((CalendarRecordable) getElement())
191: .setHourOfDay(hourOfDayField
192: .getValue());
193: }
194: }
195: });
196: editorPanel.add(hourOfDayField, c);
197: hourOfDayFieldLabel.setLabelFor(hourOfDayField);
198:
199: // The minute field label
200: c.gridx = 0;
201: c.gridy = 4;
202:
203: JLabel minuteFieldLabel = new JLabel(language
204: .getString("General.Calendar.Min")
205: + ":");
206: editorPanel.add(minuteFieldLabel, c);
207:
208: // The minute field
209: c.gridx = 1;
210: minuteField = new IntegerTextField(2);
211: minuteField.getDocument().addDocumentListener(
212: new TextValueListener() {
213: public void textValueChanged(DocumentEvent e) {
214: if (isUpdateOnChange && (getElement() != null)) {
215: ((CalendarRecordable) getElement())
216: .setMinute(minuteField.getValue());
217: }
218: }
219: });
220: editorPanel.add(minuteField, c);
221: minuteFieldLabel.setLabelFor(minuteField);
222:
223: // The second field label
224: c.gridx = 0;
225: c.gridy = 5;
226:
227: JLabel secondFieldLabel = new JLabel(language
228: .getString("General.Calendar.Sec")
229: + ":");
230: editorPanel.add(secondFieldLabel, c);
231:
232: // The second field
233: c.gridx = 1;
234: secondField = new IntegerTextField(2);
235: secondField.getDocument().addDocumentListener(
236: new TextValueListener() {
237: public void textValueChanged(DocumentEvent e) {
238: if (isUpdateOnChange && (getElement() != null)) {
239: ((CalendarRecordable) getElement())
240: .setSecond(secondField.getValue());
241: }
242: }
243: });
244: editorPanel.add(secondField, c);
245: secondFieldLabel.setLabelFor(secondField);
246:
247: //The actual time and date button
248: c.gridx = 0;
249: c.gridy = 6;
250: c.gridwidth = 2;
251: actualTimeAndDateButton = new JButton(
252: language
253: .getString("Editors.CalendarRecordableEditor.SetActual"));
254: actualTimeAndDateButton.addActionListener(new ActionListener() {
255: public void actionPerformed(ActionEvent e) {
256: if (isUpdateOnChange && (getElement() != null)) {
257: ((CalendarRecordable) getElement())
258: .setCalendar(new GregorianCalendar());
259: setElement(getElement());
260: }
261: }
262: });
263: editorPanel.add(actualTimeAndDateButton, c);
264: }
265:
266: /**
267: * Returns whether this editor is responsible for a given structure element. This editor is
268: * responsible for calendar recordables.
269: *
270: * @param element the structure element
271: *
272: * @return <code>true</code> if <i>element</i> is a calendar recordable and not
273: * <code>null</code>, otherwise <code>false</code>
274: */
275: public boolean handlesElement(StructureElement element) {
276: return (element != null)
277: && (element instanceof CalendarRecordable);
278: }
279:
280: /**
281: * Returns the actual structure element to be edited.
282: *
283: * @return DOCUMENT ME!
284: */
285: public StructureElement getElement() {
286: return element;
287: }
288:
289: /**
290: * Sets the structure element to edit.
291: *
292: * @param element DOCUMENT ME!
293: */
294: public void setElement(StructureElement element) {
295: this .element = element;
296:
297: CalendarRecordable cRecordable = (CalendarRecordable) element;
298: yearField.setValue(cRecordable.getYear());
299: monthField.setValue(cRecordable.getMonth() + 1);
300: dateField.setValue(cRecordable.getDate());
301: hourOfDayField.setValue(cRecordable.getHourOfDay());
302: minuteField.setValue(cRecordable.getMinute());
303: secondField.setValue(cRecordable.getSecond());
304: }
305:
306: /**
307: * Returns the component of the editor.
308: *
309: * @return the editor component
310: */
311: public Component getComponent() {
312: return editorPanel;
313: }
314: }
|