KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tools > mapping > reversedb2 > gui > JIFrmDatabase


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

15
16 package org.apache.ojb.tools.mapping.reversedb2.gui;
17 import java.awt.dnd.DragGestureListener JavaDoc;
18 import java.awt.dnd.DragSource JavaDoc;
19 import java.awt.dnd.DragSourceListener JavaDoc;
20
21 import org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel.DBMetaCatalogNode;
22 import org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel.DBMetaRootNode;
23 import org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel.DBMetaSchemaNode;
24 import org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel.DBMetaTableNode;
25 import org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel.DatabaseMetaDataTreeModel;
26 import org.apache.ojb.tools.mapping.reversedb2.dnd2.DragHelper;
27 import org.apache.ojb.tools.mapping.reversedb2.dnd2.DropTargetHelper;
28 import org.apache.ojb.tools.mapping.reversedb2.dnd2.ReverseDbNodesDragWorker;
29 import org.apache.ojb.tools.mapping.reversedb2.dnd2.ReverseDbNodesDropWorker;
30 import org.apache.ojb.tools.mapping.reversedb2.propertyEditors.PropertyEditor;
31 import org.apache.ojb.tools.mapping.reversedb2.propertyEditors.PropertyEditorTarget;
32
33 /**
34  *
35  * @author Administrator
36  */

37 public class JIFrmDatabase extends javax.swing.JInternalFrame JavaDoc
38 {
39     private java.sql.Connection JavaDoc jdbcConnection;
40     
41     private DragSource JavaDoc dragSource;
42     private DragGestureListener JavaDoc dgListener;
43     private DragSourceListener JavaDoc dsListener;
44     
45     private java.util.HashMap JavaDoc hmPropertyEditors = new java.util.HashMap JavaDoc();
46     
47     /** Creates new form JIFrmDatabaseMetadata */
48     public JIFrmDatabase(java.sql.Connection JavaDoc pjdbcConnection)
49     {
50         this.jdbcConnection = pjdbcConnection;
51         initComponents();
52         try
53         {
54             this.setTitle("Database: " + jdbcConnection.getMetaData().getURL());
55             DatabaseMetaDataTreeModel model = new DatabaseMetaDataTreeModel(this.jdbcConnection.getMetaData());
56             model.addStatusMessageListener(new org.apache.ojb.tools.mapping.reversedb2.events.StatusMessageAdapter()
57             {
58                 public void statusMessageReceived(final String JavaDoc s)
59                 {
60                     javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc()
61                     {
62                         public void run()
63                         {
64                             jTextField1.setText(s);
65                         }
66                     });
67                 }
68             });
69             this.jTreeDatabase.setModel(model);
70         }
71         catch (java.sql.SQLException JavaDoc sqlEx)
72         {
73             this.setTitle("Database: unknown");
74             this.jTreeDatabase.setModel(new javax.swing.tree.DefaultTreeModel JavaDoc(new javax.swing.tree.DefaultMutableTreeNode JavaDoc("Error...")));
75         }
76         
77         // new DatabaseTransferHandler().register(jTreeDatabase);
78
// enable dragging for the tree.
79
new DragHelper(new ReverseDbNodesDragWorker()).registerComponent(jTreeDatabase);
80         
81         // enable dropping for the tree.
82
DropTargetHelper helper = new DropTargetHelper();
83         helper.registerDropPasteWorker (new ReverseDbNodesDropWorker());
84         jTreeDatabase.setDropTarget(helper.getDropTarget ());
85
86     }
87     
88     private java.util.Collection JavaDoc getTableNodes(Object JavaDoc o)
89     {
90         if (o instanceof DBMetaRootNode ||
91             o instanceof DBMetaCatalogNode ||
92             o instanceof DBMetaSchemaNode)
93         {
94             java.util.ArrayList JavaDoc alTables = new java.util.ArrayList JavaDoc();
95             java.util.Enumeration JavaDoc e = ((javax.swing.tree.TreeNode JavaDoc)o).children();
96             while (e.hasMoreElements())
97             {
98                 alTables.addAll(getTableNodes(e.nextElement()));
99             }
100             return alTables;
101         }
102         else if (o instanceof DBMetaTableNode)
103         {
104             return java.util.Collections.singleton(o);
105         }
106         else
107         {
108             return null;
109         }
110     }
111     
112     /** This method is called from within the constructor to
113      * initialize the form.
114      * WARNING: Do NOT modify this code. The content of this method is
115      * always regenerated by the Form Editor.
116      */

