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.Container;
022:
023: import javax.swing.JComponent;
024: import javax.swing.undo.CannotRedoException;
025: import javax.swing.undo.CannotUndoException;
026:
027: import com.jeta.forms.gui.components.ComponentSource;
028: import com.jeta.forms.gui.form.FormComponent;
029: import com.jeta.forms.gui.form.GridComponent;
030: import com.jeta.swingbuilder.gui.editor.FormEditor;
031:
032: /**
033: * Command that moves a component from one location and replaces a component at
034: * another location. Keep in mind that every cell in the designer view has a
035: * component, so you would never have a case where your are moving to an empty
036: * cell. Rather you would move and replace an EmptyComponent in that cell.
037: *
038: * @author Jeff Tassin
039: */
040: public class MoveComponentCommand extends FormUndoableEdit {
041: /**
042: * A move command is really a Delete followed by a ReplaceCommand
043: */
044: private DeleteComponentCommand m_delete_cmd;
045:
046: private ReplaceComponentCommand m_replace_cmd;
047:
048: private SetConstraintsCommand m_constraints_cmd;
049:
050: /**
051: * Non null if the target is an EmptyComponent.
052: */
053: private GridComponent m_empty_comp;
054:
055: private FormComponent m_destform;
056:
057: private FormEditor m_editor;
058:
059: /**
060: * ctor
061: */
062: public MoveComponentCommand(FormComponent destForm,
063: GridComponent destCell, FormComponent sourceForm,
064: GridComponent sourceCell, ComponentSource compSrc) {
065: super (sourceForm);
066: try {
067: m_destform = destForm;
068: m_editor = FormEditor.getEditor(destForm);
069: m_delete_cmd = new DeleteComponentCommand(sourceCell,
070: compSrc);
071: m_replace_cmd = new ReplaceComponentCommand(sourceCell,
072: destCell, destForm);
073: } catch (Exception e) {
074:
075: }
076: }
077:
078: /**
079: * UndoableEdit implementation Override should begin with a call to super.
080: */
081: public void redo() throws CannotRedoException {
082: super .redo();
083: if (m_delete_cmd != null) {
084: m_delete_cmd.redo();
085: m_replace_cmd.redo();
086: /**
087: * We need to update the entire container hierarchy because it seems
088: * that nested forms can get confused when the delete and replace
089: * commands directly follow one another. This can be tested by
090: * running the replace command in a SwingUtilities.invokeLater
091: * event. It runs fine, but the screen has an annoying flicker.
092: * Walking up the container hierarchy and calling revalidate seems
093: * to fix the problem.
094: */
095: Container c = m_destform.getParent();
096: while (true) {
097: if (c instanceof JComponent)
098: ((JComponent) c).revalidate();
099:
100: if (c instanceof FormEditor)
101: break;
102:
103: c = c.getParent();
104: if (c == null || c instanceof java.awt.Window)
105: break;
106: }
107: }
108: }
109:
110: /**
111: * UndoableEdit implementation Override should begin with a call to super.
112: */
113: public void undo() throws CannotUndoException {
114: super .undo();
115: if (m_delete_cmd != null) {
116: m_replace_cmd.undo();
117: m_delete_cmd.undo();
118: }
119: }
120:
121: public String toString() {
122: return "MoveComponentCommand ";
123: }
124:
125: }
|