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 TrimColumnsCommand 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 TrimColumnsCommand(FormComponent form,
049: ComponentSource compsrc) {
050: super (form);
051: m_compsrc = compsrc;
052: }
053:
054: /**
055: * UndoableEdit implementation
056: */
057: public boolean canUndo() {
058: return false;
059: }
060:
061: /**
062: * UndoableEdit implementation Override should begin with a call to super.
063: */
064: public void redo() throws CannotRedoException {
065: super .redo();
066: FormComponent form = getForm();
067: GridView view = form.getChildView();
068: // first, do left to right
069: int col_count = view.getColumnCount();
070: for (int index = 0; index < col_count; index++) {
071: if (view.getColumnCount() == 1)
072: break;
073:
074: boolean bempty = true;
075: for (int row = 1; row <= view.getRowCount(); row++) {
076: GridComponent gc = view.getGridComponent(1, row);
077: Component bean_delegate = gc.getBeanDelegate();
078: if (gc != null && (bean_delegate != null)) {
079: bempty = false;
080: // System.out.println( "TrimColumnsCommand found non-empty
081: // column at 1: bean delegate: " + bean_delegate );
082: break;
083: }
084: }
085:
086: if (bempty) {
087: DeleteColumnCommand cmd = new DeleteColumnCommand(form,
088: 1, m_compsrc);
089: cmd.redo();
090: m_delete_commands.add(cmd);
091: } else {
092: break;
093: }
094: }
095:
096: // now, do right to left
097: // first, do left to right
098: col_count = view.getColumnCount();
099: for (int index = 0; index < col_count; index++) {
100: if (view.getColumnCount() == 1)
101: break;
102:
103: int col = view.getColumnCount();
104: boolean bempty = true;
105: for (int row = 1; row <= view.getRowCount(); row++) {
106: GridComponent gc = view.getGridComponent(col, row);
107: Component bean_delegate = gc.getBeanDelegate();
108: if (gc != null && (bean_delegate != null)) {
109: bempty = false;
110: break;
111: }
112: }
113:
114: if (bempty) {
115: DeleteColumnCommand cmd = new DeleteColumnCommand(form,
116: col, m_compsrc);
117: cmd.redo();
118: m_delete_commands.add(cmd);
119: } else {
120: break;
121: }
122: }
123: }
124:
125: }
|