117     private void initComponents()//GEN-BEGIN:initComponents
118
{
119         jSplitPane1 = new javax.swing.JSplitPane JavaDoc();
120         jScrlTree = new javax.swing.JScrollPane JavaDoc();
121         jTreeDatabase = new javax.swing.JTree JavaDoc();
122         jScrlProperty = new javax.swing.JScrollPane JavaDoc();
123         jToolBar1 = new javax.swing.JToolBar JavaDoc();
124         jTextField1 = new javax.swing.JTextField JavaDoc();
125
126         setClosable(true);
127         setIconifiable(true);
128         setMaximizable(true);
129         setResizable(true);
130         setTitle("Database");
131         setPreferredSize(new java.awt.Dimension JavaDoc(300, 300));
132         jTreeDatabase.setRootVisible(false);
133         jTreeDatabase.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener JavaDoc()
134         {
135             public void valueChanged(javax.swing.event.TreeSelectionEvent JavaDoc evt)
136             {
137                 jTreeDatabaseValueChanged(evt);
138             }
139         });
140
141         jScrlTree.setViewportView(jTreeDatabase);
142
143         jSplitPane1.setLeftComponent(jScrlTree);
144
145         jSplitPane1.setRightComponent(jScrlProperty);
146
147         getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);
148
149         jTextField1.setEditable(false);
150         jTextField1.setText("jTextField1");
151         jTextField1.setBorder(null);
152         jTextField1.setOpaque(false);
153         jToolBar1.add(jTextField1);
154
155         getContentPane().add(jToolBar1, java.awt.BorderLayout.SOUTH);
156
157         pack();
158     }//GEN-END:initComponents
159

160     private void jTreeDatabaseValueChanged (javax.swing.event.TreeSelectionEvent JavaDoc evt)//GEN-FIRST:event_jTreeDatabaseValueChanged
161
{//GEN-HEADEREND:event_jTreeDatabaseValueChanged
162
// Add your handling code here:
163
Object JavaDoc selectedObject = evt.getPath().getLastPathComponent();
164         if (selectedObject instanceof PropertyEditorTarget)
165         {
166             PropertyEditorTarget p = (PropertyEditorTarget)selectedObject;
167             if (p.getPropertyEditorClass() != null)
168             {
169                 try
170                 {
171                     if(!this.hmPropertyEditors.containsKey(p.getPropertyEditorClass()))
172                     {
173                         this.hmPropertyEditors.put(p.getPropertyEditorClass(), p.getPropertyEditorClass().newInstance());
174                     }
175                     PropertyEditor propertyEditor = (PropertyEditor)hmPropertyEditors.get(p.getPropertyEditorClass());
176                     this.jScrlProperty.setViewportView(propertyEditor);
177                     propertyEditor.setEditorTarget(p);
178                 }
179                 catch (Throwable JavaDoc t)
180                 {
181                     t.printStackTrace();
182                     this.jScrlProperty.setViewportView(null);
183                 }
184             }
185             else
186             {
187                 this.jScrlProperty.setViewportView(null);
188             }
189         }
190         else
191         {
192             this.jScrlProperty.setViewportView(null);
193         }
194     }//GEN-LAST:event_jTreeDatabaseValueChanged
195

196     
197     // Variables declaration - do not modify//GEN-BEGIN:variables
198
private javax.swing.JSplitPane JavaDoc jSplitPane1;
199     private javax.swing.JScrollPane JavaDoc jScrlTree;
200     private javax.swing.JTextField JavaDoc jTextField1;
201     private javax.swing.JScrollPane JavaDoc jScrlProperty;
202     private javax.swing.JTree JavaDoc jTreeDatabase;
203     private javax.swing.JToolBar JavaDoc jToolBar1;
204     // End of variables declaration//GEN-END:variables
205

206 }
207
Popular Tags