KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querybuilder > QueryBuilderInternalFrame


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 package org.netbeans.modules.db.sql.visualeditor.querybuilder;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Point JavaDoc;
25 import java.awt.event.*;
26 import java.awt.datatransfer.*;
27 import java.awt.dnd.*;
28 import java.io.*;
29
30
31 import java.beans.PropertyVetoException JavaDoc;
32 import java.beans.PropertyChangeEvent JavaDoc;
33 import java.beans.PropertyChangeListener JavaDoc;
34
35 import javax.swing.JTable JavaDoc;
36 import javax.swing.JScrollPane JavaDoc;
37 import javax.swing.BoxLayout JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.JInternalFrame JavaDoc;
40 import javax.swing.JPopupMenu JavaDoc;
41 import javax.swing.JMenu JavaDoc;
42 import javax.swing.JMenuItem JavaDoc;
43 import javax.swing.BorderFactory JavaDoc;
44 import javax.swing.JScrollBar JavaDoc;
45 import javax.swing.JViewport JavaDoc;
46 import javax.swing.SwingUtilities JavaDoc;
47 import javax.swing.event.InternalFrameEvent JavaDoc;
48 import javax.swing.event.InternalFrameListener JavaDoc;
49 import javax.swing.event.InternalFrameAdapter JavaDoc;
50 import javax.swing.table.DefaultTableModel JavaDoc;
51
52 import org.openide.NotifyDescriptor;
53 import org.openide.DialogDisplayer;
54
55 import org.openide.util.NbBundle;
56
57 import com.jgraph.graph.DefaultGraphCell;
58
59 import org.netbeans.modules.db.sql.visualeditor.querymodel.Predicate;
60 import org.netbeans.modules.db.sql.visualeditor.querymodel.SQLQueryFactory;
61
62 /**
63  * A class that implements a graph node, representing a DB table
64  * @author Sanjay Dhamankar, Jim Davidson
65  */

