Added
Link Here
|
1 |
package org.eclipse.ui.tests.layout.constraints; |
2 |
|
3 |
import org.eclipse.jface.dialogs.MessageDialog; |
4 |
import org.eclipse.jface.layout.GridDataFactory; |
5 |
import org.eclipse.jface.layout.GridLayoutFactory; |
6 |
import org.eclipse.jface.layout.LayoutConstants; |
7 |
import org.eclipse.swt.SWT; |
8 |
import org.eclipse.swt.events.SelectionAdapter; |
9 |
import org.eclipse.swt.events.SelectionEvent; |
10 |
import org.eclipse.swt.widgets.Button; |
11 |
import org.eclipse.swt.widgets.Composite; |
12 |
import org.eclipse.swt.widgets.Control; |
13 |
import org.eclipse.swt.widgets.Label; |
14 |
import org.eclipse.swt.widgets.Text; |
15 |
import org.eclipse.ui.ISizeProvider; |
16 |
import org.eclipse.ui.IWorkbenchPage; |
17 |
import org.eclipse.ui.IWorkbenchPartConstants; |
18 |
import org.eclipse.ui.PartInitException; |
19 |
import org.eclipse.ui.part.ViewPart; |
20 |
|
21 |
public class LayoutConstraintsView extends ViewPart implements ISizeProvider { |
22 |
|
23 |
private Control control; |
24 |
|
25 |
private int minWidth = ISizeProvider.INFINITE; |
26 |
private int maxWidth = ISizeProvider.INFINITE; |
27 |
private int minHeight = ISizeProvider.INFINITE; |
28 |
private int maxHeight = ISizeProvider.INFINITE; |
29 |
private int quantizedWidth = ISizeProvider.INFINITE; |
30 |
private int quantizedHeight = ISizeProvider.INFINITE; |
31 |
private int fixedArea = ISizeProvider.INFINITE; |
32 |
private Text minWidthText; |
33 |
private Text maxWidthText; |
34 |
private Text quantizedWidthText; |
35 |
private Text minHeightText; |
36 |
private Text maxHeightText; |
37 |
private Text quantizedHeightText; |
38 |
private Text fixedAreaText; |
39 |
|
40 |
public void createPartControl(Composite parent) { |
41 |
control = parent; |
42 |
|
43 |
Composite buttonBar = new Composite(parent, SWT.NONE); |
44 |
{ |
45 |
GridDataFactory buttonData = GridDataFactory.fillDefaults().grab(true, false); |
46 |
|
47 |
Button applyButton = new Button(buttonBar, SWT.PUSH); |
48 |
applyButton.setText("Apply"); |
49 |
applyButton.addSelectionListener(new SelectionAdapter() { |
50 |
/* (non-Javadoc) |
51 |
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
52 |
*/ |
53 |
public void widgetSelected(SelectionEvent e) { |
54 |
applyPressed(); |
55 |
} |
56 |
}); |
57 |
buttonData.applyTo(applyButton); |
58 |
|
59 |
Button clearButton = new Button(buttonBar, SWT.PUSH); |
60 |
clearButton.setText("Reset"); |
61 |
clearButton.addSelectionListener(new SelectionAdapter() { |
62 |
/* (non-Javadoc) |
63 |
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
64 |
*/ |
65 |
public void widgetSelected(SelectionEvent e) { |
66 |
minWidthText.setText(""); |
67 |
maxWidthText.setText(""); |
68 |
quantizedWidthText.setText(""); |
69 |
minHeightText.setText(""); |
70 |
maxHeightText.setText(""); |
71 |
quantizedHeightText.setText(""); |
72 |
fixedAreaText.setText(""); |
73 |
applyPressed(); |
74 |
} |
75 |
}); |
76 |
buttonData.applyTo(clearButton); |
77 |
|
78 |
Button newViewButton = new Button(buttonBar, SWT.PUSH); |
79 |
newViewButton.setText("New View"); |
80 |
newViewButton.addSelectionListener(new SelectionAdapter() { |
81 |
/* (non-Javadoc) |
82 |
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
83 |
*/ |
84 |
public void widgetSelected(SelectionEvent e) { |
85 |
try { |
86 |
getSite().getPage().showView("org.eclipse.ui.tests.layout.constraints.LayoutConstraintsView", |
87 |
"" + System.currentTimeMillis(), IWorkbenchPage.VIEW_ACTIVATE); |
88 |
} catch (PartInitException e1) { |
89 |
MessageDialog.openError(getSite().getShell(), "Error opening view", "Unable to open view."); |
90 |
} |
91 |
} |
92 |
}); |
93 |
buttonData.applyTo(newViewButton); |
94 |
|
95 |
GridLayoutFactory.fillDefaults().equalWidth(true).numColumns(3).applyTo(buttonBar); |
96 |
} |
97 |
GridDataFactory.fillDefaults().grab(true, false).span(2,1).applyTo(buttonBar); |
98 |
|
99 |
new Label(parent, SWT.NONE).setText("Min Width"); |
100 |
minWidthText = createText(parent); |
101 |
|
102 |
new Label(parent, SWT.NONE).setText("Max Width (blank = unbounded)"); |
103 |
maxWidthText = createText(parent); |
104 |
|
105 |
new Label(parent, SWT.NONE).setText("Quantized Width (blank = none)"); |
106 |
quantizedWidthText = createText(parent); |
107 |
|
108 |
new Label(parent, SWT.NONE).setText("Min Height"); |
109 |
minHeightText = createText(parent); |
110 |
|
111 |
new Label(parent, SWT.NONE).setText("Max Height (blank = unbounded)"); |
112 |
maxHeightText = createText(parent); |
113 |
|
114 |
new Label(parent, SWT.NONE).setText("Quantized Height (blank = none)"); |
115 |
quantizedHeightText = createText(parent); |
116 |
|
117 |
new Label(parent, SWT.NONE).setText("Fixed Area (blank = none"); |
118 |
fixedAreaText = createText(parent); |
119 |
|
120 |
|
121 |
GridLayoutFactory.fillDefaults().numColumns(2).margins(LayoutConstants.getMargins()).generateLayout(parent); |
122 |
} |
123 |
|
124 |
/** |
125 |
* |
126 |
*/ |
127 |
protected void applyPressed() { |
128 |
// Copy the values from the text boxes |
129 |
minWidth = getInt(minWidthText); |
130 |
maxWidth = getInt(maxWidthText); |
131 |
quantizedWidth = getInt(quantizedWidthText); |
132 |
minHeight = getInt(minHeightText); |
133 |
maxHeight = getInt(maxHeightText); |
134 |
quantizedHeight = getInt(quantizedHeightText); |
135 |
fixedArea = getInt(fixedAreaText); |
136 |
|
137 |
// Trigger a workbench layout |
138 |
updateLayout(); |
139 |
} |
140 |
|
141 |
/* (non-Javadoc) |
142 |
* @see org.eclipse.ui.ISizeProvider#getSizeFlags(boolean) |
143 |
*/ |
144 |
public int getSizeFlags(boolean width) { |
145 |
int flags = 0; |
146 |
if (width) { |
147 |
if (minWidth != ISizeProvider.INFINITE) { |
148 |
flags |= SWT.MIN; |
149 |
} |
150 |
if (maxWidth != ISizeProvider.INFINITE) { |
151 |
flags |= SWT.MAX; |
152 |
} |
153 |
if (quantizedWidth != ISizeProvider.INFINITE || fixedArea != ISizeProvider.INFINITE) { |
154 |
flags |= SWT.FILL; |
155 |
} |
156 |
if (fixedArea != ISizeProvider.INFINITE) { |
157 |
flags |= SWT.WRAP; |
158 |
} |
159 |
} else { |
160 |
if (minHeight != ISizeProvider.INFINITE) { |
161 |
flags |= SWT.MIN; |
162 |
} |
163 |
if (maxHeight != ISizeProvider.INFINITE) { |
164 |
flags |= SWT.MAX; |
165 |
} |
166 |
if (quantizedHeight != ISizeProvider.INFINITE || fixedArea != ISizeProvider.INFINITE) { |
167 |
flags |= SWT.FILL; |
168 |
} |
169 |
if (fixedArea != ISizeProvider.INFINITE) { |
170 |
flags |= SWT.WRAP; |
171 |
} |
172 |
} |
173 |
|
174 |
return flags; |
175 |
} |
176 |
|
177 |
/** |
178 |
* @param minWidth2 |
179 |
* @return |
180 |
*/ |
181 |
private int getInt(Text text) { |
182 |
if (text.getText().equals("")) { |
183 |
return ISizeProvider.INFINITE; |
184 |
} |
185 |
|
186 |
try { |
187 |
return Integer.parseInt(text.getText()); |
188 |
} catch (NumberFormatException e) { |
189 |
return ISizeProvider.INFINITE; |
190 |
} |
191 |
} |
192 |
|
193 |
/** |
194 |
* |
195 |
*/ |
196 |
protected void updateLayout() { |
197 |
firePropertyChange(IWorkbenchPartConstants.PROP_PREFERRED_SIZE); |
198 |
} |
199 |
|
200 |
/** |
201 |
* @param parent |
202 |
*/ |
203 |
private Text createText(Composite parent) { |
204 |
return new Text(parent, SWT.BORDER); |
205 |
} |
206 |
|
207 |
public void setFocus() { |
208 |
control.setFocus(); |
209 |
} |
210 |
|
211 |
/* (non-Javadoc) |
212 |
* @see org.eclipse.ui.ISizeProvider#computePreferredSize(boolean, int, int, int) |
213 |
*/ |
214 |
public int computePreferredSize(boolean width, int availableParallel, |
215 |
int availablePerpendicular, int preferredResult) { |
216 |
|
217 |
int area = fixedArea; |
218 |
int result = preferredResult; |
219 |
// If we're trying to maintain a fixed area, compute the preferred width in terms |
220 |
// of the available height or vise-versa. A view that is trying to exactly fit a wrapping |
221 |
// widget would use similar logic |
222 |
if (area != ISizeProvider.INFINITE) { |
223 |
if (availablePerpendicular == 0) { |
224 |
result = 0; |
225 |
} else { |
226 |
result = area / availablePerpendicular; |
227 |
} |
228 |
|
229 |
if (result < 30) { |
230 |
result = 30; |
231 |
} |
232 |
} |
233 |
|
234 |
// Get the user-entered minimum, maximum, and quantized sizes for this dimension |
235 |
int min; |
236 |
int max; |
237 |
int quant; |
238 |
|
239 |
if (width) { |
240 |
min = minWidth; |
241 |
max = maxWidth; |
242 |
quant = quantizedWidth; |
243 |
} else { |
244 |
min = minHeight; |
245 |
max = maxHeight; |
246 |
quant = quantizedHeight; |
247 |
} |
248 |
|
249 |
// If we're using quantized sizes, jump to the nearest multiple |
250 |
// of the quantized size |
251 |
if (quant != ISizeProvider.INFINITE && quant != 0) { |
252 |
result = Math.min(result + quant / 2, availableParallel); |
253 |
result = result - (result % quant); |
254 |
} |
255 |
|
256 |
// Ensure we go no smaller than the minimum size |
257 |
if (min != ISizeProvider.INFINITE && result < min) { |
258 |
result = min; |
259 |
} |
260 |
|
261 |
// Ensure we go no larger than the maximum size |
262 |
if (result > max) { |
263 |
result = max; |
264 |
} |
265 |
|
266 |
// Ensure that we don't use more than the available space |
267 |
if (result > availableParallel) { |
268 |
result = availableParallel; |
269 |
} |
270 |
|
271 |
if (result < 0) { |
272 |
result = 0; |
273 |
} |
274 |
|
275 |
return result; |
276 |
} |
277 |
|
278 |
} |