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-2007 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: /*
043: * DnDHelper.java
044: *
045: * Created on April 12, 2006, 2:44 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.schema.abe.palette;
052:
053: import java.awt.datatransfer.DataFlavor;
054: import java.awt.datatransfer.Transferable;
055: import java.awt.datatransfer.UnsupportedFlavorException;
056: import java.awt.dnd.DropTargetDragEvent;
057: import java.awt.dnd.DropTargetDropEvent;
058: import java.io.IOException;
059: import org.openide.nodes.Node;
060:
061: /**
062: *
063: * @author girix
064: */
065: public class DnDHelper {
066:
067: public static enum PaletteItem {
068: ELEMENT, ATTRIBUTE, SEQUENCE, CHOICE, ALL, COMPLEXTYPE, UNKNOWN
069: }
070:
071: /** Creates a new instance of DnDHelper */
072: public DnDHelper() {
073: }
074:
075: public static PaletteItem getDraggedPaletteItem(Transferable trans) {
076: for (DataFlavor flav : trans.getTransferDataFlavors()) {
077: Class repClass = flav.getRepresentationClass();
078: Object data = null;
079: try {
080: data = trans.getTransferData(flav);
081: } catch (UnsupportedFlavorException ex) {
082: continue;
083: } catch (IOException ex) {
084: continue;
085: }
086: if (Node.class.isAssignableFrom(repClass)) {
087: String name = ((Node) data).getName();
088: return getPaletteItem(name);
089: }
090: }
091: return PaletteItem.UNKNOWN;
092: }
093:
094: public static PaletteItem getDraggedPaletteItem(
095: DropTargetDragEvent event) {
096: return getDraggedPaletteItem(event.getTransferable());
097: }
098:
099: public static PaletteItem getDraggedPaletteItem(
100: DropTargetDropEvent event) {
101: return getDraggedPaletteItem(event.getTransferable());
102: }
103:
104: private static DnDHelper.PaletteItem getPaletteItem(String name) {
105: if (name.equalsIgnoreCase("attribute"))
106: return PaletteItem.ATTRIBUTE;
107: if (name.equalsIgnoreCase("element"))
108: return PaletteItem.ELEMENT;
109: if (name.equalsIgnoreCase("sequence"))
110: return PaletteItem.SEQUENCE;
111: if (name.equalsIgnoreCase("choice"))
112: return PaletteItem.CHOICE;
113: if (name.equalsIgnoreCase("all"))
114: return PaletteItem.ALL;
115: if (name.equalsIgnoreCase("complextype"))
116: return PaletteItem.COMPLEXTYPE;
117: return PaletteItem.UNKNOWN;
118: }
119:
120: public static boolean isCompositor(DnDHelper.PaletteItem paletteItem) {
121: switch (paletteItem) {
122: case SEQUENCE:
123: case CHOICE:
124: case ALL:
125: return true;
126: default:
127: return false;
128: }
129: }
130:
131: }
|