KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > custom > ViewForm


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.custom;
12
13
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.widgets.*;
16 import org.eclipse.swt.*;
17
18 /**
19  * Instances of this class implement a Composite that positions and sizes
20  * children and allows programmatic control of layout and border parameters.
21  * ViewForm is used in the workbench to lay out a view's label/menu/toolbar
22  * local bar.
23  * <p>
24  * Note that although this class is a subclass of <code>Composite</code>,
25  * it does not make sense to set a layout on it.
26  * </p><p>
27  * <dl>
28  * <dt><b>Styles:</b></dt>
29  * <dd>BORDER, FLAT</dd>
30  * <dt><b>Events:</b></dt>
31  * <dd>(None)</dd>
32  * </dl>
33  * <p>
34  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
35  * </p>
36  */

37
38 public class ViewForm extends Composite {
39
40     /**
41      * marginWidth specifies the number of pixels of horizontal margin
42      * that will be placed along the left and right edges of the form.
43      *
44      * The default value is 0.
45      */

46     public int marginWidth = 0;
47     /**
48      * marginHeight specifies the number of pixels of vertical margin
49      * that will be placed along the top and bottom edges of the form.
50      *
51      * The default value is 0.
52      */

53     public int marginHeight = 0;
54     /**
55      * horizontalSpacing specifies the number of pixels between the right
56      * edge of one cell and the left edge of its neighbouring cell to
57      * the right.
58      *
59      * The default value is 1.
60      */

61     public int horizontalSpacing = 1;
62     /**
63      * verticalSpacing specifies the number of pixels between the bottom
64      * edge of one cell and the top edge of its neighbouring cell underneath.
65      *
66      * The default value is 1.
67      */

68     public int verticalSpacing = 1;
69     
70     /**
71      * Color of innermost line of drop shadow border.
72      *
73      * NOTE This field is badly named and can not be fixed for backwards compatibility.
74      * It should be capitalized.
75      *
76      * @deprecated
77      */

78     public static RGB borderInsideRGB = new RGB (132, 130, 132);
79     /**
80      * Color of middle line of drop shadow border.
81      *
82      * NOTE This field is badly named and can not be fixed for backwards compatibility.
83      * It should be capitalized.
84      *
85      * @deprecated
86      */

87     public static RGB borderMiddleRGB = new RGB (143, 141, 138);
88     /**
89      * Color of outermost line of drop shadow border.
90      *
91      * NOTE This field is badly named and can not be fixed for backwards compatibility.
92      * It should be capitalized.
93      *
94      * @deprecated
95      */

96     public static RGB borderOutsideRGB = new RGB (171, 168, 165);
97     
98     // SWT widgets
99
Control topLeft;
100     Control topCenter;
101     Control topRight;
102     Control content;
103     
104     // Configuration and state info
105
boolean separateTopCenter = false;
106     boolean showBorder = false;
107     
108     int separator = -1;
109     int borderTop = 0;
110     int borderBottom = 0;
111     int borderLeft = 0;
112     int borderRight = 0;
113     int highlight = 0;
114     Point oldSize;
115     
116     Color selectionBackground;
117     
118     static final int OFFSCREEN = -200;
119     static final int BORDER1_COLOR = SWT.COLOR_WIDGET_NORMAL_SHADOW;
120     static final int SELECTION_BACKGROUND = SWT.COLOR_LIST_BACKGROUND;
121 /**
122  * Constructs a new instance of this class given its parent
123  * and a style value describing its behavior and appearance.
124  * <p>
125  * The style value is either one of the style constants defined in
126  * class <code>SWT</code> which is applicable to instances of this
127  * class, or must be built by <em>bitwise OR</em>'ing together
128  * (that is, using the <code>int</code> "|" operator) two or more
129  * of those <code>SWT</code> style constants. The class description
130  * lists the style constants that are applicable to the class.
131  * Style bits are also inherited from superclasses.
132  * </p>
133  *
134  * @param parent a widget which will be the parent of the new instance (cannot be null)
135  * @param style the style of widget to construct
136  *
137  * @exception IllegalArgumentException <ul>
138  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
139  * </ul>
140  * @exception SWTException <ul>
141  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
142  * </ul>
143  *
144  * @see SWT#BORDER
145  * @see SWT#FLAT
146  * @see #getStyle()
147  */

148 public ViewForm(Composite parent, int style) {
149     super(parent, checkStyle(style));
150     super.setLayout(new ViewFormLayout());
151     
152     setBorderVisible((style & SWT.BORDER) != 0);
153     
154     Listener listener = new Listener() {
155         public void handleEvent(Event e) {
156             switch (e.type) {
157                 case SWT.Dispose: onDispose(); break;
158                 case SWT.Paint: onPaint(e.gc); break;
159                 case SWT.Resize: onResize(); break;
160             }
161         }
162     };
163     
164     int[] events = new int[] {SWT.Dispose, SWT.Paint, SWT.Resize};
165     
166     for (int i = 0; i < events.length; i++) {
167         addListener(events[i], listener);
168     }
169 }
170
171 static int checkStyle (int style) {
172     int mask = SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;
173     return style & mask | SWT.NO_REDRAW_RESIZE;
174 }
175
176 //protected void checkSubclass () {
177
// String name = getClass().getName ();
178
// String validName = ViewForm.class.getName();
179
// if (!validName.equals(name)) {
180
// SWT.error (SWT.ERROR_INVALID_SUBCLASS);
181
// }
182
//}
183

184 public Rectangle computeTrim (int x, int y, int width, int height) {
185     checkWidget ();
186     int trimX = x - borderLeft - highlight;
187     int trimY = y - borderTop - highlight;
188     int trimWidth = width + borderLeft + borderRight + 2*highlight;
189     int trimHeight = height + borderTop + borderBottom + 2*highlight;
190     return new Rectangle(trimX, trimY, trimWidth, trimHeight);
191 }
192 public Rectangle getClientArea() {
193     checkWidget();
194     Rectangle clientArea = super.getClientArea();
195     clientArea.x += borderLeft;
196     clientArea.y += borderTop;
197     clientArea.width -= borderLeft + borderRight;
198     clientArea.height -= borderTop + borderBottom;
199     return clientArea;
200 }
201 /**
202 * Returns the content area.
203 *
204 * @return the control in the content area of the pane or null
205 */

206 public Control getContent() {
207     //checkWidget();
208
return content;
209 }
210 /**
211 * Returns Control that appears in the top center of the pane.
212 * Typically this is a toolbar.
213 *
214 * @return the control in the top center of the pane or null
215 */

216 public Control getTopCenter() {
217     //checkWidget();
218
return topCenter;
219 }
220 /**
221 * Returns the Control that appears in the top left corner of the pane.
222 * Typically this is a label such as CLabel.
223 *
224 * @return the control in the top left corner of the pane or null
225 */

226 public Control getTopLeft() {
227     //checkWidget();
228
return topLeft;
229 }
230 /**
231 * Returns the control in the top right corner of the pane.
232 * Typically this is a Close button or a composite with a Menu and Close button.
233 *
234 * @return the control in the top right corner of the pane or null
235 */

236 public Control getTopRight() {
237     //checkWidget();
238
return topRight;
239 }
240 void onDispose() {
241     topLeft = null;
242     topCenter = null;
243     topRight = null;
244     content = null;
245     oldSize = null;
246     selectionBackground = null;
247 }
248 void onPaint(GC gc) {
249     Color gcForeground = gc.getForeground();
250     Point size = getSize();
251     Color border = getDisplay().getSystemColor(BORDER1_COLOR);
252     if (showBorder) {
253         gc.setForeground(border);
254         gc.drawRectangle(0, 0, size.x - 1, size.y - 1);
255         if (highlight > 0) {
256             int x1 = 1;
257             int y1 = 1;
258             int x2 = size.x - 1;
259             int y2 = size.y - 1;
260             int[] shape = new int[] {x1,y1, x2,y1, x2,y2, x1,y2, x1,y1+highlight,
261                                x1+highlight,y1+highlight, x1+highlight,y2-highlight,
262                                x2-highlight,y2-highlight, x2-highlight,y1+highlight, x1,y1+highlight};
263             Color highlightColor = getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION);
264             gc.setBackground(highlightColor);
265             gc.fillPolygon(shape);
266         }
267     }
268     if (separator > -1) {
269         gc.setForeground(border);
270         gc.drawLine(borderLeft + highlight, separator, size.x - borderLeft - borderRight - highlight, separator);
271     }
272     gc.setForeground(gcForeground);
273 }
274 void onResize() {
275     Point size = getSize();
276     if (oldSize == null || oldSize.x == 0 || oldSize.y == 0) {
277         redraw();
278     } else {
279         int width = 0;
280         if (oldSize.x < size.x) {
281             width = size.x - oldSize.x + borderRight + highlight;
282         } else if (oldSize.x > size.x) {
283             width = borderRight + highlight;
284         }
285         redraw(size.x - width, 0, width, size.y, false);
286         
287         int height = 0;
288         if (oldSize.y < size.y) {
289             height = size.y - oldSize.y + borderBottom + highlight;
290         }
291         if (oldSize.y > size.y) {
292             height = borderBottom + highlight;
293         }
294         redraw(0, size.y - height, size.x, height, false);
295     }
296     oldSize = size;
297 }
298 /**
299 * Sets the content.
300 * Setting the content to null will remove it from
301 * the pane - however, the creator of the content must dispose of the content.
302 *
303 * @param content the control to be displayed in the content area or null
304 *
305 * @exception SWTException <ul>
306 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
307 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
308 * <li>ERROR_INVALID_ARGUMENT - if the control is not a child of this ViewForm</li>
309 * </ul>
310 */

