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.Component;
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.form.FormComponent;
028: import com.jeta.forms.gui.form.GridComponent;
029: import com.jeta.forms.gui.form.GridView;
030:
031: public class TrimRowsCommand extends FormUndoableEdit {
032: /**
033: * Factory used to create the default component for each cell in the new
034: * row.
035: *
036: * @directed
037: */
038: private ComponentSource m_compsrc;
039:
040: /**
041: * A list of DeleteColumnCommands
042: */
043: private LinkedList m_delete_commands = new LinkedList();
044:
045: /**
046: * ctor
047: */
048: public TrimRowsCommand(FormComponent form, ComponentSource compsrc) {
049: super (form);
050: m_compsrc = compsrc;
051: }
052:
053: /**
054: * UndoableEdit implementation
055: */
056: public boolean canUndo() {
057: return false;
058: }
059:
060: /**
061: * UndoableEdit implementation Override should begin with a call to super.
062: */
063: public void redo() throws CannotRedoException {
064: super .redo();
065: FormComponent form = getForm();
066: GridView view = form.getChildView();
067: // first, do left to right
068: int row_count = view.getRowCount();
069: for (int index = 0; index < row_count; index++) {
070: if (view.getRowCount() == 1)
071: break;
072:
073: boolean bempty = true;
074: for (int col = 1; col <= view.getColumnCount(); col++) {
075: GridComponent gc = view.getGridComponent(col, 1);
076: Component bean_delegate = gc.getBeanDelegate();
077: if (gc != null && (bean_delegate != null)) {
078: bempty = false;
079: // System.out.println( "TrimRowsCommand found non-empty
080: // column at 1: bean delegate: " + bean_delegate );
081: break;
082: }
083: }
084:
085: if (bempty) {
086: DeleteRowCommand cmd = new DeleteRowCommand(form, 1,
087: m_compsrc);
088: cmd.redo();
089: m_delete_commands.add(cmd);
090: } else {
091: break;
092: }
093: }
094:
095: // now, do right to left
096: // first, do left to right
097: row_count = view.getRowCount();
098: for (int index = 0; index < row_count; index++) {
099: if (view.getRowCount() == 1)
100: break;
101:
102: int row = view.getRowCount();
103: boolean bempty = true;
104: for (int col = 1; col <= view.getColumnCount(); col++) {
105: GridComponent gc = view.getGridComponent(col, row);
106: Component bean_delegate = gc.getBeanDelegate();
107: if (gc != null && (bean_delegate != null)) {
108: bempty = false;
109: break;
110: }
111: }
112:
113: if (bempty) {
114: DeleteRowCommand cmd = new DeleteRowCommand(form, row,
115: m_compsrc);
116: cmd.redo();
117: m_delete_commands.add(cmd);
118: } else {
119: break;
120: }
121: }
122: }
123:
124: /**
125: * UndoableEdit implementation Override should begin with a call to super.
126: */
127: public void undo() throws CannotRedoException {
128: super .undo();
129: assert (false);
130: }
131:
132: }
|