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:
023: import javax.swing.undo.CannotRedoException;
024: import javax.swing.undo.CannotUndoException;
025:
026: import com.jeta.forms.gui.beans.JETABean;
027: import com.jeta.forms.gui.beans.JETAPropertyDescriptor;
028: import com.jeta.forms.gui.form.FormComponent;
029: import com.jeta.forms.gui.form.GridCellEvent;
030: import com.jeta.forms.gui.form.GridComponent;
031: import com.jeta.forms.store.properties.TransformOptionsProperty;
032:
033: public class SetPropertyCommand extends FormUndoableEdit {
034: /**
035: * The JETABean whose delegate we are changing
036: */
037: private JETABean m_bean;
038:
039: /**
040: * The parent grid component that contains the bean
041: */
042: private GridComponent m_gc;
043:
044: private Object m_newvalue;
045: private Object m_oldvalue;
046:
047: private JETAPropertyDescriptor m_property_descriptor;
048:
049: public SetPropertyCommand(JETAPropertyDescriptor pd, JETABean bean,
050: Object newValue, Object oldValue, FormComponent form) {
051: super (form);
052: m_property_descriptor = pd;
053: m_bean = bean;
054: m_newvalue = newValue;
055:
056: Component comp = bean.getParent();
057: while (comp != null) {
058: if (comp instanceof GridComponent) {
059: m_gc = (GridComponent) comp;
060: break;
061: }
062: }
063:
064: /**
065: * Special case for TransformOptionsProperty
066: */
067: if (oldValue instanceof TransformOptionsProperty) {
068: m_oldvalue = ((TransformOptionsProperty) oldValue)
069: .getCurrentItem();
070: } else {
071: m_oldvalue = oldValue;
072: }
073: }
074:
075: public boolean equals(Object obj) {
076: if (obj instanceof SetPropertyCommand) {
077: SetPropertyCommand prop = (SetPropertyCommand) obj;
078: if (m_bean == prop.m_bean) {
079: if (m_newvalue == prop.m_newvalue)
080: return true;
081: return (m_newvalue != null && m_newvalue
082: .equals(prop.m_newvalue));
083: }
084: }
085: return false;
086: }
087:
088: /**
089: * UndoableEdit implementation Override should begin with a call to super.
090: */
091: public void redo() throws CannotRedoException {
092: super .redo();
093: try {
094: // System.out.println( "redo: " + toString() );
095: m_property_descriptor.setPropertyValue(m_bean, m_newvalue);
096: if (m_gc != null) {
097: m_gc.fireGridCellEvent(new GridCellEvent(
098: GridCellEvent.CELL_CHANGED, m_gc));
099: }
100: } catch (Exception e) {
101: throw new CannotRedoException();
102: }
103: }
104:
105: /**
106: * UndoableEdit implementation Override should begin with a call to super.
107: */
108: public void undo() throws CannotUndoException {
109: super .undo();
110: try {
111: m_property_descriptor.setPropertyValue(m_bean, m_oldvalue);
112: if (m_gc != null) {
113: m_gc.fireGridCellEvent(new GridCellEvent(
114: GridCellEvent.CELL_CHANGED, m_gc));
115: }
116: } catch (Exception e) {
117: throw new CannotUndoException();
118: }
119: }
120:
121: public String toString() {
122: return "SetPropertyCommand " + m_property_descriptor.getName()
123: + " newvalue: " + m_newvalue + " oldvalue: "
124: + m_oldvalue;
125: }
126:
127: }
|