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.struct.StructureElement;
027: import jacareto.system.Environment;
028: import jacareto.test.JTextComponentTest;
029: import jacareto.toolkit.event.TextValueListener;
030:
031: import org.apache.regexp.RE;
032: import org.apache.regexp.RESyntaxException;
033:
034: import java.awt.event.ActionEvent;
035: import java.awt.event.ActionListener;
036:
037: import javax.swing.JButton;
038: import javax.swing.JCheckBox;
039: import javax.swing.JLabel;
040: import javax.swing.JTextArea;
041: import javax.swing.event.DocumentEvent;
042:
043: /**
044: * This class is the editor class for <code>jacareto.test.JTextComponentTest</code> tests.
045: *
046: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
047: * @version 1.0
048: */
049: public class JTextComponentTestEditor extends JComponentTestEditor {
050: /** The regexp checkbox. */
051: protected JCheckBox chkRegExp;
052:
053: /** The target text area. */
054: protected JTextArea targetTextArea;
055:
056: /** The button for the regexp syntax check. */
057: protected JButton regexpCheckButton;
058:
059: /**
060: * Creates an editor with the specified env.
061: *
062: * @param env the environment
063: */
064: public JTextComponentTestEditor(Environment env) {
065: super (env);
066:
067: // The target text field label
068: c.gridx = 0;
069: c.gridy++;
070: c.gridwidth = 2;
071:
072: JLabel targetTextFieldLabel = new JLabel(language
073: .getString("Tests.JTextComponentTest.Text")
074: + ":");
075: editorPanel.add(targetTextFieldLabel, c);
076:
077: // The target text field.
078: c.gridx = 0;
079: c.gridy++;
080: c.gridwidth = 2;
081: targetTextArea = new JTextArea("", 4, 40);
082: targetTextArea.getDocument().addDocumentListener(
083: new TextValueListener() {
084: public void textValueChanged(DocumentEvent e) {
085: if (isUpdateOnChange && (getElement() != null)) {
086: ((JTextComponentTest) getElement())
087: .setTargetText(targetTextArea
088: .getText());
089: }
090: }
091: });
092: editorPanel.add(targetTextArea, c);
093: targetTextFieldLabel.setLabelFor(targetTextArea);
094:
095: // The regexp checkbox
096: c.gridx = 0;
097: c.gridy++;
098: c.gridwidth = 2;
099: chkRegExp = new JCheckBox(language
100: .getString("Tests.JTextComponentTest.IsRegExp"));
101: chkRegExp.addActionListener(new ActionListener() {
102: public void actionPerformed(ActionEvent e) {
103: if (isUpdateOnChange) {
104: boolean isRegexp = chkRegExp.isSelected();
105: ((JTextComponentTest) getElement())
106: .setRegExp(isRegexp);
107: regexpCheckButton.setEnabled(isRegexp);
108:
109: if (isRegexp) {
110: chkCorrect.setSelected(false);
111: }
112:
113: chkCorrect.setEnabled(!isRegexp);
114: }
115: }
116: });
117: editorPanel.add(chkRegExp, c);
118:
119: // The regexp syntax check button
120: c.gridx = 0;
121: c.gridy++;
122: c.gridwidth = 2;
123: regexpCheckButton = new JButton(
124: language
125: .getString("Editors.JTextComponentTestEditor.RegExpSyntaxCheck"));
126: editorPanel.add(regexpCheckButton, c);
127: regexpCheckButton.addActionListener(new ActionListener() {
128: public void actionPerformed(ActionEvent e) {
129: try {
130: new RE(targetTextArea.getText());
131: logger
132: .info(language
133: .getString("Editors.JTextComponentTestEditor.Msg.RegExpOk"));
134: } catch (RESyntaxException ex) {
135: logger
136: .info(language
137: .getString("Editors.JTextComponentTestEditor.Msg.RegExpError")
138: + "\n" + ex.getMessage());
139: }
140: }
141: });
142: }
143:
144: /**
145: * @see jacareto.editor.Editor#handlesElement(jacareto.struct.StructureElement)
146: */
147: public boolean handlesElement(StructureElement element) {
148: return (element != null)
149: && (element instanceof JTextComponentTest);
150: }
151:
152: /**
153: * @see jacareto.editor.Editor#setElement(jacareto.struct.StructureElement)
154: */
155: public void setElement(StructureElement element) {
156: super .setElement(element);
157:
158: JTextComponentTest test = (JTextComponentTest) element;
159: boolean isRegexp = test.isRegExp();
160: chkRegExp.setSelected(isRegexp);
161: regexpCheckButton.setEnabled(isRegexp);
162:
163: if (isRegexp) {
164: chkCorrect.setSelected(false);
165: }
166:
167: chkCorrect.setEnabled(!isRegexp);
168: targetTextArea.setText(test.getTargetText());
169: }
170: }
|