Browse Source

Make buttonbar work correcly with on the fly frame manager switching.

fixes issue 4197
fixes issue 4198

Change-Id: I80fa15683fc28124914703d6b83648399c1197b7
Reviewed-on: http://gerrit.dmdirc.com/1294
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
tags/0.6.4
Simon Mott 14 years ago
parent
commit
399082a731
1 changed files with 53 additions and 17 deletions
  1. 53
    17
      src/com/dmdirc/addons/ui_swing/framemanager/buttonbar/ButtonBar.java

+ 53
- 17
src/com/dmdirc/addons/ui_swing/framemanager/buttonbar/ButtonBar.java View File

@@ -64,6 +64,7 @@ import javax.swing.JScrollPane;
64 64
 import javax.swing.ScrollPaneConstants;
65 65
 import javax.swing.SwingConstants;
66 66
 
67
+import javax.swing.SwingUtilities;
67 68
 import net.miginfocom.layout.PlatformDefaults;
68 69
 import net.miginfocom.swing.MigLayout;
69 70
 
@@ -93,26 +94,28 @@ public final class ButtonBar implements FrameManager, ActionListener,
93 94
      private final JScrollPane scrollPane;
94 95
     /** The panel used for our buttons. */
95 96
     private final ButtonPanel buttonPanel;
96
-    /** The currently selected window. */
97
+    /** The currently selected frame. */
97 98
     private transient FrameContainer<?> selected;
98 99
     /** Selected window. */
99 100
     private Window activeWindow;
100
-    /** The number of buttons per row or column. */
101
+    /** The default number of buttons per row or column. */
101 102
     private int cells = 1;
102
-    /** The number of buttons to render per {cell,row}. */
103
+    /** The default number of buttons to render per {cell,row}. */
103 104
     private int maxButtons = Integer.MAX_VALUE;
104
-    /** The width of buttons. */
105
+    /** The default width of buttons. */
105 106
     private int buttonWidth = 0;
106
-    /** The height of buttons. */
107
+    /** The default height of buttons. */
107 108
     private int buttonHeight = 25;
108
-    /** Sort root windows */
109
+    /** Sort root windows prefs setting */
109 110
     private boolean sortRootWindows = IdentityManager.getGlobalConfig()
110 111
             .getOptionBool("ui", "sortrootwindows");
111
-    /** Sort child windows */
112
+    /** Sort child windows prefs setting */
112 113
     private boolean sortChildWindows = IdentityManager.getGlobalConfig()
113 114
             .getOptionBool("ui", "sortchildwindows");
115
+    /** UI Window Factory. */
116
+    private SwingWindowFactory windowFactory;
114 117
     /** UI Controller. */
115
-    private SwingWindowFactory controller;
118
+    private SwingController controller;
116 119
 
117 120
     /** Creates a new instance of DummyFrameManager. */
