Просмотр исходного кода

Wraps Buttonbar in a scrollPane

fixes issue 4022

Change-Id: Ibc83b9069eb802adfed4276a1b94e1f45b156280
Reviewed-on: http://gerrit.dmdirc.com/1142
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
Automatic-Compile: Gregory Holmes <greg@dmdirc.com>
tags/0.6.4
Simon Mott 14 лет назад
Родитель
Сommit
0ee836f0f5
1 измененных файлов: 103 добавлений и 53 удалений
  1. 103
    53
      src/com/dmdirc/addons/ui_swing/framemanager/buttonbar/ButtonBar.java

+ 103
- 53
src/com/dmdirc/addons/ui_swing/framemanager/buttonbar/ButtonBar.java Просмотреть файл

@@ -43,13 +43,15 @@ import java.awt.event.ComponentEvent;
43 43
 import java.awt.event.ComponentListener;
44 44
 import java.io.Serializable;
45 45
 import java.util.Collections;
46
-import java.util.HashMap;
47 46
 import java.util.List;
48 47
 import java.util.Map;
48
+import java.util.TreeMap;
49 49
 
50 50
 import javax.swing.JComponent;
51 51
 import javax.swing.JPanel;
52
+import javax.swing.JScrollPane;
52 53
 import javax.swing.JToggleButton;
54
+import javax.swing.ScrollPaneConstants;
53 55
 import javax.swing.SwingConstants;
54 56
 
55 57
 import net.miginfocom.swing.MigLayout;
@@ -78,8 +80,10 @@ public final class ButtonBar implements FrameManager, ActionListener,
78 80
     private final FramemanagerPosition position;
79 81
     /** The parent for the manager. */
80 82
     private JComponent parent;
83
+    /** The scrolling panel for our ButtonBar */
84
+     private final JScrollPane scrollPane;
81 85
     /** The panel used for our buttons. */
82
-    private final JPanel panel;
86
+    private final JPanel buttonPanel;
83 87
     /** The currently selected window. */
84 88
     private transient FrameContainer<?> selected;
85 89
     /** Selected window. */
@@ -95,46 +99,69 @@ public final class ButtonBar implements FrameManager, ActionListener,
95 99
 
96 100
     /** Creates a new instance of DummyFrameManager. */
97 101
     public ButtonBar() {
102
+        scrollPane = new JScrollPane();
103
+        scrollPane.setBorder(null);
104
+        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants
105
+                .HORIZONTAL_SCROLLBAR_NEVER);
106
+        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants
107
+                .VERTICAL_SCROLLBAR_AS_NEEDED);
108
+        scrollPane.setMinimumSize(new Dimension(0,buttonHeight));
109
+
98 110
         windows = new MapList<FrameContainer<?>, FrameContainer<?>>();
99
-        buttons = new HashMap<FrameContainer<?>, JToggleButton>();
111
+        buttons = new TreeMap<FrameContainer<?>, JToggleButton>(
112
+                new FrameContainerComparator());
100 113
         position = FramemanagerPosition.getPosition(
101
-                IdentityManager.getGlobalConfig().getOption("ui", "framemanagerPosition"));
114
+                IdentityManager.getGlobalConfig().getOption("ui",
115
+                "framemanagerPosition"));
102 116
 
103 117
         if (position.isHorizontal()) {
104
-            panel = new JPanel(new MigLayout("ins 0, fill, flowx"));
118
+            buttonPanel = new JPanel(new MigLayout("ins 0, fill, flowx"));
105 119
         } else {
106
-            panel = new JPanel(new MigLayout("ins 0, fill, flowy"));
120
+            buttonPanel = new JPanel(new MigLayout("ins 0, fill, flowy"));
107 121
         }
122
+        scrollPane.getViewport().add(buttonPanel);
108 123
     }
109 124
 
110 125
     /** {@inheritDoc} */
111 126
     @Override
112 127
     public void setParent(final JComponent parent) {
113
-        this.parent = parent;
128
+        UIUtilities.invokeLater(new Runnable() {
129
+
130
+            /** {inheritDoc} */
131
+            @Override
132
+            public void run() {
133
+                ButtonBar.this.parent = parent;
134
+                scrollPane.setSize(parent.getWidth(), parent.getHeight());
114 135
 
115
-        parent.setLayout(new MigLayout());
116
-        parent.add(panel);
117
-        parent.addComponentListener(this);
136
+                parent.setLayout(new MigLayout("ins 0"));
137
+                parent.add(scrollPane);
138
+                parent.addComponentListener(ButtonBar.this);
139
+            }
140
+        });
118 141
     }
119 142
 
120 143
     /**
121 144
      * Removes all buttons from the bar and readds them.
122 145
      */
