use of org.compiere.util.MSort in project adempiere by adempiere.
the class GridTable method dataNew.
// lobSave
/**************************************************************************
* New Record after current Row
* @param currentRow row
* @param copyCurrent copy
* @return true if success -
* Error info (Access*, AccessCannotInsert) is saved in the log
*/
public boolean dataNew(int currentRow, boolean copyCurrent) {
log.info("Current=" + currentRow + ", Copy=" + copyCurrent);
// Read only
if (m_readOnly) {
fireDataStatusEEvent("AccessCannotInsert", "", true);
return false;
}
/** @todo No TableLevel */
// || !Access.canViewInsert(m_ctx, m_WindowNo, tableLevel, true, true))
// fireDataStatusEvent(Log.retrieveError());
// see if we need to save
dataSave(-2, false);
m_inserting = true;
// Setup the buffer first so that event will be handle properly
// Create default data
int size = m_fields.size();
// "original" data
m_rowData = new Object[size];
Object[] rowData = new Object[size];
m_changed = true;
m_compareDB = true;
m_newRow = currentRow + 1;
// if there is no record, the current row could be 0 (and not -1)
if (m_sort.size() < m_newRow)
m_newRow = m_sort.size();
// add Data at end of buffer
MSort newSort = m_virtual ? new MSort(NEW_ROW_ID, null) : // index
new MSort(m_sort.size(), null);
if (m_virtual) {
m_virtualBuffer.put(NEW_ROW_ID, rowData);
} else {
m_buffer.add(rowData);
}
// add Sort pointer
m_sort.add(m_newRow, newSort);
m_rowCount++;
// fill data
if (copyCurrent) {
// Changed for is Read Only attribute
// boolean hasDocTypeTargetField = (getField("C_DocTypeTarget_ID") != null);
Object[] origData = getDataAtRow(currentRow);
for (int i = 0; i < size; i++) {
GridField field = (GridField) m_fields.get(i);
String columnName = field.getColumnName();
if (field.isVirtualColumn())
;
else if (field.isKey() || // FR [ 9223372036854775807 ]
M_Element.isReservedColumnName(columnName) || // Bug [ 1807947 ]
field.isReadOnly() || !field.IsAllowCopy()) {
rowData[i] = field.getDefault();
field.setValue(rowData[i], m_inserting);
} else
rowData[i] = origData[i];
}
} else // new
{
for (int i = 0; i < size; i++) {
GridField field = (GridField) m_fields.get(i);
rowData[i] = field.getDefault();
field.setValue(rowData[i], m_inserting);
}
}
// only changed in setValueAt
m_rowChanged = -1;
// inform
log.fine("Current=" + currentRow + ", New=" + m_newRow);
fireTableRowsInserted(m_newRow, m_newRow);
fireDataStatusIEvent(copyCurrent ? "UpdateCopied" : "Inserted", "");
log.fine("Current=" + currentRow + ", New=" + m_newRow + " - complete");
return true;
}
use of org.compiere.util.MSort in project adempiere by adempiere.
the class CTable method sort.
/**
* Sort Table
* @param modelColumnIndex model column sort index
*/
@SuppressWarnings("unchecked")
protected void sort(final int modelColumnIndex) {
int rows = getRowCount();
if (rows == 0)
return;
sorting = true;
// other column
if (modelColumnIndex != p_lastSortIndex)
p_asc = true;
else
p_asc = !p_asc;
p_lastSortIndex = modelColumnIndex;
//
log.config("#" + modelColumnIndex + " - rows=" + rows + ", asc=" + p_asc);
// Selection
Object selected = null;
int selRow = getSelectedRow();
// used to identify current row
int selCol = p_keyColumnIndex == -1 ? 0 : p_keyColumnIndex;
if (getSelectedRow() >= 0)
selected = getValueAt(selRow, selCol);
// Prepare sorting
DefaultTableModel model = (DefaultTableModel) getModel();
final MSort sort = new MSort(0, null);
sort.setSortAsc(p_asc);
/* teo_sarca: commented: [ 1585369 ] CTable sorting is TOO LAZY *
// while something to sort
sorting:
while (true)
{
// Create sortList
ArrayList<MSort> sortList = new ArrayList<MSort>(rows);
// fill with data entity
for (int i = 0; i < rows; i++)
{
Object value = model.getValueAt(i, modelColumnIndex);
sortList.add(new MSort(i, value));
}
// sort list it
Collections.sort(sortList, sort);
// move out of sequence row
for (int i = 0; i < rows; i++)
{
int index = ((MSort)sortList.get(i)).index;
if (i != index)
{
// log.config( "move " + i + " to " + index);
model.moveRow(i,i, index);
continue sorting;
}
}
// we are done
// log.config("done");
break;
} // while something to sort
*/
// teo_sarca: [ 1585369 ] CTable sorting is TOO LAZY
Collections.sort(model.getDataVector(), new Comparator<Object>() {
public int compare(Object o1, Object o2) {
Object item1 = ((Vector) o1).get(modelColumnIndex);
Object item2 = ((Vector) o2).get(modelColumnIndex);
return sort.compare(item1, item2);
}
});
// selection
clearSelection();
if (selected != null) {
for (int r = 0; r < rows; r++) {
if (selected.equals(getValueAt(r, selCol))) {
setRowSelectionInterval(r, r);
// teo_sarca: bug fix [ 1585369 ]
scrollRectToVisible(getCellRect(r, modelColumnIndex, true));
break;
}
}
}
// selected != null
sorting = false;
}
use of org.compiere.util.MSort in project adempiere by adempiere.
the class ResultTable method sort.
// autoSize
/**
* Sort Table
* @param modelColumnIndex
*/
private void sort(final int modelColumnIndex) {
int rows = getRowCount();
if (rows == 0)
return;
// other column
if (modelColumnIndex != m_lastSortIndex)
m_asc = true;
else
m_asc = !m_asc;
m_lastSortIndex = modelColumnIndex;
//
log.config("#" + modelColumnIndex + " - rows=" + rows + ", asc=" + m_asc);
ResultTableModel model = (ResultTableModel) getModel();
// Prepare sorting
final MSort sort = new MSort(0, null);
sort.setSortAsc(m_asc);
// Sort the data list - teo_sarca [ 1734327 ]
Collections.sort(model.getDataList(), new Comparator<Object>() {
public int compare(Object o1, Object o2) {
Object item1 = ((ArrayList) o1).get(modelColumnIndex);
Object item2 = ((ArrayList) o2).get(modelColumnIndex);
return sort.compare(item1, item2);
}
});
}
use of org.compiere.util.MSort in project adempiere by adempiere.
the class GridTable method sort.
// getColorCode
/**
* Sort Entries by Column.
* actually the rows are not sorted, just the access pointer ArrayList
* with the same size as m_buffer with MSort entities
* @param col col
* @param ascending ascending
*/
@SuppressWarnings("unchecked")
public void sort(int col, boolean ascending) {
log.info("#" + col + " " + ascending);
if (col < 0) {
return;
}
if (getRowCount() == 0)
return;
//cache changed row
Object[] changedRow = m_rowChanged >= 0 ? getDataAtRow(m_rowChanged) : null;
GridField field = getField(col);
// RowIDs are not sorted
if (field.getDisplayType() == DisplayType.RowID)
return;
boolean isLookup = DisplayType.isLookup(field.getDisplayType());
boolean isASI = DisplayType.PAttribute == field.getDisplayType();
// fill MSort entities with data entity
for (int i = 0; i < m_sort.size(); i++) {
MSort sort = (MSort) m_sort.get(i);
Object[] rowData = getDataAtRow(i);
if (rowData[col] == null)
sort.data = null;
else if (isLookup || isASI)
// lookup
sort.data = field.getLookup().getDisplay(rowData[col]);
else
// data
sort.data = rowData[col];
}
log.info(field.toString() + " #" + m_sort.size());
// sort it
MSort sort = new MSort(0, null);
sort.setSortAsc(ascending);
Collections.sort(m_sort, sort);
if (m_virtual) {
Object[] newRow = m_virtualBuffer.get(NEW_ROW_ID);
m_virtualBuffer.clear();
if (newRow != null && newRow.length > 0)
m_virtualBuffer.put(NEW_ROW_ID, newRow);
if (changedRow != null && changedRow.length > 0) {
if (changedRow[m_indexKeyColumn] != null && (Integer) changedRow[m_indexKeyColumn] > 0) {
m_virtualBuffer.put((Integer) changedRow[m_indexKeyColumn], changedRow);
for (int i = 0; i < m_sort.size(); i++) {
if (m_sort.get(i).index == (Integer) changedRow[m_indexKeyColumn]) {
m_rowChanged = i;
break;
}
}
}
}
//release sort memory
for (int i = 0; i < m_sort.size(); i++) {
m_sort.get(i).data = null;
}
}
// update UI
fireTableDataChanged();
// Info detected by MTab.dataStatusChanged and current row set to 0
fireDataStatusIEvent("Sorted", "#" + m_sort.size());
}
use of org.compiere.util.MSort in project adempiere by adempiere.
the class GridTable method dataIgnore.
// dataDelete
/**************************************************************************
* Ignore changes
*/
public void dataIgnore() {
if (!m_inserting && !m_changed && m_rowChanged < 0) {
log.fine("Nothing to ignore");
return;
}
log.info("Inserting=" + m_inserting);
// Inserting - delete new row
if (m_inserting) {
// Get Sort
MSort sort = (MSort) m_sort.get(m_newRow);
if (m_virtual) {
m_virtualBuffer.remove(NEW_ROW_ID);
} else {
// Delete row in Buffer and shifts all below up
m_buffer.remove(sort.index);
}
m_rowCount--;
// Delete row in Sort
// pintint to the last column, so no adjustment
m_sort.remove(m_newRow);
//
m_changed = false;
m_rowData = null;
m_rowChanged = -1;
m_inserting = false;
// inform
fireTableRowsDeleted(m_newRow, m_newRow);
} else {
// update buffer
if (m_rowData != null) {
setDataAtRow(m_rowChanged, m_rowData);
}
m_changed = false;
m_rowData = null;
m_rowChanged = -1;
m_inserting = false;
// inform
// fireTableRowsUpdated(m_rowChanged, m_rowChanged); >> messes up display?? (clearSelection)
}
m_newRow = -1;
fireDataStatusIEvent("Ignored", "");
}
Aggregations