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.UserRecordable;
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 javax.swing.JComboBox;
041: import javax.swing.JLabel;
042: import javax.swing.JPanel;
043: import javax.swing.JTextField;
044: import javax.swing.event.DocumentEvent;
045:
046: /**
047: * An editor for user recordables.
048: *
049: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
050: * @version 1.0
051: */
052: public class UserRecordableEditor extends Editor {
053: /** The structure element to edit. */
054: private StructureElement element;
055:
056: /** The editor's component. */
057: private JPanel editorPanel;
058:
059: /** The text field for the name. */
060: private JTextField nameField;
061:
062: /** The text field for the age. */
063: private IntegerTextField ageField;
064:
065: /** The combo box for the sex. */
066: private JComboBox sexBox;
067:
068: /**
069: * Create a new user recordable editor.
070: *
071: * @param env the environment
072: */
073: public UserRecordableEditor(Environment env) {
074: super (env);
075:
076: Language language = getLanguage();
077:
078: editorPanel = new JPanel();
079: editorPanel.setLayout(new GridBagLayout());
080:
081: // The name field label
082: GridBagConstraints c = new GridBagConstraints();
083: c.gridx = 0;
084: c.gridy = 0;
085: c.weightx = 30;
086:
087: //c.ipady = 3;
088: //c.ipadx = 5;
089: c.insets = new Insets(5, 5, 5, 5);
090: c.anchor = GridBagConstraints.WEST;
091:
092: JLabel nameFieldLabel = new JLabel(language
093: .getString("General.User.Name")
094: + ":");
095: editorPanel.add(nameFieldLabel, c);
096:
097: // The name field
098: c.gridx = 1;
099: c.weightx = 60;
100: nameField = new JTextField(20);
101: nameField.getDocument().addDocumentListener(
102: new TextValueListener() {
103: public void textValueChanged(DocumentEvent e) {
104: if (isUpdateOnChange && (getElement() != null)) {
105: ((UserRecordable) getElement())
106: .setName(nameField.getText());
107: }
108: }
109: });
110: editorPanel.add(nameField, c);
111: nameFieldLabel.setLabelFor(nameField);
112:
113: // The age field label
114: c.gridx = 0;
115: c.gridy = 1;
116:
117: JLabel ageFieldLabel = new JLabel(language
118: .getString("General.User.Age")
119: + ":");
120: editorPanel.add(ageFieldLabel, c);
121:
122: // The age field
123: c.gridx = 1;
124: ageField = new IntegerTextField(3);
125: ageField.getDocument().addDocumentListener(
126: new TextValueListener() {
127: public void textValueChanged(DocumentEvent e) {
128: if (isUpdateOnChange && (getElement() != null)) {
129: ((UserRecordable) getElement())
130: .setAge(ageField.getValue());
131: }
132: }
133: });
134: editorPanel.add(ageField, c);
135: ageFieldLabel.setLabelFor(ageField);
136:
137: // The is popup trigger box label
138: c.gridx = 0;
139: c.gridy = 2;
140:
141: JLabel sexBoxLabel = new JLabel(language
142: .getString("General.User.Sex")
143: + ":");
144: editorPanel.add(sexBoxLabel, c);
145:
146: // The sex box
147: c.gridx = 1;
148: sexBox = new JComboBox();
149: sexBox.addItem(language.getString("General.User.Female"));
150: sexBox.addItem(language.getString("General.User.Male"));
151: sexBox.addActionListener(new ActionListener() {
152: public void actionPerformed(ActionEvent e) {
153: if (isUpdateOnChange && (getElement() != null)) {
154: if (sexBox.getSelectedIndex() == 0) {
155: ((UserRecordable) getElement())
156: .setSex(UserRecordable.FEMALE);
157: } else {
158: ((UserRecordable) getElement())
159: .setSex(UserRecordable.MALE);
160: }
161: }
162: }
163: });
164: editorPanel.add(sexBox, c);
165: sexBoxLabel.setLabelFor(sexBox);
166: }
167:
168: /**
169: * Returns whether this editor is responsible for a given structure element. This editor is
170: * responsible for user recordables.
171: *
172: * @param element the structure element
173: *
174: * @return <code>true</code> if <i>element</i> is an user recordable and not <code>null</code>,
175: * otherwise <code>false</code>
176: */
177: public boolean handlesElement(StructureElement element) {
178: return (element != null) && (element instanceof UserRecordable);
179: }
180:
181: /**
182: * Returns the actual structure element to be edited.
183: *
184: * @return DOCUMENT ME!
185: */
186: public StructureElement getElement() {
187: return element;
188: }
189:
190: /**
191: * Sets the structure element to edit.
192: *
193: * @param element DOCUMENT ME!
194: */
195: public void setElement(StructureElement element) {
196: this .element = element;
197:
198: UserRecordable uRecordable = (UserRecordable) element;
199: nameField.setText(uRecordable.getName());
200: ageField.setValue(uRecordable.getAge());
201:
202: if (uRecordable.getSex() == UserRecordable.FEMALE) {
203: sexBox.setSelectedIndex(0);
204: } else {
205: sexBox.setSelectedIndex(1);
206: }
207: }
208:
209: /**
210: * Returns the component of the editor.
211: *
212: * @return the editor component
213: */
214: public Component getComponent() {
215: return editorPanel;
216: }
217: }
|