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.util.Collection;
022: import java.util.Iterator;
023:
024: import com.jeta.forms.gui.form.FormComponent;
025: import com.jeta.forms.gui.form.GridComponent;
026: import com.jeta.open.registry.JETARegistry;
027: import com.jeta.swingbuilder.gui.editor.FormEditor;
028: import com.jeta.swingbuilder.gui.formmgr.EditorManager;
029: import com.jeta.swingbuilder.gui.formmgr.FormManagerDesignUtils;
030: import com.jeta.swingbuilder.gui.undo.EditorUndoManager;
031:
032: public class CommandUtils {
033: /**
034: * Safely returns the classname of the bean delegate associated with this
035: * grid component. Null is returned if there is no delegate associated with
036: * the component.
037: */
038: public static String getBeanDelegate(GridComponent gc) {
039: if (gc == null)
040: return "null";
041:
042: Object comp = gc.getBeanDelegate();
043: if (comp == null)
044: return "null";
045:
046: return comp.getClass().getName();
047: }
048:
049: /**
050: * Invokes an action for the first time
051: */
052: public static void invoke(FormUndoableEdit edit, FormEditor editor) {
053: if (editor == null)
054: return;
055:
056: /** don't allow edits to top level form */
057: FormComponent fc = edit.getForm();
058: if (fc != null && fc.isTopLevelForm())
059: return;
060:
061: /**
062: * Special case for SetPropertyCommand because we can get repeated
063: * commands for the same property value given the way the property
064: * editor works.
065: */
066: if (edit instanceof SetPropertyCommand) {
067: EditorUndoManager undomgr = editor.getUndoManager();
068: if (edit.equals(undomgr.editToBeUndone()))
069: return;
070: }
071:
072: edit.redo();
073: editor.editHappened(edit);
074:
075: /**
076: * Now, post the edit to all other editors that contain the form.
077: */
078: EditorManager emgr = (EditorManager) JETARegistry
079: .lookup(EditorManager.COMPONENT_ID);
080: Collection editors = emgr.getEditors();
081: Iterator iter = editors.iterator();
082: while (iter.hasNext()) {
083: FormEditor other_editor = (FormEditor) iter.next();
084: if (other_editor != editor) {
085: if (FormManagerDesignUtils.containsForm(other_editor
086: .getTopParent(), edit.getFormId())) {
087: other_editor.editHappened(edit);
088: }
089: }
090: }
091: emgr.updateModifiedStatus();
092: editor.repaint();
093: }
094:
095: /**
096: * Redoes and edit for the given form. If the redo is successful, it is
097: * propagated to all other editors that have the same form.
098: */
099: public static FormUndoableEdit redoEdit(FormEditor editor) {
100: if (editor == null)
101: return null;
102:
103: EditorUndoManager undomgr = editor.getUndoManager();
104: if (undomgr.canRedo()) {
105: FormUndoableEdit edit = (FormUndoableEdit) undomgr
106: .editToBeRedone();
107: undomgr.redo();
108:
109: /**
110: * Now, post the edit to all other editors that contain the form.
111: */
112: EditorManager emgr = (EditorManager) JETARegistry
113: .lookup(EditorManager.COMPONENT_ID);
114: Collection editors = emgr.getEditors();
115: Iterator iter = editors.iterator();
116: while (iter.hasNext()) {
117: FormEditor other_editor = (FormEditor) iter.next();
118: if (other_editor != editor) {
119:
120: undomgr = other_editor.getUndoManager();
121: undomgr.redoHappened(editor, edit);
122: }
123: }
124: emgr.updateModifiedStatus();
125: return edit;
126: }
127: return null;
128: }
129:
130: /**
131: * Undoes and edit for the given form. If the undo is successful, it is
132: * propagated to all other editors that have the same form.
133: */
134: public static FormUndoableEdit undoEdit(FormEditor editor) {
135: if (editor == null)
136: return null;
137:
138: EditorUndoManager undomgr = editor.getUndoManager();
139: if (undomgr.canUndo()) {
140: FormUndoableEdit edit = (FormUndoableEdit) undomgr
141: .editToBeUndone();
142: undomgr.undo();
143:
144: /**
145: * Now, post the edit to all other editors that contain the form so
146: * they can add their undo managers can mark this edit as undone.
147: */
148: EditorManager emgr = (EditorManager) JETARegistry
149: .lookup(EditorManager.COMPONENT_ID);
150: Collection editors = emgr.getEditors();
151: Iterator iter = editors.iterator();
152: while (iter.hasNext()) {
153: FormEditor other_editor = (FormEditor) iter.next();
154: if (other_editor != editor) {
155:
156: undomgr = other_editor.getUndoManager();
157: undomgr.undoHappened(editor, edit);
158: }
159: }
160: emgr.updateModifiedStatus();
161: return edit;
162: }
163: return null;
164: }
165: }
|