311 public void setContent(Control content) {
312     checkWidget();
313     if (content != null && content.getParent() != this) {
314         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
315     }
316     if (this.content != null && !this.content.isDisposed()) {
317         this.content.setBounds(OFFSCREEN, OFFSCREEN, 0, 0);
318     }
319     this.content = content;
320     layout(false);
321 }
322 /**
323  * Sets the layout which is associated with the receiver to be
324  * the argument which may be null.
325  * <p>
326  * Note: No Layout can be set on this Control because it already
327  * manages the size and position of its children.
328  * </p>
329  *
330  * @param layout the receiver's new layout or null
331  *
332  * @exception SWTException <ul>
333  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
334  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
335  * </ul>
336  */

337 public void setLayout (Layout layout) {
338     checkWidget();
339     return;
340 }
341 void setSelectionBackground (Color color) {
342     checkWidget();
343     if (selectionBackground == color) return;
344     if (color == null) color = getDisplay().getSystemColor(SELECTION_BACKGROUND);
345     selectionBackground = color;
346     redraw();
347 }
348 /**
349 * Set the control that appears in the top center of the pane.
350 * Typically this is a toolbar.
351 * The topCenter is optional. Setting the topCenter to null will remove it from
352 * the pane - however, the creator of the topCenter must dispose of the topCenter.
353 *
354 * @param topCenter the control to be displayed in the top center or null
355 *
356 * @exception SWTException <ul>
357 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
358 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
359 * <li>ERROR_INVALID_ARGUMENT - if the control is not a child of this ViewForm</li>
360 * </ul>
361 */

