File: ChainingDockBoundary.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 (263 lines) | stat: -rw-r--r-- 5,999 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
/*
 *  ChainingDockBoundary.java
 *  2004-01-02
 */


package org.tigris.toolbar.layouts;

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


/**
 * A simple DockBoundary that chains the docked toolbars
 * along a single row.
 * @author Christopher Bach
 */
// package access only...
class ChainingDockBoundary extends DockBoundary
{

    private boolean         ourLayoutReflects = false;





    /**
     * Creates a ChainingDockBoundary for the specified edge.
     */
    public ChainingDockBoundary(int edge)
    {
        super(edge);
        ourLayoutReflects = (edge == DockLayout.SOUTH
                                || edge == DockLayout.EAST);
    }


    /**
     * Creates a ChainingDockBoundary for the specified edge
     * with the provided spacing.
     */
    public ChainingDockBoundary(int edge, int spacing)
    {
        super(edge, spacing);
        ourLayoutReflects = (edge == DockLayout.SOUTH
                                || edge == DockLayout.EAST);
    }















    /**
     * 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)
    {
        JToolBar[] toolbars = getToolBars();

        for (int i=0; i < toolbars.length; i++)
        {
            if (toolbars[i].getBounds().contains(p))
            {
                return i;
            }
        }

        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)
    {
        return Arrays.asList(getToolBars()).indexOf(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)
    {
        return 0;
    }



    /**
     * 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)
    {
        if (Arrays.asList(getToolBars()).contains(toolbar)) return 0;
        else 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 length = 0;
        if (orientation == DockLayout.HORIZONTAL) length = width;
        else length = height;

        JToolBar[] bars = getToolBars();
        int barDepth = getPreferredDepth();
        int barLength = 0;
        int totalBarLength = 0;

        for (int i=0; i < bars.length; i++)
        {
            JToolBar toolbar = bars[i];

            barLength = getPreferredToolBarLength(toolbar);

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

            setToolBarBounds(toolbar, totalBarLength,
                    0, Math.min(barLength, length), barDepth);

            totalBarLength += barLength;

        }

        setDepth(barDepth);
    }



















    /**
     * Returns the largest preferred height or width (depending
     * on orientation) of all of the associated toolbars.
     */
    private int getPreferredDepth()
    {
        int depth = 0;

        JToolBar[] toolbars = super.getToolBars();

        for (int i=0; i < toolbars.length; i++)
        {
            JToolBar toolbar = toolbars[i];
            Dimension d = toolbar.getPreferredSize();
            if (getOrientation() == DockLayout.HORIZONTAL)
                    depth = Math.max(depth, d.height);
            else depth = Math.max(depth, d.width);
        }

        return depth;
    }










    /**
     * Sets the bounds of the provided toolbar based on the provided
     * bounds parameters (given relative to this boundary's origin)
     * accounting for the default wrapping direction for this boundary.
     */
    private void setToolBarBounds(JToolBar toolbar, int lengthOffset,
                    int depthOffset, int length, int depth)
    {
        if (getOrientation() == DockLayout.HORIZONTAL)
        {
            toolbar.setBounds(x + lengthOffset,
                        y + depthOffset, length, depth);

            if (ourLayoutReflects)
            {
                Rectangle r = toolbar.getBounds();
                mirrorBounds(r, y);
                toolbar.setBounds(r);
            }
        }

        else
        {
            toolbar.setBounds(x + depthOffset,
                        y + lengthOffset,
                            depth, length);

            if (ourLayoutReflects)
            {
                Rectangle r = toolbar.getBounds();
                mirrorBounds(r, x);
                toolbar.setBounds(r);
            }
        }
    }






}