From 18071c1e3b73b058bd36b8d4eb90c20cecd467e8 Mon Sep 17 00:00:00 2001 From: fer_luck Date: Tue, 29 Apr 2008 18:06:22 +0000 Subject: [PATCH] [FR 1913527] - http://sourceforge.net/tracker/index.php?func=detail&aid=1913527&group_id=176962&atid=879335 Thanks goes to Michael Judd for developing this awesome functionality! I just backported it from trunk into stable. Regards, Fernando --- base/src/org/compiere/model/MColumn.java | 13 +- client/src/org/compiere/apps/search/Info.java | 91 +- .../org/compiere/apps/search/InfoProduct.java | 2 +- .../org/compiere/apps/search/Info_Column.java | 14 + .../src/org/compiere/grid/GridController.java | 111 +- client/src/org/compiere/grid/ed/VLookup.java | 3026 +++++++++-------- 6 files changed, 1755 insertions(+), 1502 deletions(-) diff --git a/base/src/org/compiere/model/MColumn.java b/base/src/org/compiere/model/MColumn.java index 8131668843..3facea2d7f 100644 --- a/base/src/org/compiere/model/MColumn.java +++ b/base/src/org/compiere/model/MColumn.java @@ -520,7 +520,18 @@ public class MColumn extends X_AD_Column } //end vpj-cd e-evolution - + /** + * Get Table Id for a column + * @param ctx context + * @param AD_Column_ID id + * @param trxName transaction + * @return MColumn + */ + public static int getTable_ID(Properties ctx, int AD_Column_ID, String trxName) + { + String sqlStmt = "SELECT AD_Table_ID FROM AD_Column WHERE AD_Column_ID=?"; + return DB.getSQLValue(trxName, sqlStmt, AD_Column_ID); + } } // MColumn diff --git a/client/src/org/compiere/apps/search/Info.java b/client/src/org/compiere/apps/search/Info.java index bb61d07f64..ceb475a4e1 100644 --- a/client/src/org/compiere/apps/search/Info.java +++ b/client/src/org/compiere/apps/search/Info.java @@ -418,6 +418,12 @@ public abstract class Info extends CDialog if (layout[i].getColClass() == IDColumn.class) m_keyColumnIndex = i; } + + // Table Selection (Invoked before setting column class so that row selection is enabled) + p_table.setRowSelectionAllowed(true); + p_table.addMouseListener(this); + p_table.setMultiSelection(p_multiSelection); + // set editors (two steps) for (int i = 0; i < layout.length; i++) p_table.setColumnClass(i, layout[i].getColClass(), layout[i].isReadOnly(), layout[i].getColHeader()); @@ -435,11 +441,6 @@ public abstract class Info extends CDialog if (m_keyColumnIndex == -1) log.log(Level.SEVERE, "No KeyColumn - " + sql); - // Table Selection - p_table.setRowSelectionAllowed(true); - p_table.addMouseListener(this); - p_table.setMultiSelection(p_multiSelection); - // Window Sizing parameterPanel.setPreferredSize(new Dimension (INFO_WIDTH, parameterPanel.getPreferredSize().height)); //Begin - [FR 1823612 ] Product Info Screen Improvements @@ -527,6 +528,7 @@ public abstract class Info extends CDialog // Multi Selection if (p_multiSelection) { + m_results.addAll(getSelectedRowKeys()); } else // singleSelection { @@ -549,17 +551,67 @@ public abstract class Info extends CDialog */ protected Integer getSelectedRowKey() { - int row = p_table.getSelectedRow(); - if (row != -1 && m_keyColumnIndex != -1) + ArrayList selectedDataList = getSelectedRowKeys(); + if (selectedDataList.size() == 0) { - Object data = p_table.getModel().getValueAt(row, m_keyColumnIndex); - if (data instanceof IDColumn) - data = ((IDColumn)data).getRecord_ID(); - if (data instanceof Integer) - return (Integer)data; + return null; + } + else + { + return selectedDataList.get(0); } - return null; } // getSelectedRowKey + + /** + * Get the keys of selected row/s based on layout defined in prepareTable + * @return IDs if selection present + * @author ashley + */ + protected ArrayList getSelectedRowKeys() + { + ArrayList selectedDataList = new ArrayList(); + + if (m_keyColumnIndex == -1) + { + return selectedDataList; + } + + if (p_multiSelection) + { + int rows = p_table.getRowCount(); + for (int row = 0; row < rows; row++) + { + Object data = p_table.getModel().getValueAt(row, m_keyColumnIndex); + if (data instanceof IDColumn) + { + IDColumn dataColumn = (IDColumn)data; + if (dataColumn.isSelected()) + { + selectedDataList.add(dataColumn.getRecord_ID()); + } + } + else + { + log.severe("For multiple selection, IDColumn should be key column for selection"); + } + } + } + + if (selectedDataList.size() == 0) + { + int row = p_table.getSelectedRow(); + if (row != -1 && m_keyColumnIndex != -1) + { + Object data = p_table.getModel().getValueAt(row, m_keyColumnIndex); + if (data instanceof IDColumn) + selectedDataList.add(((IDColumn)data).getRecord_ID()); + if (data instanceof Integer) + selectedDataList.add((Integer)data); + } + } + + return selectedDataList; + } // getSelectedRowKeys /** * Get selected Keys @@ -569,7 +621,9 @@ public abstract class Info extends CDialog { if (!m_ok || m_results.size() == 0) return null; - return m_results.toArray(); + Integer values[] = new Integer[m_results.size()]; + m_results.toArray(values); + return values; } // getSelectedKeys; /** @@ -786,12 +840,15 @@ public abstract class Info extends CDialog } // calueChanged /** - * Enable OK, History, Zoom if row selected + * Enable OK, History, Zoom if row/s selected + * --- + * Changes: Changed the logic for accomodating multiple selection + * @author ashley */ protected void enableButtons () { - boolean enable = p_table.getSelectedRow() != -1; - confirmPanel.getOKButton().setEnabled(enable); + boolean enable = (p_table.getSelectedRowCount() == 1); + confirmPanel.getOKButton().setEnabled(p_table.getSelectedRowCount() > 0); if (hasHistory()) confirmPanel.getHistoryButton().setEnabled(enable); diff --git a/client/src/org/compiere/apps/search/InfoProduct.java b/client/src/org/compiere/apps/search/InfoProduct.java index 0efa1bc1cc..f5caa8a0a9 100644 --- a/client/src/org/compiere/apps/search/InfoProduct.java +++ b/client/src/org/compiere/apps/search/InfoProduct.java @@ -936,7 +936,7 @@ public final class InfoProduct extends Info implements ActionListener if (s_productLayout == null) { ArrayList list = new ArrayList(); - list.add(new Info_Column(" ", "p.M_Product_ID", IDColumn.class)); + list.add(new Info_Column(" ", "p.M_Product_ID", IDColumn.class, !p_multiSelection)); list.add(new Info_Column(Msg.translate(Env.getCtx(), "Discontinued").substring(0, 1), "p.Discontinued", Boolean.class)); list.add(new Info_Column(Msg.translate(Env.getCtx(), "Value"), "p.Value", String.class)); list.add(new Info_Column(Msg.translate(Env.getCtx(), "Name"), "p.Name", String.class)); diff --git a/client/src/org/compiere/apps/search/Info_Column.java b/client/src/org/compiere/apps/search/Info_Column.java index 42b795821e..a5b6bcb2e9 100644 --- a/client/src/org/compiere/apps/search/Info_Column.java +++ b/client/src/org/compiere/apps/search/Info_Column.java @@ -48,6 +48,20 @@ public class Info_Column { this(colHeader, colSQL, colClass, true, false, IDcolSQL); } // Info_Column + + /** + * Create Info Column (not color column) + * + * @param colHeader Column Header + * @param colSQL SQL select code for column + * @param colClass class of column - determines display + * @param readOnly column is read only + * @author ashley + */ + public Info_Column (String colHeader, String colSQL, Class colClass, boolean readOnly) + { + this(colHeader, colSQL, colClass, readOnly, false, null); + } // Info_Column /** * Create Info Column diff --git a/client/src/org/compiere/grid/GridController.java b/client/src/org/compiere/grid/GridController.java index 4438a070c8..9a8b709683 100644 --- a/client/src/org/compiere/grid/GridController.java +++ b/client/src/org/compiere/grid/GridController.java @@ -1037,6 +1037,53 @@ public class GridController extends CPanel } // rowChanged + /************************************************************************** + * Save Multiple records - Clone a record and assign new values to each + * clone for a specific column. + * @param ctx context + * @param tableName Table Name + * @param columnName Column for which value need to be changed + * @param recordId Record to clone + * @param values Values to be assigned to clones for the specified column + * @param trxName Transaction + * @throws Exception If error is occured when loading the PO or saving clones + * + * @author ashley + */ + protected void saveMultipleRecords(Properties ctx, String tableName, + String columnName, int recordId, Integer[] values, + String trxName) throws Exception + { + if (values == null) + { + return ; + } + + int oldRow = m_mTab.getCurrentRow(); + GridField lineField = m_mTab.getField("Line"); + + for (int i = 0; i < values.length; i++) + { + if (!m_mTab.dataNew(true)) + { + throw new IllegalStateException("Could not clone tab"); + } + + m_mTab.setValue(columnName, values[i]); + + if (lineField != null) + { + m_mTab.setValue(lineField, 0); + } + + if (!m_mTab.dataSave(false)) + { + throw new IllegalStateException("Could not update tab"); + } + + m_mTab.setCurrentRow(oldRow); + } + } /************************************************************************** * Vetoable Change Listener. @@ -1104,7 +1151,38 @@ public class GridController extends CPanel else { // mTable.setValueAt (e.getNewValue(), row, col, true); - mTable.setValueAt (e.getNewValue(), row, col); // -> dataStatusChanged -> dynamicDisplay + /* + * Changes: Added the logic below to handle multiple values for a single field + * due to multiple selection in Lookup (Info). + * @author ashley + */ + Object newValue = e.getNewValue(); + Integer newValues[] = null; + + if (newValue instanceof Integer[]) + { + newValues = ((Integer[])newValue); + newValue = newValues[0]; + + if (newValues.length > 1) + { + Integer valuesCopy[] = new Integer[newValues.length - 1]; + System.arraycopy(newValues, 1, valuesCopy, 0, valuesCopy.length); + newValues = valuesCopy; + } + else + { + newValues = null; + } + } + else if (newValue instanceof Object[]) + { + log.severe("Multiple values can only be processed for IDs (Integer)"); + throw new PropertyVetoException("Multiple Selection values not available for this field", e); + } + + mTable.setValueAt (newValue, row, col); // -> dataStatusChanged -> dynamicDisplay + // Force Callout if (e.getPropertyName().equals("S_ResourceAssignment_ID")) { @@ -1112,6 +1190,37 @@ public class GridController extends CPanel if (mField != null && mField.getCallout().length() > 0) m_mTab.processFieldChange(mField); // Dependencies & Callout } + + if (newValues != null && newValues.length > 0) + { + // Save data, since record need to be used for generating clones. + if (!m_mTab.dataSave(false)) + { + throw new PropertyVetoException("SaveError", e); + } + + // Retrieve the current record ID + int recordId = m_mTab.getKeyID(m_mTab.getCurrentRow()); + + Trx trx = Trx.get(Trx.createTrxName(), true); + trx.start(); + try + { + saveMultipleRecords(Env.getCtx(), mTable.getTableName(), e.getPropertyName(), recordId, newValues, trx.getTrxName()); + trx.commit(); + m_mTab.dataRefreshAll(); + } + catch(Exception ex) + { + trx.rollback(); + log.severe(ex.getMessage()); + throw new PropertyVetoException("SaveError", e); + } + finally + { + trx.close(); + } + } } // log.config( "GridController.vetoableChange (" + m_mTab.toString() + ") - fini", e.getPropertyName() + "=" + e.getNewValue()); diff --git a/client/src/org/compiere/grid/ed/VLookup.java b/client/src/org/compiere/grid/ed/VLookup.java index 73448907c6..db2ec54481 100644 --- a/client/src/org/compiere/grid/ed/VLookup.java +++ b/client/src/org/compiere/grid/ed/VLookup.java @@ -1,1482 +1,1544 @@ -/****************************************************************************** - * Product: Adempiere ERP & CRM Smart Business Solution * - * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. * - * This program is free software; you can redistribute it and/or modify it * - * under the terms version 2 of the GNU General Public License as published * - * by the Free Software Foundation. This program is distributed in the hope * - * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * - * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * - * See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along * - * with this program; if not, write to the Free Software Foundation, Inc., * - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * - * For the text or an alternative of this public license, you may reach us * - * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA * - * or via info@compiere.org or http://www.compiere.org/license.html * - *****************************************************************************/ -package org.compiere.grid.ed; - -import java.awt.*; -import java.awt.event.*; -import java.beans.*; -import java.sql.*; -import java.util.logging.*; -import javax.swing.*; - -import org.compiere.apps.*; -import org.compiere.apps.search.*; -import org.compiere.model.*; -import org.compiere.swing.*; -import org.compiere.util.*; - -/** - * Lookup Visual Field. - *

