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:
026: import com.jeta.forms.gui.components.ComponentSource;
027: import com.jeta.forms.gui.components.EmptyComponentFactory;
028: import com.jeta.forms.gui.form.ComponentInfo;
029: import com.jeta.forms.gui.form.FormComponent;
030: import com.jeta.forms.gui.form.GridComponent;
031: import com.jeta.forms.gui.form.GridView;
032: import com.jeta.forms.store.memento.FormGroupSet;
033: import com.jgoodies.forms.layout.CellConstraints;
034: import com.jgoodies.forms.layout.ColumnSpec;
035:
036: /**
037: * Command that deletes a column from a view.
038: *
039: * @author Jeff Tassin
040: */
041: public class DeleteColumnCommand extends FormUndoableEdit {
042:
043: /**
044: * The component source
045: */
046: private ComponentSource m_compsrc;
047:
048: /**
049: * The column to delete
050: */
051: private int m_column;
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 column spec of the column we deleted
061: */
062: private ColumnSpec m_oldspec;
063:
064: private ChangeGroupCommand m_change_group_cmd = null;
065:
066: /**
067: * ctor
068: */
069: public DeleteColumnCommand(FormComponent form, int col,
070: ComponentSource compsrc) {
071: super (form);
072: m_compsrc = compsrc;
073: m_column = col;
074:
075: /** set the group to zero before deleting the column */
076: GridView view = getView();
077: FormGroupSet gset = view.getColumnGroups();
078: Integer id = gset.getGroupId(col);
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, col, false);
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: try {
094: if (m_change_group_cmd != null)
095: m_change_group_cmd.redo();
096: } catch (Exception e) {
097: e.printStackTrace();
098: }
099:
100: if (m_oldspec == null) {
101: m_oldspec = getView().getColumnSpec(m_column);
102: assert (m_oldspec != null);
103: m_components.clear();
104: for (int row = 1; row <= getView().getRowCount(); row++) {
105: GridComponent gc = getView().getGridComponent(m_column,
106: 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: }
114: getView().removeColumn(m_column);
115: }
116:
117: /**
118: * UndoableEdit implementation Override should begin with a call to super.
119: */
120: public void undo() throws CannotRedoException {
121: super .undo();
122:
123: try {
124: getView().insertColumn(m_column, 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.getColumn() == m_column);
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 "DeleteColumnCommand column = "
149: + String.valueOf(m_column);
150: }
151:
152: }
|