001: /* Copyright 2002-2005 The Apache Software Foundation
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.apache.ojb.tools.mapping.reversedb2.gui;
017:
018: import java.awt.dnd.DragGestureListener;
019: import java.awt.dnd.DragSource;
020: import java.awt.dnd.DragSourceListener;
021:
022: import org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel.DBMetaCatalogNode;
023: import org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel.DBMetaRootNode;
024: import org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel.DBMetaSchemaNode;
025: import org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel.DBMetaTableNode;
026: import org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel.DatabaseMetaDataTreeModel;
027: import org.apache.ojb.tools.mapping.reversedb2.dnd2.DragHelper;
028: import org.apache.ojb.tools.mapping.reversedb2.dnd2.DropTargetHelper;
029: import org.apache.ojb.tools.mapping.reversedb2.dnd2.ReverseDbNodesDragWorker;
030: import org.apache.ojb.tools.mapping.reversedb2.dnd2.ReverseDbNodesDropWorker;
031: import org.apache.ojb.tools.mapping.reversedb2.propertyEditors.PropertyEditor;
032: import org.apache.ojb.tools.mapping.reversedb2.propertyEditors.PropertyEditorTarget;
033:
034: /**
035: *
036: * @author Administrator
037: */
038: public class JIFrmDatabase extends javax.swing.JInternalFrame {
039: private java.sql.Connection jdbcConnection;
040:
041: private DragSource dragSource;
042: private DragGestureListener dgListener;
043: private DragSourceListener dsListener;
044:
045: private java.util.HashMap hmPropertyEditors = new java.util.HashMap();
046:
047: /** Creates new form JIFrmDatabaseMetadata */
048: public JIFrmDatabase(java.sql.Connection pjdbcConnection) {
049: this .jdbcConnection = pjdbcConnection;
050: initComponents();
051: try {
052: this .setTitle("Database: "
053: + jdbcConnection.getMetaData().getURL());
054: DatabaseMetaDataTreeModel model = new DatabaseMetaDataTreeModel(
055: this .jdbcConnection.getMetaData());
056: model
057: .addStatusMessageListener(new org.apache.ojb.tools.mapping.reversedb2.events.StatusMessageAdapter() {
058: public void statusMessageReceived(final String s) {
059: javax.swing.SwingUtilities
060: .invokeLater(new Runnable() {
061: public void run() {
062: jTextField1.setText(s);
063: }
064: });
065: }
066: });
067: this .jTreeDatabase.setModel(model);
068: } catch (java.sql.SQLException sqlEx) {
069: this .setTitle("Database: unknown");
070: this .jTreeDatabase
071: .setModel(new javax.swing.tree.DefaultTreeModel(
072: new javax.swing.tree.DefaultMutableTreeNode(
073: "Error...")));
074: }
075:
076: // new DatabaseTransferHandler().register(jTreeDatabase);
077: // enable dragging for the tree.
078: new DragHelper(new ReverseDbNodesDragWorker())
079: .registerComponent(jTreeDatabase);
080:
081: // enable dropping for the tree.
082: DropTargetHelper helper = new DropTargetHelper();
083: helper.registerDropPasteWorker(new ReverseDbNodesDropWorker());
084: jTreeDatabase.setDropTarget(helper.getDropTarget());
085:
086: }
087:
088: private java.util.Collection getTableNodes(Object o) {
089: if (o instanceof DBMetaRootNode
090: || o instanceof DBMetaCatalogNode
091: || o instanceof DBMetaSchemaNode) {
092: java.util.ArrayList alTables = new java.util.ArrayList();
093: java.util.Enumeration e = ((javax.swing.tree.TreeNode) o)
094: .children();
095: while (e.hasMoreElements()) {
096: alTables.addAll(getTableNodes(e.nextElement()));
097: }
098: return alTables;
099: } else if (o instanceof DBMetaTableNode) {
100: return java.util.Collections.singleton(o);
101: } else {
102: return null;
103: }
104: }
105:
106: /** This method is called from within the constructor to
107: * initialize the form.
108: * WARNING: Do NOT modify this code. The content of this method is
109: * always regenerated by the Form Editor.
110: */
111: private void initComponents()//GEN-BEGIN:initComponents
112: {
113: jSplitPane1 = new javax.swing.JSplitPane();
114: jScrlTree = new javax.swing.JScrollPane();
115: jTreeDatabase = new javax.swing.JTree();
116: jScrlProperty = new javax.swing.JScrollPane();
117: jToolBar1 = new javax.swing.JToolBar();
118: jTextField1 = new javax.swing.JTextField();
119:
120: setClosable(true);
121: setIconifiable(true);
122: setMaximizable(true);
123: setResizable(true);
124: setTitle("Database");
125: setPreferredSize(new java.awt.Dimension(300, 300));
126: jTreeDatabase.setRootVisible(false);
127: jTreeDatabase
128: .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
129: public void valueChanged(
130: javax.swing.event.TreeSelectionEvent evt) {
131: jTreeDatabaseValueChanged(evt);
132: }
133: });
134:
135: jScrlTree.setViewportView(jTreeDatabase);
136:
137: jSplitPane1.setLeftComponent(jScrlTree);
138:
139: jSplitPane1.setRightComponent(jScrlProperty);
140:
141: getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);
142:
143: jTextField1.setEditable(false);
144: jTextField1.setText("jTextField1");
145: jTextField1.setBorder(null);
146: jTextField1.setOpaque(false);
147: jToolBar1.add(jTextField1);
148:
149: getContentPane().add(jToolBar1, java.awt.BorderLayout.SOUTH);
150:
151: pack();
152: }//GEN-END:initComponents
153:
154: private void jTreeDatabaseValueChanged(
155: javax.swing.event.TreeSelectionEvent evt)//GEN-FIRST:event_jTreeDatabaseValueChanged
156: {//GEN-HEADEREND:event_jTreeDatabaseValueChanged
157: // Add your handling code here:
158: Object selectedObject = evt.getPath().getLastPathComponent();
159: if (selectedObject instanceof PropertyEditorTarget) {
160: PropertyEditorTarget p = (PropertyEditorTarget) selectedObject;
161: if (p.getPropertyEditorClass() != null) {
162: try {
163: if (!this .hmPropertyEditors.containsKey(p
164: .getPropertyEditorClass())) {
165: this .hmPropertyEditors
166: .put(p.getPropertyEditorClass(), p
167: .getPropertyEditorClass()
168: .newInstance());
169: }
170: PropertyEditor propertyEditor = (PropertyEditor) hmPropertyEditors
171: .get(p.getPropertyEditorClass());
172: this .jScrlProperty.setViewportView(propertyEditor);
173: propertyEditor.setEditorTarget(p);
174: } catch (Throwable t) {
175: t.printStackTrace();
176: this .jScrlProperty.setViewportView(null);
177: }
178: } else {
179: this .jScrlProperty.setViewportView(null);
180: }
181: } else {
182: this .jScrlProperty.setViewportView(null);
183: }
184: }//GEN-LAST:event_jTreeDatabaseValueChanged
185:
186: // Variables declaration - do not modify//GEN-BEGIN:variables
187: private javax.swing.JSplitPane jSplitPane1;
188: private javax.swing.JScrollPane jScrlTree;
189: private javax.swing.JTextField jTextField1;
190: private javax.swing.JScrollPane jScrlProperty;
191: private javax.swing.JTree jTreeDatabase;
192: private javax.swing.JToolBar jToolBar1;
193: // End of variables declaration//GEN-END:variables
194:
195: }
|