66 public class QueryBuilderInternalFrame extends JInternalFrame JavaDoc
67         implements ActionListener, KeyListener,
68                     DragGestureListener,
69                     DragSourceListener,
70                     DropTargetListener
71 {
72
73     // Private variables
74

75     private boolean DEBUG = false;
76
77     private Object JavaDoc _dragObject;
78     private DropTarget _dropTarget;
79
80     // private QueryBuilderInternalFrame _relatedFrame = null;
81
private QueryBuilderTableModel _queryBuilderTableModel = null;
82
83     // _graphCell is a pointer to the JGraph cell this interal frame represents
84
private DefaultGraphCell _graphCell = null;
85
86     private JPopupMenu JavaDoc _tableColumnPopup;
87     private TableNode _node; // for property sheet
88

89     private QueryBuilder _queryBuilder;
90     private QueryBuilderTable _qbTable;
91
92     // Record location of last placement, to aid layout
93
private static int _lastX = 0, _lastY = 0;
94
95
96     // Constructor
97

98     public QueryBuilderInternalFrame(QueryBuilderTableModel queryBuilderTableModel,
99                                      QueryBuilder queryBuilder)
100     {
101         _dragObject = null;
102         // Set some private variables
103
_queryBuilderTableModel = queryBuilderTableModel;
104         _queryBuilder = queryBuilder;
105
106         // Create the node that will be used for the property sheet
107
_node = new TableNode(queryBuilderTableModel.getFullTableName(),
108                               queryBuilderTableModel.getCorrName(),
109                               _queryBuilder);
110         setResizable(true);
111         setFrameIcon(null);
112         setIconifiable(false);
113
114         // Add an anonymous listener for the internalFrameOpened event
115
addInternalFrameListener(new InternalFrameAdapter JavaDoc() {
116             public void internalFrameOpened(InternalFrameEvent JavaDoc e) {
117                 // As soon as a frame is opened select it.
118
// This doesn't seem to work.
119
try {
120                     QueryBuilderInternalFrame.this.setSelected(true);
121                 } catch(PropertyVetoException JavaDoc pve) {
122                 }
123             }});
124
125         this.setBackground(Color.white);
126
127     }
128
129
130     /** Handle the key typed event */
131     public void keyTyped(KeyEvent e) {
132     }
133
134     /** Handle the key pressed event from the internal frame. */
135     public void keyPressed(KeyEvent e) {
136         if ( DEBUG )
137             System.out.println(" QBIF : key pressed called. " + "\n" ); // NOI18N
138
_queryBuilder.handleKeyPress(e);
139     }
140
141     /** Handle the key released event from the sql text area. */
142     public void keyReleased(KeyEvent e) {
143     }
144
145     // Initialize various aspects of the internal frame:
146
// main panel, model, popup listeners, scrollpane...
147

148     public void create() {
149         
150         JPanel JavaDoc mainPanel = new JPanel JavaDoc();
151         // mainPanel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
152
mainPanel.setLayout(new BoxLayout JavaDoc(mainPanel, BoxLayout.Y_AXIS));
153         
154         // Create a JTable component, with the specified TableModel behind it
155
_qbTable = new QueryBuilderTable(_queryBuilderTableModel);
156         _qbTable.setBackground(Color.white);
157         /* Currently not implemented
158         _tableColumnPopup = createTableColumnPopup();
159         MouseListener tableColumnPopupListener = new TableColumnPopupListener();
160         */

161         // Why add the listener on the panel, why not on its child the ScrollPane (see below)
162
// mainPanel.addMouseListener(tableColumnPopupListener);
163

164         // Wrap the JTable in a JScrollPane
165
JScrollPane JavaDoc sp = new JScrollPane JavaDoc(_qbTable);
166         /* Currently not implemented
167         sp.addMouseListener(tableColumnPopupListener);
168         */

169         sp.getViewport().setBackground(Color.white);
170         
171         // Wrap the JScrollPane in a JPanel
172
mainPanel.add(sp,BorderLayout.CENTER);
173         mainPanel.setBackground(Color.white);
174         
175         // And add the JPanel to the content pane of the internal frame
176
getContentPane().add(mainPanel);
177         getContentPane().setBackground(Color.white);
178         
179         DragSource dragSource = DragSource.getDefaultDragSource();
180
181         dragSource.createDefaultDragGestureRecognizer(
182                     _qbTable, // component where drag originates
183
DnDConstants.ACTION_MOVE, // actions
184
this); // drag gesture recognizer
185

186         _dropTarget = new DropTarget ( _qbTable,
187                             DnDConstants.ACTION_MOVE,
188                             this );
189
190         // Per JInternalFrame tutorial, it's importnat to set the size of the internal frame
191
// (with pack, setSize, or setBounds) and make it visible (with setVisible or show)
192
pack();
193         setSize(175,120);
194         setVisible(true);
195     }
196
197     /**
198      * Create a popup menu - XXX - all items are NOP
199      */

200     JPopupMenu JavaDoc createTableColumnPopup() {
201
202         JPopupMenu JavaDoc tableColumnPopup;
203         JMenu JavaDoc menu, subMenu;
204         JMenuItem JavaDoc menuItem;
205         JMenuItem JavaDoc subMenuItem;
206         
207         // Create the popup menu.
208
tableColumnPopup = new JPopupMenu JavaDoc();
209         
210         menuItem = new JMenuItem JavaDoc ( NbBundle.getMessage(QueryBuilderInternalFrame.class, "ADD_TO_QUERY") ); // NOI18N
211
menuItem.addActionListener(this);
212         tableColumnPopup.add(menuItem);
213         
214         menuItem = new JMenuItem JavaDoc ( NbBundle.getMessage(QueryBuilderInternalFrame.class, "SORT_ASCENDING") ); // NOI18N
215
menuItem.addActionListener(this);
216         tableColumnPopup.add(menuItem);
217         
218         menuItem = new JMenuItem JavaDoc ( NbBundle.getMessage(QueryBuilderInternalFrame.class, "SORT_DESCENDING") ); // NOI18N
219
menuItem.addActionListener(this);
220         tableColumnPopup.add(menuItem);
221         
222         menuItem = new JMenuItem JavaDoc ( NbBundle.getMessage(QueryBuilderInternalFrame.class, "REMOVE_FILTER") ); // NOI18N
223
menuItem.addActionListener(this);
224         tableColumnPopup.add(menuItem);
225         
226         return tableColumnPopup;
227     }
228
229
230     // Accessors/Mutators
231

232     public void setGraphCell(DefaultGraphCell graphCell)
233     {
234         _graphCell = graphCell;
235     }
236
237     public DefaultGraphCell getGraphCell()
238     {
239         return(_graphCell);
240     }
241
242     public QueryBuilderTableModel getQueryBuilderTableModel()
243     {
244         return(_queryBuilderTableModel);
245     }
246
247     public TableNode getNode() {
248         return _node;
249     }
250     
251     /**
252      * Suppress label printing in graph
253      */

254     public String JavaDoc toString() {
255         return ""; // NOI18N
256
}
257     
258     // Convenience methods -- return tablename/tablespec from associated model
259

260     String JavaDoc getTableName() {
261         return _queryBuilderTableModel.getTableName();
262     }
263
264     String JavaDoc getTableSpec() {
265         return _queryBuilderTableModel.getTableSpec();
266     }
267
268     String JavaDoc getFullTableName() {
269         return _queryBuilderTableModel.getFullTableName();
270     }
271
272     static Point JavaDoc getLastLocation() {
273         return new Point JavaDoc(_lastX, _lastY);
274     }
275
276     static void resetLocation() {
277         _lastX=0;
278         _lastY=0;
279     }
280     
281     // Record the new location, before passing on the set method
282

283     public void setLocation(int x, int y) {
284         _lastX = x;
285         _lastY = y;
286         super.setLocation(x, y);
287     }
288     
289
290     // Returns just the class name -- no package info.
291

292     protected String JavaDoc getClassName(Object JavaDoc o) {
293         String JavaDoc classString = o.getClass().getName();
294         int dotIndex = classString.lastIndexOf("."); // NOI18N
295
return classString.substring(dotIndex+1);
296     }
297
298
299     // Event handlers
300

301     /**
302      * ActionListener interface implementation
303      * This would presumably handle menu selections, but none of them actually
304      * do anything now.
305      */

306     public void actionPerformed(ActionEvent e) {
307         if ( DEBUG ) {
308             JMenuItem JavaDoc source = (JMenuItem JavaDoc)(e.getSource());
309             String JavaDoc s = "Action event detected." // NOI18N
310
+ "\n" // NOI18N
311
+ " Event source: " + source.getText() // NOI18N
312
+ " (an instance of " + getClassName(source) + ")"; // NOI18N
313
System.out.println (s + "\n"); // NOI18N
314
}
315     }
316
317 // public void itemStateChanged(ItemEvent e) {
318
// if ( DEBUG )
319
// {
320
// JMenuItem source = (JMenuItem)(e.getSource());
321
// String s = "Item event detected."
322
// + "\n"
323
// + " Event source: " + source.getText()
324
// + " (an instance of " + getClassName(source) + ")"
325
// + "\n"
326
// + " New state: "
327
// + ((e.getStateChange() == ItemEvent.SELECTED) ?
328
// "selected":"unselected");
329
// System.out.println (s + "\n");
330
// }
331
// }
332

333
334
335     // Inner class for popup menu
336

337     class TableColumnPopupListener extends MouseAdapter {
338         public void mousePressed(MouseEvent e) {
339             maybeShowPopup(e);
340         }
341
342         public void mouseReleased(MouseEvent e) {
343             maybeShowPopup(e);
344         }
345
346         private void maybeShowPopup(MouseEvent e) {
347             if (e.isPopupTrigger()) {
348                 _tableColumnPopup.show(e.getComponent(),
349                                        e.getX(), e.getY());
350             }
351         }
352     }
353
354     public void dragGestureRecognized(DragGestureEvent e) {
355
356         if (DEBUG) {
357             System.out.println (" Component point " + e.getDragOrigin() + "\n"); // NOI18N
358
}
359         int row = _qbTable.rowAtPoint ( e.getDragOrigin() );
360         int column = _qbTable.columnAtPoint ( e.getDragOrigin() );
361
362         _dragObject = this;
363
364         if ( ( row < 0 ) || ( column < 2 ) ) {
365             /*
366               String msg =
367               NbBundle.getMessage(QueryBuilderInternalFrame.class,
368               "DRAG_AND_DROP_COLUMNS");
369               NotifyDescriptor d = new NotifyDescriptor.Message (
370               msg +
371               "\n\n", // NOI18N
372               NotifyDescriptor.ERROR_MESSAGE);
373               DialogDisplayer.getDefault().notify(d);
374             
375             */

376
377             return;
378         }
379
380         if (DEBUG) {
381             System.out.println (" Table row " + row + " Table column " + column +
382                                 " Object " + _qbTable.getValueAt ( row, column ) + "\n"); // NOI18N
383
}
384         // drag anything ...
385
String JavaDoc dragTableColumn =
386             // used to be _queryBuilderTableModel.getFullTableName(), but that failed dragging aliased tables
387
_queryBuilderTableModel.getTableSpec() + "." + ( (String JavaDoc) _qbTable.getValueAt ( row, column ) );
388         e.startDrag ( DragSource.DefaultCopyDrop, // cursor
389
new StringSelection (dragTableColumn),
390                       this ); // drag source listener
391
}
392
393     public void dragDropEnd(DragSourceDropEvent e) {}
394     public void dragEnter(DragSourceDragEvent e) {}
395     public void dragExit(DragSourceEvent e) {}
396     public void dragOver(DragSourceDragEvent e) {}
397     public void dropActionChanged(DragSourceDragEvent e) {}
398
399
400     public void drop(DropTargetDropEvent e) {
401
402         try {
403             if (DEBUG) {
404                 System.out.println (" Component point " + e.getLocation() + "\n"); // NOI18N
405
}
406             int row = _qbTable.rowAtPoint ( e.getLocation() );
407             int column = _qbTable.columnAtPoint ( e.getLocation() );
408
409             if ( ( row < 0 ) || ( column < 2 ) || ( _dragObject == this ) ) {
410                 String JavaDoc msg =
411                     NbBundle.getMessage(QueryBuilderInternalFrame.class,
412                                         "DRAG_AND_DROP_COLUMNS");
413                 NotifyDescriptor d = new NotifyDescriptor.Message (
414                     msg +
415                     "\n\n", // NOI18N
416
NotifyDescriptor.ERROR_MESSAGE);
417                 DialogDisplayer.getDefault().notify(d);
418             
419                 _dragObject = null;
420                 return;
421             }
422
423             if (DEBUG) {
424                 System.out.println (" Table row " + row + " Table column " + column +
425                                     " Object " + _qbTable.getValueAt ( row, column ) + "\n"); // NOI18N
426
}
427
428             // drag anything ...
429
String JavaDoc dropTableColumn =
430                 _queryBuilderTableModel.getTableSpec() + "." + ( (String JavaDoc) _qbTable.getValueAt ( row, column ) );
431
432             DataFlavor stringFlavor = DataFlavor.stringFlavor;
433             Transferable tr = e.getTransferable();
434
435             if(e.isDataFlavorSupported(stringFlavor) && e.isLocalTransfer()) {
436                 String JavaDoc dragTableColumn =
437                     (String JavaDoc)tr.getTransferData(stringFlavor);
438
439                 if (DEBUG) {
440                     System.out.println ( "dragTableColumnName = " + dragTableColumn + "\n"); // NOI18N
441
System.out.println ( "dropTableColumnName = " + dropTableColumn + "\n"); // NOI18N
442
}
443                 String JavaDoc[] rel = new String JavaDoc[4];
444                 String JavaDoc[] res = dragTableColumn.split("\\."); // NOI18N
445

446                 // it has to be a column in the format
447
// schema.table.column, if not reject it.
448
if ( res.length == 2 ) {
449                     // table
450
rel[0] = res[0] ;
451                     // column
452
rel[1] = res[1];
453                 }
454                 else if ( res.length == 3 ) {
455                     // schema.table
456
rel[0] = res[0] + "." + res[1];
457                     // column
458
rel[1] = res[2];
459                 }
460                 else {
461                     String JavaDoc msg =
462                         NbBundle.getMessage(QueryBuilderInternalFrame.class, "DRAG_AND_DROP_COLUMNS");
463                     NotifyDescriptor d =
464                         new NotifyDescriptor.Message ( msg + "\n\n", NotifyDescriptor.ERROR_MESSAGE); // NOI18N
465
DialogDisplayer.getDefault().notify(d);
466                     _dragObject = null;
467              
468                     return;
469                 }
470
471                 // 'schema.table' OR just 'table'
472
rel[2] = _queryBuilderTableModel.getTableSpec();
473                 // 'column'
474
rel[3] = ( (String JavaDoc) _qbTable.getValueAt ( row, column ) );
475
476                 if (DEBUG) {
477                     System.out.println (
478                         " rel[0] = " + rel[0] +
479                         " rel[1] = " + rel[1] +
480                         " rel[2] = " + rel[2] +
481                         " rel[3] = " + rel[3] + "\n"); // NOI18N
482
}
483                 Predicate pred = SQLQueryFactory.createPredicate(rel);
484                 _queryBuilder._queryModel.addOrCreateAndExpression ( pred );
485                 if (DEBUG) {
486                     System.out.println (
487                         _queryBuilder._queryModel.getWhere ().genText () );
488                 }
489                 _queryBuilder.generate();
490                 e.acceptDrop(DnDConstants.ACTION_MOVE);
491                 e.dropComplete(true);
492
493             }
494             else {
495                 e.rejectDrop();
496             }
497             _dragObject = null;
498         }
499         catch(IOException ioe) {
500             ioe.printStackTrace();
501         }
502         catch(UnsupportedFlavorException ufe) {
503             ufe.printStackTrace();
504         }
505     }
506     public void dragEnter(DropTargetDragEvent e) { }
507     public void dragExit(DropTargetEvent e) { }
508     public void dragOver(DropTargetDragEvent e) { }
509     public void dropActionChanged(DropTargetDragEvent e) { }
510 }
511
Popular Tags