123 146
     private void relayout() {
124
-        panel.removeAll();
147
+        buttonPanel.removeAll();
125 148
 
126
-        for (Map.Entry<FrameContainer<?>, List<FrameContainer<?>>> entry : windows.entrySet()) {
127
-            buttons.get(entry.getKey()).setPreferredSize(new Dimension(buttonWidth, buttonHeight));
128
-            panel.add(buttons.get(entry.getKey()));
149
+        for (Map.Entry<FrameContainer<?>, List<FrameContainer<?>>> entry : windows
150
+                .entrySet()) {
151
+
152
+            buttons.get(entry.getKey()).setPreferredSize(new Dimension(
153
+                    buttonWidth, buttonHeight));
154
+            buttonPanel.add(buttons.get(entry.getKey()));
129 155
 
130 156
             Collections.sort(entry.getValue(), new FrameContainerComparator());
131 157
 
132 158
             for (FrameContainer<?> child : entry.getValue()) {
133
-                buttons.get(child).setPreferredSize(new Dimension(buttonWidth, buttonHeight));
134
-                panel.add(buttons.get(child));
159
+                buttons.get(child).setPreferredSize(new Dimension(
160
+                        buttonWidth, buttonHeight));
161
+                buttonPanel.add(buttons.get(child));
135 162
             }
136 163
         }
137
-        panel.validate();
164
+        buttonPanel.validate();
138 165
     }
139 166
 
140 167
     /**
@@ -146,13 +173,11 @@ public final class ButtonBar implements FrameManager, ActionListener,
146 173
     private void addButton(final FrameContainer<?> source) {
147 174
         final JToggleButton button = new JToggleButton(source.toString(),
148 175
                 IconManager.getIconManager().getIcon(source.getIcon()));
149
-
150 176
         button.addActionListener(this);
151 177
         button.setHorizontalAlignment(SwingConstants.LEFT);
152 178
         button.setMinimumSize(new Dimension(0,buttonHeight));
153 179
         button.setMargin(new Insets(0, 0, 0, 0));
154 180
         buttons.put(source, button);
155
-
156 181
     }
157 182
 
158 183
     /** {@inheritDoc} */
@@ -170,50 +195,81 @@ public final class ButtonBar implements FrameManager, ActionListener,
170 195
     /** {@inheritDoc} */
171 196
     @Override
172 197
     public void addWindow(final FrameContainer<?> window, final boolean focus) {
173
-        windows.add(window);
174
-        //This window is a root window
175
-        addButton(window);
198
+        UIUtilities.invokeLater(new Runnable() {
199
+
200
+            /** {inheritDoc} */
201
+            @Override
202
+            public void run() {
203
+                windows.add(window);
204
+                //This window is a root window
205
+                addButton(window);
206
+
207
+                relayout();
208
+                window.addNotificationListener(ButtonBar.this);
209
+                window.addSelectionListener(ButtonBar.this);
210
+                window.addFrameInfoListener(ButtonBar.this);
211
+            }
176 212
 
177
-        relayout();
178
-        window.addNotificationListener(this);
179
-        window.addSelectionListener(this);
180
-        window.addFrameInfoListener(this);
213
+        });
181 214
     }
182 215
 
183 216
     /** {@inheritDoc} */
184 217
     @Override
185 218
     public void delWindow(final FrameContainer<?> window) {
186
-        windows.remove(window);
187
-
188
-        relayout();
189
-        window.removeNotificationListener(this);
190
-        window.removeFrameInfoListener(this);
191
-        window.removeSelectionListener(this);
219
+        UIUtilities.invokeLater(new Runnable() {
220
+
221
+            /** {inheritDoc} */
222
+            @Override
223
+            public void run() {
224
+                windows.remove(window);
225
+                buttons.remove(window);
226
+
227
+                relayout();
228
+                window.removeNotificationListener(ButtonBar.this);
229
+                window.removeFrameInfoListener(ButtonBar.this);
230
+                window.removeSelectionListener(ButtonBar.this);
231
+            }
232
+        });
192 233
     }
193 234
 
194 235
     /** {@inheritDoc} */
195 236
     @Override
196 237
     public void addWindow(final FrameContainer<?> parent,
197 238
             final FrameContainer<?> window, final boolean focus) {
198
-        windows.add(parent, window);
199
-        //This window has a parent Window
200
-        addButton(window);
201
-
202
-        relayout();
203
-        window.addNotificationListener(this);
204
-        window.addSelectionListener(this);
205
-        window.addFrameInfoListener(this);
239
+        UIUtilities.invokeLater(new Runnable() {
240
+
241
+            /** {inheritDoc} */
242
+            @Override
243
+            public void run() {
244
+                windows.add(parent, window);
245
+                //This window has a parent Window
246
+                addButton(window);
247
+
248
+                relayout();
249
+                window.addNotificationListener(ButtonBar.this);
250
+                window.addSelectionListener(ButtonBar.this);
251
+                window.addFrameInfoListener(ButtonBar.this);
252
+            }
253
+        });
206 254
     }
207 255
 
208 256
     /** {@inheritDoc} */
209 257
     @Override
210 258
     public void delWindow(final FrameContainer<?> parent, final FrameContainer<?> window) {
211
-        windows.remove(parent, window);
212
-
213
-        relayout();
214
-        window.removeNotificationListener(this);
215
-        window.removeFrameInfoListener(this);
216
-        window.removeSelectionListener(this);
259
+        UIUtilities.invokeLater(new Runnable() {
260
+
261
+            /** {inheritDoc} */
262
+            @Override
263
+            public void run() {
264
+                windows.remove(parent, window);
265
+                buttons.remove(window);
266
+
267
+                relayout();
268
+            window.removeNotificationListener(ButtonBar.this);
269
+            window.removeFrameInfoListener(ButtonBar.this);
270
+            window.removeSelectionListener(ButtonBar.this);
271
+            }
272
+        });
217 273
     }
218 274
 
219 275
     /**
@@ -241,13 +297,7 @@ public final class ButtonBar implements FrameManager, ActionListener,
241 297
      */
242 298
     @Override
243 299
     public void componentResized(final ComponentEvent e) {
244
-        buttonWidth = position.isHorizontal() ? 150 : (parent.getWidth() - UIUtilities.SMALL_BORDER * 3) / cells;
245
-        
246
-        if (position.isHorizontal()) {
247
-            maxButtons = parent.getWidth() / (buttonWidth + UIUtilities.SMALL_BORDER * 2);
248
-        } else {
249
-            maxButtons = parent.getHeight() / (buttonHeight + UIUtilities.SMALL_BORDER * 2);
250
-        }
300
+        buttonWidth = position.isHorizontal() ? 150 : (parent.getWidth() / cells);
251 301
         relayout();
252 302
     }
253 303
 

Загрузка…
Отмена
Сохранить