-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDragLayout.java
More file actions
237 lines (205 loc) · 6.08 KB
/
DragLayout.java
File metadata and controls
237 lines (205 loc) · 6.08 KB
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
import java.awt.*;
import javax.swing.*;
/**
* The DragLayout is used as a layout manager for a Container that supports
* random dragging of components with the Container.
*
* The location of the component will not be managed by the layout manager.
*
* The size of the component can be either the preferred size of the component,
* or the actual size of the component.
*
* The preferred size of the container will calculated normally based on the
* actual location and size of each component.
*
* A Component cannot have a location outside the bounds of the parent container.
* That is the x/y coordinate must be withing the Insets of the Container. If
* any component is outside the bounds, then the location of all components
* will be adjusted by the same amount.
*/
public class DragLayout implements LayoutManager, java.io.Serializable
{
private boolean usePreferredSize;
/**
* Convenience constructor
*/
public DragLayout()
{
this(true);
}
/**
* Create a DragLayout and indicate how the component size is determined.
*
* @param usePreferred size see setPreferredSize() for values.
*/
public DragLayout(boolean usePreferredSize)
{
setUsePreferredSize( usePreferredSize );
}
/**
* Set the use preferred size property
*
* @param usePreferredSize when true, use the preferred size of the component
* in layout calculations. When false, use the size
* of the component, unless the size is 0, in which
* case use the preferred size as a default.
*/
public void setUsePreferredSize(boolean usePreferredSize)
{
this.usePreferredSize = usePreferredSize;
}
/**
* Get the use Preferred Size property
*
*@return the use preferred size property
*/
public boolean isUsePreferredSize()
{
return usePreferredSize;
}
/**
* Adds the specified component with the specified name to the layout.
* @param name the name of the component
* @param comp the component to be added
*/
@Override
public void addLayoutComponent(String name, Component comp) {}
/**
* Removes the specified component from the layout.
*
* @param comp the component to be removed
*/
@Override
public void removeLayoutComponent(Component component)
{
}
/**
* Determine the minimum size on the Container
*
* @param target the container in which to do the layout
* @return the minimum dimensions needed to lay out the
* subcomponents of the specified container
*/
@Override
public Dimension minimumLayoutSize(Container parent)
{
synchronized (parent.getTreeLock())
{
return preferredLayoutSize(parent);
}
}
/**
* Determine the preferred size on the Container
*
* @param parent the container in which to do the layout
* @return the preferred dimensions to lay out the
* subcomponents of the specified container
*/
@Override
public Dimension preferredLayoutSize(Container parent)
{
synchronized (parent.getTreeLock())
{
return getLayoutSize(parent);
}
}
/*
* The calculation for minimum/preferred size is the same.
*
* @param parent the container in which to do the layout
*/
private Dimension getLayoutSize(Container parent)
{
Insets parentInsets = parent.getInsets();
int x = parentInsets.left;
int y = parentInsets.top;
int width = 0;
int height = 0;
// Get extreme values of the components on the container.
// The x/y values represent the starting point relative to the
// top/left of the container. The width/height values represent
// the bottom/right value within the container.
for (Component component: parent.getComponents())
{
if (component.isVisible())
{
Point p = component.getLocation();
Dimension d = getActualSize(component);
x = Math.min(x, p.x);
y = Math.min(y, p.y);
width = Math.max(width, p.x + d.width);
height = Math.max(height, p.y + d.height);
}
}
// Width/Height is adjusted if any component is outside left/top edge
if (x < parentInsets.left)
width += parentInsets.left - x;
if (y < parentInsets.top)
height += parentInsets.top - y;
// Adjust for insets
width += parentInsets.right;
height += parentInsets.bottom;
Dimension d = new Dimension(width, height);
return d;
}
private Dimension getActualSize(Component component)
{
if (usePreferredSize)
return component.getPreferredSize();
// Use the preferred size as a default when a size has not been set.
Dimension d = component.getSize();
if (d.width == 0 || d.height == 0)
return component.getPreferredSize();
else
return d;
}
/**
* Lays out the specified container using this layout.
*
* @param target the container in which to do the layout
*/
@Override
public void layoutContainer(Container parent)
{
synchronized (parent.getTreeLock())
{
Insets parentInsets = parent.getInsets();
int x = parentInsets.left;
int y = parentInsets.top;
// Get x/y location of any component outside the bounds of the panel.
// All components will be adjust by the x/y values, if necessary.
for (Component component: parent.getComponents())
{
if (component.isVisible())
{
Point location = component.getLocation();
x = Math.min(x, location.x);
y = Math.min(y, location.y);
}
}
x = (x < parentInsets.left) ? parentInsets.left - x : 0;
y = (y < parentInsets.top) ? parentInsets.top - y : 0;
// Set bounds of each component
for (Component component: parent.getComponents())
{
if (component.isVisible())
{
Point p = component.getLocation();
Dimension d = getActualSize(component);
component.setBounds(p.x + x, p.y + y, d.width, d.height);
}
}
}}
/**
* Returns the string representation of this column layout's values.
*
* @return a string representation of this layout
*/
@Override
public String toString()
{
return "["
+ getClass().getName()
+ "]";
}
}