KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > palette > DnDHelper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21
22 /*
23  * DnDHelper.java
24  *
25  * Created on April 12, 2006, 2:44 PM
26  *
27  * To change this template, choose Tools | Template Manager
28  * and open the template in the editor.
29  */

30
31 package org.netbeans.modules.xml.schema.abe.palette;
32
33 import java.awt.datatransfer.DataFlavor JavaDoc;
34 import java.awt.datatransfer.Transferable JavaDoc;
35 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
36 import java.awt.dnd.DropTargetDragEvent JavaDoc;
37 import java.awt.dnd.DropTargetDropEvent JavaDoc;
38 import java.io.IOException JavaDoc;
39 import org.openide.nodes.Node;
40
41 /**
42  *
43  * @author girix
44  */

45 public class DnDHelper {
46     
47     
48     public static enum PaletteItem{
49         ELEMENT,
50         ATTRIBUTE,
51         SEQUENCE,
52         CHOICE,
53         ALL,
54         COMPLEXTYPE,
55         UNKNOWN
56     }
57     
58     /** Creates a new instance of DnDHelper */
59     public DnDHelper() {
60     }
61     
62     
63     public static PaletteItem getDraggedPaletteItem(Transferable JavaDoc trans){
64         for(DataFlavor JavaDoc flav: trans.getTransferDataFlavors()){
65             Class JavaDoc repClass = flav.getRepresentationClass();
66             Object JavaDoc data = null;
67             try {
68                 data = trans.getTransferData(flav);
69             } catch (UnsupportedFlavorException JavaDoc ex) {
70                 continue;
71             } catch (IOException JavaDoc ex) {
72                 continue;
73             }
74             if (Node.class.isAssignableFrom(repClass)){
75                 String JavaDoc name = ((Node)data).getName();
76                 return getPaletteItem(name);
77             }
78         }
79         return PaletteItem.UNKNOWN;
80     }
81     
82     public static PaletteItem getDraggedPaletteItem(DropTargetDragEvent JavaDoc event) {
83         return getDraggedPaletteItem(event.getTransferable());
84     }
85     
86     public static PaletteItem getDraggedPaletteItem(DropTargetDropEvent JavaDoc event){
87         return getDraggedPaletteItem(event.getTransferable());
88     }
89     
90     private static DnDHelper.PaletteItem getPaletteItem(String JavaDoc name) {
91         if(name.equalsIgnoreCase("attribute"))
92             return PaletteItem.ATTRIBUTE;
93         if(name.equalsIgnoreCase("element"))
94             return PaletteItem.ELEMENT;
95         if(name.equalsIgnoreCase("sequence"))
96             return PaletteItem.SEQUENCE;
97         if(name.equalsIgnoreCase("choice"))
98             return PaletteItem.CHOICE;
99         if(name.equalsIgnoreCase("all"))
100             return PaletteItem.ALL;
101         if(name.equalsIgnoreCase("complextype"))
102             return PaletteItem.COMPLEXTYPE;
103         return PaletteItem.UNKNOWN;
104     }
105     
106     public static boolean isCompositor(DnDHelper.PaletteItem paletteItem){
107         switch(paletteItem){
108             case SEQUENCE:
109             case CHOICE:
110             case ALL:
111                 return true;
112             default:
113                 return false;
114         }
115     }
116     
117 }
118
Popular Tags