From 2d17ff09d873ffcfc5016a352f6fc5c2baaad7a1 Mon Sep 17 00:00:00 2001 From: kthiemann Date: Mon, 7 May 2007 09:42:28 +0000 Subject: [PATCH] [ 1672362 ] Resort Lines By Pressing Shift+Up/Down --- base/src/org/compiere/model/GridTab.java | 77 +++++++++++++++ client/src/org/compiere/apps/APanel.java | 118 +++++++++++++++++++++-- 2 files changed, 186 insertions(+), 9 deletions(-) diff --git a/base/src/org/compiere/model/GridTab.java b/base/src/org/compiere/model/GridTab.java index 205d7d9d0e..bf7a5f554e 100644 --- a/base/src/org/compiere/model/GridTab.java +++ b/base/src/org/compiere/model/GridTab.java @@ -18,6 +18,7 @@ package org.compiere.model; import java.beans.*; import java.io.*; +import java.math.BigDecimal; import java.sql.*; import java.text.*; import java.util.*; @@ -2517,4 +2518,80 @@ public class GridTab implements DataStatusListener, Evaluatee, Serializable { m_mTable.setFieldVFormat(identifier, strNewFormat); } // 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 diff --git a/client/src/org/compiere/apps/APanel.java b/client/src/org/compiere/apps/APanel.java index 45c5cf328e..916e65543e 100644 --- a/client/src/org/compiere/apps/APanel.java +++ b/client/src/org/compiere/apps/APanel.java @@ -173,6 +173,7 @@ public final class APanel extends CPanel aAccount, aCalculator, aCalendar, aEditor, aPreference, aScript, aOnline, aMailSupport, aAbout, aPrintScr, aScrShot, aExit, aBPartner, aDeleteSelection; + private SwitchAction aSwitchLinesDownAction, aSwitchLinesUpAction; /************************************************************************** * Create Menu and Toolbar and registers keyboard actions. * - started from constructor @@ -636,6 +637,7 @@ public final class APanel extends CPanel log.log(Level.SEVERE, "Not Included = " + gc); } } + initSwitchLineAction(); } // normal tab if (!included) // Add to TabbedPane @@ -995,8 +997,10 @@ public final class APanel extends CPanel m_curWinTab = (JTabbedPane)tp.getSelectedComponent(); else 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(); + initSwitchLineAction(); + } // else if (m_curWinTab.getSelectedComponent() instanceof APanelTab) // isAPanelTab = true; else @@ -1069,8 +1073,10 @@ public final class APanel extends CPanel // m_curWinTab.setForegroundAt(tpIndex, AdempierePLAF.getTextColor_OK()); previousIndex = m_curTabIndex; m_curTabIndex = tpIndex; - if (!isAPanelTab) + if (!isAPanelTab) { m_curGC = gc; + initSwitchLineAction(); + } } // Sort Tab Handling @@ -1357,15 +1363,39 @@ public final class APanel extends CPanel m_curGC.getTable().removeEditor(); m_curTab.navigate(0); } - else if (cmd.equals(aPrevious.getName())) - { /*cmd_save(false);*/ + else if (cmd.equals(aSwitchLinesUpAction.getName())) + { + //up-key + shift m_curGC.getTable().removeEditor(); - m_curTab.navigateRelative(-1); - } - else if (cmd.equals(aNext.getName())) - { /*cmd_save(false); */ + 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(aPrevious.getName())) + { /* cmd_save(false); */ + //up-image + shift 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())) { /*cmd_save(false);*/ @@ -2324,4 +2354,74 @@ public final class APanel extends CPanel return s; } // 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