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.Iterator;
022: import java.util.LinkedList;
023:
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.components.EmptyComponentFactory;
029: import com.jeta.forms.gui.form.ComponentInfo;
030: import com.jeta.forms.gui.form.FormComponent;
031: import com.jeta.forms.gui.form.GridComponent;
032: import com.jeta.forms.gui.form.GridView;
033: import com.jeta.forms.store.memento.FormGroupSet;
034: import com.jgoodies.forms.layout.CellConstraints;
035: import com.jgoodies.forms.layout.RowSpec;
036:
037: /**
038: * Command that deletes a row from a view.
039: *
040: * @author Jeff Tassin
041: */
042: public class DeleteRowCommand extends FormUndoableEdit {
043: /**
044: * The component source
045: */
046: private ComponentSource m_compsrc;
047:
048: /**
049: * The row to delete
050: */
051: private int m_row;
052:
053: /**
054: * A list of components and their associated cell constraints (ComponentInfo
055: * objects)
056: */
057: private LinkedList m_components = new LinkedList();
058:
059: /**
060: * The row spec of the row we deleted
061: */
062: private RowSpec m_oldspec;
063:
064: private ChangeGroupCommand m_change_group_cmd = null;
065:
066: /**
067: * ctor
068: */
069: public DeleteRowCommand(FormComponent form, int row,
070: ComponentSource compsrc) {
071: super (form);
072: m_compsrc = compsrc;
073: m_row = row;
074:
075: /** set the group to zero before deleting the row */
076: GridView view = getView();
077: FormGroupSet gset = view.getRowGroups();
078: Integer id = gset.getGroupId(row);
079: int group_id = (id == null ? 0 : id.intValue());
080: if (group_id > 0) {
081: m_change_group_cmd = new ChangeGroupCommand(form, 0,
082: group_id, row, true);
083: }
084:
085: }
086:
087: /**
088: * UndoableEdit implementation Override should begin with a call to super.
089: */
090: public void redo() throws CannotRedoException {
091: super .redo();
092:
093: if (m_oldspec == null) {
094: try {
095: if (m_change_group_cmd != null)
096: m_change_group_cmd.redo();
097: } catch (Exception e) {
098: e.printStackTrace();
099: }
100:
101: m_oldspec = getView().getRowSpec(m_row);
102: assert (m_oldspec != null);
103: m_components.clear();
104: for (int col = 1; col <= getView().getColumnCount(); col++) {
105: GridComponent gc = getView().getGridComponent(col,
106: m_row);
107: assert (gc != null);
108: CellConstraints cc = gc.getConstraints()
109: .createCellConstraints();
110: ComponentInfo info = new ComponentInfo(gc, cc);
111: m_components.add(info);
112: }
113: getView().removeRow(m_row);
114: }
115: }
116:
117: /**
118: * UndoableEdit implementation Override should begin with a call to super.
119: */
120: public void undo() throws CannotUndoException {
121: super .undo();
122:
123: try {
124: getView().insertRow(m_row, m_oldspec,
125: new EmptyComponentFactory(m_compsrc));
126: Iterator iter = m_components.iterator();
127: while (iter.hasNext()) {
128: ComponentInfo info = (ComponentInfo) iter.next();
129: assert (info.getRow() == m_row);
130: int col = info.getColumn();
131: int row = info.getRow();
132: GridComponent oldcomp = getView().getGridComponent(col,
133: row);
134: getView().replaceComponent(info.getGridComponent(),
135: oldcomp);
136: getView().setConstraints(info.getGridComponent(),
137: info.getCellConstraints());
138: }
139:
140: if (m_change_group_cmd != null)
141: m_change_group_cmd.undo();
142: } catch (Exception e) {
143: e.printStackTrace();
144: }
145: }
146:
147: public String toString() {
148: return "DeleteRowCommand row = " + String.valueOf(m_row);
149: }
150:
151: }
|