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: package org.netbeans.modules.vmd.api.model.common;
042:
043: import org.netbeans.modules.vmd.api.model.ComponentProducer;
044: import org.netbeans.modules.vmd.api.model.DesignComponent;
045:
046: import java.awt.datatransfer.Transferable;
047: import java.util.Collections;
048:
049: /**
050: * @author David Kaspar
051: */
052: public final class AcceptSupport {
053:
054: private AcceptSupport() {
055: }
056:
057: public static boolean isAcceptable(DesignComponent component,
058: Transferable transferable, AcceptSuggestion suggestion) {
059: if (component == null || transferable == null)
060: return false;
061: ComponentProducer producer = DefaultDataFlavor
062: .decodeFromDataFlavors(component.getDocument(),
063: transferable);
064: for (AcceptPresenter presenter : component
065: .getPresenters(AcceptPresenter.class))
066: switch (presenter.getKind()) {
067: case COMPONENT_PRODUCER:
068: if (producer != null
069: && presenter.isAcceptable(producer, suggestion))
070: return true;
071: break;
072: case TRANSFERABLE:
073: if (presenter.isAcceptable(transferable, suggestion))
074: return true;
075: break;
076: }
077: return false;
078: }
079:
080: public static ComponentProducer.Result accept(
081: DesignComponent component, Transferable transferable,
082: AcceptSuggestion suggestion) {
083: if (component == null || transferable == null)
084: return null;
085: ComponentProducer producer = DefaultDataFlavor
086: .decodeFromDataFlavors(component.getDocument(),
087: transferable);
088: for (AcceptPresenter presenter : component
089: .getPresenters(AcceptPresenter.class))
090: switch (presenter.getKind()) {
091: case COMPONENT_PRODUCER:
092: if (producer != null
093: && presenter.isAcceptable(producer, suggestion))
094: return presenter.accept(producer, suggestion);
095: break;
096: case TRANSFERABLE:
097: if (presenter.isAcceptable(transferable, suggestion))
098: return presenter.accept(transferable, suggestion);
099: break;
100: }
101: return null;
102: }
103:
104: public static boolean isAcceptable(DesignComponent component,
105: ComponentProducer producer, AcceptSuggestion suggestion) {
106: if (component == null || producer == null)
107: return false;
108: for (AcceptPresenter presenter : component
109: .getPresenters(AcceptPresenter.class))
110: switch (presenter.getKind()) {
111: case COMPONENT_PRODUCER:
112: if (presenter.isAcceptable(producer, suggestion))
113: return true;
114: break;
115: case TRANSFERABLE:
116: break;
117: }
118: return false;
119: }
120:
121: public static ComponentProducer.Result accept(
122: DesignComponent component, ComponentProducer producer,
123: AcceptSuggestion suggestion) {
124: if (component == null || producer == null)
125: return null;
126: for (AcceptPresenter presenter : component
127: .getPresenters(AcceptPresenter.class))
128: switch (presenter.getKind()) {
129: case COMPONENT_PRODUCER:
130: if (presenter.isAcceptable(producer, suggestion))
131: return presenter.accept(producer, suggestion);
132: break;
133: case TRANSFERABLE:
134: break;
135: }
136: return null;
137: }
138:
139: public static void selectComponentProducerResult(
140: ComponentProducer.Result result) {
141: DesignComponent component = result != null ? result
142: .getMainComponent() : null;
143: if (component != null)
144: component.getDocument().setSelectedComponents("accept",
145: Collections.singleton(component)); // NOI18N
146: }
147:
148: }
|