KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tools > mapping > reversedb2 > ojbmetatreemodel > ReverseDbNodesToOjbMetaDropWorker


1 package org.apache.ojb.tools.mapping.reversedb2.ojbmetatreemodel;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel.*;
19 import org.apache.ojb.tools.mapping.reversedb2.datatransfer.*;
20 import org.apache.ojb.tools.mapping.reversedb2.dnd2.*;
21 import org.apache.ojb.broker.util.ClassHelper;
22
23 /**
24  * @author <a HREF="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
25  * @version $Id: ReverseDbNodesToOjbMetaDropWorker.java,v 1.1.2.1 2005/12/21 22:32:38 tomdz Exp $
26  */

27 public class ReverseDbNodesToOjbMetaDropWorker implements DropPasteWorkerInterface
28 {
29     /** Creates a new instance of ReverseDbNodesDropWorker */
30     public ReverseDbNodesToOjbMetaDropWorker()
31     {
32     }
33
34     public int getAcceptableActions (java.awt.Component JavaDoc c)
35     {
36         return DnDWorkerConstants.DRAG_MOVE | DnDWorkerConstants.DRAG_COPY ;
37     }
38
39     public int getAcceptableActions (java.awt.Component JavaDoc c, java.awt.datatransfer.DataFlavor JavaDoc[] flavor)
40     {
41         for (int j = 0; j < flavor.length; j++)
42         {
43             System.err.println("ReverseDbNodesToOjbMetaDropWorker.getAcceptableActions() flavor " + flavor[j] + " is " + flavor[j].isMimeTypeEqual(TransferableDBMetaTreeNodes.DBMETATABLENODE_FLAVOR_REMOTE));
44             if (flavor[j].equals (TransferableDBMetaTreeNodes.DBMETATABLENODE_FLAVOR_REMOTE))
45                 return DnDWorkerConstants.DRAG_MOVE | DnDWorkerConstants.DRAG_COPY;
46         }
47         return DnDWorkerConstants.NONE;
48     }
49
50     public java.awt.datatransfer.DataFlavor JavaDoc getSupportedDataFlavor ()
51     {
52         return TransferableDBMetaTreeNodes.DBMETATABLENODE_FLAVOR_REMOTE;
53     }
54
55     private java.util.Enumeration JavaDoc getTableNodes(java.util.Enumeration JavaDoc nodes)
56     {
57         java.util.Vector JavaDoc tableVector = new java.util.Vector JavaDoc();
58         while(nodes.hasMoreElements())
59         {
60             ReverseDbTreeNode aNode = (ReverseDbTreeNode)nodes.nextElement();
61             if (aNode instanceof DBMetaRootNode
62                 // || aNode instanceof DBMetaSchemaNode
63
|| aNode instanceof DBMetaCatalogNode)
64             {
65                 java.util.Enumeration JavaDoc e = getTableNodes(aNode.children());
66                 while (e.hasMoreElements()) tableVector.add(e.nextElement());
67             }
68             else if (aNode instanceof DBMetaSchemaNode)
69             {
70                 java.util.Enumeration JavaDoc e = aNode.children();
71                 while (e.hasMoreElements()) tableVector.add(e.nextElement());
72             }
73             else if (aNode instanceof DBMetaTableNode)
74             {
75                 return java.util.Collections.enumeration(java.util.Collections.singletonList(aNode));
76             }
77         }
78         return tableVector.elements();
79
80     }
81
82     public boolean importData (java.awt.Component JavaDoc c, java.awt.datatransfer.Transferable JavaDoc t, int action)
83     {
84         if (t.isDataFlavorSupported(TransferableDBMetaTreeNodes.DBMETATABLENODE_FLAVOR_REMOTE) &&
85             c instanceof javax.swing.JTree JavaDoc &&
86             ((javax.swing.JTree JavaDoc)c).getModel() instanceof OjbMetaDataTreeModel)
87         {
88             OjbMetaDataTreeModel treeModel = (OjbMetaDataTreeModel)((javax.swing.JTree JavaDoc)c).getModel();
89             OjbMetaRootNode rootNode = (OjbMetaRootNode)treeModel.getRoot();
90             ReverseDbTreeNode[] nodes;
91             try
92             {
93                 nodes = (ReverseDbTreeNode[])t.getTransferData(TransferableDBMetaTreeNodes.DBMETATABLENODE_FLAVOR_REMOTE);
94             }
95             catch (java.awt.datatransfer.UnsupportedFlavorException JavaDoc ufex)
96             {
97                 ufex.printStackTrace();
98                 return false;
99             }
100             catch (java.io.IOException JavaDoc ioex)
101             {
102                 ioex.printStackTrace();
103                 return false;
104             }
105             java.util.Enumeration JavaDoc e = getTableNodes(java.util.Collections.enumeration(java.util.Arrays.asList(nodes)));
106             while (e.hasMoreElements())
107             {
108                 Object JavaDoc o = e.nextElement();
109                 if (o instanceof DBMetaTableNode)
110                 {
111                     DBMetaTableNode dbTable = (DBMetaTableNode)o;
112                     System.err.println("Adding " + dbTable);
113                     org.apache.ojb.broker.metadata.ClassDescriptor cld =
114                         new org.apache.ojb.broker.metadata.ClassDescriptor(treeModel.getRepository());
115                     cld.setTableName(dbTable.getTableName());
116                     try
117                     {
118                         cld.setClassOfObject(ClassHelper.getClass(dbTable.getTableName()));
119                     }
120                     catch (ClassNotFoundException JavaDoc e1)
121                     {
122                         e1.printStackTrace();
123                     }
124                     treeModel.getRepository().put(cld.getClassNameOfObject(), cld);
125                     rootNode.addClassDescriptor(cld);
126                 }
127             }
128             return true;
129         }
130         else
131         {
132             System.err.println("Cannot import data");
133             return false;
134         }
135     }
136 }
137
Popular Tags