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 javax.swing.undo.CannotRedoException;
022:
023: import com.jeta.forms.gui.components.ComponentSource;
024: import com.jeta.forms.gui.components.EmptyComponentFactory;
025: import com.jeta.forms.gui.form.FormComponent;
026: import com.jeta.forms.gui.form.GridComponent;
027: import com.jeta.forms.gui.form.StandardComponent;
028: import com.jeta.open.registry.JETARegistry;
029: import com.jeta.swingbuilder.gui.formmgr.AbstractFormManager;
030: import com.jgoodies.forms.layout.CellConstraints;
031:
032: /**
033: * Command that deletes a component from a view
034: *
035: * @author Jeff Tassin
036: */
037: public class DeleteComponentCommand extends FormUndoableEdit {
038: /**
039: * The grid component to 'delete'
040: */
041: private GridComponent m_gc;
042:
043: /**
044: * The constaints assigned to the component.
045: */
046: private CellConstraints m_comp_constraints;
047:
048: /**
049: * Default constraints with a width/height = 1
050: */
051: private CellConstraints m_default_constraints;
052:
053: /**
054: * An empty component we use to replace the 'deleted' grid component
055: */
056: private GridComponent m_empty;
057:
058: /**
059: * ctor
060: */
061: public DeleteComponentCommand(GridComponent gc,
062: ComponentSource compsrc) {
063: super (gc.getParentView().getParentForm());
064:
065: m_gc = gc;
066: assert (getView() != null);
067:
068: m_comp_constraints = m_gc.getConstraints()
069: .createCellConstraints();
070: m_default_constraints = new CellConstraints(m_gc.getColumn(),
071: m_gc.getRow());
072:
073: try {
074: EmptyComponentFactory factory = new EmptyComponentFactory(
075: compsrc);
076: m_empty = (StandardComponent) factory.createComponent(
077: "empty", getView());
078: } catch (Exception e) {
079: e.printStackTrace();
080: }
081: }
082:
083: /**
084: * UndoableEdit implementation Override should begin with a call to super.
085: */
086: public void redo() throws CannotRedoException {
087: super .redo();
088: if (m_empty != null) {
089: getView().replaceComponent(m_empty, m_gc);
090: getView().setConstraints(m_empty, m_default_constraints);
091: }
092: }
093:
094: /**
095: * UndoableEdit implementation Override should begin with a call to super.
096: */
097: public void undo() throws CannotRedoException {
098: super .undo();
099: if (m_empty != null) {
100: if (m_gc instanceof FormComponent) {
101: // must be re-registered
102: AbstractFormManager fmgr = (AbstractFormManager) JETARegistry
103: .lookup(AbstractFormManager.COMPONENT_ID);
104: fmgr.registerForm((FormComponent) m_gc);
105: }
106:
107: getView().replaceComponent(m_gc, m_empty);
108: getView().setConstraints(m_gc, m_comp_constraints);
109: }
110: }
111:
112: public String toString() {
113: return "DeleteComponentCommand "
114: + CommandUtils.getBeanDelegate(m_gc);
115: }
116:
117: }
|