001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.vmd.inspector;
043:
044: import java.io.IOException;
045: import org.netbeans.modules.vmd.api.inspector.InspectorFolder;
046: import org.netbeans.modules.vmd.api.model.DesignComponent;
047: import org.netbeans.modules.vmd.api.model.DesignDocument;
048: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
049: import org.openide.nodes.AbstractNode;
050:
051: import javax.swing.*;
052: import java.awt.*;
053: import java.awt.datatransfer.DataFlavor;
054: import java.awt.datatransfer.Transferable;
055: import java.awt.datatransfer.UnsupportedFlavorException;
056: import java.awt.dnd.DnDConstants;
057: import java.lang.ref.WeakReference;
058: import org.netbeans.modules.vmd.api.io.ActiveViewSupport;
059: import org.netbeans.modules.vmd.api.io.DataEditorView;
060:
061: import org.netbeans.modules.vmd.api.io.DataObjectContext;
062: import org.netbeans.modules.vmd.api.io.IOUtils;
063: import org.netbeans.modules.vmd.api.model.common.AcceptSuggestion;
064: import org.netbeans.modules.vmd.api.model.common.AcceptSupport;
065: import org.netbeans.modules.vmd.api.model.common.DesignComponentDataFlavorSupport;
066: import org.netbeans.modules.vmd.api.properties.common.PropertiesSupport;
067: import org.openide.nodes.Sheet;
068: import org.openide.util.datatransfer.PasteType;
069:
070: /**
071: *
072: * @author Karol Harezlak
073: */
074: final class InspectorFolderNode extends AbstractNode {
075:
076: private static final Action[] EMPTY_ACTION_ARRAY = new Action[0];
077: private static DataFlavor INSPECTOR_NODE_DATA_FLAVOR = new DataFlavor(
078: NodeTransferable.class, "Nodetransferable"); //NOI18N
079:
080: private Long componentID;
081: private WeakReference<DesignComponent> component;
082: private InspectorFolder folder;
083: private Transferable transferable;
084:
085: InspectorFolderNode(DataObjectContext context) {
086: super (new InspectorChildren(), context.getDataObject()
087: .getLookup());
088: }
089:
090: InspectorFolderNode() {
091: super (new InspectorChildren());
092: }
093:
094: Long getComponentID() {
095: return componentID;
096: }
097:
098: public String getHtmlDisplayName() {
099: return folder.getHtmlDisplayName();
100: }
101:
102: public Image getIcon(int type) {
103: if (folder == null)
104: throw new IllegalStateException(
105: "Not resolved Folder- Broken tree structure. Check InspectorPosisitonPresenters and InspectorFolderPresenters"); //NOI18N
106: return folder.getIcon();
107: }
108:
109: public Image getOpenedIcon(int type) {
110: return folder.getIcon();
111: }
112:
113: public Action[] getActions(boolean context) {
114: if (folder.getActions() == null)
115: return EMPTY_ACTION_ARRAY;
116: return folder.getActions();
117: }
118:
119: public boolean canRename() {
120: return folder.canRename();
121: }
122:
123: public AcceptSuggestion createSuggestion(Transferable transferable) {
124: return folder.createSuggestion(transferable);
125: }
126:
127: public void setName(final String name) {
128: if (name == null)
129: throw new IllegalArgumentException(
130: "Argument name cant be null"); //NOI18N
131:
132: if (component == null || component.get() == null)
133: return;
134: component.get().getDocument().getTransactionManager()
135: .writeAccess(new Runnable() {
136: public void run() {
137: InfoPresenter presenter = component.get()
138: .getPresenter(InfoPresenter.class);
139: if (presenter != null) {
140: presenter.setEditableName(name);
141: }
142: }
143: });
144: }
145:
146: void resolveNode(final InspectorFolderWrapper folderWrapper,
147: final DesignDocument document) {
148: this .folder = folderWrapper.getFolder();
149: super .setDisplayName(folder.getDisplayName());
150: this .componentID = folder.getComponentID();
151: if (folder.getName() == null)
152: super .setName(folder.getDisplayName());
153: else
154: super .setName(folder.getName());
155: if (componentID != null) {
156: document.getTransactionManager().readAccess(new Runnable() {
157: public void run() {
158: component = new WeakReference<DesignComponent>(
159: document.getComponentByUID(componentID));
160: transferable = new NodeTransferable(component.get());
161: }
162: });
163: }
164: IOUtils.runInAWTNoBlocking(new Runnable() {
165: public void run() {
166: ((InspectorChildren) getChildren())
167: .setKeys(folderWrapper.getChildrenNodes());
168: }
169: });
170: }
171:
172: protected void createPasteTypes(Transferable t, java.util.List s) {
173: super .createPasteTypes(t, s);
174: if (!t.isDataFlavorSupported(INSPECTOR_NODE_DATA_FLAVOR))
175: return;
176:
177: PasteType paste = getDropType(t,
178: DnDConstants.ACTION_COPY_OR_MOVE, -1);
179: if (paste != null)
180: s.add(paste);
181: }
182:
183: public PasteType getDropType(final Transferable t,
184: final int action, final int index) {
185: final PasteType[] pasteType = new PasteType[1];
186: if (!t.isDataFlavorSupported(INSPECTOR_NODE_DATA_FLAVOR))
187: return null;
188: if (component == null || component.get() == null)
189: return null;
190: component.get().getDocument().getTransactionManager()
191: .readAccess(new Runnable() {
192: public void run() {
193: final AcceptSuggestion suggestion = createSuggestion(t);
194: if (component.get() != null
195: && AcceptSupport.isAcceptable(component
196: .get(), t, suggestion)) {
197: pasteType[0] = new PasteType() {
198: public Transferable paste()
199: throws IOException {
200: component.get().getDocument()
201: .getTransactionManager()
202: .writeAccess(
203: new Runnable() {
204: public void run() {
205: if (component
206: .get() != null
207: && AcceptSupport
208: .isAcceptable(
209: component
210: .get(),
211: t,
212: suggestion)) {
213: AcceptSupport
214: .accept(
215: component
216: .get(),
217: t,
218: null);
219: }
220: }
221: });
222: return t;
223: }
224: };
225: }
226: }
227: });
228: return pasteType[0];
229: }
230:
231: public Transferable drag() throws IOException {
232: return transferable;
233: }
234:
235: public boolean canCut() {
236: return true;
237: }
238:
239: @Override
240: public boolean canCopy() {
241: return true;
242: }
243:
244: @Override
245: public Transferable clipboardCopy() throws IOException {
246: return transferable;
247: }
248:
249: @Override
250: public Transferable clipboardCut() throws IOException {
251: return transferable;
252: }
253:
254: public boolean canDestroy() {
255: return true;
256: }
257:
258: DesignComponent getComponent() {
259: return component.get();
260: }
261:
262: void terminate() {
263: componentID = null;
264: component = null;
265: folder = null;
266: }
267:
268: public Sheet createSheet() {
269: if (component.get() == null)
270: super .createSheet();
271: DataEditorView view = ActiveViewSupport.getDefault()
272: .getActiveView();
273: if (view != null && view.getKind() == DataEditorView.Kind.MODEL)
274: return PropertiesSupport.getSheet(view, component.get());
275: return super .createSheet();
276: }
277:
278: private class NodeTransferable implements Transferable {
279:
280: private WeakReference<DesignComponent> component;
281:
282: public NodeTransferable(DesignComponent component) {
283: assert (component != null);
284: this .component = new WeakReference<DesignComponent>(
285: component);
286: }
287:
288: public DataFlavor[] getTransferDataFlavors() {
289: return new DataFlavor[] {
290: DesignComponentDataFlavorSupport.DESIGN_COMPONENT_DATA_FLAVOR,
291: INSPECTOR_NODE_DATA_FLAVOR };
292: }
293:
294: public boolean isDataFlavorSupported(DataFlavor flavor) {
295: if (flavor == DesignComponentDataFlavorSupport.DESIGN_COMPONENT_DATA_FLAVOR)
296: return true;
297: if (flavor == INSPECTOR_NODE_DATA_FLAVOR)
298: return true;
299: return false;
300: }
301:
302: public Object getTransferData(DataFlavor flavor)
303: throws UnsupportedFlavorException, IOException {
304: return component.get();
305: }
306: };
307:
308: }
|