[Schmitzm-commits] r2217 - trunk/schmitzm-core/src/main/java/de/schmitzm/swing

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Sun Feb 3 15:22:35 CET 2013


Author: mojays
Date: 2013-02-03 15:22:35 +0100 (Sun, 03 Feb 2013)
New Revision: 2217

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainanceDialog.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java
Log:
ListMaintainancePanel: edit some new methods allow sub classes to override

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainanceDialog.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainanceDialog.java	2013-02-01 13:33:17 UTC (rev 2216)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainanceDialog.java	2013-02-03 14:22:35 UTC (rev 2217)
@@ -78,8 +78,8 @@
 
     this.listPanel = new ListMaintainancePanel<T>(orderable) {
       @Override
-      protected T createNewElement() {
-        return ListMaintainanceDialog.this.createNewElement();
+      protected T[] createNewElements() {
+        return ListMaintainanceDialog.this.createNewElements();
       }
 
       @Override
@@ -94,9 +94,9 @@
   }
   
   /**
-   * Creates a new element. Called by {@link ListMaintainancePanel#actionPerformed(ActionEvent)}.
+   * Creates new elements. Called by {@link ListMaintainancePanel#actionPerformed(ActionEvent)}.
    */
-  protected abstract T createNewElement();
+  protected abstract T[] createNewElements();
     
   /**
    * Edits an element. Called by {@link ListMaintainancePanel#actionPerformed(ActionEvent)}.

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java	2013-02-01 13:33:17 UTC (rev 2216)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java	2013-02-03 14:22:35 UTC (rev 2217)
@@ -81,7 +81,7 @@
           return;
         if ( e.getButton() == MouseEvent.BUTTON1 &&
              e.getClickCount() == 2 )
-          performEditAction();
+          performEditElementAction();
       }
     });
 
@@ -222,15 +222,6 @@
     ((ListModel<T>)list.getModel()).removeElement(element);
   }
   
-  /**
-   * Creates a new element. Called by {@link #actionPerformed(ActionEvent)}.
-   */
-  protected abstract T createNewElement();
-  
-  /**
-   * Edits an element. Called by {@link #actionPerformed(ActionEvent)}.
-   */
-  protected abstract T editElement(T element);
 
   /**
    * Performs the button actions.
@@ -239,55 +230,100 @@
   public void actionPerformed(ActionEvent e) {
     String actionID = e.getActionCommand();
     
-    if ( "ADD".equals(actionID) ) {
-      T elem = createNewElement();
-      if ( elem != null )
-        addElement(elem);
+    if ( "ADD".equals(actionID) )
+      performAddElementAction();
+    if ( "EDIT".equals(actionID) )
+      performEditElementAction();
+    if ( "REMOVE".equals(actionID) )
+      performRemoveElementsAction();
+    if ( "MOVE_UP".equals(actionID) )
+      performMoveUpAction();
+    if ( "MOVE_DOWN".equals(actionID) )
+      performMoveDownAction();
+  }  
+
+  /**
+   * Returns the selected list elements as {@link List}.
+   */
+  public List<T> getSelectedElements() {
+//    List<T> selectedValues = list.getSelectedValuesList(); // TODO: Java 1.7
+    List<T> selectedValues = Arrays.asList((T[])list.getSelectedValues());
+    return selectedValues;
+  }
+
+  /**
+   * Returns the one selected element from list.
+   */
+  public T getSelectedElement() {
+//    T elem = list.getSelectedValue(); // TODO: Java 1.7
+    T elem = (T)list.getSelectedValue();
+    return elem;
+  }
+  
+  /**
+   * Performs the action when the MOVE DOWN button is clicked.
+   */
+  protected void performMoveUpAction() {
+    List<T> selectedValues = getSelectedElements();
+    int[] selectedIdx = list.getSelectedIndices();
+    int firstIdx = selectedIdx[0];
+    int lastIdx  = selectedIdx[selectedIdx.length-1];
+    if ( firstIdx > 0 ) {
+      ((ListModel<T>)list.getModel()).removeRange(firstIdx, lastIdx);
+      ((ListModel<T>)list.getModel()).addElements(firstIdx-1, selectedValues);
+      list.addSelectionInterval(firstIdx-1, lastIdx-1);
     }
-    if ( "EDIT".equals(actionID) ) {
-      performEditAction();
+  }
+  
+  /**
+   * Performs the action when the MOVE DOWN button is clicked.
+   */
+  protected void performMoveDownAction() {
+    List<T> selectedValues = getSelectedElements();
+    int[] selectedIdx = list.getSelectedIndices();
+    int firstIdx = selectedIdx[0];
+    int lastIdx  = selectedIdx[selectedIdx.length-1];
+    if ( lastIdx < ((ListModel<T>)list.getModel()).getSize()-1 ) {
+      ((ListModel<T>)list.getModel()).removeRange(firstIdx, lastIdx);
+      ((ListModel<T>)list.getModel()).addElements(firstIdx+1, selectedValues);
+      list.addSelectionInterval(firstIdx+1, lastIdx+1);
     }
-    if ( "REMOVE".equals(actionID) ) {
-//      for ( T elem : list.getSelectedValuesList() ) // TODO: Java 1.7
-      for ( Object elem : list.getSelectedValues() )
-        removeElement((T)elem);
-    }
-    if ( "MOVE_UP".equals(actionID) ) {
-//      List<T> selectedValues = list.getSelectedValuesList(); // TODO: Java 1.7
-      List<T> selectedValues = Arrays.asList((T[])list.getSelectedValues());
-      int[] selectedIdx = list.getSelectedIndices();
-      int firstIdx = selectedIdx[0];
-      int lastIdx  = selectedIdx[selectedIdx.length-1];
-      if ( firstIdx > 0 ) {
-        ((ListModel<T>)list.getModel()).removeRange(firstIdx, lastIdx);
-        ((ListModel<T>)list.getModel()).addElements(firstIdx-1, selectedValues);
-        list.addSelectionInterval(firstIdx-1, lastIdx-1);
-      }
-    }
-    if ( "MOVE_DOWN".equals(actionID) ) {
-//      List<T> selectedValues = list.getSelectedValuesList(); // TODO: Java 1.7
-      List<T> selectedValues = Arrays.asList((T[])list.getSelectedValues());
-      int[] selectedIdx = list.getSelectedIndices();
-      int firstIdx = selectedIdx[0];
-      int lastIdx  = selectedIdx[selectedIdx.length-1];
-      if ( lastIdx < ((ListModel<T>)list.getModel()).getSize()-1 ) {
-        ((ListModel<T>)list.getModel()).removeRange(firstIdx, lastIdx);
-        ((ListModel<T>)list.getModel()).addElements(firstIdx+1, selectedValues);
-        list.addSelectionInterval(firstIdx+1, lastIdx+1);
-      }
-    }
-    
-  }  
+  }
   
   /**
+   * Performs the action when the ADD button is clicked. This default implementation
+   * calls {@link #createNewElements()} and simply adds the returned element(s) to the list
+   * by successively calling {@link #addElement(Object)}
+   */
+  protected void performAddElementAction() {
+    T[] elems = createNewElements();
+    if ( elems != null )
+      for (T e : elems)
+        addElement(e);  
+  }
+  
+  /**
+   * Creates one or more new elements. Called by {@link #performAddElementAction()}.
+   */
+  protected abstract T[] createNewElements();
+
+  /**
+   * Performs the action when the REMOVE button is clicked. This default implementation
+   * simply removed all selected elements from the list by calling {@link #removeElement(Object)}
+   */
+  protected void performRemoveElementsAction() {
+    for ( Object elem : getSelectedElements() )
+      removeElement((T)elem);
+  }
+
+  /**
    * Invokes the edit action for the selected list item. Does nothing if
    * edit action is not enabled or no list item is selected. 
    */
-  protected void performEditAction() {
+  protected void performEditElementAction() {
     if ( editAction == null )
       return;
-//    T elem = list.getSelectedValue(); // TODO: Java 1.7
-    T elem = (T)list.getSelectedValue();
+    T elem = getSelectedElement();
     if ( elem == null )
       return;
     // Edit action:
@@ -298,8 +334,13 @@
     if ( elem != null )
       ((ListModel<T>)getList().getModel()).setElementAt(elem, elemIdx);
   }
-  
+
   /**
+   * Edits an element. Called by {@link #performEditElementAction()}.
+   */
+  protected abstract T editElement(T element);
+
+  /**
    * Data model for the {@link ListMaintainancePanel}.
    * @author Martin O.J. Schmitz
    */



More information about the Schmitzm-commits mailing list