📄 mtab.java
字号:
/**************************************************************************
* Data Status Listener from MTable.
* - get raw info and add current row information
* - update the current row
* - redistribute (fire) Data Status event
* @param e event
*/
public void dataStatusChanged (DataStatusEvent e)
{
log.fine("#" + m_vo.TabNo + " - " + e.toString());
int oldCurrentRow = e.getCurrentRow();
m_DataStatusEvent = e; // save it
// when sorted set current row to 0
String msg = m_DataStatusEvent.getAD_Message();
if (msg != null && msg.equals("Sorted"))
setCurrentRow(0, true);
// set current row
m_DataStatusEvent.setCurrentRow(m_currentRow);
// Same row - update value
if (oldCurrentRow == m_currentRow)
{
MField field = m_mTable.getField(e.getChangedColumn());
if (field != null)
{
Object value = m_mTable.getValueAt(m_currentRow, e.getChangedColumn());
field.setValue(value, m_mTable.isInserting());
}
}
else // Redistribute Info with current row info
fireDataStatusChanged(m_DataStatusEvent);
// log.fine("dataStatusChanged #" + m_vo.TabNo + "- fini", e.toString());
} // dataStatusChanged
/**
* Inform Listeners and build WHO info
* @param e event
*/
private void fireDataStatusChanged (DataStatusEvent e)
{
DataStatusListener[] listeners = m_listenerList.getListeners(DataStatusListener.class);
if (listeners.length == 0)
return;
log.fine(e.toString());
// WHO Info
if (e.getCurrentRow() >= 0)
{
e.Created = (Timestamp)getValue("Created");
e.CreatedBy = (Integer)getValue("CreatedBy");
e.Updated = (Timestamp)getValue("Updated");
e.UpdatedBy = (Integer)getValue("UpdatedBy");
e.Record_ID = getValue(m_keyColumnName);
// Info
StringBuffer info = new StringBuffer(getTableName());
// We have a key column
if (m_keyColumnName != null && m_keyColumnName.length() > 0)
{
info.append(" - ")
.append(m_keyColumnName).append("=").append(e.Record_ID);
}
else // we have multiple parents
{
for (int i = 0; i < m_parents.size(); i++)
{
String keyCol = (String)m_parents.get(i);
info.append(" - ")
.append(keyCol).append("=").append(getValue(keyCol));
}
}
e.Info = info.toString();
}
e.setInserting(m_mTable.isInserting());
// Distribute/fire it
for (int i = 0; i < listeners.length; i++)
listeners[i].dataStatusChanged(e);
// log.fine("fini - " + e.toString());
} // fireDataStatusChanged
/**
* Create and fire Data Status Error Event
* @param AD_Message message
* @param info info
* @param isError if not true, it is a Warning
*/
protected void fireDataStatusEEvent(String AD_Message, String info, boolean isError)
{
m_mTable.fireDataStatusEEvent(AD_Message, info, isError);
} // fireDataStatusEvent
/**
* Create and fire Data Status Error Event (from Error Log)
* @param errorLog log
*/
protected void fireDataStatusEEvent (ValueNamePair errorLog)
{
if (errorLog != null)
m_mTable.fireDataStatusEEvent(errorLog);
} // fireDataStatusEvent
/**
* Get Current Row
* @return current row
*/
public int getCurrentRow()
{
if (m_currentRow != verifyRow(m_currentRow))
setCurrentRow(m_mTable.getRowCount()-1, true);
return m_currentRow;
} // getCurrentRow
/**
* Get Current Table Key ID
* @return Record_ID
*/
public int getRecord_ID()
{
return m_mTable.getKeyID(m_currentRow);
} // getRecord_ID
/**
* Get Key ID of row
* @param row row number
* @return The Key ID of the row or -1 if not found
*/
public int getKeyID (int row)
{
return m_mTable.getKeyID (row);
} // getCurrentKeyID
/**
* Navigate absolute - goto Row - (zero based).
* - does nothing, if in current row
* - saves old row if required
* @param targetRow target row
* @return current row
*/
public int navigate (int targetRow)
{
// nothing to do
if (targetRow == m_currentRow)
return m_currentRow;
log.info ("Row=" + targetRow);
// Row range check
int newRow = verifyRow(targetRow);
// Check, if we have old uncommitted data
m_mTable.dataSave(newRow, false);
// new position
return setCurrentRow(newRow, true);
} // navigate
/**
* Navigate relatively - i.e. plus/minus from current position
* @param rowChange row change
* @return current row
*/
public int navigateRelative (int rowChange)
{
return navigate (m_currentRow + rowChange);
} // navigateRelative
/**
* Navigate to current now (reload)
* @return current row
*/
public int navigateCurrent()
{
log.info("Row=" + m_currentRow);
return setCurrentRow(m_currentRow, true);
} // navigateCurrent
/**
* Row Range check
* @param targetRow target row
* @return checked row
*/
private int verifyRow (int targetRow)
{
int newRow = targetRow;
// Table Open?
if (!m_mTable.isOpen())
{
log.severe ("Table not open");
return -1;
}
// Row Count
int rows = getRowCount();
if (rows == 0)
{
log.fine("No Rows");
return -1;
}
if (newRow >= rows)
{
newRow = rows-1;
log.fine("Set to max Row: " + newRow);
}
else if (newRow < 0)
{
newRow = 0;
log.fine("Set to first Row");
}
return newRow;
} // verifyRow
/**
* Set current row and load data into fields.
* If there is no row - load nulls
* @param newCurrentRow new current row
* @param fireEvents fire events
* @return current row
*/
private int setCurrentRow (int newCurrentRow, boolean fireEvents)
{
int oldCurrentRow = m_currentRow;
m_currentRow = verifyRow (newCurrentRow);
log.fine("Row=" + m_currentRow + " - fire=" + fireEvents);
// Update Field Values
int size = m_mTable.getColumnCount();
for (int i = 0; i < size; i++)
{
MField mField = m_mTable.getField(i);
// get Value from Table
if (m_currentRow >= 0)
{
Object value = m_mTable.getValueAt(m_currentRow, i);
mField.setValue(value, m_mTable.isInserting());
if (m_mTable.isInserting()) // set invalid values to null
mField.validateValue();
}
else
{ // no rows - set to a reasonable value - not updateable
// Object value = null;
// if (mField.isKey() || mField.isParent() || mField.getColumnName().equals(m_linkColumnName))
// value = mField.getDefault();
mField.setValue();
}
}
loadDependentInfo();
if (!fireEvents) // prevents informing twice
return m_currentRow;
// inform VTable/.. -> rowChanged
m_propertyChangeSupport.firePropertyChange(PROPERTY, oldCurrentRow, m_currentRow);
// inform APanel/.. -> dataStatus with row updated
if (m_DataStatusEvent == null)
m_DataStatusEvent = new DataStatusEvent(this, getRowCount(),
m_mTable.isInserting(), // changed
Env.isAutoCommit(Env.getCtx(), m_vo.WindowNo), m_mTable.isInserting());
//
m_DataStatusEvent.setCurrentRow(m_currentRow);
String status = m_DataStatusEvent.getAD_Message();
if (status == null || status.length() == 0)
m_DataStatusEvent.setInfo("NavigateOrUpdate", null, false,false);
fireDataStatusChanged(m_DataStatusEvent);
return m_currentRow;
} // setCurrentRow
/**************************************************************************
* Get RowCount
* @return row count
*/
public int getRowCount()
{
int count = m_mTable.getRowCount();
// Wait a bit if currently loading
if (count == 0 && m_mTable.isLoading())
{
try
{
Thread.sleep(100); // .1 sec
}
catch (Exception e) {}
count = m_mTable.getRowCount();
}
return count;
} // getRowCount
/**
* Get Column/Field Count
* @return field count
*/
public int getFieldCount()
{
return m_mTable.getColumnCount();
} // getFieldCount
/**
* Get Field by index
* @param index index
* @return MField
*/
public MField getField (int index)
{
return m_mTable.getField(index);
} // getField
/**
* Get Field by DB column name
* @param columnName column name
* @return MField
*/
public MField getField (String columnName)
{
return m_mTable.getField(columnName);
} // getField
/**
* Get all Fields
* @return MFields
*/
public MField[] getFields ()
{
return m_mTable.getFields();
} // getField
/**
* Set New Value & call Callout
* @param columnName database column name
* @param value value
* @return error message or ""
*/
public String setValue (String columnName, Object value)
{
if (columnName == null)
return "NoColumn";
return setValue(m_mTable.getField(columnName), value);
} // setValue
/**
* Set New Value & call Callout
* @param field field
* @param value value
* @return error message or ""
*/
public String setValue (MField field, Object value)
{
if (field == null)
return "NoField";
log.fine(field.getColumnName() + "=" + value + " - Row=" + m_currentRow);
int col = m_mTable.findColumn(field.getColumnName());
m_mTable.setValueAt(value, m_currentRow, col, false);
//
return processFieldChange (field);
} // setValue
/**
* Is Processed
* @return true if current record is processed
*/
public boolean isProcessed()
{
int index = m_mTable.findColumn("Processed");
if (index != -1)
{
Object oo = m_mTable.getValueAt(m_currentRow, index);
if (oo instanceof String)
return "Y".equals(oo);
if (oo instanceof Boolean)
return ((Boolean)oo).booleanValue();
}
return "Y".equals(Env.getContext(m_vo.ctx, m_vo.WindowNo, "Processed"));
} // isProcessed
/**
* Process Field Change - evaluate Dependencies and process Callouts.
*
* called from MTab.setValue or GridController.dataStatusChanged
* @param changedField changed field
* @return error message or ""
*/
public String processFieldChange (MField changedField)
{
processDependencies (changedField);
return processCallout (changedField);
} // processFieldChange
/**
* Evaluate Dependencies
* @param changedField changed field
*/
private void processDependencies (MField changedField)
{
String columnName = changedField.getColumnName();
// log.trace(log.l4_Data, "Changed Column", columnName);
// when column name is not in list of DependentOn fields - fini
if (!hasDependants(columnName))
return;
// Get dependent MFields (may be because of display or dynamic lookup)
ArrayList list = getDependantList(columnName);
for (int i = 0; i < list.size(); i++)
{
MField dependentField = (MField)list.get(i);
// log.trace(log.l5_DData, "Dependent Field", dependentField==null ? "null" : dependentField.getColumnName());
// if the field has a lookup
if (dependentField != null && dependentField.getLookup() instanceof MLookup)
{
MLookup mLookup = (MLookup)dependentField.getLookup();
// log.trace(log.l6_Database, "Lookup Validation", mLookup.getValidation());
// if the lookup is dynamic (i.e. contains this columnName as variable)
if (mLookup.getValidation().indexOf("@"+columnName+"@") != -1)
{
log.fine(columnName + " changed - "
+ dependentField.getColumnName() + " set to null");
// invalidate current selection
setValue(dependentField, null);
}
}
} // for all dependent fields
} // processDependencies
/**************************************************************************
* Process Callout(s).
* <p>
* The Callout is in the string of
* "class.method;class.method;"
* If there is no class name, i.e. only a method name, the class is regarded
* as CalloutSystem.
* The class needs to comply with the Interface Callout.
*
* For a limited time, the old notation of Sx_matheod / Ux_menthod is maintained.
*
* @param field field
* @return error message or ""
* @see org.compiere.model.Callout
*/
private String processCallout (MField field)
{
String callout = field.getCallout();
if (callout.length() == 0)
return "";
//
if (isProcessed()) // only active records
return ""; // "DocProcessed";
Object value = field.getValue();
Object oldValue = field.getOldValue();
log.fine(field.getColumnName() + "=" + value
+ " (" + callout + ") - old=" + oldValue);
StringTokenizer st = new StringTokenizer(callout, ";,", false);
while (st.hasMoreTokens()) // for each callout
{
String cmd = st.nextToken().trim();
Callout call = null;
String method = null;
int methodStart = cmd.lastIndexOf(".");
try
{
if (methodStart != -1) // no class
{
Class cClass = Class.forName(cmd.substring(0,methodStart));
call = (Callout)cClass.newInstance();
method = cmd.substring(methodStart+1);
}
}
catch (Exception e)
{
log.log(Level.SEVERE, "class", e);
return "Callout Invalid: " + cmd + " (" + e.toString() + ")";
}
if (call == null || method == null || method.length() == 0)
return "Callout Invalid: " + method;
String retValue = "";
try
{
retValue = call.start(m_vo.ctx, method, m_vo.WindowNo, this, field, value, oldValue);
}
catch (Exception e)
{
log.log(Level.SEVERE, "start", e);
retValue = "Callout Invalid: " + e.toString();
return retValue;
}
if (!retValue.equals("")) // interrupt on first error
{
log.severe (retValue);
return retValue;
}
} // for each callout
return "";
} // processCallout
/**
* Get Value of Field with columnName
* @param columnName column name
* @return value
*/
public Object getValue (String columnName)
{
if (columnName == null)
return null;
MField field = m_mTable.getField(columnName);
return getValue(field);
} // getValue
/**
* Get Value of Field
* @param field field
* @return value
*/
public Object getValue (MField field)
{
if (field == null)
return null;
return field.getValue();
} // getValue
/**
* Get Value of Field in row
* @param row row
* @param columnName column name
* @return value
*/
public Object getValue (int row, String columnName)
{
int col = m_mTable.findColumn(columnName);
if (col == -1)
return null;
return m_mTable.getValueAt(row, col);
} // getValue
/**
* toString
* @return String representation
*/
public String toString()
{
String retValue = "MTab #" + m_vo.TabNo;
if (m_vo != null)
retValue += " " + m_vo.Name + " (" + m_vo.AD_Tab_ID + ")";
return retValue;
} // toString
/**************************************************************************
* @param l listener
*/
public synchronized void removePropertyChangeListener(PropertyChangeListener l)
{
m_propertyChangeSupport.removePropertyChangeListener(l);
}
/**
* @param l listener
*/
public synchronized void addPropertyChangeListener(PropertyChangeListener l)
{
m_propertyChangeSupport.addPropertyChangeListener(l);
}
/**
* @param l listener
*/
public synchronized void removeDataStatusListener(DataStatusListener l)
{
m_listenerList.remove(DataStatusListener.class, l);
}
/**
* @param l listener
*/
public synchronized void addDataStatusListener(DataStatusListener l)
{
m_listenerList.add(DataStatusListener.class, l);
}
} // MTab
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -