001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.commands;
020:
021: import java.awt.Component;
022:
023: import javax.swing.text.JTextComponent;
024: import javax.swing.undo.CannotRedoException;
025: import javax.swing.undo.CannotUndoException;
026:
027: import com.jeta.forms.gui.form.FormComponent;
028: import com.jeta.forms.gui.form.GridCellEvent;
029: import com.jeta.forms.gui.form.GridComponent;
030: import com.jeta.forms.gui.form.StandardComponent;
031: import com.jeta.open.gui.framework.JETADialog;
032: import com.jeta.open.gui.utils.JETAToolbox;
033: import com.jeta.open.i18n.I18N;
034: import com.jeta.swingbuilder.gui.components.text.TextPropertyView;
035: import com.jeta.swingbuilder.gui.editor.FormEditor;
036:
037: /**
038: * Command that edits the text property of a component if that component has a
039: * text property.
040: *
041: * @author Jeff Tassin
042: */
043: public class EditTextPropertyCommand extends FormUndoableEdit {
044: /**
045: * The component we are editing.
046: */
047: private GridComponent m_comp;
048:
049: /**
050: * The old text value
051: */
052: private String m_old_value;
053:
054: /**
055: * The new value the user typed.
056: */
057: private String m_new_value;
058:
059: /**
060: * Adds a new component to a form.
061: */
062: public EditTextPropertyCommand(FormComponent fc, GridComponent gc,
063: String newValue, String oldValue) {
064: super (fc);
065: m_comp = gc;
066: m_new_value = newValue;
067: m_old_value = oldValue;
068: }
069:
070: public static void tryEditTextProperty(FormEditor editor,
071: FormComponent fc, GridComponent gc) {
072: if (editor != null && fc != null && gc != null) {
073: try {
074: Component comp = gc.getBeanDelegate();
075: if (comp != null) {
076: Class c = comp.getClass();
077: Class[] setparams = new Class[] { String.class };
078: Class[] getparams = new Class[0];
079: java.lang.reflect.Method setm = c.getMethod(
080: "setText", setparams);
081: java.lang.reflect.Method getm = c.getMethod(
082: "getText", getparams);
083:
084: Object[] getvalues = new Object[0];
085: String oldvalue = (String) getm.invoke(comp,
086: getvalues);
087:
088: String newvalue = null;
089: if (comp instanceof JTextComponent) {
090: TextPropertyView view = new TextPropertyView(
091: oldvalue);
092: JETADialog dlg = (JETADialog) JETAToolbox
093: .createDialog(JETADialog.class, editor,
094: true);
095: dlg.setPrimaryPanel(view);
096: dlg
097: .setTitle(I18N
098: .getLocalizedMessage("Set Text Property"));
099: dlg
100: .setInitialFocusComponent((javax.swing.JComponent) view
101: .getComponentByName(TextPropertyView.ID_TEXT_AREA));
102: dlg.setSize(dlg.getPreferredSize());
103:
104: /**
105: * override the ENTER key handling for the dialog. We
106: * need to do this because the ENTER key is bound to
107: * setting the Text property. However, this key also
108: * closes a dialog. The dialog can sometimes get the
109: * ENTER event for editing the text and close
110: * prematurely
111: */
112: javax.swing.KeyStroke ks = javax.swing.KeyStroke
113: .getKeyStroke(
114: java.awt.event.KeyEvent.VK_ENTER,
115: 0, true);
116: dlg.getRootPane().getInputMap().put(ks,
117: "EnterAction");
118: dlg.getRootPane().getActionMap().put(
119: "EnterAction",
120: new javax.swing.AbstractAction() {
121: public void actionPerformed(
122: java.awt.event.ActionEvent ae) {
123: // ignore.
124: }
125: });
126:
127: dlg.showCenter();
128: if (dlg.isOk())
129: newvalue = view.getText();
130: } else {
131: newvalue = javax.swing.JOptionPane
132: .showInputDialog(
133: editor,
134: I18N
135: .getLocalizedMessage("Set Text Property"),
136: oldvalue);
137: }
138:
139: if (newvalue != null) {
140: // if we are here, then the component has both set and
141: // get methods, so
142: // let's pass a valid command
143: EditTextPropertyCommand cmd = new EditTextPropertyCommand(
144: fc, gc, newvalue, oldvalue);
145: CommandUtils.invoke(cmd, editor);
146: }
147: }
148: } catch (Exception e) {
149: // ignore
150: }
151: }
152: }
153:
154: /**
155: * UndoableEdit implementation Override should begin with a call to super.
156: */
157: public void redo() throws CannotRedoException {
158: super .redo();
159: try {
160: if (m_comp instanceof StandardComponent) {
161: Component comp = m_comp.getBeanDelegate();
162: if (comp != null) {
163: Class c = comp.getClass();
164: Class[] setparams = new Class[] { String.class };
165: java.lang.reflect.Method setm = c.getMethod(
166: "setText", setparams);
167: Object[] setvalues = new Object[] { m_new_value };
168: setm.invoke(comp, setvalues);
169: m_comp.fireGridCellEvent(new GridCellEvent(
170: GridCellEvent.CELL_CHANGED, m_comp));
171: if (comp instanceof JTextComponent)
172: ((JTextComponent) comp).setCaretPosition(0);
173: }
174: }
175: } catch (Exception e) {
176:
177: }
178: }
179:
180: /**
181: * UndoableEdit implementation Override should begin with a call to super.
182: */
183: public void undo() throws CannotUndoException {
184: super .undo();
185:
186: try {
187: if (m_comp instanceof StandardComponent) {
188: Component comp = m_comp.getBeanDelegate();
189: if (comp != null) {
190: Class c = comp.getClass();
191: Class[] setparams = new Class[] { String.class };
192: java.lang.reflect.Method setm = c.getMethod(
193: "setText", setparams);
194: Object[] setvalues = new Object[] { m_old_value };
195: setm.invoke(comp, setvalues);
196: m_comp.fireGridCellEvent(new GridCellEvent(
197: GridCellEvent.CELL_CHANGED, m_comp));
198: }
199: }
200: } catch (Exception e) {
201: // ignore
202: }
203: }
204:
205: public String toString() {
206: return "EditTextPropertyCommand ";
207: }
208:
209: }
|