KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > tracelog > ui > ShellOptionLogConfigColorChooser


1 package net.sourceforge.tracelog.ui;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map.Entry;
6
7 import net.sourceforge.tracelog.listeners.GenericListener;
8
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.MouseEvent;
11 import org.eclipse.swt.graphics.Cursor;
12 import org.eclipse.swt.graphics.Point;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.swt.widgets.Label;
17 import org.eclipse.swt.widgets.Shell;
18
19 public class ShellOptionLogConfigColorChooser extends AbstractWidget {
20     private static final HashMap JavaDoc<Integer JavaDoc, String JavaDoc> FONT_COLORS = new HashMap JavaDoc<Integer JavaDoc, String JavaDoc>();
21
22     static {
23         FONT_COLORS.put(SWT.COLOR_BLACK, "Black");
24         FONT_COLORS.put(SWT.COLOR_BLUE, "Blue");
25         FONT_COLORS.put(SWT.COLOR_CYAN, "Cyan");
26         FONT_COLORS.put(SWT.COLOR_DARK_BLUE, "Dark Blue");
27         FONT_COLORS.put(SWT.COLOR_DARK_CYAN, "Dark Cyan");
28         FONT_COLORS.put(SWT.COLOR_DARK_GRAY, "Dark Gray");
29         FONT_COLORS.put(SWT.COLOR_DARK_GREEN, "Dark Green");
30         FONT_COLORS.put(SWT.COLOR_DARK_MAGENTA, "Dark Magenta");
31         FONT_COLORS.put(SWT.COLOR_DARK_RED, "Dark Red");
32         FONT_COLORS.put(SWT.COLOR_DARK_YELLOW, "Dark Yellow");
33         FONT_COLORS.put(SWT.COLOR_GRAY, "Gray");
34         FONT_COLORS.put(SWT.COLOR_GREEN, "Green");
35         FONT_COLORS.put(SWT.COLOR_MAGENTA, "Magenta");
36         FONT_COLORS.put(SWT.COLOR_RED, "Red");
37         FONT_COLORS.put(SWT.COLOR_YELLOW, "Yellow");
38         FONT_COLORS.put(SWT.COLOR_WHITE, "White");
39     }
40
41     private Shell colorChooser;
42
43     ShellOptionLogConfigColorChooser() {
44         super();
45     }
46
47     public void run() {
48     }
49
50     private Point getAbsoluteLocation(Control control) {
51
52         if (control instanceof Shell) {
53             return control.getLocation();
54         }
55
56         Point pt = getAbsoluteLocation(control.getParent());
57
58         return new Point(control.getLocation().x + pt.x, control.getLocation().y + pt.y);
59     }
60
61     public void displayColorChooser(final Label text) {
62         // a color chooser already opened, then close it first
63
if (colorChooser != null && !colorChooser.isDisposed()) {
64             colorChooser.dispose();
65         }
66
67         // colorChooser = new Shell(parentShell, SWT.MODELESS | SWT.TOOL);
68
colorChooser = new Shell(parentShell, SWT.TOOL | SWT.SYSTEM_MODAL);
69         GridLayout gridLayout = new GridLayout(4, true);
70         gridLayout.marginHeight = 0;
71         gridLayout.marginWidth = 0;
72         gridLayout.horizontalSpacing = 0;
73         gridLayout.verticalSpacing = 0;
74         colorChooser.setLayout(gridLayout);
75         colorChooser.setSize(150, 150);
76
77         Point pt = getAbsoluteLocation(text);
78         colorChooser.setLocation(pt.x + 20, pt.y + 20);
79
80         for (Iterator JavaDoc<Entry<Integer JavaDoc, String JavaDoc>> ite = FONT_COLORS.entrySet().iterator(); ite.hasNext();) {
81             final Entry<Integer JavaDoc, String JavaDoc> entry = ite.next();
82
83             Label label = new Label(colorChooser, SWT.BORDER);
84             label.setCursor(new Cursor(display, SWT.CURSOR_HAND));
85             label.setLayoutData(new GridData(GridData.FILL_BOTH));
86             label.setBackground(display.getSystemColor(entry.getKey()));
87             label.setToolTipText(entry.getValue());
88
89             label.addMouseListener(new GenericListener() {
90                 public void mouseUp(MouseEvent e) {
91                     text.setBackground(display.getSystemColor(entry.getKey()));
92                     text.setData(entry.getKey());
93                     colorChooser.dispose();
94                 }
95             });
96         }
97
98         colorChooser.open();
99     }
100 }
101
Popular Tags