changeset 736:fb3632bc0f7f

More cleanup on HeaderPanel review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-October/003922.html reviewed-by: omajid
author Mario Torre <neugens.limasoftware@gmail.com>
date Thu, 25 Oct 2012 19:43:33 +0200
parents d07e5cc900a3
children 8beb773bfe8b
files client/swing/src/main/java/com/redhat/thermostat/client/swing/components/ActionButton.java client/swing/src/main/java/com/redhat/thermostat/client/swing/components/ActionToggleButton.java client/swing/src/main/java/com/redhat/thermostat/client/swing/components/HeaderPanel.java client/swing/src/main/java/com/redhat/thermostat/client/swing/components/ToolbarButton.java
diffstat 4 files changed, 63 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/ActionButton.java	Thu Oct 25 12:48:30 2012 -0400
+++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/ActionButton.java	Thu Oct 25 19:43:33 2012 +0200
@@ -49,7 +49,10 @@
 
 @SuppressWarnings("serial")
 public class ActionButton extends JButton implements ToolbarButton {
-        
+    
+    private String lastText;
+    private boolean showText;
+    
     public ActionButton(final Icon icon) {
         this(icon, "");
     }
@@ -57,6 +60,7 @@
     public ActionButton(final Icon icon, String text) {
         super(icon);
                 
+        showText = true;
         setText(text);
         
         setUI(new ActionButtonUI());
@@ -71,11 +75,25 @@
     }
     
     @Override
-    public ToolbarButton copy() {
-        ActionButton copy = new ActionButton(getIcon(), getText());
-        copy.setName(getName());
-        copy.setToolTipText(getToolTipText());
-        return copy;
+    public void setText(String text) {
+        lastText = text;
+        if (showText) {
+            super.setText(text);
+        }
+    }
+    
+    private void setText_noClient(String text) {
+        super.setText(text);
+    }
+    
+    @Override
+    public void toggleText(boolean showText) {
+        this.showText = showText;
+        if (showText) {
+            setText_noClient(lastText);
+        } else {
+            setText_noClient("");
+        }
     }
     
     public static void main(String[] args) throws InvocationTargetException, InterruptedException {
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/ActionToggleButton.java	Thu Oct 25 12:48:30 2012 -0400
+++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/ActionToggleButton.java	Thu Oct 25 19:43:33 2012 +0200
@@ -50,6 +50,8 @@
 @SuppressWarnings("serial")
 public class ActionToggleButton extends JToggleButton implements ToolbarButton {
         
+    private String lastText;
+    private boolean showText;
     public ActionToggleButton(final Icon icon) {
         this(icon, "");
     }
@@ -57,6 +59,7 @@
     public ActionToggleButton(final Icon icon, String text) {
         super(icon);
                 
+        showText = true;
         setText(text);
         
         setUI(new ActionButtonUI());
@@ -71,12 +74,25 @@
     }
     
     @Override
-    public ToolbarButton copy() {
-        ActionToggleButton copy = new ActionToggleButton(getIcon(), getText());
-        copy.setSelected(isSelected());
-        copy.setName(getName());
-        copy.setToolTipText(getToolTipText());
-        return copy;
+    public void setText(String text) {
+        lastText = text;
+        if (showText) {
+            super.setText(text);
+        }
+    }
+    
+    private void setText_noClient(String text) {
+        super.setText(text);
+    }
+    
+    @Override
+    public void toggleText(boolean showText) {
+        this.showText = showText;
+        if (showText) {
+            setText_noClient(lastText);
+        } else {
+            setText_noClient("");
+        }
     }
     
     public static void main(String[] args) throws InvocationTargetException, InterruptedException {
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/HeaderPanel.java	Thu Oct 25 12:48:30 2012 -0400
+++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/HeaderPanel.java	Thu Oct 25 19:43:33 2012 +0200
@@ -47,8 +47,6 @@
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.lang.reflect.InvocationTargetException;
-import java.util.ArrayList;
-import java.util.List;
 import java.util.prefs.PreferenceChangeEvent;
 import java.util.prefs.PreferenceChangeListener;
 import java.util.prefs.Preferences;
@@ -80,8 +78,8 @@
     private JPanel headerPanel;
     private JPanel controlPanel;
     
-    private List<ToolbarButton> buttons;
-
+    private boolean hasButtons;
+    
     private Preferences prefs;
     
     public HeaderPanel() {
@@ -93,9 +91,7 @@
     }
     
     public HeaderPanel(Preferences prefs, String header) {
-        
-        buttons = new ArrayList<ToolbarButton>();
-        
+                
         this.prefs = prefs;
         
         this.header = header;
@@ -121,8 +117,8 @@
         contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.X_AXIS));
         
         add(contentPanel, BorderLayout.CENTER);
+        showText = prefs.getBoolean(HeaderPanel.class.getName(), false);
         registerPreferences();
-        showText = prefs.getBoolean(HeaderPanel.class.getName(), false);
         
         headerPanel.addMouseListener(new PreferencesPopupListener());
     }
@@ -147,12 +143,6 @@
                     public void run() {
                         boolean oldShowText = showText;
                         showText = value;
-                        controlPanel.removeAll();
-                        for (ToolbarButton button : buttons) {
-                            addToolBarButton_noClient(button);
-                        }
-                        revalidate();
-                        
                         firePropertyChange(SHOW_TEXT, oldShowText, showText);
                     }
                 });
@@ -175,41 +165,23 @@
         contentPanel.revalidate();
         repaint();
     }
-
-    private void addToolBarButton_noClient(final ToolbarButton button) {
+    
+    public void addToolBarButton(final ToolbarButton button) {
         AbstractButton theButton = button.getToolbarButton();
-        if (!showText) {
-            final AbstractButton proxy = button.copy().getToolbarButton();
-            proxy.setText("");
-            proxy.addActionListener(new ActionListener() {
-                @Override
-                public void actionPerformed(ActionEvent e) {
-                    button.getToolbarButton().setSelected(proxy.isSelected());                   
-                }
-            });
-            // need this so that if one of those properties change on the real button
-            // we can reflect it on the proxy
-            button.getToolbarButton().addPropertyChangeListener(new PropertyChangeListener() {
-                @Override
-                public void propertyChange(PropertyChangeEvent evt) {
-                    AbstractButton theButton = button.getToolbarButton();
-                    proxy.setName(theButton.getName());
-                    proxy.setToolTipText(theButton.getToolTipText());                
-                }
-            });
-            theButton = proxy;
-        }
+        button.toggleText(isShowToolbarText());
+        addPropertyChangeListener(SHOW_TEXT, new PropertyChangeListener() {
+            @Override
+            public void propertyChange(PropertyChangeEvent evt) {
+                button.toggleText(isShowToolbarText());
+            }
+        });
         controlPanel.add(theButton);
-    }
-    
-    public void addToolBarButton(ToolbarButton button) {
-        buttons.add(button);
-        addToolBarButton_noClient(button);
+        hasButtons = true;
     }
     
     class PreferencesPopup extends JPopupMenu {
         JMenuItem preferencesMenu;
-        public PreferencesPopup(){
+        public PreferencesPopup() {
             // TODO: localize
             String text = "Show button text";
             if (showText) {
@@ -240,7 +212,7 @@
         }
 
         private void popupPreferences(MouseEvent e){
-            if (buttons.size() > 0) {
+            if (hasButtons) {
                 PreferencesPopup menu = new PreferencesPopup();
                 menu.show(e.getComponent(), e.getX(), e.getY());
             }
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/ToolbarButton.java	Thu Oct 25 12:48:30 2012 -0400
+++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/ToolbarButton.java	Thu Oct 25 19:43:33 2012 +0200
@@ -40,5 +40,5 @@
 
 public interface ToolbarButton {
     AbstractButton getToolbarButton();
-    ToolbarButton copy();
+    void toggleText(boolean showText);
 }