📄 vlookup.java
字号:
} // setField
/*************************************************************************/
/**
* Action Listener - data binding
* @param e ActionEvent
*/
public void actionPerformed (ActionEvent e)
{
if (m_settingValue || m_settingFocus)
return;
Log.trace(Log.l4_Data, "VLookup.actionPerformed", e.getActionCommand() + ", ComboValue=" + m_combo.getSelectedItem());
// Log.trace(Log.l5_DData, "VLookupHash=" + this.hashCode());
// Preference
if (e.getActionCommand().equals(ValuePreference.NAME))
{
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.trace(Log.l1_User, "VLookup.actionPerformed - 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();
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.trace(Log.l6_Database, "VLookup.actionCombo", value==null ? "null" : value.toString());
try
{
fireVetoableChange (m_columnName, null, value);
}
catch (PropertyVetoException pve)
{
Log.error("VLookup.actionCombo", 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.trace(Log.l6_Database, "VLookup.actionCombo - 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
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 = "";
if (m_lookup.getZoomQuery() != null)
whereClause = m_lookup.getZoomQuery().getWhereClause();
if (whereClause.length() == 0)
whereClause = m_lookup.getValidation();
//
Log.trace(Log.l5_DData, "VLookup.actionButton - " + 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"))
{
// 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_WindowNo, "M_Warehouse_ID");
int M_PriceList_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "M_PriceList_ID");
InfoProduct ip = new InfoProduct (frame, true, m_WindowNo,
M_Warehouse_ID, M_PriceList_ID, queryValue, false, whereClause);
ip.show();
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_WindowNo, "IsSOTrx").equals("N"))
isSOTrx = false;
InfoBPartner ip = new InfoBPartner (frame, true, m_WindowNo,
queryValue, isSOTrx, false, whereClause);
ip.show();
cancelled = ip.isCancelled();
result = ip.getSelectedKey();
}
else
{
String tableName = col;
int index = m_columnName.indexOf("_ID");
if (index != -1)
tableName = m_columnName.substring(0, index);
Info ig = Info.create (frame, true, m_WindowNo, tableName, m_columnName, queryValue, false, whereClause);
ig.show();
cancelled = ig.isCancelled();
result = ig.getSelectedKey();
}
// Result
if (result != null)
{
Log.trace(Log.l4_Data, "VLookupactionButton", "Result = " + result.toString() + " " + result.getClass().getName());
// make sure that value is in cache
m_lookup.getDirect(result, true);
if (resetValue)
actionCombo (null);
actionCombo (result);
}
else if (cancelled)
{
Log.trace(Log.l4_Data, "VLookup.actionButton", "Result = null (cancelled)");
actionCombo (null);
}
else
{
Log.trace(Log.l4_Data, "VLookup.actionButton", "Result = null (not cancelled)");
setValue(m_value); // to re-display value
}
//
m_button.setEnabled(true);
} // actionButton
/**
* Check, if data returns unique entry, otherwise involve Info via Button
*/
private void actionText()
{
String text = m_text.getText();
// Nothing entered
if (m_text.getText().length() == 0)
{
actionButton(text);
return;
}
// Always like
if (!text.endsWith("%"))
text += "%";
StringBuffer SQL = new StringBuffer();
if (m_columnName.equals("M_Product_ID"))
{
SQL.append("SELECT M_Product_ID FROM M_Product WHERE UPPER(Value) LIKE '")
.append(text.toUpperCase()).append("'");
}
else if (m_columnName.equals("C_BPartner_ID"))
{
SQL.append("SELECT C_BPartner_ID FROM C_BPartner WHERE (UPPER(Value) LIKE '")
.append(text.toUpperCase())
.append("' OR UPPER(Name) LIKE '").append(text.toUpperCase()).append("')");
}
else if (m_columnName.equals("C_Order_ID"))
{
SQL.append("SELECT C_Order_ID FROM C_Order WHERE UPPER(DocumentNo) LIKE '")
.append(text.toUpperCase()).append("'");
}
else if (m_columnName.equals("C_Invoice_ID"))
{
SQL.append("SELECT C_Invoice_ID FROM C_Invoice WHERE UPPER(DocumentNo) LIKE '")
.append(text.toUpperCase()).append("'");
}
else if (m_columnName.equals("M_InOut_ID"))
{
SQL.append("SELECT M_InOut_ID FROM M_InOut WHERE UPPER(DocumentNo) LIKE '")
.append(text.toUpperCase()).append("'");
}
else if (m_columnName.equals("C_Payment_ID"))
{
SQL.append("SELECT C_Payment_ID FROM C_Payment WHERE UPPER(DocumentNo) LIKE '")
.append(text.toUpperCase()).append("'");
}
else
{
actionButton(text);
return;
}
// Finish SQL
SQL.append(" AND IsActive='Y'");
// AddSecurity
String tableName = m_columnName.substring(0, m_columnName.length()-3);
String finalSQL = Access.addROAccessSQL(Env.getCtx(), SQL.toString(), tableName, false);
int id = 0;
try
{
PreparedStatement pstmt = DB.prepareStatement(finalSQL);
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 (SQLException e)
{
Log.error("VLookup.actionText\nSQL=" + finalSQL, e);
id = -2;
}
Log.trace(Log.l5_DData, "VLookup.actionText ID => " + id);
// No (unique) result
if (id <= 0)
{
Log.trace(Log.l6_Database, finalSQL);
actionButton(text);
return;
}
m_value = null; // forces re-display if value is unchanged but text updated and still unique
actionCombo (new Integer(id)); // data binding
} // actionText
/**
* 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_WindowNo);
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.show();
// get result
int result = vbp.getBPartner_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), true);
actionCombo (new Integer(result)); // data binding
} // actionBPartner
/**
* Action - Zoom
*/
private void actionZoom()
{
if (m_lookup == null)
return;
//
int AD_Window_ID = m_lookup.getZoom();
MQuery zoomQuery = m_lookup.getZoomQuery();
String IsSOTrx = Env.getContext(Env.getCtx(), m_WindowNo, "IsSOTrx");
Log.trace(Log.l1_User, "VLookup.actionZoom - " + AD_Window_ID + " - Query=" + zoomQuery + " - Value=" + getValue());
// If not already exist
if (zoomQuery == null || (!zoomQuery.isActive() && getValue() != null))
{
zoomQuery = new MQuery();
zoomQuery.addRestriction(m_columnName, MQuery.EQUAL, getValue());
}
//
setCursor(Cursor.getDefaultCursor());
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
//
AWindow frame = new AWindow();
if (!frame.initWindow(AD_Window_ID, zoomQuery, IsSOTrx.equals("Y")))
{
setCursor(Cursor.getDefaultCursor());
ValueNamePair pp = Log.retrieveError();
String msg = pp==null ? "AccessTableNoView" : pp.getValue();
ADialog.error(m_WindowNo, this, msg, pp==null ? "" : pp.getName());
}
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.trace(Log.l1_User, "VLookup.actionRefresh - #" + m_lookup.getSize(), "Selected=" + obj);
m_lookup.refresh();
if (m_lookup.isValidated())
m_lookup.fillComboBox(isMandatory(), false, false, false);
else
m_lookup.fillComboBox(isMandatory(), true, false, false);
m_combo.setSelectedItem(obj);
// m_combo.revalidate();
//
setCursor(Cursor.getDefaultCursor());
Log.trace(Log.l1_User, "VLookup.actionRefresh - #" + 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 (e.getSource() != m_combo || e.isTemporary() || m_haveFocus)
return;
//
m_haveFocus = true; // prevents calling focus gained twice
m_settingFocus = true; // prevents actionPerformed
//
Object obj = m_lookup.getSelectedItem();
Log.trace(Log.l4_Data, "VLookup.focusGained", m_columnName + " Start Count=" + m_combo.getItemCount() + ", Selected=" + obj);
// Log.trace(Log.l5_DData, "VLookupHash=" + this.hashCode());
m_lookup.fillComboBox(isMandatory(), true, true, true); // only validated & active & temporary
// Log.trace(Log.l4_Data, "VLookup.focusGained", m_columnName + " Update Count=" + m_combo.getItemCount() + ", Selected=" + m_lookup.getSelectedItem());
m_lookup.setSelectedItem(obj);
Log.trace(Log.l4_Data, "VLookup.focusGained", 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.getSource() != m_combo || e.isTemporary())
return;
//
m_settingFocus = true; // prevents actionPerformed
//
Log.trace(Log.l4_Data, "VLookup.focusLost", 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.trace(Log.l6_Database, "VLookup.focusLost " + 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
} // VLookup
/*****************************************************************************/
/**
* Mouse Listener for Popup Menu
*/
final class VLookup_mouseAdapter extends java.awt.event.MouseAdapter
{
/**
* Constructor
* @param adaptee adaptee
*/
VLookup_mouseAdapter(VLookup adaptee)
{
this.adaptee = adaptee;
} // VLookup_mouseAdapter
private VLookup 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))
adaptee.popupMenu.show((Component)e.getSource(), e.getX(), e.getY());
} // mouse Clicked
} // VLookup_mouseAdapter
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -