[ 1672362 ] Resort Lines By Pressing Shift+Up/Down

This commit is contained in:
kthiemann 2007-05-07 09:42:28 +00:00
parent cfaa8b1e45
commit 2d17ff09d8
2 changed files with 186 additions and 9 deletions

View File

@ -18,6 +18,7 @@ package org.compiere.model;
import java.beans.*; import java.beans.*;
import java.io.*; import java.io.*;
import java.math.BigDecimal;
import java.sql.*; import java.sql.*;
import java.text.*; import java.text.*;
import java.util.*; import java.util.*;
@ -2517,4 +2518,80 @@ public class GridTab implements DataStatusListener, Evaluatee, Serializable
{ {
m_mTable.setFieldVFormat(identifier, strNewFormat); m_mTable.setFieldVFormat(identifier, strNewFormat);
} // setFieldVFormat } // setFieldVFormat
/**
* Switches the line/seqNo of the two rows
* @param from row index
* @param to row index
* @param sortColumn column index of sort column
* @param ascending sorting modus
*/
public void switchRows(int from, int to, int sortColumn, boolean ascending) {
log.fine(from + " - " + to + " - " + sortColumn + " - " + ascending);
// nothing to do
if (from == to) {
log.finest("nothing to do - from == to");
return;
}
//check if lines are editable
if(!(m_mTable.isRowEditable(from)&& m_mTable.isRowEditable(to))){
log.finest("row not editable - return");
return;
}
// Row range check
to = verifyRow(to);
if (to == -1) {
log.finest("Row range check - return");
return;
}
// Check, if we have old uncommitted data
m_mTable.dataSave(to, false);
//find the line column
int lineCol = m_mTable.findColumn("Line");
if (lineCol == -1) {
lineCol = m_mTable.findColumn("SeqNo");
}
if(lineCol == -1){
//no Line, no SeqNo
return;
}
//get the line/seq numbers
Integer lineNoCurrentRow = null;
Integer lineNoNextRow = null;
if (m_mTable.getValueAt(from, lineCol) instanceof Integer) {
lineNoCurrentRow = (Integer) m_mTable.getValueAt(from, lineCol);
lineNoNextRow = (Integer) m_mTable.getValueAt(to, lineCol);
} else if (m_mTable.getValueAt(from, lineCol) instanceof BigDecimal) {
lineNoCurrentRow = new Integer(((BigDecimal) m_mTable.getValueAt(from, lineCol))
.intValue());
lineNoNextRow = new Integer(((BigDecimal) m_mTable.getValueAt(to, lineCol))
.intValue());
} else {
log.fine("unknown value format - return");
return;
}
//don't sort special lines like taxes
if (lineNoCurrentRow >= 9900
|| lineNoNextRow >= 9900) {
log.fine("don't sort - might be special lines");
return;
}
// switch the line numbers and save new values
m_mTable.setValueAt(lineNoNextRow, from, lineCol);
setCurrentRow(to, false);
m_mTable.dataSave(true);
m_mTable.setValueAt(lineNoCurrentRow, to, lineCol);
setCurrentRow(from, false);
m_mTable.dataSave(true);
//resort
if(sortColumn != -1) {
m_mTable.sort(sortColumn, ascending);
} else {
m_mTable.sort(lineCol, true);
}
navigate(to);
}
} // MTab } // MTab

View File

@ -173,6 +173,7 @@ public final class APanel extends CPanel
aAccount, aCalculator, aCalendar, aEditor, aPreference, aScript, aAccount, aCalculator, aCalendar, aEditor, aPreference, aScript,
aOnline, aMailSupport, aAbout, aPrintScr, aScrShot, aExit, aBPartner, aDeleteSelection; aOnline, aMailSupport, aAbout, aPrintScr, aScrShot, aExit, aBPartner, aDeleteSelection;
private SwitchAction aSwitchLinesDownAction, aSwitchLinesUpAction;
/************************************************************************** /**************************************************************************
* Create Menu and Toolbar and registers keyboard actions. * Create Menu and Toolbar and registers keyboard actions.
* - started from constructor * - started from constructor
@ -636,6 +637,7 @@ public final class APanel extends CPanel
log.log(Level.SEVERE, "Not Included = " + gc); log.log(Level.SEVERE, "Not Included = " + gc);
} }
} }
initSwitchLineAction();
} // normal tab } // normal tab
if (!included) // Add to TabbedPane if (!included) // Add to TabbedPane
@ -995,8 +997,10 @@ public final class APanel extends CPanel
m_curWinTab = (JTabbedPane)tp.getSelectedComponent(); m_curWinTab = (JTabbedPane)tp.getSelectedComponent();
else else
throw new java.lang.IllegalArgumentException("Window does not contain Tabs"); throw new java.lang.IllegalArgumentException("Window does not contain Tabs");
if (m_curWinTab.getSelectedComponent() instanceof GridController) if (m_curWinTab.getSelectedComponent() instanceof GridController) {
m_curGC = (GridController)m_curWinTab.getSelectedComponent(); m_curGC = (GridController)m_curWinTab.getSelectedComponent();
initSwitchLineAction();
}
// else if (m_curWinTab.getSelectedComponent() instanceof APanelTab) // else if (m_curWinTab.getSelectedComponent() instanceof APanelTab)
// isAPanelTab = true; // isAPanelTab = true;
else else
@ -1069,8 +1073,10 @@ public final class APanel extends CPanel
// m_curWinTab.setForegroundAt(tpIndex, AdempierePLAF.getTextColor_OK()); // m_curWinTab.setForegroundAt(tpIndex, AdempierePLAF.getTextColor_OK());
previousIndex = m_curTabIndex; previousIndex = m_curTabIndex;
m_curTabIndex = tpIndex; m_curTabIndex = tpIndex;
if (!isAPanelTab) if (!isAPanelTab) {
m_curGC = gc; m_curGC = gc;
initSwitchLineAction();
}
} }
// Sort Tab Handling // Sort Tab Handling
@ -1357,15 +1363,39 @@ public final class APanel extends CPanel
m_curGC.getTable().removeEditor(); m_curGC.getTable().removeEditor();
m_curTab.navigate(0); m_curTab.navigate(0);
} }
else if (cmd.equals(aPrevious.getName())) else if (cmd.equals(aSwitchLinesUpAction.getName()))
{ /*cmd_save(false);*/ {
//up-key + shift
m_curGC.getTable().removeEditor(); m_curGC.getTable().removeEditor();
m_curTab.navigateRelative(-1); m_curTab.switchRows(m_curTab.getCurrentRow(), m_curTab.getCurrentRow() - 1, m_curGC.getTable().getSortColumn(), m_curGC.getTable().isSortAscending());
} m_curGC.getTable().requestFocus();
else if (cmd.equals(aNext.getName())) }
{ /*cmd_save(false); */ else if (cmd.equals(aPrevious.getName()))
{ /* cmd_save(false); */
//up-image + shift
m_curGC.getTable().removeEditor(); m_curGC.getTable().removeEditor();
m_curTab.navigateRelative(+1); if ((e.getModifiers() & ActionEvent.SHIFT_MASK) != 0) {
m_curTab.switchRows(m_curTab.getCurrentRow(), m_curTab.getCurrentRow() - 1, m_curGC.getTable().getSortColumn(), m_curGC.getTable().isSortAscending());
} else {
m_curTab.navigateRelative(-1);
}
}
else if (cmd.equals(aSwitchLinesDownAction.getName()))
{
//down-key + shift
m_curGC.getTable().removeEditor();
m_curTab.switchRows(m_curTab.getCurrentRow(), m_curTab.getCurrentRow() + 1, m_curGC.getTable().getSortColumn(), m_curGC.getTable().isSortAscending());
m_curGC.getTable().requestFocus();
}
else if (cmd.equals(aNext.getName()))
{ /* cmd_save(false); */
//down-image + shift
m_curGC.getTable().removeEditor();
if ((e.getModifiers() & ActionEvent.SHIFT_MASK) != 0) {
m_curTab.switchRows(m_curTab.getCurrentRow(), m_curTab.getCurrentRow() + 1, m_curGC.getTable().getSortColumn(), m_curGC.getTable().isSortAscending());
} else {
m_curTab.navigateRelative(+1);
}
} }
else if (cmd.equals(aLast.getName())) else if (cmd.equals(aLast.getName()))
{ /*cmd_save(false);*/ { /*cmd_save(false);*/
@ -2324,4 +2354,74 @@ public final class APanel extends CPanel
return s; return s;
} // toString } // toString
/**
* Simple action class for the resort of tablelines (switch line no). Delegates actionPerformed
* to APanel.
*
* @author Karsten Thiemann, kthiemann@adempiere.org
*
*/
class SwitchAction extends AbstractAction {
/** the action listener - APanel */
private ActionListener al;
/** action name */
private String name;
/**
* Constructor.
* @param name
* @param accelerator
* @param al
*/
SwitchAction(String name, KeyStroke accelerator, ActionListener al) {
super(name);
putValue(Action.NAME, name); // Display
putValue(Action.SHORT_DESCRIPTION, name); // Tooltip
putValue(Action.ACCELERATOR_KEY, accelerator); // KeyStroke
putValue(Action.ACTION_COMMAND_KEY, name); // ActionCammand
this.al = al;
this.name = name;
}
public void actionPerformed(ActionEvent e) {
al.actionPerformed(e);
} // actionPerformed
public String getName() {
return name;
}
}
/**
* Removes the default KeyStroke action for the up/down keys and adds switch
* line actions.
*/
private void initSwitchLineAction() {
aSwitchLinesDownAction = new SwitchAction("switchLinesDown", KeyStroke.getKeyStroke(
KeyEvent.VK_DOWN, Event.SHIFT_MASK), this);
aSwitchLinesUpAction = new SwitchAction("switchLinesUp", KeyStroke.getKeyStroke(
KeyEvent.VK_UP, Event.SHIFT_MASK), this);
JTable table = m_curGC.getTable();
table.getInputMap(CPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, Event.SHIFT_MASK), "none");
table.getInputMap(CPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
KeyStroke.getKeyStroke(KeyEvent.VK_UP, Event.SHIFT_MASK), "none");
table.getInputMap(CPanel.WHEN_FOCUSED).put(
KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, Event.SHIFT_MASK), "none");
table.getInputMap(CPanel.WHEN_FOCUSED).put(
KeyStroke.getKeyStroke(KeyEvent.VK_UP, Event.SHIFT_MASK), "none");
getInputMap(CPanel.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, Event.SHIFT_MASK),
aSwitchLinesDownAction.getName());
getActionMap().put(aSwitchLinesDownAction.getName(), aSwitchLinesDownAction);
getInputMap(CPanel.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_UP, Event.SHIFT_MASK),
aSwitchLinesUpAction.getName());
getActionMap().put(aSwitchLinesUpAction.getName(), aSwitchLinesUpAction);
}
} // APanel } // APanel