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.dnd;
020:
021: import java.awt.datatransfer.DataFlavor;
022: import java.awt.datatransfer.Transferable;
023: import java.util.Collection;
024: import java.util.LinkedList;
025:
026: import com.jeta.forms.gui.common.FormException;
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: import com.jeta.forms.store.memento.ComponentMemento;
031: import com.jeta.forms.store.memento.ComponentMementoProxy;
032: import com.jeta.forms.store.memento.FormMemento;
033: import com.jeta.forms.store.memento.StateRequest;
034: import com.jeta.forms.store.properties.effects.PaintProperty;
035: import com.jeta.swingbuilder.gui.formmgr.FormManagerDesignUtils;
036:
037: /**
038: * This class represents a transferable for a ComponentMemento user is dragging
039: * or copying.
040: *
041: * @author Jeff Tassin.
042: */
043: public class ComponentTransferable implements Transferable {
044:
045: /**
046: * The data flavors supported by this component
047: */
048: private DataFlavor[] m_flavors;
049:
050: private ComponentMementoProxy m_memento_proxy;
051:
052: /**
053: * If this transferable contains linked forms, this is the list of form ids
054: * (Strings)
055: */
056: private LinkedList m_paths;
057:
058: /**
059: * The absolute path of a top level linked form.
060: */
061: private String m_form_path;
062:
063: private PaintProperty m_paint;
064:
065: /**
066: * ctor
067: */
068: public ComponentTransferable(GridComponent gc) throws FormException {
069: initialize(gc);
070: }
071:
072: /**
073: * ctor
074: */
075: private void initialize(ComponentMemento cm) {
076: setComponentMemento(cm);
077: m_flavors = new DataFlavor[] { FormObjectFlavor.COMPONENT_MEMENTO };
078: }
079:
080: /**
081: * for a linked form
082: */
083: private void initialize(GridComponent fc) throws FormException {
084: if (fc.getBeanDelegate() != null) {
085: Collection linked_forms = FormManagerDesignUtils
086: .getLinkedForms(fc);
087: if (linked_forms.size() > 0) {
088: m_paths = new LinkedList();
089: m_paths.addAll(linked_forms);
090:
091: if ((fc instanceof FormComponent)
092: && ((FormComponent) fc).isLinked()) {
093: if (((FormComponent) fc).getAbsolutePath() == null) {
094: initialize(fc.getState(StateRequest.DEEP_COPY));
095: } else {
096: m_flavors = new DataFlavor[] {
097: FormObjectFlavor.LINKED_FORM_SET,
098: FormObjectFlavor.LINKED_FORM,
099: FormObjectFlavor.COMPONENT_MEMENTO };
100:
101: FormMemento fm = (FormMemento) ((FormComponent) fc)
102: .getExternalState(StateRequest.DEEP_COPY);
103: fm.setRelativePath(null);
104: setComponentMemento(fm);
105: m_form_path = ((FormComponent) fc)
106: .getAbsolutePath();
107: }
108: } else {
109: m_flavors = new DataFlavor[] {
110: FormObjectFlavor.LINKED_FORM_SET,
111: FormObjectFlavor.COMPONENT_MEMENTO };
112: /**
113: * this must be a shallow copy. we don't support 'Paste
114: * Embedded' an embbeded form with linked nests
115: */
116: if (fc instanceof FormComponent) {
117: FormComponent form = (FormComponent) fc;
118: FormMemento fm = form
119: .getExternalState(StateRequest.SHALLOW_COPY);
120: fm.setRelativePath(null);
121: setComponentMemento(fm);
122: } else {
123: /** this can happen if we have a FormContainerComponent */
124: setComponentMemento(fc
125: .getState(StateRequest.SHALLOW_COPY));
126: }
127: }
128: } else {
129: initialize(fc.getState(StateRequest.DEEP_COPY));
130: }
131: } else {
132: m_flavors = new DataFlavor[0];
133: }
134:
135: GridView parentview = fc.getParentView();
136: PaintProperty paint = parentview.getPaintProperty(fc
137: .getColumn(), fc.getRow());
138: if (paint != null) {
139: m_paint = paint;
140:
141: DataFlavor[] flavors = new DataFlavor[m_flavors.length + 1];
142: System
143: .arraycopy(m_flavors, 0, flavors, 0,
144: m_flavors.length);
145: flavors[flavors.length - 1] = FormObjectFlavor.CELL_BACKGROUND;
146: m_flavors = flavors;
147: }
148: }
149:
150: private ComponentMementoProxy getComponentMemento() {
151: return m_memento_proxy;
152: }
153:
154: private void setComponentMemento(ComponentMemento cm) {
155: m_memento_proxy = new ComponentMementoProxy(cm);
156:
157: }
158:
159: /**
160: * @return the transfer data for the given flavor Note that we can return an
161: * array as well as a single object
162: */
163: public Object getTransferData(DataFlavor flavor) {
164: if (flavor.equals(FormObjectFlavor.COMPONENT_MEMENTO)) {
165: return getComponentMemento();
166: } else if (flavor.equals(FormObjectFlavor.LINKED_FORM_SET)) {
167: return m_paths;
168: } else if (flavor.equals(FormObjectFlavor.LINKED_FORM)) {
169: return m_form_path;
170: } else if (flavor.equals(FormObjectFlavor.CELL_BACKGROUND)) {
171: return m_paint;
172: } else {
173: return null;
174: }
175: }
176:
177: /**
178: * Transferable implementation.
179: */
180: public DataFlavor[] getTransferDataFlavors() {
181: return m_flavors;
182: }
183:
184: /**
185: * Transferable implemenetation
186: */
187: public boolean isDataFlavorSupported(DataFlavor flavor) {
188: for (int index = 0; index < m_flavors.length; index++) {
189: if (flavor.equals(m_flavors[index]))
190: return true;
191: }
192: return false;
193: }
194: }
|