📄 vlookup.java
字号:
// 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) // <key>
{
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)
{
// log.fine( "VLookup.propertyChange", evt);
if (evt.getPropertyName().equals(MField.PROPERTY))
{
m_inserting = MField.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 (MField 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)
return;
log.config(m_columnName + " - " + e.getActionCommand() + ", ComboValue=" + m_combo.getSelectedItem());
// log.fine( "VLookupHash=" + 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.
* <br>
* == dataBinding == inform of new value
* <pre>
* VLookup.actionCombo
* GridController.vetoableChange
* MTable.setValueAt
* MField.setValue
* VLookup.setValue
* MTab.dataStatusChanged
* </pre>
* @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);
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
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");
// 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 ...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -