1 14 package org.compiere.apps.search; 15 16 import java.awt.BorderLayout ; 17 import java.awt.Cursor ; 18 import java.awt.Dimension ; 19 import java.awt.Frame ; 20 import java.awt.event.ActionEvent ; 21 import java.awt.event.ActionListener ; 22 import java.awt.event.MouseEvent ; 23 import java.awt.event.MouseListener ; 24 import java.math.BigDecimal ; 25 import java.sql.PreparedStatement ; 26 import java.sql.ResultSet ; 27 import java.sql.SQLException ; 28 import java.sql.Timestamp ; 29 import java.util.ArrayList ; 30 31 import javax.swing.ImageIcon ; 32 import javax.swing.JDialog ; 33 import javax.swing.JFrame ; 34 import javax.swing.JMenuItem ; 35 import javax.swing.JPopupMenu ; 36 import javax.swing.JScrollPane ; 37 import javax.swing.SwingUtilities ; 38 import javax.swing.event.ListSelectionEvent ; 39 import javax.swing.event.ListSelectionListener ; 40 41 import org.compiere.apps.AEnv; 42 import org.compiere.apps.AWindow; 43 import org.compiere.apps.ConfirmPanel; 44 import org.compiere.apps.StatusBar; 45 import org.compiere.grid.ed.Calculator; 46 import org.compiere.minigrid.IDColumn; 47 import org.compiere.minigrid.MiniTable; 48 import org.compiere.model.MQuery; 49 import org.compiere.model.MRole; 50 import org.compiere.plaf.CompiereColor; 51 import org.compiere.swing.*; 52 import org.compiere.util.*; 53 54 69 public abstract class Info extends CDialog 70 implements ListSelectionListener 71 { 72 84 public static Info create (Frame frame, boolean modal, int WindowNo, 85 String tableName, String keyColumn, String value, 86 boolean multiSelection, String whereClause) 87 { 88 Info info = null; 89 90 if (tableName.equals("C_BPartner")) 91 info = new InfoBPartner (frame, modal, WindowNo, value, !Env.getContext(Env.getCtx(),"IsSOTrx").equals("N"), 92 multiSelection, whereClause); 93 else if (tableName.equals("M_Product")) 94 info = new InfoProduct (frame, modal, WindowNo, 0,0, value, 95 multiSelection, whereClause); 96 else if (tableName.equals("C_Invoice")) 97 info = new InfoInvoice (frame, modal, WindowNo, value, 98 multiSelection, whereClause); 99 else if (tableName.equals("A_Asset")) 100 info = new InfoAsset (frame, modal, WindowNo, 0, value, 101 multiSelection, whereClause); 102 else if (tableName.equals("C_Order")) 103 info = new InfoOrder (frame, modal, WindowNo, value, 104 multiSelection, whereClause); 105 else if (tableName.equals("M_InOut")) 106 info = new InfoInOut (frame, modal, WindowNo, value, 107 multiSelection, whereClause); 108 else if (tableName.equals("C_Payment")) 109 info = new InfoPayment (frame, modal, WindowNo, value, 110 multiSelection, whereClause); 111 else if (tableName.equals("C_CashLine")) 112 info = new InfoCashLine (frame, modal, WindowNo, value, 113 multiSelection, whereClause); 114 else if (tableName.equals("S_ResourceAssigment")) 115 info = new InfoAssignment (frame, modal, WindowNo, value, 116 multiSelection, whereClause); 117 else 118 info = new InfoGeneral (frame, modal, WindowNo, value, 119 tableName, keyColumn, 120 multiSelection, whereClause); 121 AEnv.positionCenterWindow(frame, info); 123 return info; 124 } 126 131 public static void showBPartner (Frame frame, int WindowNo) 132 { 133 Info info = new InfoBPartner (frame, false, WindowNo, "", 134 !Env.getContext(Env.getCtx(),"IsSOTrx").equals("N"), false, ""); 135 AEnv.showCenterWindow(frame, info); 136 } 138 143 public static void showAsset (Frame frame, int WindowNo) 144 { 145 Info info = new InfoAsset (frame, false, WindowNo, 146 0, "", false, ""); 147 AEnv.showCenterWindow(frame, info); 148 } 150 155 public static void showProduct (Frame frame, int WindowNo) 156 { 157 Info info = new InfoProduct (frame, false, WindowNo, 158 Env.getContextAsInt(Env.getCtx(), WindowNo, "M_Warehouse_ID"), 159 Env.getContextAsInt(Env.getCtx(), WindowNo, "M_PriceList_ID"), 160 "", false, ""); 162 AEnv.showCenterWindow(frame, info); 163 } 165 171 public static void showOrder (Frame frame, int WindowNo, String value) 172 { 173 Info info = new InfoOrder (frame, false, WindowNo, value, 174 false, ""); 175 AEnv.showCenterWindow(frame, info); 176 } 178 184 public static void showInvoice (Frame frame, int WindowNo, String value) 185 { 186 Info info = new InfoInvoice (frame, false, WindowNo, value, 187 false, ""); 188 AEnv.showCenterWindow(frame, info); 189 } 191 197 public static void showInOut (Frame frame, int WindowNo, String value) 198 { 199 Info info = new InfoInOut (frame, false, WindowNo, value, 200 false, ""); 201 AEnv.showCenterWindow(frame, info); 202 } 204 210 public static void showPayment (Frame frame, int WindowNo, String value) 211 { 212 Info info = new InfoPayment (frame, false, WindowNo, value, 213 false, ""); 214 AEnv.showCenterWindow(frame, info); 215 } 217 223 public static void showCashLine (Frame frame, int WindowNo, String value) 224 { 225 Info info = new InfoCashLine (frame, false, WindowNo, value, 226 false, ""); 227 AEnv.showCenterWindow(frame, info); 228 } 230 236 public static void showAssignment (Frame frame, int WindowNo, String value) 237 { 238 Info info = new InfoAssignment (frame, false, WindowNo, value, 239 false, ""); 240 AEnv.showCenterWindow(frame, info); 241 } 243 244 static final int INFO_WIDTH = 800; 245 246 247 248 258 protected Info (Frame frame, boolean modal, int WindowNo, 259 String tableName, String keyColumn, 260 boolean multiSelection, String whereClause) 261 { 262 super (frame, modal); 263 Log.trace(Log.l1_User, "Info for WinNo=" + p_WindowNo + " " + whereClause); 264 p_WindowNo = WindowNo; 265 p_tableName = tableName; 266 p_keyColumn = keyColumn; 267 p_multiSelection = multiSelection; 268 p_whereClause = whereClause; 269 270 try 271 { 272 jbInit(); 273 } 274 catch(Exception ex) 275 { 276 Log.error("Info", ex); 277 } 278 } 280 281 282 protected int p_WindowNo; 283 284 protected String p_tableName; 285 286 protected String p_keyColumn; 287 288 protected boolean p_multiSelection; 289 290 protected String p_whereClause = ""; 291 292 293 protected MiniTable p_table = new MiniTable(); 294 295 private int m_keyColumnIndex = -1; 296 297 private boolean m_ok = false; 298 299 private boolean m_cancel = false; 300 301 private ArrayList m_results = new ArrayList (3); 302 303 304 protected Info_Column[] p_layout; 305 306 private String m_sqlMain; 307 308 private String m_sqlAdd; 309 310 311 protected boolean p_loadedOK = false; 312 313 314 private Worker m_worker = null; 315 316 317 private CPanel southPanel = new CPanel(); 318 private BorderLayout southLayout = new BorderLayout (); 319 ConfirmPanel confirmPanel = new ConfirmPanel(true, true, true, true, true, true, true); 320 protected StatusBar statusBar = new StatusBar(); 321 protected CPanel parameterPanel = new CPanel(); 322 private JScrollPane scrollPane = new JScrollPane (); 323 private JPopupMenu popup = new JPopupMenu (); 325 private JMenuItem calcMenu = new JMenuItem (); 326 327 331 protected void jbInit() throws Exception 332 { 333 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 334 southPanel.setLayout(southLayout); 335 southPanel.add(confirmPanel, BorderLayout.CENTER); 336 southPanel.add(statusBar, BorderLayout.SOUTH); 337 getContentPane().add(southPanel, BorderLayout.SOUTH); 338 this.getContentPane().add(parameterPanel, BorderLayout.NORTH); 339 this.getContentPane().add(scrollPane, BorderLayout.CENTER); 340 scrollPane.getViewport().add(p_table, null); 341 confirmPanel.addActionListener(this); 343 confirmPanel.getResetButton().setVisible(hasReset()); 344 confirmPanel.getCustomizeButton().setVisible(hasCustomize()); 345 confirmPanel.getHistoryButton().setVisible(hasHistory()); 346 confirmPanel.getZoomButton().setVisible(hasZoom()); 347 popup.add(calcMenu); 349 calcMenu.setText(Msg.getMsg(Env.getCtx(), "Calculator")); 350 calcMenu.setIcon(new ImageIcon (org.compiere.Compiere.class.getResource("images/Calculator16.gif"))); 351 calcMenu.addActionListener(this); 352 p_table.getSelectionModel().addListSelectionListener(this); 354 enableButtons(); 355 } 357 361 public boolean loadedOK() 362 { 363 return p_loadedOK; 364 } 366 371 public void setStatusLine (String text, boolean error) 372 { 373 statusBar.setStatusLine(text, error); 374 Thread.yield(); 375 } 377 381 public void setStatusDB (String text) 382 { 383 statusBar.setStatusDB(text); 384 } 386 387 388 389 397 protected void prepareTable (Info_Column[] layout, String from, String staticWhere, String orderBy) 398 { 399 p_layout = layout; 400 StringBuffer sql = new StringBuffer ("SELECT "); 401 for (int i = 0; i < layout.length; i++) 403 { 404 if (i > 0) 405 sql.append(", "); 406 sql.append(layout[i].getColSQL()); 407 if (layout[i].isIDcol()) 409 sql.append(",").append(layout[i].getIDcolSQL()); 410 p_table.addColumn(layout[i].getColHeader()); 412 if (layout[i].isColorColumn()) 413 p_table.setColorColumn(i); 414 if (layout[i].getColClass() == IDColumn.class) 415 m_keyColumnIndex = i; 416 } 417 for (int i = 0; i < layout.length; i++) 419 p_table.setColumnClass(i, layout[i].getColClass(), layout[i].isReadOnly(), layout[i].getColHeader()); 420 421 sql.append( " FROM ").append(from); 422 sql.append(" WHERE ").append(staticWhere); 423 m_sqlMain = sql.toString(); 424 m_sqlAdd = ""; 425 if (orderBy != null && orderBy.length() > 0) 426 m_sqlAdd = " ORDER BY " + orderBy; 427 428 p_table.setRowSelectionAllowed(true); 430 p_table.addMouseListener(this); 431 p_table.setMultiSelection(p_multiSelection); 432 433 parameterPanel.setPreferredSize(new Dimension (INFO_WIDTH, parameterPanel.getPreferredSize().height)); 435 scrollPane.setPreferredSize(new Dimension (INFO_WIDTH, 400)); 436 } 438 439 440 441 444 void executeQuery() 445 { 446 if (m_worker != null && m_worker.isAlive()) 448 return; 449 m_worker = new Worker(); 450 m_worker.start(); 451 } 453 456 protected void saveSelection () 457 { 458 if (p_table == null) 460 return; 461 462 Log.trace(Log.l3_Util, "Info.saveSelection"); 463 if (!m_ok) { 465 m_results.clear(); 466 p_table.removeAll(); 467 p_table = null; 468 return; 469 } 470 471 if (p_multiSelection) 473 { 474 } 475 else { 477 Object data = getSelectedRowKey(); 478 if (data != null) 479 m_results.add(data); 480 } 481 Log.trace(Log.l4_Data, "SQL= " + getSelectedSQL()); 482 483 saveSelectionDetail(); 485 p_table.removeAll(); 487 p_table = null; 488 } 490 494 protected Integer getSelectedRowKey() 495 { 496 int row = p_table.getSelectedRow(); 497 if (row != -1 && m_keyColumnIndex != -1) 498 { 499 Object data = p_table.getModel().getValueAt(row, m_keyColumnIndex); 500 if (data instanceof IDColumn) 501 data = ((IDColumn)data).getRecord_ID(); 502 if (data instanceof Integer ) 503 return (Integer )data; 504 } 505 return null; 506 } 508 512 public Object [] getSelectedKeys() 513 { 514 if (!m_ok || m_results.size() == 0) 515 return null; 516 return m_results.toArray(); 517 } 519 523 public Object getSelectedKey() 524 { 525 if (!m_ok || m_results.size() == 0) 526 return null; 527 return m_results.get(0); 528 } 530 536 public boolean isCancelled() 537 { 538 return m_cancel; 539 } 541 545 public String getSelectedSQL() 546 { 547 Object [] keys = getSelectedKeys(); 549 if (keys == null || keys.length == 0) 550 { 551 Log.trace(Log.l3_Util, "Info.getSelectedSQL - no Results - OK=" + m_ok + ", Cancel=" + m_cancel); 552 return ""; 553 } 554 StringBuffer sb = new StringBuffer (getKeyColumn()); 556 if (keys.length > 1) 557 sb.append(" IN ("); 558 else 559 sb.append("="); 560 561 for (int i = 0; i < keys.length; i++) 563 { 564 if (getKeyColumn().endsWith("_ID")) 565 sb.append(keys[i].toString()).append(","); 566 else 567 sb.append("'").append(keys[i].toString()).append("',"); 568 } 569 570 sb.replace(sb.length()-1, sb.length(), ""); 571 if (keys.length > 1) 572 sb.append(")"); 573 return sb.toString(); 574 } 576 580 public void actionPerformed(ActionEvent e) 581 { 582 if (e.getSource().equals(calcMenu)) 584 { 585 BigDecimal number = null; 586 Object data = p_table.getSelectedValue(); 587 try 588 { 589 if (data != null) 590 { 591 if (data instanceof BigDecimal ) 592 number = (BigDecimal )data; 593 else 594 number = new BigDecimal (data.toString()); 595 } 596 } 597 catch (Exception ex) {} 598 Calculator c = new Calculator(null, number); 599 c.setVisible(true); 600 return; 601 } 603 String cmd = e.getActionCommand(); 605 if (cmd.equals(ConfirmPanel.A_OK)) 606 { 607 dispose(true); 608 } 609 else if (cmd.equals(ConfirmPanel.A_CANCEL)) 610 { 611 m_cancel = true; 612 dispose(false); 613 } 614 else if (cmd.equals(ConfirmPanel.A_HISTORY)) 616 showHistory(); 617 else if (cmd.equals(ConfirmPanel.A_CUSTOMIZE)) 618 customize(); 619 else if (cmd.equals(ConfirmPanel.A_ZOOM)) 620 zoom(); 621 else if (cmd.equals(ConfirmPanel.A_RESET)) 622 doReset(); 623 else 625 executeQuery(); 626 } 628 633 void zoom (int AD_Window_ID, MQuery zoomQuery) 634 { 635 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 636 AWindow frame = new AWindow(); 637 if (!frame.initWindow(AD_Window_ID, zoomQuery, false)) return; 639 AEnv.showCenterScreen(frame); 640 if (this.isModal()) 641 { 642 this.setModal(false); this.dispose(); 644 } 645 646 frame = null; 647 setCursor(Cursor.getDefaultCursor()); 648 } 650 653 public void dispose() 654 { 655 dispose(false); 656 } 658 662 public void dispose(boolean ok) 663 { 664 Log.trace(Log.l3_Util, "Info.dispose", "OK=" + ok); 665 m_ok = ok; 666 667 if (m_worker != null) 669 { 670 if (m_worker.isAlive()) 672 m_worker.interrupt(); 673 Log.trace(Log.l4_Data, "Info.dispose - Worker alive=" + m_worker.isAlive()); 674 } 675 m_worker = null; 676 saveSelection(); 678 removeAll(); 679 super.dispose(); 680 } 682 686 String getTableName() 687 { 688 return p_tableName; 689 } 691 695 String getKeyColumn() 696 { 697 return p_keyColumn; 698 } 700 701 702 706 public void valueChanged(ListSelectionEvent e) 707 { 708 if (e.getValueIsAdjusting()) 709 return; 710 enableButtons(); 711 } 713 716 void enableButtons () 717 { 718 boolean enable = p_table.getSelectedRow() != -1; 719 confirmPanel.getOKButton().setEnabled(enable); 720 if (hasHistory()) 721 confirmPanel.getHistoryButton().setEnabled(enable); 722 if (hasZoom()) 723 confirmPanel.getZoomButton().setEnabled(enable); 724 } 726 727 728 733 abstract String getSQLWhere(); 734 740 abstract void setParameters(PreparedStatement pstmt) throws SQLException ; 741 742 746 void doReset() {} 747 752 boolean hasReset() {return false;} 753 757 void showHistory() {} 758 763 boolean hasHistory() {return false;} 764 768 void customize() {} 769 774 boolean hasCustomize() {return false;} 775 779 void zoom() {} 780 785 boolean hasZoom() {return false;} 786 790 void saveSelectionDetail() {} 791 792 793 794 798 public void mouseClicked(MouseEvent e) 799 { 800 804 if (e.getClickCount() > 1 && p_table.getSelectedRow() != -1) 806 { 807 dispose(true); } 809 else if (SwingUtilities.isRightMouseButton(e)) 811 { 812 popup.show(e.getComponent(), e.getX(), e.getY()); 813 } 814 } 816 817 820 class Worker extends Thread 821 { 822 825 public void run() 826 { 827 Log.trace(Log.l3_Util, "Info.Worker.run"); 828 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 829 setStatusLine(Msg.getMsg(Env.getCtx(), "StartSearch"), false); 830 831 p_table.setRowCount(0); 833 StringBuffer sql = new StringBuffer (m_sqlMain); 835 String dynWhere = getSQLWhere(); 836 if (dynWhere.length() > 0) 837 sql.append(dynWhere); sql.append(m_sqlAdd); 839 String xSql = MRole.getDefault().addAccessSQL(sql.toString(), getTableName(), 840 MRole.SQL_FULLYQUALIFIED, MRole.SQL_RO); 841 842 try 843 { 844 PreparedStatement pstmt = DB.prepareStatement(xSql); 845 Log.trace(Log.l4_Data, "SQL=" + xSql); 846 setParameters (pstmt); 847 ResultSet rs = pstmt.executeQuery(); 849 while (!isInterrupted() & rs.next()) 851 { 852 int row = p_table.getRowCount(); 853 p_table.setRowCount(row+1); 854 int colOffset = 1; for (int col = 0; col < p_layout.length; col++) 856 { 857 Object data = null; 858 Class c = p_layout[col].getColClass(); 859 int colIndex = col + colOffset; 860 if (c == IDColumn.class) 861 data = new IDColumn(rs.getInt(colIndex)); 862 else if (c == Boolean .class) 863 data = new Boolean ("Y".equals(rs.getString(colIndex))); 864 else if (c == Timestamp .class) 865 data = rs.getTimestamp(colIndex); 866 else if (c == BigDecimal .class) 867 data = rs.getBigDecimal(colIndex); 868 else if (c == Double .class) 869 data = new Double (rs.getDouble(colIndex)); 870 else if (c == Integer .class) 871 data = new Integer (rs.getInt(colIndex)); 872 else if (c == KeyNamePair.class) 873 { 874 String display = rs.getString(colIndex); 875 int key = rs.getInt(colIndex+1); 876 data = new KeyNamePair(key, display); 877 colOffset++; 878 } 879 else 880 data = rs.getString(colIndex); 881 p_table.setValueAt(data, row, col); 883 } 886 } 887 Log.trace(Log.l4_Data, "Info.Worker.run - interrupted=" + isInterrupted()); 888 rs.close(); 889 pstmt.close(); 890 } 891 catch (SQLException e) 892 { 893 Log.error("Info.Worker.run - " + xSql, e); 894 } 895 p_table.autoSize(); 896 setCursor(Cursor.getDefaultCursor()); 898 int no = p_table.getRowCount(); 899 setStatusLine(Integer.toString(no) + " " + Msg.getMsg(Env.getCtx(), "SearchRows_EnterQuery"), false); 900 setStatusDB(Integer.toString(no)); 901 } } 904 } | Popular Tags |