001: package org.apache.ojb.tools.mapping.reversedb2.ojbmetatreemodel;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel.*;
019: import org.apache.ojb.tools.mapping.reversedb2.datatransfer.*;
020: import org.apache.ojb.tools.mapping.reversedb2.dnd2.*;
021: import org.apache.ojb.broker.util.ClassHelper;
022:
023: /**
024: * @author <a href="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
025: * @version $Id: ReverseDbNodesToOjbMetaDropWorker.java,v 1.1.2.1 2005/12/21 22:32:38 tomdz Exp $
026: */
027: public class ReverseDbNodesToOjbMetaDropWorker implements
028: DropPasteWorkerInterface {
029: /** Creates a new instance of ReverseDbNodesDropWorker */
030: public ReverseDbNodesToOjbMetaDropWorker() {
031: }
032:
033: public int getAcceptableActions(java.awt.Component c) {
034: return DnDWorkerConstants.DRAG_MOVE
035: | DnDWorkerConstants.DRAG_COPY;
036: }
037:
038: public int getAcceptableActions(java.awt.Component c,
039: java.awt.datatransfer.DataFlavor[] flavor) {
040: for (int j = 0; j < flavor.length; j++) {
041: System.err
042: .println("ReverseDbNodesToOjbMetaDropWorker.getAcceptableActions() flavor "
043: + flavor[j]
044: + " is "
045: + flavor[j]
046: .isMimeTypeEqual(TransferableDBMetaTreeNodes.DBMETATABLENODE_FLAVOR_REMOTE));
047: if (flavor[j]
048: .equals(TransferableDBMetaTreeNodes.DBMETATABLENODE_FLAVOR_REMOTE))
049: return DnDWorkerConstants.DRAG_MOVE
050: | DnDWorkerConstants.DRAG_COPY;
051: }
052: return DnDWorkerConstants.NONE;
053: }
054:
055: public java.awt.datatransfer.DataFlavor getSupportedDataFlavor() {
056: return TransferableDBMetaTreeNodes.DBMETATABLENODE_FLAVOR_REMOTE;
057: }
058:
059: private java.util.Enumeration getTableNodes(
060: java.util.Enumeration nodes) {
061: java.util.Vector tableVector = new java.util.Vector();
062: while (nodes.hasMoreElements()) {
063: ReverseDbTreeNode aNode = (ReverseDbTreeNode) nodes
064: .nextElement();
065: if (aNode instanceof DBMetaRootNode
066: // || aNode instanceof DBMetaSchemaNode
067: || aNode instanceof DBMetaCatalogNode) {
068: java.util.Enumeration e = getTableNodes(aNode
069: .children());
070: while (e.hasMoreElements())
071: tableVector.add(e.nextElement());
072: } else if (aNode instanceof DBMetaSchemaNode) {
073: java.util.Enumeration e = aNode.children();
074: while (e.hasMoreElements())
075: tableVector.add(e.nextElement());
076: } else if (aNode instanceof DBMetaTableNode) {
077: return java.util.Collections
078: .enumeration(java.util.Collections
079: .singletonList(aNode));
080: }
081: }
082: return tableVector.elements();
083:
084: }
085:
086: public boolean importData(java.awt.Component c,
087: java.awt.datatransfer.Transferable t, int action) {
088: if (t
089: .isDataFlavorSupported(TransferableDBMetaTreeNodes.DBMETATABLENODE_FLAVOR_REMOTE)
090: && c instanceof javax.swing.JTree
091: && ((javax.swing.JTree) c).getModel() instanceof OjbMetaDataTreeModel) {
092: OjbMetaDataTreeModel treeModel = (OjbMetaDataTreeModel) ((javax.swing.JTree) c)
093: .getModel();
094: OjbMetaRootNode rootNode = (OjbMetaRootNode) treeModel
095: .getRoot();
096: ReverseDbTreeNode[] nodes;
097: try {
098: nodes = (ReverseDbTreeNode[]) t
099: .getTransferData(TransferableDBMetaTreeNodes.DBMETATABLENODE_FLAVOR_REMOTE);
100: } catch (java.awt.datatransfer.UnsupportedFlavorException ufex) {
101: ufex.printStackTrace();
102: return false;
103: } catch (java.io.IOException ioex) {
104: ioex.printStackTrace();
105: return false;
106: }
107: java.util.Enumeration e = getTableNodes(java.util.Collections
108: .enumeration(java.util.Arrays.asList(nodes)));
109: while (e.hasMoreElements()) {
110: Object o = e.nextElement();
111: if (o instanceof DBMetaTableNode) {
112: DBMetaTableNode dbTable = (DBMetaTableNode) o;
113: System.err.println("Adding " + dbTable);
114: org.apache.ojb.broker.metadata.ClassDescriptor cld = new org.apache.ojb.broker.metadata.ClassDescriptor(
115: treeModel.getRepository());
116: cld.setTableName(dbTable.getTableName());
117: try {
118: cld.setClassOfObject(ClassHelper
119: .getClass(dbTable.getTableName()));
120: } catch (ClassNotFoundException e1) {
121: e1.printStackTrace();
122: }
123: treeModel.getRepository().put(
124: cld.getClassNameOfObject(), cld);
125: rootNode.addClassDescriptor(cld);
126: }
127: }
128: return true;
129: } else {
130: System.err.println("Cannot import data");
131: return false;
132: }
133: }
134: }
|