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

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Sun Feb 3 18:42:43 CET 2013


Author: mojays
Date: 2013-02-03 18:42:43 +0100 (Sun, 03 Feb 2013)
New Revision: 2218

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
Log:
ListMaintainacePanel: fix bug in button enabling/disabling; class not abstract anymore
SwingUtil: avoid unnecessary recursions in setEnabled(.); enable/disable Buttons via corresponding Action (if possible) to avoid inconsistencies between Button enables state and Action enabled state

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-03 14:22:35 UTC (rev 2217)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java	2013-02-03 17:42:43 UTC (rev 2218)
@@ -28,7 +28,7 @@
  * Panel showing a {@link JList} which allows to modify the list.
  * @author Martin O.J. Schmitz
  */
-public abstract class ListMaintainancePanel<T> extends JPanel implements ActionListener, ListSelectionListener {
+public class ListMaintainancePanel<T> extends JPanel implements ActionListener, ListSelectionListener {
 //  protected JList<T> list; // TODO: Java 1.7
   protected JList list;
 
@@ -136,6 +136,13 @@
   }
   
   /**
+   * Returns the list model.
+   */
+  public ListModel<T> getModel() {
+    return (ListModel<T>)list.getModel();
+  }
+  
+  /**
    * Returns the data. Always returns a new {@link List} instance. Changes
    * on this object does not effect the GUI!
    */
@@ -163,16 +170,18 @@
   /**
    * Updates the enabled state of actions according to list selections.
    */
-  protected void updateActions() {
+  public void updateActions() {
     boolean listSelected = list.getSelectedIndices().length > 0;
+    boolean panelEnabled = isEnabled();
+
     if ( editAction != null )
-      editAction.setEnabled( listSelected );
+      editAction.setEnabled( panelEnabled && listSelected );
     if ( removeAction != null )
-      removeAction.setEnabled( listSelected );
+      removeAction.setEnabled( panelEnabled && listSelected );
     if ( moveUpAction != null )
-      moveUpAction.setEnabled( listSelected );
+      moveUpAction.setEnabled( panelEnabled && listSelected );
     if ( moveDownAction != null )
-      moveDownAction.setEnabled( listSelected );
+      moveDownAction.setEnabled( panelEnabled && listSelected );
   }
   
   /**
@@ -191,6 +200,16 @@
   }
 
   /**
+   * Besides calling the super-method, {@link #updateActions()} is called to
+   * de/activates buttons according to selection state. 
+   */
+  @Override
+  public void setEnabled(boolean enabled) {
+    super.setEnabled(enabled);
+    updateActions();
+  }
+
+  /**
    * Removes all list entries from list. 
    */
   public void clearList() {
@@ -297,15 +316,27 @@
    */
   protected void performAddElementAction() {
     T[] elems = createNewElements();
-    if ( elems != null )
-      for (T e : elems)
-        addElement(e);  
+    if ( elems != null ) {
+      int insertIdx = list.getSelectedIndex();
+      if ( insertIdx < 0 )
+        insertIdx = list.getModel().getSize();
+      for (int i=elems.length-1; i>=0; i--) {
+        addElement(insertIdx,elems[i]);  
+      }
+    }
   }
   
   /**
    * Creates one or more new elements. Called by {@link #performAddElementAction()}.
+   * This default implementation does nothing! Sub-classes must override this method
+   * if the {@link #addAction} is activated! To ensure that, an {@link UnsupportedOperationException}
+   * is thrown if {@link #addAction} is not deactivated!  
    */
-  protected abstract T[] createNewElements();
+  protected T[] createNewElements() {
+    if ( addAction != null )
+      throw new UnsupportedOperationException("addAction is activated so createNewElements() must be overridden!");
+    return null;
+  }
 
   /**
    * Performs the action when the REMOVE button is clicked. This default implementation
@@ -337,14 +368,21 @@
 
   /**
    * Edits an element. Called by {@link #performEditElementAction()}.
+   * This default implementation does nothing! Sub-classes must override this method
+   * if the {@link #editAction} is activated! To ensure that, an {@link UnsupportedOperationException}
+   * is thrown if {@link #editAction} is not deactivated!  
    */
-  protected abstract T editElement(T element);
+  protected T editElement(T element) {
+    if ( editAction != null )
+      throw new UnsupportedOperationException("editAction is activated so editElement(.) must be overridden!");
+    return null;    
+  }
 
   /**
    * Data model for the {@link ListMaintainancePanel}.
    * @author Martin O.J. Schmitz
    */
-  protected class ListModel<T> extends ReferencedListModel<T> {
+  public class ListModel<T> extends ReferencedListModel<T> {
     /**
      * Creates empty list model.
      */

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2013-02-03 14:22:35 UTC (rev 2217)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2013-02-03 17:42:43 UTC (rev 2218)
@@ -85,6 +85,7 @@
 import javax.swing.Icon;
 import javax.swing.ImageIcon;
 import javax.swing.JApplet;
+import javax.swing.JButton;
 import javax.swing.JCheckBox;
 import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JComboBox;
@@ -1102,8 +1103,21 @@
      */
     public static void setEnabled(Component component, boolean enabled) {
       // avoid recursion for de.schmitzm.swing.JPanel
-      if ( component.isEnabled() != enabled )
-        component.setEnabled(enabled);
+      if ( component.isEnabled() != enabled ) {
+        if ( component instanceof AbstractButton && ((AbstractButton)component).getAction() != null )
+          // If possible, enable/disable Buttons by Action
+          // 1. advantage: enabled/disabled inconsistancies between button and action are avoided! 
+          // 2. advantage: possible MenuItems are automatically enabled/disabled too
+          ((AbstractButton)component).getAction().setEnabled(enabled);
+        else {
+          component.setEnabled(enabled);
+          // if component is a de.schmitzm.swing.JPanel the recursion
+          // was already done by component.setEnabled(enabled)
+          // --> terminate method
+          if ( component instanceof de.schmitzm.swing.JPanel )
+            return;
+        }
+      }
       
       // recursive components
       if ( component instanceof javax.swing.JPanel )



More information about the Schmitzm-commits mailing list