1 32 33 package it.businesslogic.ireport.data; 34 35 import it.businesslogic.ireport.FieldsProviderEditor; 36 import it.businesslogic.ireport.JRField; 37 import it.businesslogic.ireport.ReportClassLoader; 38 import it.businesslogic.ireport.gui.JBTreeCellRenderer; 39 import it.businesslogic.ireport.gui.MainFrame; 40 import it.businesslogic.ireport.gui.ReportQueryDialog; 41 import it.businesslogic.ireport.gui.TreeJRField; 42 import it.businesslogic.ireport.data.hibernate.HQLFieldsReader; 43 import it.businesslogic.ireport.util.Misc; 44 import java.util.ArrayList ; 45 import java.util.List ; 46 import java.util.Vector ; 47 import javax.swing.JTable ; 48 import javax.swing.SwingUtilities ; 49 import javax.swing.table.DefaultTableModel ; 50 import javax.swing.tree.DefaultMutableTreeNode ; 51 import javax.swing.tree.DefaultTreeModel ; 52 import javax.swing.tree.TreePath ; 53 import it.businesslogic.ireport.util.I18n; 54 55 59 public class BeanInspectorPanel extends javax.swing.JPanel implements FieldsProviderEditor { 60 61 private boolean pathOnDescription = false; 62 private JTable jTableFields = null; 63 private boolean comboVisible = true; 64 private ReportQueryDialog reportQueryDialog = null; 65 66 67 68 public BeanInspectorPanel() { 69 initComponents(); 70 71 DefaultTreeModel dttm = (DefaultTreeModel )jTree1.getModel(); 72 DefaultMutableTreeNode root = new DefaultMutableTreeNode (); 73 jTree1.setModel(new DefaultTreeModel ( root )); 74 75 jTree1.setCellRenderer( new JBTreeCellRenderer()); 76 77 applyI18n(); 78 } 79 80 81 public void setClassNames(List classNames) 82 { 83 if (classNames == null) return; 84 85 jComboBox1.removeAllItems(); 86 for (int i=0; i<classNames.size(); ++i) 87 { 88 jComboBox1.addItem( classNames.get(i)); 89 } 90 91 jComboBox1ActionPerformed(null); 92 } 93 94 97 public void setClassName(String className) 98 { 99 DefaultMutableTreeNode root = (DefaultMutableTreeNode )jTree1.getModel().getRoot(); 100 root.removeAllChildren(); 101 jTree1.updateUI(); 102 if (className != null && className.trim().length() > 0) 103 { 104 exploreBean( root,className, ""); 105 } 106 } 107 108 113 private void initComponents() { 115 java.awt.GridBagConstraints gridBagConstraints; 116 117 jPanel1 = new javax.swing.JPanel (); 118 jLabel1 = new javax.swing.JLabel (); 119 jComboBox1 = new javax.swing.JComboBox (); 120 jScrollPane1 = new javax.swing.JScrollPane (); 121 jTree1 = new javax.swing.JTree (); 122 jPanel2 = new javax.swing.JPanel (); 123 jButton2 = new javax.swing.JButton (); 124 jButton3 = new javax.swing.JButton (); 125 126 setLayout(new java.awt.GridBagLayout ()); 127 128 jPanel1.setLayout(new java.awt.GridBagLayout ()); 129 130 jLabel1.setText("Bean"); 131 gridBagConstraints = new java.awt.GridBagConstraints (); 132 gridBagConstraints.insets = new java.awt.Insets (4, 4, 4, 4); 133 jPanel1.add(jLabel1, gridBagConstraints); 134 135 jComboBox1.setMinimumSize(new java.awt.Dimension (51, 22)); 136 jComboBox1.addActionListener(new java.awt.event.ActionListener () { 137 public void actionPerformed(java.awt.event.ActionEvent evt) { 138 jComboBox1ActionPerformed(evt); 139 } 140 }); 141 142 gridBagConstraints = new java.awt.GridBagConstraints (); 143 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 144 gridBagConstraints.weightx = 1.0; 145 gridBagConstraints.insets = new java.awt.Insets (4, 0, 4, 0); 146 jPanel1.add(jComboBox1, gridBagConstraints); 147 148 gridBagConstraints = new java.awt.GridBagConstraints (); 149 gridBagConstraints.gridx = 0; 150 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 151 gridBagConstraints.weightx = 1.0; 152 add(jPanel1, gridBagConstraints); 153 154 jTree1.setRootVisible(false); 155 jTree1.addMouseListener(new java.awt.event.MouseAdapter () { 156 public void mouseClicked(java.awt.event.MouseEvent evt) { 157 jTree1MouseClicked1(evt); 158 } 159 }); 160 161 jScrollPane1.setViewportView(jTree1); 162 163 gridBagConstraints = new java.awt.GridBagConstraints (); 164 gridBagConstraints.gridx = 0; 165 gridBagConstraints.gridwidth = 3; 166 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 167 gridBagConstraints.weightx = 1.0; 168 gridBagConstraints.weighty = 1.0; 169 gridBagConstraints.insets = new java.awt.Insets (0, 4, 0, 0); 170 add(jScrollPane1, gridBagConstraints); 171 172 jPanel2.setLayout(new java.awt.GridBagLayout ()); 173 174 jButton2.setText("Add selected field(s)"); 175 jButton2.addActionListener(new java.awt.event.ActionListener () { 176 public void actionPerformed(java.awt.event.ActionEvent evt) { 177 jButton2ActionPerformed(evt); 178 } 179 }); 180 181 gridBagConstraints = new java.awt.GridBagConstraints (); 182 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST; 183 gridBagConstraints.weightx = 1.0; 184 gridBagConstraints.insets = new java.awt.Insets (2, 0, 2, 4); 185 jPanel2.add(jButton2, gridBagConstraints); 186 187 jButton3.setText("Clear fields list"); 188 jButton3.addActionListener(new java.awt.event.ActionListener () { 189 public void actionPerformed(java.awt.event.ActionEvent evt) { 190 jButton2ActionPerformed1(evt); 191 } 192 }); 193 194 gridBagConstraints = new java.awt.GridBagConstraints (); 195 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST; 196 gridBagConstraints.insets = new java.awt.Insets (2, 0, 2, 0); 197 jPanel2.add(jButton3, gridBagConstraints); 198 199 gridBagConstraints = new java.awt.GridBagConstraints (); 200 gridBagConstraints.gridx = 0; 201 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 202 gridBagConstraints.weightx = 1.0; 203 gridBagConstraints.insets = new java.awt.Insets (0, 4, 0, 0); 204 add(jPanel2, gridBagConstraints); 205 206 } 208 private void jButton2ActionPerformed1(java.awt.event.ActionEvent evt) { 210 DefaultTableModel dtm = (DefaultTableModel )getJTableFields().getModel(); 211 dtm.setRowCount(0); 212 213 } 215 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 217 218 if (MainFrame.getMainInstance().getActiveReportFrame() == null || getJTableFields() == null){ 220 return; 221 } 222 223 DefaultTableModel dtm = (DefaultTableModel )getJTableFields().getModel(); 224 225 TreePath [] paths = jTree1.getSelectionPaths(); 226 if (paths == null) return; 227 for (int i=0; i<paths.length; ++i) 228 { 229 boolean valid = true; 230 TreePath tp = paths[i]; 231 232 TreeJRField tjrf = (TreeJRField)((DefaultMutableTreeNode )tp.getLastPathComponent()).getUserObject(); 233 String returnType = Misc.getJRFieldType( tjrf.getObj().getName() ); 234 it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(tjrf.getField().getName(), returnType); 235 field.setDescription(tjrf.getField().getDescription() ); 236 Vector row = new Vector (); 237 row.addElement(field); 238 row.addElement(field.getClassType()); 239 row.addElement(field.getDescription()); 240 241 if (isComboVisible() && jComboBox1.getSelectedItem() instanceof FieldClassWrapper) 242 { 243 FieldClassWrapper cw = (FieldClassWrapper)jComboBox1.getSelectedItem(); 244 field.setName( cw.getFieldName() + "." + field.getDescription() ); 245 field.setDescription( field.getName() ); 246 } 247 248 boolean found = fieldAlreadyExists(field); 250 String baseName = field.getName(); 251 for (int j=1; isPathOnDescription() && found; ++j) 252 { 253 field.setName( baseName + "_" + j); 254 found = fieldAlreadyExists(field); 255 } 256 257 if (!found) 258 { 259 dtm.addRow(row); 260 getJTableFields().getSelectionModel().addSelectionInterval( getJTableFields().getRowCount()-1, getJTableFields().getRowCount()-1 ); 261 } 262 } 263 } 265 private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) { 267 Object obj = jComboBox1.getSelectedItem(); 268 if (obj == null) 269 { 270 setClassName(null); 271 return; 272 } 273 if (obj instanceof String ) setClassName( (String )obj ); 274 if (obj instanceof FieldClassWrapper) setClassName( ((FieldClassWrapper)obj).getClassType()); 275 276 } 278 279 private boolean fieldAlreadyExists(JRField field) 280 { 281 boolean found = false; 282 for (int j=0; j<getJTableFields().getRowCount(); ++j) 283 { 284 Object ff = getJTableFields().getValueAt(j, 0); 285 if ( ff instanceof it.businesslogic.ireport.JRField ) 286 { 287 if ( ((it.businesslogic.ireport.JRField)ff).getName().equals(field.getName())) 288 { 289 found = true; 290 break; 291 } 292 } 293 } 294 295 return found; 296 } 297 298 private void jTree1MouseClicked1(java.awt.event.MouseEvent evt) { 300 301 if (evt.getClickCount() == 2 && evt.getButton() == evt.BUTTON1) 302 { 303 DefaultMutableTreeNode tn = (DefaultMutableTreeNode )jTree1.getSelectionPath().getLastPathComponent(); 304 305 if (tn.getChildCount()>0) return; 306 307 314 if (tn.getUserObject() instanceof TreeJRField) 315 { 316 TreeJRField jrf = (TreeJRField)tn.getUserObject(); 317 if (!jrf.getObj().isPrimitive() && !jrf.getObj().getName().startsWith("java.lang.")) 318 { 319 exploreBean(tn, jrf.getObj().getName(), 320 isPathOnDescription() ? 321 Misc.nvl( jrf.getField().getDescription() , "") : 322 Misc.nvl( jrf.getField().getName() , "") ); 323 } 324 } 325 } 326 327 } 329 330 public void exploreBean(DefaultMutableTreeNode root, String classname, String parentPath) 331 { 332 try { 333 334 root.removeAllChildren(); 335 if (parentPath.length() > 0) parentPath += "."; 336 337 MainFrame.reportClassLoader.rescanLibDirectory(); 338 Class clazz = Class.forName(classname,true,MainFrame.reportClassLoader); 339 340 java.beans.PropertyDescriptor [] pd = org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptors(clazz); 341 for (int nd =0; nd < pd.length; ++nd) 342 { 343 String fieldName = pd[nd].getName(); 344 if (pd[nd].getPropertyType() != null && pd[nd].getReadMethod() != null) 345 { 346 String returnType = pd[nd].getPropertyType().getName(); 347 it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField( Misc.getJRFieldType( fieldName ), returnType); 348 if (isPathOnDescription()) 349 { 350 field.setDescription(parentPath + fieldName); 351 } 352 else 353 { 354 field.setName(parentPath + fieldName); 355 } 356 357 TreeJRField jtf = new TreeJRField(); 358 359 jtf.setField( field ); 360 jtf.setObj( pd[nd].getPropertyType() ); 361 362 boolean bChildrens = true; 363 if (pd[nd].getPropertyType().isPrimitive() || pd[nd].getPropertyType().getName().startsWith("java.lang.")) 364 { 365 bChildrens = false; 366 } 367 root.add(new DefaultMutableTreeNode (jtf, bChildrens)); 368 } 369 } 370 371 jTree1.expandPath( new TreePath (root.getPath()) ); 372 jTree1.updateUI(); 373 374 } catch (ClassNotFoundException cnf) 375 { 376 javax.swing.JOptionPane.showMessageDialog(this, 377 I18n.getFormattedString("messages.BeanInspectorPanel.classNotFoundError", 378 "Class not found error!!\nCheck your classpath and retry!\n{0}", new Object []{cnf.getMessage()}), 379 I18n.getString("message.title.error","Error"),javax.swing.JOptionPane.ERROR_MESSAGE); 380 return; 381 } catch (Exception ex) 382 { 383 javax.swing.JOptionPane.showMessageDialog(this, ex.getMessage() ,I18n.getString("message.title.error","Error"),javax.swing.JOptionPane.ERROR_MESSAGE); 384 return; 385 } 386 } 387 388 public JTable getJTableFields() { 389 return jTableFields; 390 } 391 392 public void setJTableFields(JTable jTableFields) { 393 this.jTableFields = jTableFields; 394 } 395 396 public boolean isComboVisible() { 397 return comboVisible; 398 } 399 400 public void setComboVisible(boolean comboVisible) { 401 this.comboVisible = comboVisible; 402 jPanel1.setVisible( comboVisible ); 403 } 404 405 public boolean isPathOnDescription() { 406 return pathOnDescription; 407 } 408 409 public void setPathOnDescription(boolean pathOnDescription) { 410 this.pathOnDescription = pathOnDescription; 411 } 412 413 private javax.swing.JButton jButton2; 415 private javax.swing.JButton jButton3; 416 private javax.swing.JComboBox jComboBox1; 417 private javax.swing.JLabel jLabel1; 418 private javax.swing.JPanel jPanel1; 419 private javax.swing.JPanel jPanel2; 420 private javax.swing.JScrollPane jScrollPane1; 421 private javax.swing.JTree jTree1; 422 424 425 426 427 public void applyI18n(){ 428 jButton2.setText(I18n.getString("beanInspectorPanel.button2","Add selected field(s)")); 430 jButton3.setText(I18n.getString("beanInspectorPanel.button3","Clear fields list")); 431 jLabel1.setText(I18n.getString("beanInspectorPanel.label1","Bean")); 432 } 434 435 int lastExecution = 0; 436 437 public void queryChanged(String newQuery) { 438 439 lastExecution++; 440 int thisExecution = lastExecution; 441 443 String error_msg = ""; 444 lastExecution++; 445 446 int in = lastExecution; 447 448 if (getReportQueryDialog() == null) return; 449 450 getReportQueryDialog().getJLabelStatusSQL().setText("Executing HQL query...."); 451 453 try { 454 Thread.currentThread().setContextClassLoader( MainFrame.getMainInstance().getReportClassLoader()); 455 } catch (Exception ex) 456 { 457 ex.printStackTrace(); 458 } 459 460 if (in < lastExecution) return; 462 HQLFieldsReader hqlFR = new HQLFieldsReader(newQuery, getReportQueryDialog().getSubDataset().getParameters()); 463 464 if (in < lastExecution) return; 466 try { 467 Vector fields = hqlFR.readFields(); 468 469 List columns = new ArrayList (); 470 for (int i=0; i<fields.size(); ++i) 471 { 472 JRField field = (JRField)fields.elementAt(i); 473 columns.add( new Object []{field, field.getClassType(), field.getDescription()} ); 474 } 475 476 Vector v = hqlFR.getNotScalars(); 477 478 if (v.size() == 0) v = null; 479 480 if (in < lastExecution) return; setBeanExplorerFromWorker(v,true,true); 482 483 if (in < lastExecution) return; setColumnsFromWorker(columns); 485 486 } catch (Exception ex) 487 { 488 ex.printStackTrace(); 489 setBeanExplorerFromWorker(null,true,true); 490 setColumnErrorFromWork( "Error: " + ex.getMessage() ); 491 } 492 493 getReportQueryDialog().getJLabelStatusSQL().setText("Ready"); 494 } 495 496 505 protected void setColumnErrorFromWork( final String error_msg) { 506 try { 507 508 Runnable r = new Runnable () { 509 public void run() { 510 getReportQueryDialog().setColumnsError( error_msg ); 511 getReportQueryDialog().getSQLExpressionArea().requestFocusInWindow(); 512 } 513 }; 514 515 if (SwingUtilities.isEventDispatchThread()) { r.run(); } 516 else { SwingUtilities.invokeAndWait( r ); } 517 518 } catch(Exception e) { 519 } 521 } 522 523 532 protected void setColumnsFromWorker( final List columns) { 533 try { 534 535 Runnable r = new Runnable () { 536 public void run() { 537 getReportQueryDialog().setColumns( columns ); 538 getReportQueryDialog().getSQLExpressionArea().requestFocusInWindow(); 539 } 540 }; 541 542 if (SwingUtilities.isEventDispatchThread()) { r.run(); } 543 else { SwingUtilities.invokeAndWait( r ); } 544 545 } catch(Exception e) { 546 } 548 } 549 550 555 protected void setBeanExplorerFromWorker( final Vector v,final boolean pathOnDescription, final boolean useCombo){ 556 try { 557 SwingUtilities.invokeAndWait( new Runnable () { 558 public void run() { 559 Vector v1 = v; 560 if (v1 == null) v1 = new Vector (); 561 562 setComboVisible(useCombo); 563 setPathOnDescription(pathOnDescription); 564 setClassNames(v1); 565 566 getReportQueryDialog().getSQLExpressionArea().requestFocusInWindow(); 567 getReportQueryDialog().getSQLExpressionArea().requestFocus(); 568 } 569 } ); 570 } catch(Exception e) { 571 e.printStackTrace(); 573 } 574 } 575 576 public ReportQueryDialog getReportQueryDialog() { 577 return reportQueryDialog; 578 } 579 580 public void setReportQueryDialog(ReportQueryDialog reportQueryDialog) { 581 this.reportQueryDialog = reportQueryDialog; 582 } 583 584 585 } 586 | Popular Tags |