1 19 package org.netbeans.modules.db.sql.visualeditor.querybuilder; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Dimension ; 23 import java.awt.Color ; 24 import java.awt.Point ; 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 ; 32 import java.beans.PropertyChangeEvent ; 33 import java.beans.PropertyChangeListener ; 34 35 import javax.swing.JTable ; 36 import javax.swing.JScrollPane ; 37 import javax.swing.BoxLayout ; 38 import javax.swing.JPanel ; 39 import javax.swing.JInternalFrame ; 40 import javax.swing.JPopupMenu ; 41 import javax.swing.JMenu ; 42 import javax.swing.JMenuItem ; 43 import javax.swing.BorderFactory ; 44 import javax.swing.JScrollBar ; 45 import javax.swing.JViewport ; 46 import javax.swing.SwingUtilities ; 47 import javax.swing.event.InternalFrameEvent ; 48 import javax.swing.event.InternalFrameListener ; 49 import javax.swing.event.InternalFrameAdapter ; 50 import javax.swing.table.DefaultTableModel ; 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 66 public class QueryBuilderInternalFrame extends JInternalFrame 67 implements ActionListener, KeyListener, 68 DragGestureListener, 69 DragSourceListener, 70 DropTargetListener 71 { 72 73 75 private boolean DEBUG = false; 76 77 private Object _dragObject; 78 private DropTarget _dropTarget; 79 80 private QueryBuilderTableModel _queryBuilderTableModel = null; 82 83 private DefaultGraphCell _graphCell = null; 85 86 private JPopupMenu _tableColumnPopup; 87 private TableNode _node; 89 private QueryBuilder _queryBuilder; 90 private QueryBuilderTable _qbTable; 91 92 private static int _lastX = 0, _lastY = 0; 94 95 96 98 public QueryBuilderInternalFrame(QueryBuilderTableModel queryBuilderTableModel, 99 QueryBuilder queryBuilder) 100 { 101 _dragObject = null; 102 _queryBuilderTableModel = queryBuilderTableModel; 104 _queryBuilder = queryBuilder; 105 106 _node = new TableNode(queryBuilderTableModel.getFullTableName(), 108 queryBuilderTableModel.getCorrName(), 109 _queryBuilder); 110 setResizable(true); 111 setFrameIcon(null); 112 setIconifiable(false); 113 114 addInternalFrameListener(new InternalFrameAdapter () { 116 public void internalFrameOpened(InternalFrameEvent e) { 117 try { 120 QueryBuilderInternalFrame.this.setSelected(true); 121 } catch(PropertyVetoException pve) { 122 } 123 }}); 124 125 this.setBackground(Color.white); 126 127 } 128 129 130 131 public void keyTyped(KeyEvent e) { 132 } 133 134 135 public void keyPressed(KeyEvent e) { 136 if ( DEBUG ) 137 System.out.println(" QBIF : key pressed called. " + "\n" ); _queryBuilder.handleKeyPress(e); 139 } 140 141 142 public void keyReleased(KeyEvent e) { 143 } 144 145 148 public void create() { 149 150 JPanel mainPanel = new JPanel (); 151 mainPanel.setLayout(new BoxLayout (mainPanel, BoxLayout.Y_AXIS)); 153 154 _qbTable = new QueryBuilderTable(_queryBuilderTableModel); 156 _qbTable.setBackground(Color.white); 157 161 164 JScrollPane sp = new JScrollPane (_qbTable); 166 169 sp.getViewport().setBackground(Color.white); 170 171 mainPanel.add(sp,BorderLayout.CENTER); 173 mainPanel.setBackground(Color.white); 174 175 getContentPane().add(mainPanel); 177 getContentPane().setBackground(Color.white); 178 179 DragSource dragSource = DragSource.getDefaultDragSource(); 180 181 dragSource.createDefaultDragGestureRecognizer( 182 _qbTable, DnDConstants.ACTION_MOVE, this); 186 _dropTarget = new DropTarget ( _qbTable, 187 DnDConstants.ACTION_MOVE, 188 this ); 189 190 pack(); 193 setSize(175,120); 194 setVisible(true); 195 } 196 197 200 JPopupMenu createTableColumnPopup() { 201 202 JPopupMenu tableColumnPopup; 203 JMenu menu, subMenu; 204 JMenuItem menuItem; 205 JMenuItem subMenuItem; 206 207 tableColumnPopup = new JPopupMenu (); 209 210 menuItem = new JMenuItem ( NbBundle.getMessage(QueryBuilderInternalFrame.class, "ADD_TO_QUERY") ); menuItem.addActionListener(this); 212 tableColumnPopup.add(menuItem); 213 214 menuItem = new JMenuItem ( NbBundle.getMessage(QueryBuilderInternalFrame.class, "SORT_ASCENDING") ); menuItem.addActionListener(this); 216 tableColumnPopup.add(menuItem); 217 218 menuItem = new JMenuItem ( NbBundle.getMessage(QueryBuilderInternalFrame.class, "SORT_DESCENDING") ); menuItem.addActionListener(this); 220 tableColumnPopup.add(menuItem); 221 222 menuItem = new JMenuItem ( NbBundle.getMessage(QueryBuilderInternalFrame.class, "REMOVE_FILTER") ); menuItem.addActionListener(this); 224 tableColumnPopup.add(menuItem); 225 226 return tableColumnPopup; 227 } 228 229 230 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 254 public String toString() { 255 return ""; } 257 258 260 String getTableName() { 261 return _queryBuilderTableModel.getTableName(); 262 } 263 264 String getTableSpec() { 265 return _queryBuilderTableModel.getTableSpec(); 266 } 267 268 String getFullTableName() { 269 return _queryBuilderTableModel.getFullTableName(); 270 } 271 272 static Point getLastLocation() { 273 return new Point (_lastX, _lastY); 274 } 275 276 static void resetLocation() { 277 _lastX=0; 278 _lastY=0; 279 } 280 281 283 public void setLocation(int x, int y) { 284 _lastX = x; 285 _lastY = y; 286 super.setLocation(x, y); 287 } 288 289 290 292 protected String getClassName(Object o) { 293 String classString = o.getClass().getName(); 294 int dotIndex = classString.lastIndexOf("."); return classString.substring(dotIndex+1); 296 } 297 298 299 301 306 public void actionPerformed(ActionEvent e) { 307 if ( DEBUG ) { 308 JMenuItem source = (JMenuItem )(e.getSource()); 309 String s = "Action event detected." + "\n" + " Event source: " + source.getText() + " (an instance of " + getClassName(source) + ")"; System.out.println (s + "\n"); } 315 } 316 317 333 334 335 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"); } 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 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"); } 384 String dragTableColumn = 386 _queryBuilderTableModel.getTableSpec() + "." + ( (String ) _qbTable.getValueAt ( row, column ) ); 388 e.startDrag ( DragSource.DefaultCopyDrop, new StringSelection (dragTableColumn), 390 this ); } 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"); } 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 msg = 411 NbBundle.getMessage(QueryBuilderInternalFrame.class, 412 "DRAG_AND_DROP_COLUMNS"); 413 NotifyDescriptor d = new NotifyDescriptor.Message ( 414 msg + 415 "\n\n", 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"); } 427 428 String dropTableColumn = 430 _queryBuilderTableModel.getTableSpec() + "." + ( (String ) _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 dragTableColumn = 437 (String )tr.getTransferData(stringFlavor); 438 439 if (DEBUG) { 440 System.out.println ( "dragTableColumnName = " + dragTableColumn + "\n"); System.out.println ( "dropTableColumnName = " + dropTableColumn + "\n"); } 443 String [] rel = new String [4]; 444 String [] res = dragTableColumn.split("\\."); 446 if ( res.length == 2 ) { 449 rel[0] = res[0] ; 451 rel[1] = res[1]; 453 } 454 else if ( res.length == 3 ) { 455 rel[0] = res[0] + "." + res[1]; 457 rel[1] = res[2]; 459 } 460 else { 461 String msg = 462 NbBundle.getMessage(QueryBuilderInternalFrame.class, "DRAG_AND_DROP_COLUMNS"); 463 NotifyDescriptor d = 464 new NotifyDescriptor.Message ( msg + "\n\n", NotifyDescriptor.ERROR_MESSAGE); DialogDisplayer.getDefault().notify(d); 466 _dragObject = null; 467 468 return; 469 } 470 471 rel[2] = _queryBuilderTableModel.getTableSpec(); 473 rel[3] = ( (String ) _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"); } 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 |