File: StackingDockBoundary.java

package info (click to toggle)
libtoolbar-java 1.3-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 240 kB
  • ctags: 413
  • sloc: java: 2,284; xml: 64; makefile: 10
file content (446 lines) | stat: -rw-r--r-- 10,158 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 *  StackingDockBoundary.java
 *  2004-01-02
 */


package org.tigris.toolbar.layouts;

import java.awt.*;
import javax.swing.*;
import java.util.*;


/**
 * A DockBoundary that allows stacking toolbars into rows (or columns).
 * Rows are NOT automatically wrapped when the toolbars don't fit.
 * @author Christopher Bach
 */
// Package access only...
class StackingDockBoundary extends DockBoundary
{

    private ArrayList   ourDockSlivers = new ArrayList();




    /**
     * Creates a StackingDockBoundary for the specified edge.
     */
    public StackingDockBoundary(int edge)
    {
        super(edge);
    }

    /**
     * Creates a StackingDockBoundary for the specified edge
     * with the provided spacing.
     */
    public StackingDockBoundary(int edge, int spacing)
    {
        super(edge, spacing);
    }












    /**
     * Implementation of the abstract superclass method, returns
     * the index at which the toolbar should be added when dropped
     * at the specified point.
     */
    public int getDockIndex(Point p)
    {
        for (int i=0; i < ourDockSlivers.size(); i++)
        {
            DockSliver sliver = (DockSliver)ourDockSlivers.get(i);
            if (sliver.contains(p)) return sliver.getDockIndex(p);
        }

        return DockLayout.MAX;
    }



    /**
     * Implementation of the absract superclass method, returns
     * the index of the specified toolbar within this boundary,
     * or -1 if the toolbar is not present.
     */
    public int getDockIndex(JToolBar toolbar)
    {
        DockSliver sliver = getDockSliver(toolbar);
        if (sliver == null) return -1;
        else return sliver.getDockIndex(toolbar);
    }




    /**
     * Implementation of the abstract superclass method, returns
     * the row index at which the toolbar should be added when
     * dropped at the specified point.
     */
    public int getRowIndex(Point p)
    {
        for (int i=0; i < ourDockSlivers.size(); i++)
        {
            DockSliver sliver = (DockSliver)ourDockSlivers.get(i);
            if (sliver.contains(p)) return i;
        }

        return DockLayout.MAX;
    }



    /**
     * Implementation of the abstract superclass method, returns
     * the row index of the specified toolbar in this boundary,
     * or -1 if the toolbar is not present.
     */
    public int getRowIndex(JToolBar toolbar)
    {
        for (int i=0; i < ourDockSlivers.size(); i++)
        {
            DockSliver sliver = (DockSliver)ourDockSlivers.get(i);
            if (sliver.containsToolBar(toolbar)) return i;
        }

        return -1;
    }





    /**
     * Implementation of the abstract superclass method, lays out
     * the registered toolbars and calculates the depth of
     * this boundary.  When this method is called, the depth of
     * the boundary is assumed to be 0 and is not determined until
     * the validation completes.  For convenience, the
     * subcomponents are arranged in a top-down or left-to-right
     * fashion relative to the origin of this boundary, which
     * presumably lies at the parent container's edge.  For the
     * south and east boundaries, this puts the bounds of the
     * subcomponents outside the bounds of the parent container.
     * To compensate, these subcomponents are reflected about a
     * line passing through the boundary's origin as they are placed.
     */
    public void validate()
    {
        int spacing = getSpacing();
        int orientation = getOrientation();
        int edge = getEdge();

        int length = 0;
        if (orientation == DockLayout.HORIZONTAL) length = width;
        else length = height;

        int totalDepth = 0;


        for (int i=0; i < ourDockSlivers.size(); i++)
        {
            DockSliver sliver = (DockSliver)ourDockSlivers.get(i);

            if (totalDepth != 0) totalDepth += spacing;

            if (orientation == DockLayout.HORIZONTAL)
                sliver.setPosition(x, y + totalDepth, length);
            else sliver.setPosition(x + totalDepth, y, length);

            if (edge == DockLayout.EAST || edge == DockLayout.SOUTH)
            {
                Rectangle r = sliver.getBounds();

                if (orientation == DockLayout.HORIZONTAL)
                                mirrorBounds(r, y);
                else mirrorBounds(r, x);

                sliver.setBounds(r);
                sliver.validate();
            }

            totalDepth += sliver.getDepth();
        }


        setDepth(totalDepth);

    }


















    // Override superclass methods to provide additional behavior


