comparison jface/snippets/window/Snippet020CustomizedControlTooltips.d @ 146:7c4b76583cb8

Added new JFace snippets
author Frank Benoit <benoit@tionex.de>
date Mon, 11 Aug 2008 11:17:37 +0200
parents
children
comparison
equal deleted inserted replaced
145:161f7698cfb8 146:7c4b76583cb8
1 /*******************************************************************************
2 * Copyright (c) 2006 Tom Schindl 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 * Tom Schindl - initial API and implementation
10 *******************************************************************************/
11
12 module dwtx.jface.snippets.window.Snippet020CustomizedControlTooltips;
13
14
15 import dwtx.jface.resource.ImageDescriptor;
16 import dwtx.jface.resource.JFaceResources;
17 import dwtx.jface.util.Policy;
18 import dwtx.jface.window.DefaultToolTip;
19 import dwtx.jface.window.ToolTip;
20 import dwt.DWT;
21 import dwt.events.MouseAdapter;
22 import dwt.events.MouseEvent;
23 import dwt.events.SelectionAdapter;
24 import dwt.events.SelectionEvent;
25 import dwt.events.SelectionListener;
26 import dwt.graphics.Point;
27 import dwt.graphics.RGB;
28 import dwt.layout.FillLayout;
29 import dwt.layout.GridData;
30 import dwt.layout.GridLayout;
31 import dwt.layout.RowLayout;
32 import dwt.widgets.Button;
33 import dwt.widgets.Composite;
34 import dwt.widgets.Control;
35 import dwt.widgets.Display;
36 import dwt.widgets.Event;
37 import dwt.widgets.Label;
38 import dwt.widgets.Link;
39 import dwt.widgets.MessageBox;
40 import dwt.widgets.Shell;
41 import dwt.widgets.Text;
42
43 import dwt.dwthelper.utils;
44 version(JIVE) import jive.stacktrace;
45
46 /**
47 * Demonstrate usage of custom toolstips for controls
48 *
49 * @author Tom Schindl
50 *
51 */
52 public class Snippet020CustomizedControlTooltips {
53 protected class MyToolTip : ToolTip {
54
55 private Shell parentShell;
56
57 private String headerText = "ToolTip-Header";
58
59 public static const String HEADER_BG_COLOR = Policy.JFACE ~ ".TOOLTIP_HEAD_BG_COLOR";
60
61 public static const String HEADER_FG_COLOR = Policy.JFACE ~ ".TOOLTIP_HEAD_FG_COLOR";
62
63 public static const String HEADER_FONT = Policy.JFACE ~ ".TOOLTIP_HEAD_FONT";
64
65 public static const String HEADER_CLOSE_ICON = Policy.JFACE ~ ".TOOLTIP_CLOSE_ICON";
66 public static const String HEADER_HELP_ICON = Policy.JFACE ~ ".TOOLTIP_HELP_ICON";
67
68 public this(Control control) {
69 super(control);
70 this.parentShell = control.getShell();
71 }
72
73 protected Composite createToolTipContentArea(Event event,
74 Composite parent) {
75 Composite comp = new Composite(parent,DWT.NONE);
76
77 GridLayout gl = new GridLayout(1,false);
78 gl.marginBottom=0;
79 gl.marginTop=0;
80 gl.marginHeight=0;
81 gl.marginWidth=0;
82 gl.marginLeft=0;
83 gl.marginRight=0;
84 gl.verticalSpacing=1;
85 comp.setLayout(gl);
86
87 Composite topArea = new Composite(comp,DWT.NONE);
88 GridData data = new GridData(DWT.FILL,DWT.FILL,true,false);
89 data.widthHint=200;
90 topArea.setLayoutData(data);
91 topArea.setBackground(JFaceResources.getColorRegistry().get(HEADER_BG_COLOR));
92
93 gl = new GridLayout(2,false);
94 gl.marginBottom=2;
95 gl.marginTop=2;
96 gl.marginHeight=0;
97 gl.marginWidth=0;
98 gl.marginLeft=5;
99 gl.marginRight=2;
100
101 topArea.setLayout(gl);
102
103 Label l = new Label(topArea,DWT.NONE);
104 l.setText(headerText);
105 l.setBackground(JFaceResources.getColorRegistry().get(HEADER_BG_COLOR));
106 l.setFont(JFaceResources.getFontRegistry().get(HEADER_FONT));
107 l.setForeground(JFaceResources.getColorRegistry().get(HEADER_FG_COLOR));
108 l.setLayoutData(new GridData(GridData.FILL_BOTH));
109
110 Composite iconComp = new Composite(topArea,DWT.NONE);
111 iconComp.setLayoutData(new GridData());
112 iconComp.setLayout(new GridLayout(2,false));
113 iconComp.setBackground(JFaceResources.getColorRegistry().get(HEADER_BG_COLOR));
114
115 gl = new GridLayout(2,false);
116 gl.marginBottom=0;
117 gl.marginTop=0;
118 gl.marginHeight=0;
119 gl.marginWidth=0;
120 gl.marginLeft=0;
121 gl.marginRight=0;
122 iconComp.setLayout(gl);
123
124 Label helpIcon = new Label(iconComp,DWT.NONE);
125 helpIcon.setBackground(JFaceResources.getColorRegistry().get(HEADER_BG_COLOR));
126 helpIcon.setImage(JFaceResources.getImage(HEADER_HELP_ICON));
127 helpIcon.addMouseListener(new class MouseAdapter {
128
129 public void mouseDown(MouseEvent e) {
130 hide();
131 openHelp();
132 }
133 });
134
135
136 Label closeIcon = new Label(iconComp,DWT.NONE);
137 closeIcon.setBackground(JFaceResources.getColorRegistry().get(HEADER_BG_COLOR));
138 closeIcon.setImage(JFaceResources.getImage(HEADER_CLOSE_ICON));
139 closeIcon.addMouseListener(new class MouseAdapter {
140
141 public void mouseDown(MouseEvent e) {
142 parentShell.setFocus();
143 hide();
144 }
145 });
146
147 createContentArea(comp).setLayoutData(new GridData(GridData.FILL_BOTH));
148
149 return comp;
150 }
151
152 protected Composite createContentArea(Composite parent) {
153 return new Composite(parent,DWT.NONE);
154 }
155
156 protected void openHelp() {
157 parentShell.setFocus();
158 }
159 }
160
161 class MyToolTip2 : MyToolTip {
162 public this(Control control) {
163 super(control);
164 }
165 protected Composite createContentArea(Composite parent) {
166 Composite comp = super.createContentArea(parent);
167 comp.setBackground(parent.getDisplay().getSystemColor(DWT.COLOR_INFO_BACKGROUND));
168 FillLayout layout = new FillLayout();
169 layout.marginWidth=5;
170 comp.setLayout(layout);
171 Link l = new Link(comp,DWT.NONE);
172 l.setText("This a custom tooltip you can: \n- pop up any control you want\n- define delays\n - ... \nGo and get Eclipse M4 from <a>http://www.eclipse.org</a>");
173 l.setBackground(parent.getDisplay().getSystemColor(DWT.COLOR_INFO_BACKGROUND));
174 l.addSelectionListener(new class SelectionAdapter {
175 public void widgetSelected(SelectionEvent e) {
176 openURL();
177 }
178 });
179 return comp;
180 }
181
182 protected void openURL() {
183 MessageBox box = new MessageBox(parent,DWT.ICON_INFORMATION);
184 box.setText("Eclipse.org");
185 box.setMessage("Here is where we'd open the URL.");
186 box.open();
187 }
188
189 protected void openHelp() {
190 MessageBox box = new MessageBox(parent,DWT.ICON_INFORMATION);
191 box.setText("Info");
192 box.setMessage("Here is where we'd show some information.");
193 box.open();
194 }
195
196 };
197 Shell parent;
198 DefaultToolTip toolTipDelayed;
199
200 public this(Shell parent_) {
201 this.parent = parent_;
202 JFaceResources.getColorRegistry().put(MyToolTip.HEADER_BG_COLOR, new RGB(255,255,255));
203 JFaceResources.getFontRegistry().put(MyToolTip.HEADER_FONT, JFaceResources.getFontRegistry().getBold(JFaceResources.getDefaultFont().getFontData()[0].getName()).getFontData());
204
205
206 JFaceResources.getImageRegistry().put(MyToolTip.HEADER_CLOSE_ICON,ImageDescriptor.createFromFile(getImportData!("jface.snippets.showerr_tsk.gif")));
207 JFaceResources.getImageRegistry().put(MyToolTip.HEADER_HELP_ICON,ImageDescriptor.createFromFile(getImportData!("jface.snippets.linkto_help.gif")));
208
209 Text text = new Text(parent,DWT.BORDER);
210 text.setText("Hello World");
211
212 MyToolTip myTooltipLabel = new MyToolTip2(text);
213 myTooltipLabel.setShift(new Point(-5, -5));
214 myTooltipLabel.setHideOnMouseDown(false);
215 myTooltipLabel.activate();
216
217 text = new Text(parent,DWT.BORDER);
218 text.setText("Hello World");
219 DefaultToolTip toolTip = new DefaultToolTip(text);
220 toolTip.setText("Hello World\nHello World");
221 toolTip.setBackgroundColor(parent.getDisplay().getSystemColor(DWT.COLOR_RED));
222
223 Button b = new Button(parent,DWT.PUSH);
224 b.setText("Popup on press");
225
226 toolTipDelayed = new DefaultToolTip(b,ToolTip.RECREATE,true);
227 toolTipDelayed.setText("Hello World\nHello World");
228 toolTipDelayed.setBackgroundColor(parent.getDisplay().getSystemColor(DWT.COLOR_RED));
229 toolTipDelayed.setHideDelay(2000);
230
231 b.addSelectionListener(new class SelectionAdapter {
232 public void widgetSelected(SelectionEvent e) {
233 toolTipDelayed.show(new Point(0,0));
234 }
235 });
236
237
238 }
239
240 public static void main(String[] args) {
241 Display display = new Display();
242
243 Shell shell = new Shell(display);
244 shell.setLayout(new RowLayout());
245 new Snippet020CustomizedControlTooltips(shell);
246
247 shell.open();
248
249 while (!shell.isDisposed()) {
250 if (!display.readAndDispatch())
251 display.sleep();
252 }
253
254 display.dispose();
255 }
256 }
257
258 void main(){
259 Snippet020CustomizedControlTooltips.main(null);
260 }
261