- * When r/o - display a Label - * When STABLE - display a ComboBox - * Otherwise show Selection Dialog - *

- * Special handling of BPartner and Product - * - * @author Jorg Janke - * @version $Id: VLookup.java,v 1.5 2006/10/06 00:42:38 jjanke Exp $ - * - * @author Teo Sarca - BF [ 1740835 ] - */ -public class VLookup extends JComponent - implements VEditor, ActionListener, FocusListener -{ - @Override - protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, - int condition, boolean pressed) { - if (e.getSource() == m_combo || e.getSource() == m_text || e.getSource() == this) { - return super.processKeyBinding(ks, e, condition, pressed); - } - - JComponent editorComp = null; - if (m_lookup != null && m_lookup.getDisplayType() != DisplayType.Search) - editorComp = m_combo; - else - editorComp = m_text; - InputMap map = editorComp.getInputMap(condition); - ActionMap am = editorComp.getActionMap(); - - if(map!=null && am!=null && isEnabled()){ - Object binding = map.get(ks); - Action action = (binding==null) ? null : am.get(binding); - if(action!=null){ - return SwingUtilities.notifyAction(action, ks, e, editorComp, - e.getModifiers()); - } - } - return false; - } - - /** - * Create Optional BPartner Search Lookup - * @param WindowNo window - * @return VLookup - */ - public static VLookup createBPartner (int WindowNo) - { - int AD_Column_ID = 3499; // C_Invoice.C_BPartner_ID - try - { - Lookup lookup = MLookupFactory.get (Env.getCtx(), WindowNo, - 0, AD_Column_ID, DisplayType.Search); - return new VLookup ("C_BPartner_ID", false, false, true, lookup); - } - catch (Exception e) - { - log.log(Level.SEVERE, "", e); - } - return null; - } // createBPartner - - /** - * Create Optional Product Search Lookup - * @param WindowNo window - * @return VLookup - */ - public static VLookup createProduct (int WindowNo) - { - int AD_Column_ID = 3840; // C_InvoiceLine.M_Product_ID - try - { - Lookup lookup = MLookupFactory.get (Env.getCtx(), WindowNo, 0, - AD_Column_ID, DisplayType.Search); - return new VLookup ("M_Product_ID", false, false, true, lookup); - } - catch (Exception e) - { - log.log(Level.SEVERE, "", e); - } - return null; - } // createProduct - - /** - * Create Optional User Search Lookup - * @param WindowNo window - * @return VLookup - */ - public static VLookup createUser (int WindowNo) - { - int AD_Column_ID = 10443; // AD_WF_Activity.AD_User_UD - try - { - Lookup lookup = MLookupFactory.get (Env.getCtx(), WindowNo, 0, - AD_Column_ID, DisplayType.Search); - return new VLookup ("AD_User_ID", false, false, true, lookup); - } - catch (Exception e) - { - log.log(Level.SEVERE, "", e); - } - return null; - } // createProduct - - - /************************************************************************* - * Detail Constructor - * - * @param columnName column - * @param mandatory mandatory - * @param isReadOnly read only - * @param isUpdateable updateable - * @param lookup lookup - */ - public VLookup (String columnName, boolean mandatory, boolean isReadOnly, boolean isUpdateable, - Lookup lookup) - { - super(); - super.setName(columnName); - m_combo.setName(columnName); - m_columnName = columnName; - setMandatory(mandatory); - m_lookup = lookup; - if (m_lookup != null) - m_lookup.setMandatory(mandatory); - // - setLayout(new BorderLayout()); - mouseAdapter = new VLookup_mouseAdapter(this); // popup - - // *** Text & Button *** - m_text.addActionListener(this); - m_text.addFocusListener(this); - m_text.addMouseListener(mouseAdapter); - // Button - m_button.addActionListener(this); - m_button.addMouseListener(mouseAdapter); - m_button.setFocusable(false); // don't focus when tabbing - m_button.setMargin(new Insets(0, 0, 0, 0)); - if (columnName.equals("C_BPartner_ID")) - m_button.setIcon(Env.getImageIcon("BPartner10.gif")); - else if (columnName.equals("M_Product_ID")) - m_button.setIcon(Env.getImageIcon("Product10.gif")); - else - m_button.setIcon(Env.getImageIcon("PickOpen10.gif")); - - // *** VComboBox *** - if (m_lookup != null && m_lookup.getDisplayType() != DisplayType.Search) // No Search - { - // Don't have to fill up combobox if it is readonly - if (!isReadOnly && isUpdateable) - m_lookup.fillComboBox (isMandatory(), true, true, false); - m_combo.setModel(m_lookup); - // - AutoCompletion.enable(m_combo); - m_combo.addActionListener(this); // Selection - m_combo.getEditor().getEditorComponent().addMouseListener(mouseAdapter); // popup - // FocusListener to refresh selection before opening - m_combo.addFocusListener(this); - m_combo.getEditor().getEditorComponent().addFocusListener(this); - } - - setUI (true); - // ReadWrite - decides what components to show - if (isReadOnly || !isUpdateable || m_lookup == null) - setReadWrite(false); - else - setReadWrite(true); - - // Popup - if (m_lookup != null) - { - if ((m_lookup.getDisplayType() == DisplayType.List && Env.getContextAsInt(Env.getCtx(), "#AD_Role_ID") == 0) - || m_lookup.getDisplayType() != DisplayType.List) // only system admins can change lists, so no need to zoom for others - { - mZoom = new CMenuItem(Msg.getMsg(Env.getCtx(), "Zoom"), Env.getImageIcon("Zoom16.gif")); - mZoom.addActionListener(this); - popupMenu.add(mZoom); - } - mRefresh = new CMenuItem(Msg.getMsg(Env.getCtx(), "Refresh"), Env.getImageIcon("Refresh16.gif")); - mRefresh.addActionListener(this); - popupMenu.add(mRefresh); - } - // VBPartner quick entry link - if (columnName.equals("C_BPartner_ID")) - { - mBPartnerNew = new CMenuItem (Msg.getMsg(Env.getCtx(), "New"), Env.getImageIcon("InfoBPartner16.gif")); - mBPartnerNew.addActionListener(this); - mBPartnerNew.setVisible(isReadWrite()); // visible only if the field is editable - teo_sarca [ 1721710 ] - popupMenu.add(mBPartnerNew); - mBPartnerUpd = new CMenuItem (Msg.getMsg(Env.getCtx(), "Update"), Env.getImageIcon("InfoBPartner16.gif")); - mBPartnerUpd.addActionListener(this); - popupMenu.add(mBPartnerUpd); - } - // - if (m_lookup != null && m_lookup.getZoom() == 0) - mZoom.setEnabled(false); - } // VLookup - - /** - * Dispose - */ - public void dispose() - { - m_text = null; - m_button = null; - m_lookup = null; - m_mField = null; - // - m_combo.getEditor().getEditorComponent().removeFocusListener(this); - m_combo.getEditor().getEditorComponent().removeMouseListener(mouseAdapter); - m_combo.removeFocusListener(this); - m_combo.removeActionListener(this); - m_combo.setModel(new DefaultComboBoxModel()); // remove reference - // m_combo.removeAllItems(); - m_combo = null; - } // dispose - - /** Display Length for Lookups (15) */ - public final static int DISPLAY_LENGTH = 15; - /** Field Height */ - public static int FIELD_HIGHT = 0; - - /** Search: The Editable Text Field */ - private CTextField m_text = new CTextField (DISPLAY_LENGTH); - /** Search: The Button to open Editor */ - private CButton m_button = new CButton(); - /** The Combo Box if not a Search Lookup */ - private VComboBox m_combo = new VComboBox(); - /** Indicator that value is being set */ - private volatile boolean m_settingValue = false; - /** Indicator that docus is being set */ - private volatile boolean m_settingFocus = false; - /** Indicator that Lookup has focus */ - private volatile boolean m_haveFocus = false; - /** Indicator - inserting new value */ - private volatile boolean m_inserting = false; - /** Last Display */ - private String m_lastDisplay = ""; - /** Column Name */ - private String m_columnName; - /** Lookup */ - private Lookup m_lookup; - /** Conbo Box Active */ - private boolean m_comboActive = true; - /** The Value */ - private Object m_value; - - private boolean m_stopediting = false; - - // Popup - JPopupMenu popupMenu = new JPopupMenu(); - private CMenuItem mZoom; - private CMenuItem mRefresh; - private CMenuItem mBPartnerNew; - private CMenuItem mBPartnerUpd; - // Mouse Listener - private VLookup_mouseAdapter mouseAdapter; - - - // Field for Value Preference - private GridField m_mField = null; - /** Logger */ - private static CLogger log = CLogger.getCLogger(VLookup.class); - - /** - * Set Content and Size of Components - * @param initial if true, size and margins will be set - */ - private void setUI (boolean initial) - { - if (initial) - { - Dimension size = m_text.getPreferredSize(); - setPreferredSize(new Dimension(size)); // causes r/o to be the same length - m_combo.setPreferredSize(new Dimension(size)); - setMinimumSize(new Dimension (30, size.height)); - FIELD_HIGHT = size.height; - // - m_text.setBorder(null); - Dimension bSize = new Dimension(size.height, size.height); - m_button.setPreferredSize (bSize); - } - - // What to show - this.remove(m_combo); - this.remove(m_button); - this.remove(m_text); - // - if (!isReadWrite()) // r/o - show text only - { - LookAndFeel.installBorder(this, "TextField.border"); - this.add(m_text, BorderLayout.CENTER); - m_text.setReadWrite(false); - m_combo.setReadWrite(false); - m_comboActive = false; - } - else if (m_lookup != null && m_lookup.getDisplayType() != DisplayType.Search) // show combo if not Search - { - this.setBorder(null); - this.add(m_combo, BorderLayout.CENTER); - m_comboActive = true; - } - else // Search or unstable - show text & button - { - LookAndFeel.installBorder(this, "TextField.border"); - this.add(m_text, BorderLayout.CENTER); - this.add(m_button, BorderLayout.EAST); - m_text.setReadWrite (true); - m_comboActive = false; - } - } // setUI - - /** - * Set ReadWrite - * @param value ReadWrite - */ - public void setReadWrite (boolean value) - { - boolean rw = value; - if (m_lookup == null) - rw = false; - if (m_combo.isReadWrite() != value) - { - m_combo.setReadWrite(rw); - setUI (false); - if (value && m_comboActive) { - m_settingValue = true; // disable actions - refresh(); - m_settingValue = false; - } - if (m_comboActive) - setValue (m_value); - } - // If the field is readonly the BPartner new option should be hidden - teo_sarca [ 1721710 ] - if (mBPartnerNew != null) - mBPartnerNew.setVisible(value); - } // setReadWrite - - /** - * IsEditable - * @return is lookup ReadWrite - */ - public boolean isReadWrite() - { - return m_combo.isReadWrite(); - } // isReadWrite - - /** - * Set Mandatory (and back color) - * @param mandatory mandatory - */ - public void setMandatory (boolean mandatory) - { - m_combo.setMandatory(mandatory); - m_text.setMandatory(mandatory); - } // setMandatory - - /** - * Is it mandatory - * @return true if mandatory - */ - public boolean isMandatory() - { - return m_combo.isMandatory(); - } // isMandatory - - /** - * Set Background - * @param color color - */ - public void setBackground(Color color) - { - m_text.setBackground(color); - m_combo.setBackground(color); - } // setBackground - - /** - * Set Background - * @param error error - */ - public void setBackground (boolean error) - { - m_text.setBackground(error); - m_combo.setBackground(error); - } // setBackground - - /** - * Set Foreground - * @param fg Foreground color - */ - public void setForeground(Color fg) - { - m_text.setForeground(fg); - m_combo.setForeground(fg); - } // setForeground - - /** - * Request Focus - */ - public void requestFocus () - { - if (m_lookup != null && m_lookup.getDisplayType() != DisplayType.Search) - m_combo.requestFocus (); - else - m_text.requestFocus (); - } // requestFocus - - - /** - * Set Editor to value - * @param value new Value - */ - public void setValue (Object value) - { - log.fine(m_columnName + "=" + value); - m_settingValue = true; // disable actions - m_value = value; - - // Set both for switching - if (value == null) - { - m_combo.setValue (value); - m_text.setText (null); - m_lastDisplay = ""; - m_settingValue = false; - return; - } - if (m_lookup == null) - { - m_combo.setValue (value); - m_text.setText (value.toString()); - m_lastDisplay = value.toString(); - m_settingValue = false; - return; - } - - //must call m_combo.setvalue after m_lookup as - //loading of combo data might happen in m_lookup.getDisplay - m_lastDisplay = m_lookup.getDisplay(value); - m_combo.setValue (value); - - if (m_lastDisplay.equals("<-1>")) - { - m_lastDisplay = ""; - m_value = null; - } - boolean notFound = m_lastDisplay.startsWith("<") && m_lastDisplay.endsWith(">"); - m_text.setText (m_lastDisplay); - m_text.setCaretPosition (0); // show beginning - - // Nothing showing in Combo and should be showing - if (m_combo.getSelectedItem() == null - && (m_comboActive || (m_inserting && m_lookup.getDisplayType() != DisplayType.Search))) - { - // lookup found nothing too - if (notFound) - { - log.finest(m_columnName + "=" + value + ": Not found - " + m_lastDisplay); - // we may have a new value - m_lookup.refresh(); - m_combo.setValue (value); - m_lastDisplay = m_lookup.getDisplay(value); - m_text.setText (m_lastDisplay); - m_text.setCaretPosition (0); // show beginning - notFound = m_lastDisplay.startsWith("<") && m_lastDisplay.endsWith(">"); - } - if (notFound) // - { - m_value = null; - actionCombo (null); // data binding - log.fine(m_columnName + "=" + value + ": Not found"); - } - // we have lookup - else if (m_combo.getSelectedItem() == null) - { - NamePair pp = m_lookup.get(value); - if (pp != null) - { - log.fine(m_columnName + " added to combo - " + pp); - // Add to Combo - m_combo.addItem (pp); - m_combo.setValue (value); - } - } - // Not in Lookup - set to Null - if (m_combo.getSelectedItem() == null) - { - log.info(m_columnName + "=" + value + ": not in Lookup - set to NULL"); - actionCombo (null); // data binding (calls setValue again) - m_value = null; - } - } - m_settingValue = false; - } // setValue - - /** - * Property Change Listener - * @param evt PropertyChangeEvent - */ - public void propertyChange (PropertyChangeEvent evt) - { - if (m_stopediting) - return; - - // log.fine( "VLookup.propertyChange", evt); - if (evt.getPropertyName().equals(GridField.PROPERTY)) - { - m_inserting = GridField.INSERTING.equals(evt.getOldValue()); // MField.setValue - setValue(evt.getNewValue()); - m_inserting = false; - } - } // propertyChange - - /** - * Return Editor value (Integer) - * @return value - */ - public Object getValue() - { - if (m_comboActive) - return m_combo.getValue (); - return m_value; - } // getValue - - /** - * Return editor display - * @return display value - */ - public String getDisplay() - { - String retValue = null; - if (m_comboActive) - retValue = m_combo.getDisplay(); - // check lookup - else if (m_lookup == null) - retValue = m_value == null ? null : m_value.toString(); - else - retValue = m_lookup.getDisplay(m_value); - // log.fine( "VLookup.getDisplay - " + retValue, "ComboActive=" + m_comboActive); - return retValue; - } // getDisplay - - /** - * Set Field/WindowNo for ValuePreference - * @param mField Model Field for Lookup - */ - public void setField (GridField mField) - { - m_mField = mField; - if (m_mField != null - && MRole.getDefault().isShowPreference()) - ValuePreference.addMenu (this, popupMenu); - } // setField - - - /************************************************************************** - * Action Listener - data binding - * @param e ActionEvent - */ - public void actionPerformed (ActionEvent e) - { - if (m_settingValue || m_settingFocus || m_stopediting) - return; - log.config(m_columnName + " - " + e.getActionCommand() + ", ComboValue=" + m_combo.getSelectedItem()); - // log.fine("Hash=" + this.hashCode()); - - // Preference - if (e.getActionCommand().equals(ValuePreference.NAME)) - { - if (MRole.getDefault().isShowPreference()) - ValuePreference.start (m_mField, getValue(), getDisplay()); - return; - } - - // Combo Selection - else if (e.getSource() == m_combo) - { - Object value = getValue(); - Object o = m_combo.getSelectedItem(); - if (o != null) - { - String s = o.toString(); - // don't allow selection of inactive - if (s.startsWith(MLookup.INACTIVE_S) && s.endsWith(MLookup.INACTIVE_E)) - { - log.info(m_columnName + " - selection inactive set to NULL"); - value = null; - } - } - actionCombo (value); // data binding - } - // Button pressed - else if (e.getSource() == m_button) - actionButton (""); - // Text entered - else if (e.getSource() == m_text) - actionText(); - - // Popup Menu - else if (e.getSource() == mZoom) - actionZoom(m_combo.getSelectedItem()); - else if (e.getSource() == mRefresh) - actionRefresh(); - else if (e.getSource() == mBPartnerNew) - actionBPartner(true); - else if (e.getSource() == mBPartnerUpd) - actionBPartner(false); - } // actionPerformed - - /** - * Action Listener Interface - * @param listener listener - */ - public void addActionListener(ActionListener listener) - { - m_combo.addActionListener(listener); - m_text.addActionListener(listener); - } // addActionListener - - /** - * Action - Combo. - *
- * == dataBinding == inform of new value - *

-	 *  VLookup.actionCombo
-	 *      GridController.vetoableChange
-	 *          MTable.setValueAt
-	 *              MField.setValue
-	 *                  VLookup.setValue
-	 *          MTab.dataStatusChanged
-	 *  
- * @param value new value - */ - private void actionCombo (Object value) - { - log.fine("Value=" + value); - try - { - // -> GridController.vetoableChange - fireVetoableChange (m_columnName, null, value); - } - catch (PropertyVetoException pve) - { - log.log(Level.SEVERE, m_columnName, pve); - } - // is the value updated ? - boolean updated = false; - if (value == null && m_value == null) - updated = true; - else if (value != null && value.equals(m_value)) - updated = true; - if (!updated) - { - // happens if VLookup is used outside of APanel/GridController (no property listener) - log.fine(m_columnName + " - Value explicitly set - new=" + value + ", old=" + m_value); - if (getListeners(PropertyChangeListener.class).length <= 0) - setValue(value); - } - } // actionCombo - - - /** - * Action - Button. - * - Call Info - * @param queryValue initial query value - */ - private void actionButton (String queryValue) - { - m_button.setEnabled(false); // disable double click - if (m_lookup == null) - return; // leave button disabled - m_text.requestFocus(); // closes other editors - Frame frame = Env.getFrame(this); - - /** - * Three return options: - * - Value Selected & OK pressed => store result => result has value - * - Cancel pressed => store null => result == null && cancelled - * - Window closed -> ignore => result == null && !cancalled - */ - Object result = null; - boolean cancelled = false; - // - String col = m_lookup.getColumnName(); // fully qualified name - if (col.indexOf('.') != -1) - col = col.substring(col.indexOf('.')+1); - // Zoom / Validation - String whereClause = getWhereClause(); - // - log.fine(col - + ", Zoom=" + m_lookup.getZoom() - + " (" + whereClause + ")"); - // - boolean resetValue = false; // reset value so that is always treated as new entry - String infoFactoryClass = m_lookup.getInfoFactoryClass(); - if (infoFactoryClass != null && infoFactoryClass.trim().length() > 0) - { - try { - Class clazz = (Class)this.getClass().getClassLoader().loadClass(infoFactoryClass); - InfoFactory factory = clazz.newInstance(); - if (m_tableName == null) // sets table name & key column - getDirectAccessSQL("*"); - Info ig = factory.create (frame, true, m_lookup.getWindowNo(), - m_tableName, m_keyColumnName, queryValue, false, whereClause); - ig.setVisible(true); - cancelled = ig.isCancelled(); - result = ig.getSelectedKey(); - } catch (Exception e) { - log.log(Level.SEVERE, "Failed to load custom InfoFactory - " + e.getLocalizedMessage(), e); - } - } - else if (col.equals("M_Product_ID")) - { - // Reset - Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_Product_ID", "0"); - Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID", "0"); - Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_Lookup_ID", "0"); - // Replace Value with name if no value exists - if (queryValue.length() == 0 && m_text.getText().length() > 0) - queryValue = "@" + m_text.getText() + "@"; // Name indicator - otherwise Value - int M_Warehouse_ID = Env.getContextAsInt(Env.getCtx(), m_lookup.getWindowNo(), "M_Warehouse_ID"); - int M_PriceList_ID = Env.getContextAsInt(Env.getCtx(), m_lookup.getWindowNo(), "M_PriceList_ID"); - // Show Info - InfoProduct ip = new InfoProduct (frame, true, m_lookup.getWindowNo(), - M_Warehouse_ID, M_PriceList_ID, queryValue, false, whereClause); - ip.setVisible(true); - cancelled = ip.isCancelled(); - result = ip.getSelectedKey(); - resetValue = true; - } - else if (col.equals("C_BPartner_ID")) - { - // Replace Value with name if no value exists - if (queryValue.length() == 0 && m_text.getText().length() > 0) - queryValue = m_text.getText(); - boolean isSOTrx = true; // default - if (Env.getContext(Env.getCtx(), m_lookup.getWindowNo(), "IsSOTrx").equals("N")) - isSOTrx = false; - InfoBPartner ip = new InfoBPartner (frame, true, m_lookup.getWindowNo(), - queryValue, isSOTrx, false, whereClause); - ip.setVisible(true); - cancelled = ip.isCancelled(); - result = ip.getSelectedKey(); - } - else // General Info - { - if (m_tableName == null) // sets table name & key column - getDirectAccessSQL("*"); - Info ig = Info.create (frame, true, m_lookup.getWindowNo(), - m_tableName, m_keyColumnName, queryValue, false, whereClause); - ig.setVisible(true); - cancelled = ig.isCancelled(); - result = ig.getSelectedKey(); - } - - // Result - if (result != null) - { - log.config(m_columnName + " - Result = " + result.toString() + " (" + result.getClass().getName() + ")"); - // make sure that value is in cache - m_lookup.getDirect(result, false, true); - if (resetValue) - actionCombo (null); - actionCombo (result); // data binding - } - else if (cancelled) - { - log.config(m_columnName + " - Result = null (cancelled)"); - actionCombo(null); - } - else - { - log.config(m_columnName + " - Result = null (not cancelled)"); - setValue(m_value); // to re-display value - } - // - m_button.setEnabled(true); - m_text.requestFocus(); - } // actionButton - - /** - * Get Where Clause - * @return where clause or "" - */ - private String getWhereClause() - { - String whereClause = ""; - if (m_lookup == null) - return ""; - if (m_lookup.getZoomQuery() != null) - whereClause = m_lookup.getZoomQuery().getWhereClause(); - String validation = m_lookup.getValidation(); - if (validation == null) - validation = ""; - if (whereClause.length() == 0) - whereClause = validation; - else if (validation.length() > 0) - whereClause += " AND " + validation; - // log.finest("ZoomQuery=" + (m_lookup.getZoomQuery()==null ? "" : m_lookup.getZoomQuery().getWhereClause()) - // + ", Validation=" + m_lookup.getValidation()); - if (whereClause.indexOf('@') != -1) - { - String validated = Env.parseContext(Env.getCtx(), m_lookup.getWindowNo(), whereClause, false); - if (validated.length() == 0) - log.severe(m_columnName + " - Cannot Parse=" + whereClause); - else - { - log.fine(m_columnName + " - Parsed: " + validated); - return validated; - } - } - return whereClause; - } // getWhereClause - - /** - * Check, if data returns unique entry, otherwise involve Info via Button - */ - private void actionText() - { - String text = m_text.getText(); - // Nothing entered - if (text == null || text.length() == 0 || text.equals("%")) - { - actionButton(text); - return; - } - text = text.toUpperCase(); - log.config(m_columnName + " - " + text); - - // Exact first - PreparedStatement pstmt = null; - String finalSQL = Msg.parseTranslation(Env.getCtx(), getDirectAccessSQL(text)); - int id = -3; - try - { - pstmt = DB.prepareStatement(finalSQL, null); - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) - { - id = rs.getInt(1); // first - if (rs.next()) - id = -1; // only if unique - } - rs.close(); - pstmt.close(); - } - catch (Exception e) - { - log.log(Level.SEVERE, finalSQL, e); - id = -2; - } - // Try like - if (id == -3 && !text.endsWith("%")) - { - text += "%"; - finalSQL = Msg.parseTranslation(Env.getCtx(), getDirectAccessSQL(text)); - try - { - pstmt = DB.prepareStatement(finalSQL, null); - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) - { - id = rs.getInt(1); // first - if (rs.next()) - id = -1; // only if unique - } - rs.close(); - pstmt.close(); - } - catch (Exception e) - { - log.log(Level.SEVERE, finalSQL, e); - id = -2; - } - } - try - { - if (pstmt != null) - pstmt.close(); - } - catch (Exception e) - { - } - - // No (unique) result - if (id <= 0) - { - if (id == -3) - log.fine(m_columnName + " - Not Found - " + finalSQL); - else - log.fine(m_columnName + " - Not Unique - " + finalSQL); - m_value = null; // force re-display - actionButton(m_text.getText()); - return; - } - log.fine(m_columnName + " - Unique ID=" + id); - m_value = null; // forces re-display if value is unchanged but text updated and still unique - actionCombo (new Integer(id)); // data binding - m_text.requestFocus(); - } // actionText - - - private String m_tableName = null; - private String m_keyColumnName = null; - - /** - * Generate Access SQL for Search. - * The SQL returns the ID of the value entered - * Also sets m_tableName and m_keyColumnName - * @param text uppercase text for LIKE comparison - * @return sql or "" - * Example - * SELECT C_Payment_ID FROM C_Payment WHERE UPPER(DocumentNo) LIKE x OR ... - */ - private String getDirectAccessSQL (String text) - { - StringBuffer sql = new StringBuffer(); - m_tableName = m_columnName.substring(0, m_columnName.length()-3); - m_keyColumnName = m_columnName; - // - if (m_columnName.equals("M_Product_ID")) - { - // Reset - Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_Product_ID", "0"); - Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID", "0"); - Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_Locator_ID", "0"); - // - sql.append("SELECT M_Product_ID FROM M_Product WHERE (UPPER(Value) LIKE ") - .append(DB.TO_STRING(text)) - .append(" OR UPPER(Name) LIKE ").append(DB.TO_STRING(text)) - .append(" OR UPC LIKE ").append(DB.TO_STRING(text)).append(")"); - } - else if (m_columnName.equals("C_BPartner_ID")) - { - sql.append("SELECT C_BPartner_ID FROM C_BPartner WHERE (UPPER(Value) LIKE ") - .append(DB.TO_STRING(text)) - .append(" OR UPPER(Name) LIKE ").append(DB.TO_STRING(text)).append(")"); - } - else if (m_columnName.equals("C_Order_ID")) - { - sql.append("SELECT C_Order_ID FROM C_Order WHERE UPPER(DocumentNo) LIKE ") - .append(DB.TO_STRING(text)); - } - else if (m_columnName.equals("C_Invoice_ID")) - { - sql.append("SELECT C_Invoice_ID FROM C_Invoice WHERE UPPER(DocumentNo) LIKE ") - .append(DB.TO_STRING(text)); - } - else if (m_columnName.equals("M_InOut_ID")) - { - sql.append("SELECT M_InOut_ID FROM M_InOut WHERE UPPER(DocumentNo) LIKE ") - .append(DB.TO_STRING(text)); - } - else if (m_columnName.equals("C_Payment_ID")) - { - sql.append("SELECT C_Payment_ID FROM C_Payment WHERE UPPER(DocumentNo) LIKE ") - .append(DB.TO_STRING(text)); - } - else if (m_columnName.equals("GL_JournalBatch_ID")) - { - sql.append("SELECT GL_JournalBatch_ID FROM GL_JournalBatch WHERE UPPER(DocumentNo) LIKE ") - .append(DB.TO_STRING(text)); - } - else if (m_columnName.equals("SalesRep_ID")) - { - sql.append("SELECT AD_User_ID FROM AD_User WHERE UPPER(Name) LIKE ") - .append(DB.TO_STRING(text)); - m_tableName = "AD_User"; - m_keyColumnName = "AD_User_ID"; - } - // Predefined - if (sql.length() > 0) - { - String wc = getWhereClause(); - if (wc != null && wc.length() > 0) - sql.append(" AND ").append(wc); - sql.append(" AND IsActive='Y'"); - // *** - log.finest(m_columnName + " (predefined) " + sql.toString()); - return MRole.getDefault().addAccessSQL(sql.toString(), - m_tableName, MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); - } - - // Check if it is a Table Reference - if (m_lookup != null && m_lookup instanceof MLookup) - { - int AD_Reference_ID = ((MLookup)m_lookup).getAD_Reference_Value_ID(); - if (AD_Reference_ID != 0) - { - String query = "SELECT kc.ColumnName, dc.ColumnName, t.TableName " - + "FROM AD_Ref_Table rt" - + " INNER JOIN AD_Column kc ON (rt.AD_Key=kc.AD_Column_ID)" - + " INNER JOIN AD_Column dc ON (rt.AD_Display=dc.AD_Column_ID)" - + " INNER JOIN AD_Table t ON (rt.AD_Table_ID=t.AD_Table_ID) " - + "WHERE rt.AD_Reference_ID=?"; - String displayColumnName = null; - PreparedStatement pstmt = null; - try - { - pstmt = DB.prepareStatement(query, null); - pstmt.setInt(1, AD_Reference_ID); - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) - { - m_keyColumnName = rs.getString(1); - displayColumnName = rs.getString(2); - m_tableName = rs.getString(3); - } - rs.close(); - pstmt.close(); - pstmt = null; - } - catch (Exception e) - { - log.log(Level.SEVERE, query, e); - } - try - { - if (pstmt != null) - pstmt.close(); - pstmt = null; - } - catch (Exception e) - { - pstmt = null; - } - if (displayColumnName != null) - { - sql = new StringBuffer(); - sql.append("SELECT ").append(m_keyColumnName) - .append(" FROM ").append(m_tableName) - .append(" WHERE UPPER(").append(displayColumnName) - .append(") LIKE ").append(DB.TO_STRING(text)) - .append(" AND IsActive='Y'"); - String wc = getWhereClause(); - if (wc != null && wc.length() > 0) - sql.append(" AND ").append(wc); - // *** - log.finest(m_columnName + " (Table) " + sql.toString()); - return MRole.getDefault().addAccessSQL(sql.toString(), - m_tableName, MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); - } - } // Table Reference - } // MLookup - - /** Check Well Known Columns of Table - assumes TableDir **/ - String query = "SELECT t.TableName, c.ColumnName " - + "FROM AD_Column c " - + " INNER JOIN AD_Table t ON (c.AD_Table_ID=t.AD_Table_ID AND t.IsView='N') " - + "WHERE (c.ColumnName IN ('DocumentNo', 'Value', 'Name') OR c.IsIdentifier='Y')" - + " AND c.AD_Reference_ID IN (10,14)" - + " AND EXISTS (SELECT * FROM AD_Column cc WHERE cc.AD_Table_ID=t.AD_Table_ID" - + " AND cc.IsKey='Y' AND cc.ColumnName=?)"; - m_keyColumnName = m_columnName; - sql = new StringBuffer(); - PreparedStatement pstmt = null; - try - { - pstmt = DB.prepareStatement(query, null); - pstmt.setString(1, m_keyColumnName); - ResultSet rs = pstmt.executeQuery(); - while (rs.next()) - { - if (sql.length() != 0) - sql.append(" OR "); - m_tableName = rs.getString(1); - sql.append("UPPER(").append(rs.getString(2)).append(") LIKE ").append(DB.TO_STRING(text)); - } - rs.close(); - pstmt.close(); - pstmt = null; - } - catch (SQLException ex) - { - log.log(Level.SEVERE, query, ex); - } - try - { - if (pstmt != null) - pstmt.close(); - } - catch (SQLException ex1) - { - } - pstmt = null; - // - if (sql.length() == 0) - { - log.log(Level.SEVERE, m_columnName + " (TableDir) - no standard/identifier columns"); - return ""; - } - // - StringBuffer retValue = new StringBuffer ("SELECT ") - .append(m_columnName).append(" FROM ").append(m_tableName) - .append(" WHERE ").append(sql) - .append(" AND IsActive='Y'"); - String wc = getWhereClause(); - if (wc != null && wc.length() > 0) - retValue.append(" AND ").append(wc); - // *** - log.finest(m_columnName + " (TableDir) " + sql.toString()); - return MRole.getDefault().addAccessSQL(retValue.toString(), - m_tableName, MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); - } // getDirectAccessSQL - - - /** - * Action - Special BPartner Screen - * @param newRecord true if new record should be created - */ - private void actionBPartner (boolean newRecord) - { - VBPartner vbp = new VBPartner (Env.getFrame(this), m_lookup.getWindowNo()); - int BPartner_ID = 0; - // if update, get current value - if (!newRecord) - { - if (m_value instanceof Integer) - BPartner_ID = ((Integer)m_value).intValue(); - else if (m_value != null) - BPartner_ID = Integer.parseInt(m_value.toString()); - } - - vbp.loadBPartner (BPartner_ID); - vbp.setVisible(true); - // get result - int result = vbp.getC_BPartner_ID(); - if (result == 0 // 0 = not saved - && result == BPartner_ID) // the same - return; - // Maybe new BPartner - put in cache - m_lookup.getDirect(new Integer(result), false, true); - - actionCombo (new Integer(result)); // data binding - } // actionBPartner - - /** - * Action - Zoom - * @param selectedItem item - */ - private void actionZoom (Object selectedItem) - { - if (m_lookup == null) - return; - // - MQuery zoomQuery = m_lookup.getZoomQuery(); - Object value = getValue(); - if (value == null) - value = selectedItem; - // If not already exist or exact value - if (zoomQuery == null || value != null) - { - zoomQuery = new MQuery(); // ColumnName might be changed in MTab.validateQuery - String keyColumnName = null; - // Check if it is a Table Reference - if (m_lookup != null && m_lookup instanceof MLookup) - { - int AD_Reference_ID = ((MLookup)m_lookup).getAD_Reference_Value_ID(); - if (AD_Reference_ID != 0) - { - String query = "SELECT kc.ColumnName" - + " FROM AD_Ref_Table rt" - + " INNER JOIN AD_Column kc ON (rt.AD_Key=kc.AD_Column_ID)" - + "WHERE rt.AD_Reference_ID=?"; - - PreparedStatement pstmt = null; - try - { - pstmt = DB.prepareStatement(query, null); - pstmt.setInt(1, AD_Reference_ID); - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) - { - keyColumnName = rs.getString(1); - } - rs.close(); - pstmt.close(); - pstmt = null; - } - catch (Exception e) - { - log.log(Level.SEVERE, query, e); - } - try - { - if (pstmt != null) - pstmt.close(); - pstmt = null; - } - catch (Exception e) - { - pstmt = null; - } - } // Table Reference - } // MLookup - - if(keyColumnName != null && keyColumnName.length() !=0) - zoomQuery.addRestriction(keyColumnName, MQuery.EQUAL, value); - else - zoomQuery.addRestriction(m_columnName, MQuery.EQUAL, value); - - zoomQuery.setRecordCount(1); // guess - } - int AD_Window_ID = m_lookup.getZoom(zoomQuery); - // - log.info(m_columnName + " - AD_Window_ID=" + AD_Window_ID - + " - Query=" + zoomQuery + " - Value=" + value); - // - setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - // - AWindow frame = new AWindow(); - if (!frame.initWindow(AD_Window_ID, zoomQuery)) - { - setCursor(Cursor.getDefaultCursor()); - ValueNamePair pp = CLogger.retrieveError(); - String msg = pp==null ? "AccessTableNoView" : pp.getValue(); - ADialog.error(m_lookup.getWindowNo(), this, msg, pp==null ? "" : pp.getName()); - } - else - { - AEnv.addToWindowManager(frame); - if (Ini.isPropertyBool(Ini.P_OPEN_WINDOW_MAXIMIZED)) - { - AEnv.showMaximized(frame); - } - else - { - AEnv.showCenterScreen(frame); - } - } - // async window - not able to get feedback - frame = null; - // - setCursor(Cursor.getDefaultCursor()); - } // actionZoom - - /** - * Action - Refresh - */ - private void actionRefresh() - { - if (m_lookup == null) - return; - // - setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - // - Object obj = m_combo.getSelectedItem(); - log.info(m_columnName + " #" + m_lookup.getSize() + ", Selected=" + obj); - //no need to refresh readonly lookup, just remove direct cache - if (!isReadWrite()) - { - m_settingValue = true; // disable actions - m_lookup.removeAllElements(); - m_lastDisplay = m_lookup.getDisplay(m_value); - m_text.setText(m_lastDisplay); - m_text.setCaretPosition(0); - m_settingValue = false; - } - else - { - m_lookup.refresh(); - m_lookup.fillComboBox(isMandatory(), true, true, false); - m_combo.setSelectedItem(obj); - //m_combo.revalidate(); - } - // - setCursor(Cursor.getDefaultCursor()); - log.info(m_columnName + " #" + m_lookup.getSize() + ", Selected=" + m_combo.getSelectedItem()); - } // actionRefresh - - - /************************************************************************** - * Focus Listener for ComboBoxes with missing Validation or invalid entries - * - Requery listener for updated list - * @param e FocusEvent - */ - public void focusGained (FocusEvent e) - { - if (m_combo == null || m_combo.getEditor() == null) - return; - if ((e.getSource() != m_combo && e.getSource() != m_combo.getEditor().getEditorComponent()) - || e.isTemporary() || m_haveFocus || m_lookup == null) - return; - - //avoid repeated query - if (m_lookup.isValidated() && m_lookup.isLoaded()) - { - m_haveFocus = true; - return; - } - // - m_haveFocus = true; // prevents calling focus gained twice - m_settingFocus = true; // prevents actionPerformed - // - Object obj = m_lookup.getSelectedItem(); - log.config(m_columnName - + " - Start Count=" + m_combo.getItemCount() + ", Selected=" + obj); - // log.fine( "VLookupHash=" + this.hashCode()); - boolean popupVisible = m_combo.isPopupVisible(); - m_lookup.fillComboBox(isMandatory(), true, true, false); // only validated & active - if (popupVisible) - { - //refresh - m_combo.hidePopup(); - m_combo.showPopup(); - } - log.config(m_columnName - + " - Update Count=" + m_combo.getItemCount() + ", Selected=" + m_lookup.getSelectedItem()); - m_lookup.setSelectedItem(obj); - log.config(m_columnName - + " - Selected Count=" + m_combo.getItemCount() + ", Selected=" + m_lookup.getSelectedItem()); - // - m_settingFocus = false; - } // focusGained - - /** - * Reset Selection List - * @param e FocusEvent - */ - public void focusLost(FocusEvent e) - { - if (e.isTemporary() - || m_lookup == null - || !m_button.isEnabled() ) // set by actionButton - return; - // Text Lost focus - if (e.getSource() == m_text) - { - String text = m_text.getText(); - log.config(m_columnName + " (Text) " + m_columnName + " = " + m_value + " - " + text); - m_haveFocus = false; - // Skip if empty - if ((m_value == null - && m_text.getText().length() == 0)) - return; - if (m_lastDisplay.equals(text)) - return; - // - actionText(); // re-display - return; - } - // Combo lost focus - if (e.getSource() != m_combo && e.getSource() != m_combo.getEditor().getEditorComponent()) - return; - if (m_lookup.isValidated() && !m_lookup.hasInactive()) - { - m_haveFocus = false; - return; - } - // - m_settingFocus = true; // prevents actionPerformed - // - log.config(m_columnName + " = " + m_combo.getSelectedItem()); - Object obj = m_combo.getSelectedItem(); - /* - // set original model - if (!m_lookup.isValidated()) - m_lookup.fillComboBox(true); // previous selection - */ - // Set value - if (obj != null) - { - m_combo.setSelectedItem(obj); - // original model may not have item - if (!m_combo.getSelectedItem().equals(obj)) - { - log.fine(m_columnName + " - added to combo - " + obj); - m_combo.addItem(obj); - m_combo.setSelectedItem(obj); - } - } - // actionCombo(getValue()); - m_settingFocus = false; - m_haveFocus = false; // can gain focus again - } // focusLost - - /** - * Set ToolTip - * @param text tool tip text - */ - public void setToolTipText(String text) - { - super.setToolTipText(text); - m_button.setToolTipText(text); - m_text.setToolTipText(text); - m_combo.setToolTipText(text); - } // setToolTipText - - /** - * Refresh Query - * @return count - */ - public int refresh() - { - if (m_lookup == null) - return -1; - - //no need to refresh readonly lookup, just remove direct cache - if (!isReadWrite()) { - m_lookup.removeAllElements(); - return 0; - } - - return m_lookup.refresh(); - } // refresh - - /** - * Use by vcelleditor to indicate editing is off and don't invoke databinding - * @param stopediting - */ - public void setStopEditing(boolean stopediting) { - m_stopediting = stopediting; - } - - -} // VLookup - -/***************************************************************************** - * Mouse Listener for Popup Menu - */ -final class VLookup_mouseAdapter extends java.awt.event.MouseAdapter -{ - /** - * Constructor - * @param adaptee adaptee - */ - VLookup_mouseAdapter(VLookup adaptee) - { - m_adaptee = adaptee; - } // VLookup_mouseAdapter - - private VLookup m_adaptee; - - /** - * Mouse Listener - * @param e MouseEvent - */ - public void mouseClicked(MouseEvent e) - { - // System.out.println("mouseClicked " + e.getID() + " " + e.getSource().getClass().toString()); - // popup menu - if (SwingUtilities.isRightMouseButton(e)) - m_adaptee.popupMenu.show((Component)e.getSource(), e.getX(), e.getY()); - // Hide the popup if not right click - teo_sarca [ 1734802 ] - else - m_adaptee.popupMenu.setVisible(false); - } // mouse Clicked - -} // VLookup_mouseAdapter +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * For the text or an alternative of this public license, you may reach us * + * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA * + * or via info@compiere.org or http://www.compiere.org/license.html * + *****************************************************************************/ +package org.compiere.grid.ed; + +import java.awt.*; +import java.awt.event.*; +import java.beans.*; +import java.sql.*; +import java.util.logging.*; +import javax.swing.*; + +import org.compiere.apps.*; +import org.compiere.apps.search.*; +import org.compiere.model.*; +import org.compiere.swing.*; +import org.compiere.util.*; + +/** + * Lookup Visual Field. + *

+ * When r/o - display a Label + * When STABLE - display a ComboBox + * Otherwise show Selection Dialog + *

+ * Special handling of BPartner and Product + * + * @author Jorg Janke + * @version $Id: VLookup.java,v 1.5 2006/10/06 00:42:38 jjanke Exp $ + * + * @author Teo Sarca - BF [ 1740835 ] + * @author Michael Judd (MultiSelect) + */ +public class VLookup extends JComponent + implements VEditor, ActionListener, FocusListener +{ + @Override + protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, + int condition, boolean pressed) { + if (e.getSource() == m_combo || e.getSource() == m_text || e.getSource() == this) { + return super.processKeyBinding(ks, e, condition, pressed); + } + + JComponent editorComp = null; + if (m_lookup != null && m_lookup.getDisplayType() != DisplayType.Search) + editorComp = m_combo; + else + editorComp = m_text; + InputMap map = editorComp.getInputMap(condition); + ActionMap am = editorComp.getActionMap(); + + if(map!=null && am!=null && isEnabled()){ + Object binding = map.get(ks); + Action action = (binding==null) ? null : am.get(binding); + if(action!=null){ + return SwingUtilities.notifyAction(action, ks, e, editorComp, + e.getModifiers()); + } + } + return false; + } + + /** + * Create Optional BPartner Search Lookup + * @param WindowNo window + * @return VLookup + */ + public static VLookup createBPartner (int WindowNo) + { + int AD_Column_ID = 3499; // C_Invoice.C_BPartner_ID + try + { + Lookup lookup = MLookupFactory.get (Env.getCtx(), WindowNo, + 0, AD_Column_ID, DisplayType.Search); + return new VLookup ("C_BPartner_ID", false, false, true, lookup); + } + catch (Exception e) + { + log.log(Level.SEVERE, "", e); + } + return null; + } // createBPartner + + /** + * Create Optional Product Search Lookup + * @param WindowNo window + * @return VLookup + */ + public static VLookup createProduct (int WindowNo) + { + int AD_Column_ID = 3840; // C_InvoiceLine.M_Product_ID + try + { + Lookup lookup = MLookupFactory.get (Env.getCtx(), WindowNo, 0, + AD_Column_ID, DisplayType.Search); + return new VLookup ("M_Product_ID", false, false, true, lookup); + } + catch (Exception e) + { + log.log(Level.SEVERE, "", e); + } + return null; + } // createProduct + + /** + * Create Optional User Search Lookup + * @param WindowNo window + * @return VLookup + */ + public static VLookup createUser (int WindowNo) + { + int AD_Column_ID = 10443; // AD_WF_Activity.AD_User_UD + try + { + Lookup lookup = MLookupFactory.get (Env.getCtx(), WindowNo, 0, + AD_Column_ID, DisplayType.Search); + return new VLookup ("AD_User_ID", false, false, true, lookup); + } + catch (Exception e) + { + log.log(Level.SEVERE, "", e); + } + return null; + } // createProduct + + + /************************************************************************* + * Detail Constructor + * + * @param columnName column + * @param mandatory mandatory + * @param isReadOnly read only + * @param isUpdateable updateable + * @param lookup lookup + */ + public VLookup (String columnName, boolean mandatory, boolean isReadOnly, boolean isUpdateable, + Lookup lookup) + { + super(); + super.setName(columnName); + m_combo.setName(columnName); + m_columnName = columnName; + setMandatory(mandatory); + m_lookup = lookup; + if (m_lookup != null) + m_lookup.setMandatory(mandatory); + // + setLayout(new BorderLayout()); + mouseAdapter = new VLookup_mouseAdapter(this); // popup + + // *** Text & Button *** + m_text.addActionListener(this); + m_text.addFocusListener(this); + m_text.addMouseListener(mouseAdapter); + // Button + m_button.addActionListener(this); + m_button.addMouseListener(mouseAdapter); + m_button.setFocusable(false); // don't focus when tabbing + m_button.setMargin(new Insets(0, 0, 0, 0)); + if (columnName.equals("C_BPartner_ID")) + m_button.setIcon(Env.getImageIcon("BPartner10.gif")); + else if (columnName.equals("M_Product_ID")) + m_button.setIcon(Env.getImageIcon("Product10.gif")); + else + m_button.setIcon(Env.getImageIcon("PickOpen10.gif")); + + // *** VComboBox *** + if (m_lookup != null && m_lookup.getDisplayType() != DisplayType.Search) // No Search + { + // Don't have to fill up combobox if it is readonly + if (!isReadOnly && isUpdateable) + m_lookup.fillComboBox (isMandatory(), true, true, false); + m_combo.setModel(m_lookup); + // + AutoCompletion.enable(m_combo); + m_combo.addActionListener(this); // Selection + m_combo.getEditor().getEditorComponent().addMouseListener(mouseAdapter); // popup + // FocusListener to refresh selection before opening + m_combo.addFocusListener(this); + m_combo.getEditor().getEditorComponent().addFocusListener(this); + } + + setUI (true); + // ReadWrite - decides what components to show + if (isReadOnly || !isUpdateable || m_lookup == null) + setReadWrite(false); + else + setReadWrite(true); + + // Popup + if (m_lookup != null) + { + if ((m_lookup.getDisplayType() == DisplayType.List && Env.getContextAsInt(Env.getCtx(), "#AD_Role_ID") == 0) + || m_lookup.getDisplayType() != DisplayType.List) // only system admins can change lists, so no need to zoom for others + { + mZoom = new CMenuItem(Msg.getMsg(Env.getCtx(), "Zoom"), Env.getImageIcon("Zoom16.gif")); + mZoom.addActionListener(this); + popupMenu.add(mZoom); + } + mRefresh = new CMenuItem(Msg.getMsg(Env.getCtx(), "Refresh"), Env.getImageIcon("Refresh16.gif")); + mRefresh.addActionListener(this); + popupMenu.add(mRefresh); + } + // VBPartner quick entry link + if (columnName.equals("C_BPartner_ID")) + { + mBPartnerNew = new CMenuItem (Msg.getMsg(Env.getCtx(), "New"), Env.getImageIcon("InfoBPartner16.gif")); + mBPartnerNew.addActionListener(this); + mBPartnerNew.setVisible(isReadWrite()); // visible only if the field is editable - teo_sarca [ 1721710 ] + popupMenu.add(mBPartnerNew); + mBPartnerUpd = new CMenuItem (Msg.getMsg(Env.getCtx(), "Update"), Env.getImageIcon("InfoBPartner16.gif")); + mBPartnerUpd.addActionListener(this); + popupMenu.add(mBPartnerUpd); + } + // + if (m_lookup != null && m_lookup.getZoom() == 0) + mZoom.setEnabled(false); + } // VLookup + + /** + * Dispose + */ + public void dispose() + { + m_text = null; + m_button = null; + m_lookup = null; + m_mField = null; + // + m_combo.getEditor().getEditorComponent().removeFocusListener(this); + m_combo.getEditor().getEditorComponent().removeMouseListener(mouseAdapter); + m_combo.removeFocusListener(this); + m_combo.removeActionListener(this); + m_combo.setModel(new DefaultComboBoxModel()); // remove reference + // m_combo.removeAllItems(); + m_combo = null; + } // dispose + + /** Display Length for Lookups (15) */ + public final static int DISPLAY_LENGTH = 15; + /** Field Height */ + public static int FIELD_HIGHT = 0; + + /** Search: The Editable Text Field */ + private CTextField m_text = new CTextField (DISPLAY_LENGTH); + /** Search: The Button to open Editor */ + private CButton m_button = new CButton(); + /** The Combo Box if not a Search Lookup */ + private VComboBox m_combo = new VComboBox(); + /** Indicator that value is being set */ + private volatile boolean m_settingValue = false; + /** Indicator that docus is being set */ + private volatile boolean m_settingFocus = false; + /** Indicator that Lookup has focus */ + private volatile boolean m_haveFocus = false; + /** Indicator - inserting new value */ + private volatile boolean m_inserting = false; + /** Last Display */ + private String m_lastDisplay = ""; + /** Column Name */ + private String m_columnName; + /** Lookup */ + private Lookup m_lookup; + /** Conbo Box Active */ + private boolean m_comboActive = true; + /** The Value */ + private Object m_value; + + private boolean m_stopediting = false; + + // Popup + JPopupMenu popupMenu = new JPopupMenu(); + private CMenuItem mZoom; + private CMenuItem mRefresh; + private CMenuItem mBPartnerNew; + private CMenuItem mBPartnerUpd; + // Mouse Listener + private VLookup_mouseAdapter mouseAdapter; + + + // Field for Value Preference + private GridField m_mField = null; + /** Logger */ + private static CLogger log = CLogger.getCLogger(VLookup.class); + + /** + * Set Content and Size of Components + * @param initial if true, size and margins will be set + */ + private void setUI (boolean initial) + { + if (initial) + { + Dimension size = m_text.getPreferredSize(); + setPreferredSize(new Dimension(size)); // causes r/o to be the same length + m_combo.setPreferredSize(new Dimension(size)); + setMinimumSize(new Dimension (30, size.height)); + FIELD_HIGHT = size.height; + // + m_text.setBorder(null); + Dimension bSize = new Dimension(size.height, size.height); + m_button.setPreferredSize (bSize); + } + + // What to show + this.remove(m_combo); + this.remove(m_button); + this.remove(m_text); + // + if (!isReadWrite()) // r/o - show text only + { + LookAndFeel.installBorder(this, "TextField.border"); + this.add(m_text, BorderLayout.CENTER); + m_text.setReadWrite(false); + m_combo.setReadWrite(false); + m_comboActive = false; + } + else if (m_lookup != null && m_lookup.getDisplayType() != DisplayType.Search) // show combo if not Search + { + this.setBorder(null); + this.add(m_combo, BorderLayout.CENTER); + m_comboActive = true; + } + else // Search or unstable - show text & button + { + LookAndFeel.installBorder(this, "TextField.border"); + this.add(m_text, BorderLayout.CENTER); + this.add(m_button, BorderLayout.EAST); + m_text.setReadWrite (true); + m_comboActive = false; + } + } // setUI + + /** + * Set ReadWrite + * @param value ReadWrite + */ + public void setReadWrite (boolean value) + { + boolean rw = value; + if (m_lookup == null) + rw = false; + if (m_combo.isReadWrite() != value) + { + m_combo.setReadWrite(rw); + setUI (false); + if (value && m_comboActive) { + m_settingValue = true; // disable actions + refresh(); + m_settingValue = false; + } + if (m_comboActive) + setValue (m_value); + } + // If the field is readonly the BPartner new option should be hidden - teo_sarca [ 1721710 ] + if (mBPartnerNew != null) + mBPartnerNew.setVisible(value); + } // setReadWrite + + /** + * IsEditable + * @return is lookup ReadWrite + */ + public boolean isReadWrite() + { + return m_combo.isReadWrite(); + } // isReadWrite + + /** + * Set Mandatory (and back color) + * @param mandatory mandatory + */ + public void setMandatory (boolean mandatory) + { + m_combo.setMandatory(mandatory); + m_text.setMandatory(mandatory); + } // setMandatory + + /** + * Is it mandatory + * @return true if mandatory + */ + public boolean isMandatory() + { + return m_combo.isMandatory(); + } // isMandatory + + /** + * Set Background + * @param color color + */ + public void setBackground(Color color) + { + m_text.setBackground(color); + m_combo.setBackground(color); + } // setBackground + + /** + * Set Background + * @param error error + */ + public void setBackground (boolean error) + { + m_text.setBackground(error); + m_combo.setBackground(error); + } // setBackground + + /** + * Set Foreground + * @param fg Foreground color + */ + public void setForeground(Color fg) + { + m_text.setForeground(fg); + m_combo.setForeground(fg); + } // setForeground + + /** + * Request Focus + */ + public void requestFocus () + { + if (m_lookup != null && m_lookup.getDisplayType() != DisplayType.Search) + m_combo.requestFocus (); + else + m_text.requestFocus (); + } // requestFocus + + + /** + * Set Editor to value + * @param value new Value + */ + public void setValue (Object value) + { + log.fine(m_columnName + "=" + value); + m_settingValue = true; // disable actions + m_value = value; + + // Set both for switching + if (value == null) + { + m_combo.setValue (value); + m_text.setText (null); + m_lastDisplay = ""; + m_settingValue = false; + return; + } + if (m_lookup == null) + { + m_combo.setValue (value); + m_text.setText (value.toString()); + m_lastDisplay = value.toString(); + m_settingValue = false; + return; + } + + //must call m_combo.setvalue after m_lookup as + //loading of combo data might happen in m_lookup.getDisplay + m_lastDisplay = m_lookup.getDisplay(value); + m_combo.setValue (value); + + if (m_lastDisplay.equals("<-1>")) + { + m_lastDisplay = ""; + m_value = null; + } + boolean notFound = m_lastDisplay.startsWith("<") && m_lastDisplay.endsWith(">"); + m_text.setText (m_lastDisplay); + m_text.setCaretPosition (0); // show beginning + + // Nothing showing in Combo and should be showing + if (m_combo.getSelectedItem() == null + && (m_comboActive || (m_inserting && m_lookup.getDisplayType() != DisplayType.Search))) + { + // lookup found nothing too + if (notFound) + { + log.finest(m_columnName + "=" + value + ": Not found - " + m_lastDisplay); + // we may have a new value + m_lookup.refresh(); + m_combo.setValue (value); + m_lastDisplay = m_lookup.getDisplay(value); + m_text.setText (m_lastDisplay); + m_text.setCaretPosition (0); // show beginning + notFound = m_lastDisplay.startsWith("<") && m_lastDisplay.endsWith(">"); + } + if (notFound) // + { + m_value = null; + actionCombo (null); // data binding + log.fine(m_columnName + "=" + value + ": Not found"); + } + // we have lookup + else if (m_combo.getSelectedItem() == null) + { + NamePair pp = m_lookup.get(value); + if (pp != null) + { + log.fine(m_columnName + " added to combo - " + pp); + // Add to Combo + m_combo.addItem (pp); + m_combo.setValue (value); + } + } + // Not in Lookup - set to Null + if (m_combo.getSelectedItem() == null) + { + log.info(m_columnName + "=" + value + ": not in Lookup - set to NULL"); + actionCombo (null); // data binding (calls setValue again) + m_value = null; + } + } + m_settingValue = false; + } // setValue + + /** + * Property Change Listener + * @param evt PropertyChangeEvent + */ + public void propertyChange (PropertyChangeEvent evt) + { + if (m_stopediting) + return; + + // log.fine( "VLookup.propertyChange", evt); + if (evt.getPropertyName().equals(GridField.PROPERTY)) + { + m_inserting = GridField.INSERTING.equals(evt.getOldValue()); // MField.setValue + setValue(evt.getNewValue()); + m_inserting = false; + } + } // propertyChange + + /** + * Return Editor value (Integer) + * @return value + */ + public Object getValue() + { + if (m_comboActive) + return m_combo.getValue (); + return m_value; + } // getValue + + /** + * Return editor display + * @return display value + */ + public String getDisplay() + { + String retValue = null; + if (m_comboActive) + retValue = m_combo.getDisplay(); + // check lookup + else if (m_lookup == null) + retValue = m_value == null ? null : m_value.toString(); + else + retValue = m_lookup.getDisplay(m_value); + // log.fine( "VLookup.getDisplay - " + retValue, "ComboActive=" + m_comboActive); + return retValue; + } // getDisplay + + /** + * Set Field/WindowNo for ValuePreference + * @param mField Model Field for Lookup + */ + public void setField (GridField mField) + { + m_mField = mField; + if (m_mField != null + && MRole.getDefault().isShowPreference()) + ValuePreference.addMenu (this, popupMenu); + } // setField + + + /************************************************************************** + * Action Listener - data binding + * @param e ActionEvent + */ + public void actionPerformed (ActionEvent e) + { + if (m_settingValue || m_settingFocus || m_stopediting) + return; + log.config(m_columnName + " - " + e.getActionCommand() + ", ComboValue=" + m_combo.getSelectedItem()); + // log.fine("Hash=" + this.hashCode()); + + // Preference + if (e.getActionCommand().equals(ValuePreference.NAME)) + { + if (MRole.getDefault().isShowPreference()) + ValuePreference.start (m_mField, getValue(), getDisplay()); + return; + } + + // Combo Selection + else if (e.getSource() == m_combo) + { + Object value = getValue(); + Object o = m_combo.getSelectedItem(); + if (o != null) + { + String s = o.toString(); + // don't allow selection of inactive + if (s.startsWith(MLookup.INACTIVE_S) && s.endsWith(MLookup.INACTIVE_E)) + { + log.info(m_columnName + " - selection inactive set to NULL"); + value = null; + } + } + actionCombo (value); // data binding + } + // Button pressed + else if (e.getSource() == m_button) + actionButton (""); + // Text entered + else if (e.getSource() == m_text) + actionText(); + + // Popup Menu + else if (e.getSource() == mZoom) + actionZoom(m_combo.getSelectedItem()); + else if (e.getSource() == mRefresh) + actionRefresh(); + else if (e.getSource() == mBPartnerNew) + actionBPartner(true); + else if (e.getSource() == mBPartnerUpd) + actionBPartner(false); + } // actionPerformed + + /** + * Action Listener Interface + * @param listener listener + */ + public void addActionListener(ActionListener listener) + { + m_combo.addActionListener(listener); + m_text.addActionListener(listener); + } // addActionListener + + /** + * Action - Combo. + *
+ * == dataBinding == inform of new value + *

+	 *  VLookup.actionCombo
+	 *      GridController.vetoableChange
+	 *          MTable.setValueAt
+	 *              MField.setValue
+	 *                  VLookup.setValue
+	 *          MTab.dataStatusChanged
+	 *  
+ * @param value new value + */ + private void actionCombo (Object value) + { + log.fine("Value=" + value); + try + { + // -> GridController.vetoableChange + fireVetoableChange (m_columnName, null, value); + } + catch (PropertyVetoException pve) + { + log.log(Level.SEVERE, m_columnName, pve); + } + // is the value updated ? + boolean updated = false; + + Object updatedValue = value; + + if (updatedValue instanceof Object[] && ((Object[])updatedValue).length > 0) + { + updatedValue = ((Object[])updatedValue)[0]; + } + + if (updatedValue == null && m_value == null) + updated = true; + else if (updatedValue != null && value.equals(m_value)) + updated = true; + if (!updated) + { + // happens if VLookup is used outside of APanel/GridController (no property listener) + log.fine(m_columnName + " - Value explicitly set - new=" + updatedValue + ", old=" + m_value); + if (getListeners(PropertyChangeListener.class).length <= 0) + setValue(updatedValue); + } + } // actionCombo + + + /** + * Action - Button. + * - Call Info + * @param queryValue initial query value + */ + private void actionButton (String queryValue) + { + m_button.setEnabled(false); // disable double click + if (m_lookup == null) + return; // leave button disabled + m_text.requestFocus(); // closes other editors + Frame frame = Env.getFrame(this); + + /** + * Three return options: + * - Value Selected & OK pressed => store result => result has value + * - Cancel pressed => store null => result == null && cancelled + * - Window closed -> ignore => result == null && !cancalled + */ + + Object result[] = null; + boolean cancelled = false; + boolean multipleSelection = false; + // + String col = m_lookup.getColumnName(); // fully qualified name + if (col.indexOf('.') != -1) + col = col.substring(col.indexOf('.')+1); + // Zoom / Validation + String whereClause = getWhereClause(); + // + log.fine(col + + ", Zoom=" + m_lookup.getZoom() + + " (" + whereClause + ")"); + // + boolean resetValue = false; // reset value so that is always treated as new entry + String infoFactoryClass = m_lookup.getInfoFactoryClass(); + if (infoFactoryClass != null && infoFactoryClass.trim().length() > 0) + { + try { + Class clazz = (Class)this.getClass().getClassLoader().loadClass(infoFactoryClass); + InfoFactory factory = clazz.newInstance(); + if (m_tableName == null) // sets table name & key column + getDirectAccessSQL("*"); + Info ig = factory.create (frame, true, m_lookup.getWindowNo(), + m_tableName, m_keyColumnName, queryValue, false, whereClause); + ig.setVisible(true); + cancelled = ig.isCancelled(); + result = ig.getSelectedKeys(); + } catch (Exception e) { + log.log(Level.SEVERE, "Failed to load custom InfoFactory - " + e.getLocalizedMessage(), e); + } + } + else if (col.equals("M_Product_ID")) + { + // Reset + Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_Product_ID", "0"); + Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID", "0"); + Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_Lookup_ID", "0"); + // Replace Value with name if no value exists + if (queryValue.length() == 0 && m_text.getText().length() > 0) + queryValue = "@" + m_text.getText() + "@"; // Name indicator - otherwise Value + int M_Warehouse_ID = Env.getContextAsInt(Env.getCtx(), m_lookup.getWindowNo(), "M_Warehouse_ID"); + int M_PriceList_ID = Env.getContextAsInt(Env.getCtx(), m_lookup.getWindowNo(), "M_PriceList_ID"); + + int AD_Table_ID = MColumn.getTable_ID(Env.getCtx(), m_mField.getAD_Column_ID(), null); + multipleSelection = (MOrderLine.Table_ID == AD_Table_ID) || (MInvoiceLine.Table_ID == AD_Table_ID); + + // Show Info + InfoProduct ip = new InfoProduct (frame, true, m_lookup.getWindowNo(), + M_Warehouse_ID, M_PriceList_ID, queryValue, multipleSelection, whereClause); + ip.setVisible(true); + cancelled = ip.isCancelled(); + result = ip.getSelectedKeys(); + resetValue = true; + } + else if (col.equals("C_BPartner_ID")) + { + // Replace Value with name if no value exists + if (queryValue.length() == 0 && m_text.getText().length() > 0) + queryValue = m_text.getText(); + boolean isSOTrx = true; // default + if (Env.getContext(Env.getCtx(), m_lookup.getWindowNo(), "IsSOTrx").equals("N")) + isSOTrx = false; + InfoBPartner ip = new InfoBPartner (frame, true, m_lookup.getWindowNo(), + queryValue, isSOTrx, multipleSelection, whereClause); + ip.setVisible(true); + cancelled = ip.isCancelled(); + result = ip.getSelectedKeys(); + } + else // General Info + { + if (m_tableName == null) // sets table name & key column + getDirectAccessSQL("*"); + Info ig = Info.create (frame, true, m_lookup.getWindowNo(), + m_tableName, m_keyColumnName, queryValue, multipleSelection, whereClause); + ig.setVisible(true); + cancelled = ig.isCancelled(); + result = ig.getSelectedKeys(); + } + + // Result + if (result != null && result.length > 0) + { + log.config(m_columnName + " - Result = " + result.toString() + " (" + result.getClass().getName() + ")"); + // make sure that value is in cache + m_lookup.getDirect(result[0], false, true); + if (resetValue) + actionCombo (null); + // juddm added logic for multi-select handling + if (result.length > 1) + actionCombo (result); // data binding + else + actionCombo (result[0]); + + } + else if (cancelled) + { + log.config(m_columnName + " - Result = null (cancelled)"); + actionCombo(null); + } + else + { + log.config(m_columnName + " - Result = null (not cancelled)"); + setValue(m_value); // to re-display value + } + // + m_button.setEnabled(true); + m_text.requestFocus(); + } // actionButton + + /** + * Get Where Clause + * @return where clause or "" + */ + private String getWhereClause() + { + String whereClause = ""; + if (m_lookup == null) + return ""; + if (m_lookup.getZoomQuery() != null) + whereClause = m_lookup.getZoomQuery().getWhereClause(); + String validation = m_lookup.getValidation(); + if (validation == null) + validation = ""; + if (whereClause.length() == 0) + whereClause = validation; + else if (validation.length() > 0) + whereClause += " AND " + validation; + // log.finest("ZoomQuery=" + (m_lookup.getZoomQuery()==null ? "" : m_lookup.getZoomQuery().getWhereClause()) + // + ", Validation=" + m_lookup.getValidation()); + if (whereClause.indexOf('@') != -1) + { + String validated = Env.parseContext(Env.getCtx(), m_lookup.getWindowNo(), whereClause, false); + if (validated.length() == 0) + log.severe(m_columnName + " - Cannot Parse=" + whereClause); + else + { + log.fine(m_columnName + " - Parsed: " + validated); + return validated; + } + } + return whereClause; + } // getWhereClause + + /** + * + * + * + */ + private String getExtraWhereClause (String text) + { + StringBuffer sql = new StringBuffer(); + m_tableName = m_columnName.substring(0, m_columnName.length()-3); + m_keyColumnName = m_columnName; + // + if (m_columnName.equals("M_Product_ID")) + { + // Reset + Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_Product_ID", "0"); + Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID", "0"); + Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_Locator_ID", "0"); + // + sql.append(" AND (UPPER(p.Value) LIKE ") + .append(DB.TO_STRING(text)) + .append(" OR UPPER(p.Name) LIKE ").append(DB.TO_STRING(text)) + .append(" OR p.SKU LIKE ").append(DB.TO_STRING(text)).append(")"); + //.append(" OR p.SKU LIKE ").append(DB.TO_STRING(text)) + //.append(" OR p.UPC LIKE ").append(DB.TO_STRING(text)).append(")"); + } + // Predefined + /* + if (sql.length() > 0) + { + String wc = getWhereClause(); + if (wc != null && wc.length() > 0) + sql.append(" AND ").append(wc); + sql.append(" AND IsActive='Y'"); + // *** + log.finest(m_columnName + " (predefined) " + sql.toString()); + return MRole.getDefault().addAccessSQL(sql.toString(), + m_tableName, MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); + }*/ + + return sql.toString(); + } + /** + * Check, if data returns unique entry, otherwise involve Info via Button + */ + private void actionText() + { + String text = m_text.getText(); + // Nothing entered + if (text == null || text.length() == 0 || text.equals("%")) + { + actionButton(text); + return; + } + text = text.toUpperCase(); + log.config(m_columnName + " - " + text); + + // Exact first + PreparedStatement pstmt = null; + String finalSQL = Msg.parseTranslation(Env.getCtx(), getDirectAccessSQL(text)); + int id = -3; + try + { + pstmt = DB.prepareStatement(finalSQL, null); + ResultSet rs = pstmt.executeQuery(); + if (rs.next()) + { + id = rs.getInt(1); // first + if (rs.next()) + id = -1; // only if unique + } + rs.close(); + pstmt.close(); + } + catch (Exception e) + { + log.log(Level.SEVERE, finalSQL, e); + id = -2; + } + // Try like + if (id == -3 && !text.endsWith("%")) + { + text += "%"; + finalSQL = Msg.parseTranslation(Env.getCtx(), getDirectAccessSQL(text)); + try + { + pstmt = DB.prepareStatement(finalSQL, null); + ResultSet rs = pstmt.executeQuery(); + if (rs.next()) + { + id = rs.getInt(1); // first + if (rs.next()) + id = -1; // only if unique + } + rs.close(); + pstmt.close(); + } + catch (Exception e) + { + log.log(Level.SEVERE, finalSQL, e); + id = -2; + } + } + try + { + if (pstmt != null) + pstmt.close(); + } + catch (Exception e) + { + } + + // No (unique) result + if (id <= 0) + { + if (id == -3) + log.fine(m_columnName + " - Not Found - " + finalSQL); + else + log.fine(m_columnName + " - Not Unique - " + finalSQL); + m_value = null; // force re-display + actionButton(m_text.getText()); + return; + } + log.fine(m_columnName + " - Unique ID=" + id); + m_value = null; // forces re-display if value is unchanged but text updated and still unique + actionCombo (new Integer(id)); // data binding + m_text.requestFocus(); + } // actionText + + + private String m_tableName = null; + private String m_keyColumnName = null; + + /** + * Generate Access SQL for Search. + * The SQL returns the ID of the value entered + * Also sets m_tableName and m_keyColumnName + * @param text uppercase text for LIKE comparison + * @return sql or "" + * Example + * SELECT C_Payment_ID FROM C_Payment WHERE UPPER(DocumentNo) LIKE x OR ... + */ + private String getDirectAccessSQL (String text) + { + StringBuffer sql = new StringBuffer(); + m_tableName = m_columnName.substring(0, m_columnName.length()-3); + m_keyColumnName = m_columnName; + // + if (m_columnName.equals("M_Product_ID")) + { + // Reset + Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_Product_ID", "0"); + Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_AttributeSetInstance_ID", "0"); + Env.setContext(Env.getCtx(), Env.WINDOW_INFO, Env.TAB_INFO, "M_Locator_ID", "0"); + // + sql.append("SELECT M_Product_ID FROM M_Product WHERE (UPPER(Value) LIKE ") + .append(DB.TO_STRING(text)) + .append(" OR UPPER(Name) LIKE ").append(DB.TO_STRING(text)) + .append(" OR SKU LIKE ").append(DB.TO_STRING(text)) + .append(" OR UPC LIKE ").append(DB.TO_STRING(text)).append(")"); + } + else if (m_columnName.equals("C_BPartner_ID")) + { + sql.append("SELECT C_BPartner_ID FROM C_BPartner WHERE (UPPER(Value) LIKE ") + .append(DB.TO_STRING(text)) + .append(" OR UPPER(Name) LIKE ").append(DB.TO_STRING(text)).append(")"); + } + else if (m_columnName.equals("C_Order_ID")) + { + sql.append("SELECT C_Order_ID FROM C_Order WHERE UPPER(DocumentNo) LIKE ") + .append(DB.TO_STRING(text)); + } + else if (m_columnName.equals("C_Invoice_ID")) + { + sql.append("SELECT C_Invoice_ID FROM C_Invoice WHERE UPPER(DocumentNo) LIKE ") + .append(DB.TO_STRING(text)); + } + else if (m_columnName.equals("M_InOut_ID")) + { + sql.append("SELECT M_InOut_ID FROM M_InOut WHERE UPPER(DocumentNo) LIKE ") + .append(DB.TO_STRING(text)); + } + else if (m_columnName.equals("C_Payment_ID")) + { + sql.append("SELECT C_Payment_ID FROM C_Payment WHERE UPPER(DocumentNo) LIKE ") + .append(DB.TO_STRING(text)); + } + else if (m_columnName.equals("GL_JournalBatch_ID")) + { + sql.append("SELECT GL_JournalBatch_ID FROM GL_JournalBatch WHERE UPPER(DocumentNo) LIKE ") + .append(DB.TO_STRING(text)); + } + else if (m_columnName.equals("SalesRep_ID")) + { + sql.append("SELECT AD_User_ID FROM AD_User WHERE UPPER(Name) LIKE ") + .append(DB.TO_STRING(text)); + m_tableName = "AD_User"; + m_keyColumnName = "AD_User_ID"; + } + // Predefined + if (sql.length() > 0) + { + String wc = getWhereClause(); + if (wc != null && wc.length() > 0) + sql.append(" AND ").append(wc); + sql.append(" AND IsActive='Y'"); + // *** + log.finest(m_columnName + " (predefined) " + sql.toString()); + return MRole.getDefault().addAccessSQL(sql.toString(), + m_tableName, MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); + } + + // Check if it is a Table Reference + if (m_lookup != null && m_lookup instanceof MLookup) + { + int AD_Reference_ID = ((MLookup)m_lookup).getAD_Reference_Value_ID(); + if (AD_Reference_ID != 0) + { + String query = "SELECT kc.ColumnName, dc.ColumnName, t.TableName " + + "FROM AD_Ref_Table rt" + + " INNER JOIN AD_Column kc ON (rt.AD_Key=kc.AD_Column_ID)" + + " INNER JOIN AD_Column dc ON (rt.AD_Display=dc.AD_Column_ID)" + + " INNER JOIN AD_Table t ON (rt.AD_Table_ID=t.AD_Table_ID) " + + "WHERE rt.AD_Reference_ID=?"; + String displayColumnName = null; + PreparedStatement pstmt = null; + try + { + pstmt = DB.prepareStatement(query, null); + pstmt.setInt(1, AD_Reference_ID); + ResultSet rs = pstmt.executeQuery(); + if (rs.next()) + { + m_keyColumnName = rs.getString(1); + displayColumnName = rs.getString(2); + m_tableName = rs.getString(3); + } + rs.close(); + pstmt.close(); + pstmt = null; + } + catch (Exception e) + { + log.log(Level.SEVERE, query, e); + } + try + { + if (pstmt != null) + pstmt.close(); + pstmt = null; + } + catch (Exception e) + { + pstmt = null; + } + if (displayColumnName != null) + { + sql = new StringBuffer(); + sql.append("SELECT ").append(m_keyColumnName) + .append(" FROM ").append(m_tableName) + .append(" WHERE UPPER(").append(displayColumnName) + .append(") LIKE ").append(DB.TO_STRING(text)) + .append(" AND IsActive='Y'"); + String wc = getWhereClause(); + if (wc != null && wc.length() > 0) + sql.append(" AND ").append(wc); + // *** + log.finest(m_columnName + " (Table) " + sql.toString()); + return MRole.getDefault().addAccessSQL(sql.toString(), + m_tableName, MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); + } + } // Table Reference + } // MLookup + + /** Check Well Known Columns of Table - assumes TableDir **/ + String query = "SELECT t.TableName, c.ColumnName " + + "FROM AD_Column c " + + " INNER JOIN AD_Table t ON (c.AD_Table_ID=t.AD_Table_ID AND t.IsView='N') " + + "WHERE (c.ColumnName IN ('DocumentNo', 'Value', 'Name') OR c.IsIdentifier='Y')" + + " AND c.AD_Reference_ID IN (10,14)" + + " AND EXISTS (SELECT * FROM AD_Column cc WHERE cc.AD_Table_ID=t.AD_Table_ID" + + " AND cc.IsKey='Y' AND cc.ColumnName=?)"; + m_keyColumnName = m_columnName; + sql = new StringBuffer(); + PreparedStatement pstmt = null; + try + { + pstmt = DB.prepareStatement(query, null); + pstmt.setString(1, m_keyColumnName); + ResultSet rs = pstmt.executeQuery(); + while (rs.next()) + { + if (sql.length() != 0) + sql.append(" OR "); + m_tableName = rs.getString(1); + sql.append("UPPER(").append(rs.getString(2)).append(") LIKE ").append(DB.TO_STRING(text)); + } + rs.close(); + pstmt.close(); + pstmt = null; + } + catch (SQLException ex) + { + log.log(Level.SEVERE, query, ex); + } + try + { + if (pstmt != null) + pstmt.close(); + } + catch (SQLException ex1) + { + } + pstmt = null; + // + if (sql.length() == 0) + { + log.log(Level.SEVERE, m_columnName + " (TableDir) - no standard/identifier columns"); + return ""; + } + // + StringBuffer retValue = new StringBuffer ("SELECT ") + .append(m_columnName).append(" FROM ").append(m_tableName) + .append(" WHERE ").append(sql) + .append(" AND IsActive='Y'"); + String wc = getWhereClause(); + if (wc != null && wc.length() > 0) + retValue.append(" AND ").append(wc); + // *** + log.finest(m_columnName + " (TableDir) " + sql.toString()); + return MRole.getDefault().addAccessSQL(retValue.toString(), + m_tableName, MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); + } // getDirectAccessSQL + + + /** + * Action - Special BPartner Screen + * @param newRecord true if new record should be created + */ + private void actionBPartner (boolean newRecord) + { + VBPartner vbp = new VBPartner (Env.getFrame(this), m_lookup.getWindowNo()); + int BPartner_ID = 0; + // if update, get current value + if (!newRecord) + { + if (m_value instanceof Integer) + BPartner_ID = ((Integer)m_value).intValue(); + else if (m_value != null) + BPartner_ID = Integer.parseInt(m_value.toString()); + } + + vbp.loadBPartner (BPartner_ID); + vbp.setVisible(true); + // get result + int result = vbp.getC_BPartner_ID(); + if (result == 0 // 0 = not saved + && result == BPartner_ID) // the same + return; + // Maybe new BPartner - put in cache + m_lookup.getDirect(new Integer(result), false, true); + + actionCombo (new Integer(result)); // data binding + } // actionBPartner + + /** + * Action - Zoom + * @param selectedItem item + */ + private void actionZoom (Object selectedItem) + { + if (m_lookup == null) + return; + // + MQuery zoomQuery = m_lookup.getZoomQuery(); + Object value = getValue(); + if (value == null) + value = selectedItem; + // If not already exist or exact value + if (zoomQuery == null || value != null) + { + zoomQuery = new MQuery(); // ColumnName might be changed in MTab.validateQuery + String keyColumnName = null; + // Check if it is a Table Reference + if (m_lookup != null && m_lookup instanceof MLookup) + { + int AD_Reference_ID = ((MLookup)m_lookup).getAD_Reference_Value_ID(); + if (AD_Reference_ID != 0) + { + String query = "SELECT kc.ColumnName" + + " FROM AD_Ref_Table rt" + + " INNER JOIN AD_Column kc ON (rt.AD_Key=kc.AD_Column_ID)" + + "WHERE rt.AD_Reference_ID=?"; + + PreparedStatement pstmt = null; + try + { + pstmt = DB.prepareStatement(query, null); + pstmt.setInt(1, AD_Reference_ID); + ResultSet rs = pstmt.executeQuery(); + if (rs.next()) + { + keyColumnName = rs.getString(1); + } + rs.close(); + pstmt.close(); + pstmt = null; + } + catch (Exception e) + { + log.log(Level.SEVERE, query, e); + } + try + { + if (pstmt != null) + pstmt.close(); + pstmt = null; + } + catch (Exception e) + { + pstmt = null; + } + } // Table Reference + } // MLookup + + if(keyColumnName != null && keyColumnName.length() !=0) + zoomQuery.addRestriction(keyColumnName, MQuery.EQUAL, value); + else + zoomQuery.addRestriction(m_columnName, MQuery.EQUAL, value); + + zoomQuery.setRecordCount(1); // guess + } + int AD_Window_ID = m_lookup.getZoom(zoomQuery); + // + log.info(m_columnName + " - AD_Window_ID=" + AD_Window_ID + + " - Query=" + zoomQuery + " - Value=" + value); + // + setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); + // + AWindow frame = new AWindow(); + if (!frame.initWindow(AD_Window_ID, zoomQuery)) + { + setCursor(Cursor.getDefaultCursor()); + ValueNamePair pp = CLogger.retrieveError(); + String msg = pp==null ? "AccessTableNoView" : pp.getValue(); + ADialog.error(m_lookup.getWindowNo(), this, msg, pp==null ? "" : pp.getName()); + } + else + { + AEnv.addToWindowManager(frame); + if (Ini.isPropertyBool(Ini.P_OPEN_WINDOW_MAXIMIZED)) + { + AEnv.showMaximized(frame); + } + else + { + AEnv.showCenterScreen(frame); + } + } + // async window - not able to get feedback + frame = null; + // + setCursor(Cursor.getDefaultCursor()); + } // actionZoom + + /** + * Action - Refresh + */ + private void actionRefresh() + { + if (m_lookup == null) + return; + // + setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); + // + Object obj = m_combo.getSelectedItem(); + log.info(m_columnName + " #" + m_lookup.getSize() + ", Selected=" + obj); + //no need to refresh readonly lookup, just remove direct cache + if (!isReadWrite()) + { + m_settingValue = true; // disable actions + m_lookup.removeAllElements(); + m_lastDisplay = m_lookup.getDisplay(m_value); + m_text.setText(m_lastDisplay); + m_text.setCaretPosition(0); + m_settingValue = false; + } + else + { + m_lookup.refresh(); + m_lookup.fillComboBox(isMandatory(), true, true, false); + m_combo.setSelectedItem(obj); + //m_combo.revalidate(); + } + // + setCursor(Cursor.getDefaultCursor()); + log.info(m_columnName + " #" + m_lookup.getSize() + ", Selected=" + m_combo.getSelectedItem()); + } // actionRefresh + + + /************************************************************************** + * Focus Listener for ComboBoxes with missing Validation or invalid entries + * - Requery listener for updated list + * @param e FocusEvent + */ + public void focusGained (FocusEvent e) + { + if (m_combo == null || m_combo.getEditor() == null) + return; + if ((e.getSource() != m_combo && e.getSource() != m_combo.getEditor().getEditorComponent()) + || e.isTemporary() || m_haveFocus || m_lookup == null) + return; + + //avoid repeated query + if (m_lookup.isValidated() && m_lookup.isLoaded()) + { + m_haveFocus = true; + return; + } + // + m_haveFocus = true; // prevents calling focus gained twice + m_settingFocus = true; // prevents actionPerformed + // + Object obj = m_lookup.getSelectedItem(); + log.config(m_columnName + + " - Start Count=" + m_combo.getItemCount() + ", Selected=" + obj); + // log.fine( "VLookupHash=" + this.hashCode()); + boolean popupVisible = m_combo.isPopupVisible(); + m_lookup.fillComboBox(isMandatory(), true, true, false); // only validated & active + if (popupVisible) + { + //refresh + m_combo.hidePopup(); + m_combo.showPopup(); + } + log.config(m_columnName + + " - Update Count=" + m_combo.getItemCount() + ", Selected=" + m_lookup.getSelectedItem()); + m_lookup.setSelectedItem(obj); + log.config(m_columnName + + " - Selected Count=" + m_combo.getItemCount() + ", Selected=" + m_lookup.getSelectedItem()); + // + m_settingFocus = false; + } // focusGained + + /** + * Reset Selection List + * @param e FocusEvent + */ + public void focusLost(FocusEvent e) + { + if (e.isTemporary() + || m_lookup == null + || !m_button.isEnabled() ) // set by actionButton + return; + // Text Lost focus + if (e.getSource() == m_text) + { + String text = m_text.getText(); + log.config(m_columnName + " (Text) " + m_columnName + " = " + m_value + " - " + text); + m_haveFocus = false; + // Skip if empty + if ((m_value == null + && m_text.getText().length() == 0)) + return; + if (m_lastDisplay.equals(text)) + return; + // + actionText(); // re-display + return; + } + // Combo lost focus + if (e.getSource() != m_combo && e.getSource() != m_combo.getEditor().getEditorComponent()) + return; + if (m_lookup.isValidated() && !m_lookup.hasInactive()) + { + m_haveFocus = false; + return; + } + // + m_settingFocus = true; // prevents actionPerformed + // + log.config(m_columnName + " = " + m_combo.getSelectedItem()); + Object obj = m_combo.getSelectedItem(); + /* + // set original model + if (!m_lookup.isValidated()) + m_lookup.fillComboBox(true); // previous selection + */ + // Set value + if (obj != null) + { + m_combo.setSelectedItem(obj); + // original model may not have item + if (!m_combo.getSelectedItem().equals(obj)) + { + log.fine(m_columnName + " - added to combo - " + obj); + m_combo.addItem(obj); + m_combo.setSelectedItem(obj); + } + } + // actionCombo(getValue()); + m_settingFocus = false; + m_haveFocus = false; // can gain focus again + } // focusLost + + /** + * Set ToolTip + * @param text tool tip text + */ + public void setToolTipText(String text) + { + super.setToolTipText(text); + m_button.setToolTipText(text); + m_text.setToolTipText(text); + m_combo.setToolTipText(text); + } // setToolTipText + + /** + * Refresh Query + * @return count + */ + public int refresh() + { + if (m_lookup == null) + return -1; + + //no need to refresh readonly lookup, just remove direct cache + if (!isReadWrite()) { + m_lookup.removeAllElements(); + return 0; + } + + return m_lookup.refresh(); + } // refresh + + /** + * Use by vcelleditor to indicate editing is off and don't invoke databinding + * @param stopediting + */ + public void setStopEditing(boolean stopediting) { + m_stopediting = stopediting; + } + + +} // VLookup + +/***************************************************************************** + * Mouse Listener for Popup Menu + */ +final class VLookup_mouseAdapter extends java.awt.event.MouseAdapter +{ + /** + * Constructor + * @param adaptee adaptee + */ + VLookup_mouseAdapter(VLookup adaptee) + { + m_adaptee = adaptee; + } // VLookup_mouseAdapter + + private VLookup m_adaptee; + + /** + * Mouse Listener + * @param e MouseEvent + */ + public void mouseClicked(MouseEvent e) + { + // System.out.println("mouseClicked " + e.getID() + " " + e.getSource().getClass().toString()); + // popup menu + if (SwingUtilities.isRightMouseButton(e)) + m_adaptee.popupMenu.show((Component)e.getSource(), e.getX(), e.getY()); + // Hide the popup if not right click - teo_sarca [ 1734802 ] + else + m_adaptee.popupMenu.setVisible(false); + } // mouse Clicked + +} // VLookup_mouseAdapter \ No newline at end of file