362 public void setTopCenter(Control topCenter) {
363     checkWidget();
364     if (topCenter != null && topCenter.getParent() != this) {
365         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
366     }
367     if (this.topCenter != null && !this.topCenter.isDisposed()) {
368         Point size = this.topCenter.getSize();
369         this.topCenter.setLocation(OFFSCREEN - size.x, OFFSCREEN - size.y);
370     }
371     this.topCenter = topCenter;
372     layout(false);
373 }
374 /**
375 * Set the control that appears in the top left corner of the pane.
376 * Typically this is a label such as CLabel.
377 * The topLeft is optional. Setting the top left control to null will remove it from
378 * the pane - however, the creator of the control must dispose of the control.
379 *
380 * @param c the control to be displayed in the top left corner or null
381 *
382 * @exception SWTException <ul>
383 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
384 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
385 * <li>ERROR_INVALID_ARGUMENT - if the control is not a child of this ViewForm</li>
386 * </ul>
387 */

388 public void setTopLeft(Control c) {
389     checkWidget();
390     if (c != null && c.getParent() != this) {
391         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
392     }
393     if (this.topLeft != null && !this.topLeft.isDisposed()) {
394         Point size = this.topLeft.getSize();
395         this.topLeft.setLocation(OFFSCREEN - size.x, OFFSCREEN - size.y);
396     }
397     this.topLeft = c;
398     layout(false);
399 }
400 /**
401 * Set the control that appears in the top right corner of the pane.
402 * Typically this is a Close button or a composite with a Menu and Close button.
403 * The topRight is optional. Setting the top right control to null will remove it from
404 * the pane - however, the creator of the control must dispose of the control.
405 *
406 * @param c the control to be displayed in the top right corner or null
407 *
408 * @exception SWTException <ul>
409 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
410 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
411 * <li>ERROR_INVALID_ARGUMENT - if the control is not a child of this ViewForm</li>
412 * </ul>
413 */

414 public void setTopRight(Control c) {
415     checkWidget();
416     if (c != null && c.getParent() != this) {
417         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
418     }
419     if (this.topRight != null && !this.topRight.isDisposed()) {
420         Point size = this.topRight.getSize();
421         this.topRight.setLocation(OFFSCREEN - size.x, OFFSCREEN - size.y);
422     }
423     this.topRight = c;
424     layout(false);
425 }
426 /**
427 * Specify whether the border should be displayed or not.
428 *
429 * @param show true if the border should be displayed
430 *
431 * @exception SWTException <ul>
432 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
433 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
434 * </ul>
435 */

436 public void setBorderVisible(boolean show) {
437     checkWidget();
438     if (showBorder == show) return;
439     
440     showBorder = show;
441     if (showBorder) {
442         borderLeft = borderTop = borderRight = borderBottom = 1;
443         if ((getStyle() & SWT.FLAT)== 0) highlight = 2;
444     } else {
445         borderBottom = borderTop = borderLeft = borderRight = 0;
446         highlight = 0;
447     }
448     layout(false);
449     redraw();
450 }
451 /**
452 * If true, the topCenter will always appear on a separate line by itself, otherwise the
453 * topCenter will appear in the top row if there is room and will be moved to the second row if
454 * required.
455 *
456 * @param show true if the topCenter will always appear on a separate line by itself
457 *
458 * @exception SWTException <ul>
459 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
460 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
461 * </ul>
462 */

463 public void setTopCenterSeparate(boolean show) {
464     checkWidget();
465     separateTopCenter = show;
466     layout(false);
467 }
468 }
469
Popular Tags