Source code: com/port80/eclipse/util/UIUtil.java
1 package com.port80.eclipse.util;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.custom.CTabFolder;
5 import org.eclipse.swt.custom.CTabItem;
6 import org.eclipse.swt.custom.ScrolledComposite;
7 import org.eclipse.swt.events.MouseAdapter;
8 import org.eclipse.swt.events.MouseEvent;
9 import org.eclipse.swt.graphics.Color;
10 import org.eclipse.swt.graphics.Font;
11 import org.eclipse.swt.graphics.GC;
12 import org.eclipse.swt.graphics.Rectangle;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.layout.RowLayout;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Combo;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Label;
20 import org.eclipse.swt.widgets.List;
21 import org.eclipse.swt.widgets.Table;
22 import org.eclipse.swt.widgets.TableColumn;
23 import org.eclipse.swt.widgets.Text;
24
25 /**
26 * Static utilities class for this package.
27 *
28 * @author chrisl
29 */
30 public class UIUtil {
31
32 ////////////////////////////////////////////////////////////////////////
33
34 static Color fColorShadowDark = ColorFactory.getDefault().create(0);
35 static Color fColorShadowLight = ColorFactory.getDefault().create(0xffffff);
36
37 ////////////////////////////////////////////////////////////////////////
38
39 /** Create a CTabItem that contains a label control. */
40 public static Label createCTabLabel(CTabFolder parent, String name, String text, Color bg) {
41 CTabItem item = new CTabItem(parent, SWT.NONE);
42 item.setText(name);
43 item.setToolTipText(name);
44 Label label = new Label(parent, SWT.NONE);
45 label.setText(text);
46 if (bg != null)
47 label.setBackground(bg);
48 item.setControl(label);
49 return label;
50 }
51
52 public static Label createLabel(Composite parent, Color bg) {
53 Label label = new Label(parent, SWT.NONE);
54 if (bg != null)
55 label.setBackground(bg);
56 return label;
57 }
58
59 public static Label createLabel(Composite parent, Color bg, Color fg) {
60 Label label = new Label(parent, SWT.NONE);
61 if (bg != null)
62 label.setBackground(bg);
63 if (fg != null)
64 label.setForeground(fg);
65 return label;
66 }
67
68 public static Label createLabel(Composite parent, int align, Color bg, Color fg) {
69 Label label = new Label(parent, SWT.NONE);
70 label.setAlignment(align);
71 if (bg != null)
72 label.setBackground(bg);
73 if (fg != null)
74 label.setForeground(fg);
75 return label;
76 }
77
78 public static Label createLabel(Composite parent, int align, String text, Color bg, Color fg, Font font) {
79 Label label = new Label(parent, SWT.NONE);
80 label.setAlignment(align);
81 if (bg != null)
82 label.setBackground(bg);
83 if (fg != null)
84 label.setForeground(fg);
85 if (font != null)
86 label.setFont(font);
87 if (text != null)
88 label.setText(text);
89 return label;
90 }
91
92 /**
93 * Create a label with default GridLayoutData of fillCell(1,1,true,false).
94 */
95 public static Label createGridLabel(Composite parent, int align, String text, Color bg, Color fg, Font font) {
96 Label label = new Label(parent, SWT.NONE);
97 label.setAlignment(align);
98 if (bg != null)
99 label.setBackground(bg);
100 if (fg != null)
101 label.setForeground(fg);
102 if (font != null)
103 label.setFont(font);
104 if (text != null)
105 label.setText(text);
106 label.setLayoutData(fillCell(1, 1, true, false));
107 return label;
108 }
109
110 public static Label createGridLabel(
111 Composite parent,
112 int align,
113 String text,
114 Color bg,
115 Color fg,
116 Font font,
117 int vspan,
118 int hspan,
119 boolean hgrab,
120 boolean vgrab) {
121 Label label = new Label(parent, SWT.NONE);
122 label.setAlignment(align);
123 if (bg != null)
124 label.setBackground(bg);
125 if (fg != null)
126 label.setForeground(fg);
127 if (font != null)
128 label.setFont(font);
129 if (text != null)
130 label.setText(text);
131 label.setLayoutData(fillCell(vspan, hspan, hgrab, vgrab));
132 return label;
133 }
134
135 public static Label createGridLabel(
136 Composite parent,
137 int align,
138 String text,
139 Color bg,
140 Color fg,
141 Font font,
142 int vspan,
143 int hspan,
144 int halign,
145 int valign,
146 boolean hgrab,
147 boolean vgrab) {
148 Label label = new Label(parent, SWT.NONE);
149 label.setAlignment(align);
150 if (bg != null)
151 label.setBackground(bg);
152 if (fg != null)
153 label.setForeground(fg);
154 if (font != null)
155 label.setFont(font);
156 if (text != null)
157 label.setText(text);
158 label.setLayoutData(alignCell(vspan, hspan, halign, valign, hgrab, vgrab));
159 return label;
160 }
161
162 public static Label createLabel(
163 Composite parent,
164 int align,
165 Color bg,
166 Color fg,
167 int vspan,
168 int hspan,
169 boolean hgrab,
170 boolean vgrab,
171 Color border_color,
172 int xmargin,
173 int ymargin) {
174 //
175 Composite border = new Composite(parent, SWT.NONE);
176 border.setLayout(gridLayout(1, xmargin, ymargin, 0, 0));
177 border.setLayoutData(fillCell(vspan, hspan, hgrab, vgrab));
178 if (border_color != null)
179 border.setBackground(border_color);
180 //
181 Label label = new Label(border, SWT.NONE);
182 label.setLayoutData(fillCell(1, 1, true, true));
183 label.setAlignment(align);
184 if (bg != null)
185 label.setBackground(bg);
186 if (fg != null)
187 label.setForeground(fg);
188 return label;
189 }
190
191 ////////////////////////////////////////////////////////////////////////
192
193 public static Button createButton(Composite parent, int style, String text, Color bg, Font font) {
194 Button ret = new Button(parent, style);
195 if (bg != null)
196 ret.setBackground(bg);
197 if (font != null)
198 ret.setFont(font);
199 ret.setText(text);
200 ret.setLayoutData(fillCell(1, 1, true, false));
201 return ret;
202 }
203
204 public static Button createButton(
205 Composite parent,
206 int style,
207 String text,
208 Color bg,
209 Font font,
210 int vspan,
211 int hspan,
212 boolean hgrab,
213 boolean vgrab) {
214 Button ret = new Button(parent, style);
215 if (bg != null)
216 ret.setBackground(bg);
217 if (font != null)
218 ret.setFont(font);
219 ret.setText(text);
220 ret.setLayoutData(fillCell(vspan, hspan, hgrab, vgrab));
221 return ret;
222 }
223
224 public static Label labelButton(
225 Composite parent,
226 int style,
227 String text,
228 final Color bg,
229 Font font,
230 final Runnable action) {
231 Label ret = new Label(parent, style);
232 if (bg != null)
233 ret.setBackground(bg);
234 if (font != null)
235 ret.setFont(font);
236 ret.setText(text);
237 ret.addMouseListener(new MouseAdapter() {
238 public void mouseDown(MouseEvent e) {
239 Label label = (Label) e.widget;
240 Rectangle b = label.getBounds();
241 GC gc = new GC(label);
242 gc.fillRectangle(b);
243 gc.setForeground(fColorShadowDark);
244 gc.setLineWidth(1);
245 gc.drawRectangle(0, 0, b.width, b.height);
246 gc.setForeground(fColorShadowLight);
247 gc.drawRectangle(-1, -1, b.width, b.height);
248 gc.dispose();
249 }
250 public void mouseUp(MouseEvent e) {
251 Label label = (Label) e.widget;
252 label.redraw();
253 }
254 });
255 ret.addMouseListener(new MouseAdapter() {
256 public void mouseUp(MouseEvent e) {
257 Label button = (Label) e.widget;
258 Rectangle bounds = button.getBounds();
259 if (!bounds.contains(e.x + bounds.x, e.y + bounds.y))
260 return;
261 action.run();
262 }
263 });
264 return ret;
265 }
266
267 ////////////////////////////////////////////////////////////////////////
268
269 /**
270 * Create a label and a single line text control on a GridLayout row with given layout data.
271 */
272 public static Text labelText(
273 Composite parent,
274 String label,
275 String text,
276 Color labelbg,
277 Color textbg,
278 int labelspan,
279 int textspan,
280 boolean hgrab) {
281 Label l = new Label(parent, SWT.NONE);
282 l.setBackground(labelbg);
283 l.setText(label);
284 Text t = new Text(parent, SWT.SINGLE);
285 t.setText(text);
286 t.setBackground(textbg);
287 l.setLayoutData(fillCell(1, labelspan, false, false));
288 t.setLayoutData(fillCell(1, textspan, hgrab, false));
289 return t;
290 }
291
292 ////////////////////////////////////////////////////////////////////////
293
294 public static void createSep(Composite parent, int style, int span, int size, Color bg) {
295 boolean vertical = (style & SWT.VERTICAL) != 0;
296 int vspan = (vertical ? span : 1);
297 int hspan = (vertical ? 1 : span);
298 Label top = new Label(parent, SWT.NONE);
299 if (vertical)
300 top.setLayoutData(fillCell(vspan, hspan, size, 0, false, true));
301 else
302 top.setLayoutData(fillCell(vspan, hspan, 0, size, true, false));
303 if (bg != null)
304 top.setBackground(bg);
305 }
306
307 public static void createSep(
308 Composite parent,
309 int style,
310 int span,
311 int topmargin,
312 int bottommargin,
313 Color bg) {
314 int orientation = style & (SWT.HORIZONTAL | SWT.VERTICAL);
315 boolean vertical = (style & SWT.VERTICAL) != 0;
316 int vspan = (vertical ? span : 1);
317 int hspan = (vertical ? 1 : span);
318 Composite box = createBox(parent, vspan, hspan, 1, 0, 0, bg);
319 Label top = new Label(box, SWT.NONE);
320 Label sep = new Label(box, SWT.SEPARATOR | orientation);
321 Label bottom = new Label(box, SWT.NONE);
322 if (bg != null) {
323 top.setBackground(bg);
324 sep.setBackground(bg);
325 bottom.setBackground(bg);
326 }
327 if (vertical) {
328 top.setLayoutData(fillCell(vspan, hspan, topmargin, 0, false, true));
329 sep.setLayoutData(fillCell(vspan, hspan, false, true));
330 bottom.setLayoutData(fillCell(vspan, hspan, bottommargin, 0, false, true));
331 } else {
332 top.setLayoutData(fillCell(vspan, hspan, 0, topmargin, true, false));
333 sep.setLayoutData(fillCell(vspan, hspan, true, false));
334 bottom.setLayoutData(fillCell(vspan, hspan, 0, bottommargin, true, false));
335 }
336 }
337
338 ////////////////////////////////////////////////////////////////////////
339
340 /**
341 * Create a standard composite box, typically for enclosing other Controls.
342 */
343 public static Composite createBox(
344 Composite parent,
345 int vspan,
346 int hspan,
347 int columns,
348 int xmargin,
349 int ymargin,
350 Color bg) {
351 Composite ret = new Composite(parent, SWT.NONE);
352 ret.setLayoutData(fillCell(vspan, hspan, true, false));
353 ret.setLayout(gridLayout(columns, xmargin, ymargin, 0, 0));
354 if (bg != null)
355 ret.setBackground(bg);
356 return ret;
357 }
358
359 /**
360 * Create a composite box with grid layout but no LayoutData.
361 */
362 public static Composite createBox(
363 Composite parent,
364 Color bg,
365 int columns,
366 int xmargin,
367 int ymargin,
368 int xspacing,
369 int yspacing,
370 boolean equalwidth) {
371 Composite ret = new Composite(parent, SWT.NONE);
372 ret.setLayout(gridLayout(columns, xmargin, ymargin, xspacing, yspacing, equalwidth));
373 if (bg != null)
374 ret.setBackground(bg);
375 return ret;
376 }
377
378 public static Composite createBox(
379 Composite parent,
380 int vspan,
381 int hspan,
382 boolean hgrab,
383 boolean vgrab,
384 int columns,
385 int xmargin,
386 int ymargin,
387 int xspacing,
388 int yspacing,
389 Color bg) {
390 Composite ret = new Composite(parent, SWT.NONE);
391 ret.setLayoutData(fillCell(vspan, hspan, hgrab, vgrab));
392 ret.setLayout(gridLayout(columns, xmargin, ymargin, xspacing, yspacing));
393 if (bg != null)
394 ret.setBackground(bg);
395 return ret;
396 }
397
398 public static Composite createBox(
399 Composite parent,
400 int vspan,
401 int hspan,
402 boolean hgrab,
403 boolean vgrab,
404 int columns,
405 int xmargin,
406 int ymargin,
407 int xspacing,
408 int yspacing,
409 boolean equalwidth,
410 Color bg) {
411 Composite ret = new Composite(parent, SWT.NONE);
412 ret.setLayoutData(fillCell(vspan, hspan, hgrab, vgrab));
413 ret.setLayout(gridLayout(columns, xmargin, ymargin, xspacing, yspacing, equalwidth));
414 if (bg != null)
415 ret.setBackground(bg);
416 return ret;
417 }
418
419 ////////////////////////////////////////////////////////////////////////
420
421 public static TableColumn tableColumn(Table parent, String header, int width) {
422 TableColumn ret = new TableColumn(parent, SWT.NONE);
423 ret.setText(header);
424 if (width >= 0)
425 ret.setWidth(width);
426 ret.setResizable(true);
427 return ret;
428 }
429
430 /**
431 * Create a Composite nested in a ScrolledComposite inside the given parent.
432 * FIXME:
433 * <li>ScrolledComposite have predefined SHADOW_IN border.</li>
434 */
435 public static Composite scrolledComposiste(Composite parent, CTabItem item) {
436 ScrolledComposite scrolled = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
437 scrolled.setBackground(parent.getBackground());
438 item.setControl(scrolled);
439 //scrolled.setExpandHorizontal(true);
440 //scrolled.setExpandVertical(true);
441 Composite top = new Composite(scrolled, SWT.NONE);
442 scrolled.setContent(top);
443 scrolled.getVerticalBar().setEnabled(true);
444 scrolled.getHorizontalBar().setEnabled(false);
445 // This is required to display the ScrollComposite when top contents are initialized.
446 //top.setSize(top.computeSize(SWT.DEFAULT, SWT.DEFAULT));
447 return top;
448 }
449
450 public static Combo createCombo(Composite parent, int style, Color bg, Font font, String[] items) {
451 Combo ret = new Combo(parent, style);
452 if (bg != null)
453 ret.setBackground(bg);
454 if (font != null)
455 ret.setFont(font);
456 if (items != null && items.length > 0) {
457 ret.setItems(items);
458 ret.select(0);
459 }
460 return ret;
461 }
462
463 public static List createList(Composite parent, int style, Color bg, Font font, String[] items) {
464 List ret = new List(parent, style);
465 if (bg != null)
466 ret.setBackground(bg);
467 if (font != null)
468 ret.setFont(font);
469 if (items != null && items.length > 0) {
470 ret.setItems(items);
471 ret.select(0);
472 }
473 return ret;
474 }
475
476 public static Table createTable(
477 Composite parent,
478 int style,
479 String[] headers,
480 int[] widths,
481 Color bg,
482 Font font) {
483 Table ret = new Table(parent, style);
484 if (bg != null)
485 ret.setBackground(bg);
486 if (font != null)
487 ret.setFont(font);
488 if (headers != null) {
489 for (int i = 0; i < headers.length; ++i) {
490 UIUtil.tableColumn(ret, headers[i], (widths == null) ? -1 : widths[i]);
491 }
492 ret.setHeaderVisible(true);
493 ret.setLinesVisible(true);
494 }
495 return ret;
496 }
497
498 ////////////////////////////////////////////////////////////////////////
499
500 /**
501 * @return A RowLayout with tight margins and spacing.
502 */
503 public static RowLayout rowLayout(int vertical, int xmargin, int ymargin, int spacing) {
504 RowLayout ret = new RowLayout();
505 ret.type = (vertical & SWT.VERTICAL) == 0 ? SWT.HORIZONTAL : SWT.VERTICAL;
506 ret.pack = false;
507 ret.wrap = false;
508 ret.marginBottom = ymargin;
509 ret.marginTop = ymargin;
510 ret.marginLeft = xmargin;
511 ret.marginRight = xmargin;
512 ret.spacing = spacing;
513 return ret;
514 }
515
516 /**
517 * @return A RowLayout with tight margins and spacing.
518 */
519 public static RowLayout compactRow() {
520 RowLayout ret = new RowLayout();
521 ret.pack = true;
522 ret.wrap = true;
523 ret.marginBottom = 2;
524 ret.marginTop = 2;
525 ret.marginLeft = 2;
526 ret.marginRight = 2;
527 ret.spacing = 0;
528 return ret;
529 }
530
531 /**
532 * @return A RowLayout with tight margins and spacing.
533 */
534 public static RowLayout compactVerticalRow() {
535 RowLayout ret = new RowLayout();
536 ret.type = SWT.VERTICAL;
537 ret.pack = true;
538 ret.wrap = false;
539 ret.marginBottom = 2;
540 ret.marginTop = 2;
541 ret.marginLeft = 2;
542 ret.marginRight = 2;
543 ret.spacing = 0;
544 return ret;
545 }
546
547 ////////////////////////////////////////////////////////////////////////
548
549 public static GridLayout gridLayout(int columns, int xmargin, int ymargin, int xspacing, int yspacing) {
550 GridLayout ret = new GridLayout(columns, false);
551 ret.marginWidth = xmargin;
552 ret.marginHeight = ymargin;
553 ret.horizontalSpacing = xspacing;
554 ret.verticalSpacing = yspacing;
555 return ret;
556 }
557
558 public static GridLayout gridLayout(
559 int columns,
560 int xmargin,
561 int ymargin,
562 int xspacing,
563 int yspacing,
564 boolean equalwidth) {
565 GridLayout ret = new GridLayout(columns, equalwidth);
566 ret.marginWidth = xmargin;
567 ret.marginHeight = ymargin;
568 ret.horizontalSpacing = xspacing;
569 ret.verticalSpacing = yspacing;
570 return ret;
571 }
572
573 public static GridLayout compactGrid(int columns) {
574 GridLayout ret = new GridLayout(columns, false);
575 ret.marginHeight = 2;
576 ret.marginWidth = 2;
577 ret.horizontalSpacing = 0;
578 ret.verticalSpacing = 1;
579 return ret;
580 }
581
582 ////////////////////////////////////////////////////////////////////////
583
584 /**
585 * Grid layout cell configuration with default h/v FILL alignment.
586 */
587 private static GridData fillCell(int vspan, int hspan) {
588 GridData ret = new GridData();
589 ret.verticalSpan = vspan;
590 ret.horizontalSpan = hspan;
591 ret.horizontalAlignment = GridData.FILL;
592 ret.verticalAlignment = GridData.FILL;
593 return ret;
594 }
595
596 /**
597 * Grid layout cell configuration with default h/v FILL alignment.
598 */
599 public static GridData fillCell(int vspan, int hspan, boolean hgrab, boolean vgrab) {
600 GridData ret = new GridData();
601 ret.verticalSpan = vspan;
602 ret.horizontalSpan = hspan;
603 ret.grabExcessHorizontalSpace = hgrab;
604 ret.grabExcessVerticalSpace = vgrab;
605 ret.horizontalAlignment = GridData.FILL;
606 ret.verticalAlignment = GridData.FILL;
607 return ret;
608 }
609
610 /**
611 * Grid layout cell configuration with default h/v FILL alignment.
612 */
613 public static GridData fillCell(int vspan, int hspan, int w, int h, boolean hgrab, boolean vgrab) {
614 GridData ret = new GridData();
615 ret.verticalSpan = vspan;
616 ret.horizontalSpan = hspan;
617 if (w > 0)
618 ret.widthHint = w;
619 if (h > 0)
620 ret.heightHint = h;
621 ret.grabExcessHorizontalSpace = hgrab;
622 ret.grabExcessVerticalSpace = vgrab;
623 ret.horizontalAlignment = GridData.FILL;
624 ret.verticalAlignment = GridData.FILL;
625 return ret;
626 }
627
628 /**
629 * Grid layout cell configuration.
630 * @param halign GridData.BEGINNING|CENTER|END|FILL
631 * @param valign GridData.BEGINNING|CENTER|END|FILL
632 */
633 public static GridData alignCell(int vspan, int hspan, int halign, int valign, boolean hgrab, boolean vgrab) {
634 GridData ret = new GridData();
635 ret.verticalSpan = vspan;
636 ret.horizontalSpan = hspan;
637 if (halign > 0)
638 ret.horizontalAlignment = halign;
639 if (valign > 0)
640 ret.verticalAlignment = valign;
641 ret.grabExcessHorizontalSpace = hgrab;
642 ret.grabExcessVerticalSpace = vgrab;
643 return ret;
644 }
645
646 public static GridData clone(GridData data) {
647 GridData ret = new GridData();
648 ret.horizontalSpan = data.horizontalSpan;
649 ret.verticalSpan = data.verticalSpan;
650 ret.verticalAlignment = data.verticalAlignment;
651 ret.horizontalAlignment = data.horizontalAlignment;
652 ret.horizontalIndent = data.horizontalIndent;
653 ret.widthHint = data.widthHint;
654 ret.heightHint = data.heightHint;
655 ret.grabExcessHorizontalSpace = data.grabExcessHorizontalSpace;
656 ret.grabExcessVerticalSpace = data.grabExcessVerticalSpace;
657 return ret;
658 }
659
660 ////////////////////////////////////////////////////////////////////////
661
662 }