    /**
     * Inserts the specified toolbar into this boundary at the
     * provided indices.
     */
    protected void toolBarAdded(JToolBar toolbar, int rowIndex, int index)
    {
        getDockSliver(rowIndex).addToolBar(toolbar, index);
    }



    /**
     * Removes the specified toolbar from this boundary.
     */
    protected void toolBarRemoved(JToolBar toolbar)
    {
        DockSliver sliver = getDockSliver(toolbar);
        if (sliver != null)
        {
            sliver.removeToolBar(toolbar);
            if (sliver.getToolBarCount() == 0)
                    ourDockSlivers.remove(sliver);
        }
    }
















    /**
     * Returns a DockSliver for the specified row.  If none exists
     * at this index, a new one is created and inserted.
     */
    private DockSliver getDockSliver(int row)
    {
        if (row < 0)
        {
            DockSliver sliver = new DockSliver();
            ourDockSlivers.add(0, sliver);
            return sliver;
        }

        else if (row >= ourDockSlivers.size())
        {
            DockSliver sliver = new DockSliver();
            ourDockSlivers.add(sliver);
            return sliver;
        }

        else return (DockSliver)ourDockSlivers.get(row);
    }



    /**
     * Returns the DockSliver containing the specified toolbar or
     * null if no DockSliver contains this toolbar.
     */
    private DockSliver getDockSliver(JToolBar toolbar)
    {
        for (int i=0; i < ourDockSlivers.size(); i++)
        {
            DockSliver sliver = (DockSliver)ourDockSlivers.get(i);
            if (sliver.containsToolBar(toolbar)) return sliver;
        }

        return null;
    }















    /**
     * Inner class defining a row or "sliver" of toolbars stacked
     * within this boundary.
     */
    private class DockSliver extends Rectangle
    {

        private ArrayList   myToolBars = new ArrayList();


        public DockSliver()
        {

        }



        public void addToolBar(JToolBar toolbar)
        {
            myToolBars.add(toolbar);
        }


        public void addToolBar(JToolBar toolbar, int index)
        {
            if (index < 0) myToolBars.add(0, toolbar);
            else if (index >= myToolBars.size()) myToolBars.add(toolbar);
            else myToolBars.add(index, toolbar);
        }


        public void removeToolBar(JToolBar toolbar)
        {
            myToolBars.remove(toolbar);
        }


        public int getToolBarCount()
        {
            return myToolBars.size();
        }


        public boolean containsToolBar(JToolBar toolbar)
        {
            return myToolBars.contains(toolbar);
        }






        public void setPosition(int x, int y, int length)
        {
            setLocation(x, y);
            if (getOrientation() == DockLayout.HORIZONTAL) width = length;
            else height = length;
            validate();
        }



        public void validate()
        {
            int pos = 0;
            int base = 0;
            int orient = getOrientation();
            int space = getSpacing();
            int length = 0;

            if (orient == DockLayout.HORIZONTAL)
            {
                pos = x;
                base = x;
                length = width;
                height = getPreferredDepth();
            }

            else
            {
                pos = y;
                base = y;
                length = height;
                width = getPreferredDepth();
            }

            for (int i=0; i < myToolBars.size(); i++)
            {
                JToolBar toolbar = (JToolBar)myToolBars.get(i);
                int barLength = getPreferredToolBarLength(toolbar);
                if (pos + barLength > length + base)
                        barLength = base + length - pos;

                if (orient == DockLayout.HORIZONTAL)
                {
                    toolbar.setBounds(pos, y, barLength, height);
                }

                else toolbar.setBounds(x, pos, width, barLength);

                pos += barLength + space;
            }
        }


        public int getPreferredDepth()
        {
            int depth = 0;

            for (int i=0; i < myToolBars.size(); i++)
            {
                JToolBar toolbar = (JToolBar)myToolBars.get(i);
                int barDepth = getPreferredToolBarDepth(toolbar);
                depth = Math.max(depth, barDepth);

            }

            return depth;
        }


        public int getDepth()
        {
            if (getOrientation() == DockLayout.HORIZONTAL) return height;
            else return width;
        }



        public int getDockIndex(Point p)
        {
            for (int i=0; i < myToolBars.size(); i++)
            {
                JToolBar toolbar = (JToolBar)myToolBars.get(i);
                if (toolbar.getBounds().contains(p)) return i;
            }

            return DockLayout.MAX;
        }


        public int getDockIndex(JToolBar toolbar)
        {
            return myToolBars.indexOf(toolbar);
        }
    }

}