File: SplitterLayout.java

package info (click to toggle)
libswidgets-java 0.1.4-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze
  • size: 356 kB
  • ctags: 667
  • sloc: java: 3,436; xml: 64; makefile: 11
file content (217 lines) | stat: -rw-r--r-- 7,729 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
/*
 * SplitterLayout.java
 */
package org.tigris.swidgets;

import java.awt.*;
import java.util.*;
import java.awt.event.*;

/**
 * A <code>ProportionalLayout</code> which recognises a contained
 * <code>Splitter</code> and automatically registers components either
 * side to be resized.
 *
 * @author Bob Tarling
 */
public class SplitterLayout extends ProportionalLayout {

    private SplitterMouseListener splitterMouseListener;

    public SplitterLayout() {
        this(HORIZONTAL);
    }

    /**
     * The constructor. 
     * 
     * @param orientation the orientation
     */
    public SplitterLayout(Orientation orientation) {
        super(orientation);
        splitterMouseListener = new SplitterMouseListener();
    }

    /**
     * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, 
     * java.awt.Component)
     */
    public void addLayoutComponent(String name, Component comp) {
        super.addLayoutComponent(name, comp);

        comp.setSize(comp.getPreferredSize());

        Component parent = comp.getParent();
        if (parent instanceof Container) {

            Container container = (Container) parent;
            int componentPosition = getComponentPosition(comp);
            int componentCount = container.getComponentCount();

            if (comp instanceof Splitter) {
                Splitter splitter = (Splitter) comp;
                splitter.addMouseListener(splitterMouseListener);

                if (componentPosition > 0) {
                    Component westComponent =
			container.getComponent(componentPosition - 1);
                    if (!(westComponent instanceof Splitter)) {
                        splitter.registerComponent(Splitter.WEST,
						   westComponent);
                    }
                }
                if (componentPosition < componentCount - 1) {
                    Component eastComponent =
			container.getComponent(componentPosition + 1);
                    if (!(eastComponent instanceof Splitter)) {
                        splitter.registerComponent(Splitter.EAST,
						   eastComponent);
                    }
                }
            }
            else {
                if (componentPosition > 0) {
                    Component westComponent =
			container.getComponent(componentPosition - 1);
                    if (westComponent instanceof Splitter) {
                        Splitter splitter = (Splitter) westComponent;
                        splitter.registerComponent(Splitter.EAST, comp);
                    }
                }
                if (componentPosition < componentCount - 1) {
                    Component eastComponent =
			container.getComponent(componentPosition + 1);
                    if (eastComponent instanceof Splitter) {
                        Splitter splitter = (Splitter) eastComponent;
                        splitter.registerComponent(Splitter.WEST, comp);
                    }
                }
            }
        }
    }

    private void calculateProportions() {
        // Find the total proportional size of all visible components
        double totalProportionalLength = 0;

        Enumeration enumKeys = componentTable.keys();
        while (enumKeys.hasMoreElements()) {
            Component comp = (Component) enumKeys.nextElement();
            String size = (String) (componentTable.get(comp));
            if (size.length() != 0) {
                totalProportionalLength += _orientation.getLength(comp);
            }
        }

        enumKeys = componentTable.keys();
        while (enumKeys.hasMoreElements()) {
            Component comp = (Component) enumKeys.nextElement();
            String size = (String) (componentTable.get(comp));
            if (size.length() != 0) {
                double proportionalLength =
		    _orientation.getLength(comp) * 100
		    / totalProportionalLength;
                componentTable.put(comp, Double.toString(proportionalLength));
            }
        }
    }

    // Looks at all components that are register as
    // proportional. Recalculates the proportions as a percentage
    // based on their current size.
    private void calculateProportions(Component westComponent,
				      Component eastComponent)
    {
        String westProportionalLength =
	    (String) (componentTable.get(westComponent));
        String eastProportionalLength =
	    (String) (componentTable.get(eastComponent));

        double westComponentLength = _orientation.getLength(westComponent);
        double eastComponentLength = _orientation.getLength(eastComponent);
        double totalProportionalLength =
	    Double.parseDouble(westProportionalLength)
	    + Double.parseDouble(eastProportionalLength);
        double newWestProportionalLength =
	    totalProportionalLength * westComponentLength
	    / (westComponentLength + eastComponentLength);
        double newEastProportionalLength =
	    totalProportionalLength - newWestProportionalLength;
        componentTable.put(westComponent,
			   Double.toString(newWestProportionalLength));
        componentTable.put(eastComponent,
			   Double.toString(newEastProportionalLength));
        return;
    }

    public int getComponentPosition(Component comp) {
	Component parent = comp.getParent();
        if (parent instanceof Container) {
            Container container = (Container) parent;
            int numberOfComponents = container.getComponentCount();
            for (int i = 0; i < numberOfComponents; ++i) {
                if (comp == container.getComponent(i)) return i;
            }
        }
        return -1;
    }

    private class SplitterMouseListener implements MouseListener {
        public void mouseReleased(MouseEvent me)
        {
            Splitter splitter = (Splitter) me.getComponent();
            Component westComponent =
		splitter.getRegisteredComponent(Splitter.WEST);
            Component eastComponent =
		splitter.getRegisteredComponent(Splitter.EAST);
            // Only act on a splitter release if it has both
            // components registered
            if (westComponent != null && eastComponent != null) {
                String westProportionalLength =
		    (String) (componentTable.get(westComponent));
                String eastProportionalLength =
		    (String) (componentTable.get(eastComponent));
                if (westProportionalLength.length() != 0
		    && eastProportionalLength.length() != 0)
		{
                    // If the components resized were both flagged to
                    // keep proportion then we only have to
                    // recalculate their proportions to eachother
                    calculateProportions(westComponent, eastComponent);
                }
                else if (westProportionalLength.length() != 0
			 || eastProportionalLength.length() != 0)
		{
		    // If only one component is flagged as
		    // proportioned then all proportioned
		    // components need to have their proportions
		    // recalculated.
                    calculateProportions();
                }
            }
        }

        public void mouseEntered(MouseEvent me)
        {
        }
        public void mouseExited(MouseEvent me)
        {
        }

        public void mousePressed(MouseEvent me)
        {
        }

        public void mouseClicked(MouseEvent me)
        {
        }

        public void mouseMoved(MouseEvent me)
        {
        }

        public void mouseDragged(MouseEvent me)
        {
        }
    }
}