118 121
     public ButtonBar() {
@@ -129,6 +132,7 @@ public final class ButtonBar implements FrameManager, ActionListener,
129 132
         position = FramemanagerPosition.getPosition(
130 133
                 IdentityManager.getGlobalConfig().getOption("ui",
131 134
                 "framemanagerPosition"));
135
+
132 136
         IdentityManager.getGlobalConfig().addChangeListener("ui",
133 137
                         "sortrootwindows", this);
134 138
         IdentityManager.getGlobalConfig().addChangeListener("ui",
@@ -167,7 +171,7 @@ public final class ButtonBar implements FrameManager, ActionListener,
167 171
     /** {@inheritDoc} */
168 172
     @Override
169 173
     public void setParent(final JComponent parent) {
170
-        UIUtilities.invokeLater(new Runnable() {
174
+        SwingUtilities.invokeLater(new Runnable() {
171 175
 
172 176
             /** {inheritDoc} */
173 177
             @Override
@@ -179,12 +183,44 @@ public final class ButtonBar implements FrameManager, ActionListener,
179 183
                 parent.setLayout(new MigLayout("ins 0"));
180 184
                 parent.add(scrollPane);
181 185
                 parent.addComponentListener(ButtonBar.this);
186
+                ButtonBar.this.buttonWidth = position.isHorizontal()
187
+                        ? 150 : (parent.getWidth() / cells);
188
+                initButtons(WindowManager.getRootWindows());
189
+                if (controller.getMainFrame().getActiveFrame() != null) {
190
+                    selectionChanged(controller.getMainFrame()
191
+                            .getActiveFrame().getContainer());
192
+                }
182 193
                 parent.setVisible(true);
183
-                relayout();
184 194
             }
185 195
         });
186 196
     }
187 197
 
198
+    /**
199
+     * Initialises buttons for the currently available windows. This should only
200
+     * be called once when this buttonbar is made active in the client.
201
+     * See {@link #setParent}. This method essentially does nothing if the
202
+     * client is started with the buttonbar enabled.
203
+     *
204
+     * @param windowCollection Collection of windows {@link FrameContainer}
205
+     * @author Simon Mott
206
+     * @since 0.6.4
207
+     */
208
+    private void initButtons(final Collection<FrameContainer<?>> windowCollection) {
209
+        Window window;
210
+        Window parentWindow;
211
+        for (FrameContainer<?> frame : windowCollection) {
212
+            window = windowFactory.getSwingWindow(frame);
213
+            parentWindow = windowFactory.getSwingWindow(frame.getParent());
214
+            windowAdded(parentWindow, window);
215
+
216
+            if (!frame.getChildren().isEmpty()) {
217
+                final ArrayList<FrameContainer<?>> childList = new ArrayList
218
+                        <FrameContainer<?>>(frame.getChildren());
219
+                initButtons(childList);
220
+            }
221
+        }
222
+    }
223
+
188 224
     /**
189 225
      * Retreives the button object associated with {@link FrameContainer}.
190 226
      *
@@ -193,7 +229,7 @@ public final class ButtonBar implements FrameManager, ActionListener,
193 229
      * Returns null if none exist
194 230
      */
195 231
     public FrameToggleButton getButton(final FrameContainer<?> frame) {
196
-        final Window window = controller.getSwingWindow(frame);
232
+        final Window window = windowFactory.getSwingWindow(frame);
197 233
         if (buttons.containsKey(window)) {
198 234
             return buttons.get(window);
199 235
         }
@@ -207,7 +243,8 @@ public final class ButtonBar implements FrameManager, ActionListener,
207 243
             throw new IllegalArgumentException("Controller must be an instance" +
208 244
                     " of SwingController");
209 245
         }
210
-        this.controller = ((SwingController) controller).getWindowFactory();
246
+        this.windowFactory = ((SwingController) controller).getWindowFactory();
247
+        this.controller = ((SwingController) controller);
211 248
     }
212 249
 
213 250
     /**
@@ -223,13 +260,12 @@ public final class ButtonBar implements FrameManager, ActionListener,
223 260
      * @author Simon Mott
224 261
      * @since 0.6.4
225 262
      */
226
-    private void insertButtons(final Collection<FrameContainer<?>> windowCollection) {
263
+    private void displayButtons(final Collection<FrameContainer<?>> windowCollection) {
227 264
         FrameToggleButton button;
228 265
         for (FrameContainer<?> window : windowCollection) {
229 266
             button = getButton(window);
230 267
             if (button != null) {
231
-                button.setPreferredSize(new Dimension(
232
-                            buttonWidth, buttonHeight));
268
+                button.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
233 269
                 buttonPanel.add(button);
234 270
                 if (!window.getChildren().isEmpty()) {
235 271
                     final ArrayList<FrameContainer<?>> childList = new ArrayList
@@ -237,7 +273,7 @@ public final class ButtonBar implements FrameManager, ActionListener,
237 273
                     if (sortChildWindows) {
238 274
                          Collections.sort(childList, new FrameContainerComparator());
239 275
                     }
240
-                    insertButtons(childList);
276
+                    displayButtons(childList);
241 277
                 }
242 278
             }
243 279
         }
@@ -256,7 +292,7 @@ public final class ButtonBar implements FrameManager, ActionListener,
256 292
             Collections.sort(windowList, new FrameContainerComparator());
257 293
         }
258 294
 
259
-        insertButtons(windowList);
295
+        displayButtons(windowList);
260 296
         buttonPanel.setVisible(true);
261 297
     }
262 298
 

Loading…
Cancel
Save