]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2008, 2011 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.jdt.internal.ui.javaeditor.breadcrumb; | |
12 | ||
13 | import org.eclipse.swt.SWT; | |
14 | import org.eclipse.swt.accessibility.AccessibleAdapter; | |
15 | import org.eclipse.swt.accessibility.AccessibleEvent; | |
16 | import org.eclipse.swt.events.FocusEvent; | |
17 | import org.eclipse.swt.events.FocusListener; | |
18 | import org.eclipse.swt.events.KeyEvent; | |
19 | import org.eclipse.swt.events.KeyListener; | |
20 | import org.eclipse.swt.events.MenuDetectEvent; | |
21 | import org.eclipse.swt.events.MenuDetectListener; | |
22 | import org.eclipse.swt.events.MouseEvent; | |
23 | import org.eclipse.swt.events.MouseListener; | |
24 | import org.eclipse.swt.events.PaintEvent; | |
25 | import org.eclipse.swt.events.PaintListener; | |
26 | import org.eclipse.swt.events.TraverseEvent; | |
27 | import org.eclipse.swt.events.TraverseListener; | |
28 | import org.eclipse.swt.graphics.Color; | |
29 | import org.eclipse.swt.graphics.Image; | |
30 | import org.eclipse.swt.layout.GridData; | |
31 | import org.eclipse.swt.layout.GridLayout; | |
32 | import org.eclipse.swt.widgets.Composite; | |
33 | import org.eclipse.swt.widgets.Control; | |
34 | import org.eclipse.swt.widgets.Display; | |
35 | import org.eclipse.swt.widgets.Label; | |
36 | import org.eclipse.swt.widgets.Shell; | |
37 | ||
38 | ||
39 | /** | |
40 | * The label and icon part of the breadcrumb item. | |
41 | * | |
42 | * @since 3.4 | |
43 | */ | |
44 | class BreadcrumbItemDetails { | |
45 | ||
46 | private final Label fElementImage; | |
47 | private final Label fElementText; | |
48 | private final Composite fDetailComposite; | |
49 | private final BreadcrumbItem fParent; | |
50 | private final Composite fTextComposite; | |
51 | private final Composite fImageComposite; | |
52 | ||
53 | private boolean fTextVisible; | |
54 | private boolean fSelected; | |
55 | private boolean fHasFocus; | |
56 | ||
57 | ||
58 | public BreadcrumbItemDetails(BreadcrumbItem parent, Composite parentContainer) { | |
59 | fParent= parent; | |
60 | fTextVisible= true; | |
61 | ||
62 | fDetailComposite= new Composite(parentContainer, SWT.NONE); | |
63 | fDetailComposite.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); | |
64 | GridLayout layout= new GridLayout(2, false); | |
65 | layout.marginHeight= 0; | |
66 | layout.marginWidth= 0; | |
67 | layout.horizontalSpacing= 0; | |
68 | fDetailComposite.setLayout(layout); | |
69 | addElementListener(fDetailComposite); | |
70 | ||
71 | fImageComposite= new Composite(fDetailComposite, SWT.NONE); | |
72 | fImageComposite.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); | |
73 | layout= new GridLayout(1, false); | |
74 | layout.marginHeight= 1; | |
75 | layout.marginWidth= 2; | |
76 | fImageComposite.setLayout(layout); | |
77 | fImageComposite.addPaintListener(new PaintListener() { | |
78 | public void paintControl(PaintEvent e) { | |
79 | if (fHasFocus && !isTextVisible()) { | |
80 | e.gc.drawFocus(e.x, e.y, e.width, e.height); | |
81 | } | |
82 | } | |
83 | }); | |
84 | installFocusComposite(fImageComposite); | |
85 | addElementListener(fImageComposite); | |
86 | ||
87 | fElementImage= new Label(fImageComposite, SWT.NONE); | |
88 | GridData layoutData= new GridData(SWT.BEGINNING, SWT.CENTER, false, false); | |
89 | fElementImage.setLayoutData(layoutData); | |
90 | addElementListener(fElementImage); | |
91 | ||
92 | fTextComposite= new Composite(fDetailComposite, SWT.NONE); | |
93 | fTextComposite.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); | |
94 | layout= new GridLayout(1, false); | |
95 | layout.marginHeight= 2; | |
96 | layout.marginWidth= 2; | |
97 | fTextComposite.setLayout(layout); | |
98 | addElementListener(fTextComposite); | |
99 | fTextComposite.addPaintListener(new PaintListener() { | |
100 | public void paintControl(PaintEvent e) { | |
101 | if (fHasFocus && isTextVisible()) { | |
102 | e.gc.drawFocus(e.x, e.y, e.width, e.height); | |
103 | } | |
104 | } | |
105 | }); | |
106 | installFocusComposite(fTextComposite); | |
107 | ||
108 | fElementText= new Label(fTextComposite, SWT.NONE); | |
109 | layoutData= new GridData(SWT.BEGINNING, SWT.CENTER, false, false); | |
110 | fElementText.setLayoutData(layoutData); | |
111 | addElementListener(fElementText); | |
112 | ||
113 | fTextComposite.getAccessible().addAccessibleListener(new AccessibleAdapter() { | |
114 | @Override | |
115 | public void getName(AccessibleEvent e) { | |
116 | e.result= fElementText.getText(); | |
117 | } | |
118 | }); | |
119 | fImageComposite.getAccessible().addAccessibleListener(new AccessibleAdapter() { | |
120 | @Override | |
121 | public void getName(AccessibleEvent e) { | |
122 | e.result= fElementText.getText(); | |
123 | } | |
124 | }); | |
125 | ||
126 | fDetailComposite.setTabList(new Control[] { fTextComposite }); | |
127 | } | |
128 | ||
129 | /** | |
130 | * Returns whether this element has the keyboard focus. | |
131 | * | |
132 | * @return true if this element has the keyboard focus. | |
133 | */ | |
134 | public boolean hasFocus() { | |
135 | return fHasFocus; | |
136 | } | |
137 | ||
138 | /** | |
139 | * Sets the tool tip to the given text. | |
140 | * | |
141 | * @param text the tool tip | |
142 | */ | |
143 | public void setToolTip(String text) { | |
144 | if (isTextVisible()) { | |
145 | fElementText.getParent().setToolTipText(text); | |
146 | fElementText.setToolTipText(text); | |
147 | ||
148 | fElementImage.setToolTipText(text); | |
149 | } else { | |
150 | fElementText.getParent().setToolTipText(null); | |
151 | fElementText.setToolTipText(null); | |
152 | ||
153 | fElementImage.setToolTipText(text); | |
154 | } | |
155 | } | |
156 | ||
157 | /** | |
158 | * Sets the image to the given image. | |
159 | * | |
160 | * @param image the image to use | |
161 | */ | |
162 | public void setImage(Image image) { | |
163 | if (image != fElementImage.getImage()) { | |
164 | fElementImage.setImage(image); | |
165 | } | |
166 | } | |
167 | ||
168 | /** | |
169 | * Sets the text to the given text. | |
170 | * | |
171 | * @param text the text to use | |
172 | */ | |
173 | public void setText(String text) { | |
174 | if (text == null) { | |
175 | text= ""; //$NON-NLS-1$ | |
176 | } | |
177 | if (!text.equals(fElementText.getText())) { | |
178 | fElementText.setText(text); | |
179 | } | |
180 | } | |
181 | ||
182 | /** | |
183 | * Returns the width of this element. | |
184 | * | |
185 | * @return current width of this element | |
186 | */ | |
187 | public int getWidth() { | |
188 | int result= 2; | |
189 | ||
190 | if (fElementImage.getImage() != null) | |
191 | result+= fElementImage.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; | |
192 | ||
193 | if (fTextVisible && fElementText.getText().length() > 0) | |
194 | result+= fElementText.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; | |
195 | ||
196 | return result; | |
197 | } | |
198 | ||
199 | public void setTextVisible(boolean enabled) { | |
200 | if (fTextVisible == enabled) | |
201 | return; | |
202 | ||
203 | fTextVisible= enabled; | |
204 | ||
205 | GridData data= (GridData) fTextComposite.getLayoutData(); | |
206 | data.exclude= !enabled; | |
207 | fTextComposite.setVisible(enabled); | |
208 | ||
209 | if (fTextVisible) { | |
210 | fDetailComposite.setTabList(new Control[] { fTextComposite }); | |
211 | } else { | |
212 | fDetailComposite.setTabList(new Control[] { fImageComposite }); | |
213 | } | |
214 | ||
215 | if (fHasFocus) { | |
216 | if (isTextVisible()) { | |
217 | fTextComposite.setFocus(); | |
218 | } else { | |
219 | fImageComposite.setFocus(); | |
220 | } | |
221 | } | |
222 | updateSelection(); | |
223 | } | |
224 | ||
225 | /** | |
226 | * Tells whether this item shows a text or only an image. | |
227 | * | |
228 | * @return <code>true</code> if it shows a text and an image, false if it only shows the image | |
229 | */ | |
230 | public boolean isTextVisible() { | |
231 | return fTextVisible; | |
232 | } | |
233 | ||
234 | /** | |
235 | * Sets whether details should be shown. | |
236 | * | |
237 | * @param visible <code>true</code> if details should be shown | |
238 | */ | |
239 | public void setVisible(boolean visible) { | |
240 | fDetailComposite.setVisible(visible); | |
241 | ||
242 | GridData data= (GridData) fDetailComposite.getLayoutData(); | |
243 | data.exclude= !visible; | |
244 | } | |
245 | ||
246 | public void setSelected(boolean selected) { | |
247 | if (selected == fSelected) | |
248 | return; | |
249 | ||
250 | fSelected= selected; | |
251 | if (!fSelected) | |
252 | fHasFocus= false; | |
253 | ||
254 | updateSelection(); | |
255 | } | |
256 | ||
257 | public void setFocus(boolean enabled) { | |
258 | if (enabled == fHasFocus) | |
259 | return; | |
260 | ||
261 | fHasFocus= enabled; | |
262 | if (fHasFocus) { | |
263 | if (isTextVisible()) { | |
264 | fTextComposite.setFocus(); | |
265 | } else { | |
266 | fImageComposite.setFocus(); | |
267 | } | |
268 | } | |
269 | updateSelection(); | |
270 | } | |
271 | ||
272 | private void updateSelection() { | |
273 | Color background; | |
274 | Color foreground; | |
275 | ||
276 | if (fSelected && fHasFocus) { | |
277 | background= Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION); | |
278 | foreground= Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT); | |
279 | } else { | |
280 | foreground= null; | |
281 | background= null; | |
282 | } | |
283 | ||
284 | if (isTextVisible()) { | |
285 | fTextComposite.setBackground(background); | |
286 | fElementText.setBackground(background); | |
287 | fElementText.setForeground(foreground); | |
288 | ||
289 | fImageComposite.setBackground(null); | |
290 | fElementImage.setBackground(null); | |
291 | } else { | |
292 | fImageComposite.setBackground(background); | |
293 | fElementImage.setBackground(background); | |
294 | ||
295 | fTextComposite.setBackground(null); | |
296 | fElementText.setBackground(null); | |
297 | fElementText.setForeground(null); | |
298 | } | |
299 | ||
300 | fTextComposite.redraw(); | |
301 | fImageComposite.redraw(); | |
302 | } | |
303 | ||
304 | /** | |
305 | * Install focus and key listeners to the given composite. | |
306 | * | |
307 | * @param composite the composite which may get focus | |
308 | */ | |
309 | private void installFocusComposite(Composite composite) { | |
310 | composite.addTraverseListener(new TraverseListener() { | |
311 | public void keyTraversed(TraverseEvent e) { | |
312 | if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) { | |
313 | int index= fParent.getViewer().getIndexOfItem(fParent); | |
314 | if (e.detail == SWT.TRAVERSE_TAB_NEXT) { | |
315 | index++; | |
316 | } else { | |
317 | index--; | |
318 | } | |
319 | ||
320 | if (index > 0 && index < fParent.getViewer().getItemCount()) { | |
321 | fParent.getViewer().selectItem(fParent.getViewer().getItem(index)); | |
322 | } | |
323 | ||
324 | e.doit= true; | |
325 | } | |
326 | } | |
327 | }); | |
328 | composite.addKeyListener(new KeyListener() { | |
329 | public void keyPressed(KeyEvent e) { | |
330 | BreadcrumbViewer viewer= fParent.getViewer(); | |
331 | ||
332 | switch (e.keyCode) { | |
333 | case SWT.ARROW_LEFT: | |
334 | if (fSelected) { | |
335 | viewer.doTraverse(false); | |
336 | e.doit= false; | |
337 | } else { | |
338 | viewer.selectItem(fParent); | |
339 | } | |
340 | break; | |
341 | case SWT.ARROW_RIGHT: | |
342 | if (fSelected) { | |
343 | viewer.doTraverse(true); | |
344 | e.doit= false; | |
345 | } else { | |
346 | viewer.selectItem(fParent); | |
347 | } | |
348 | break; | |
349 | case SWT.ARROW_DOWN: | |
350 | if (!fSelected) { | |
351 | viewer.selectItem(fParent); | |
352 | } | |
353 | openDropDown(); | |
354 | e.doit= false; | |
355 | break; | |
356 | case SWT.KEYPAD_ADD: | |
357 | if (!fSelected) { | |
358 | viewer.selectItem(fParent); | |
359 | } | |
360 | openDropDown(); | |
361 | e.doit= false; | |
362 | break; | |
363 | case SWT.CR: | |
364 | if (!fSelected) { | |
365 | viewer.selectItem(fParent); | |
366 | } | |
367 | viewer.fireOpen(); | |
368 | break; | |
369 | default: | |
370 | if (e.character == ' ') { | |
371 | if (!fSelected) { | |
372 | viewer.selectItem(fParent); | |
373 | } | |
374 | openDropDown(); | |
375 | e.doit= false; | |
376 | } | |
377 | break; | |
378 | } | |
379 | } | |
380 | ||
381 | private void openDropDown() { | |
382 | BreadcrumbViewer viewer= fParent.getViewer(); | |
383 | ||
384 | int index= viewer.getIndexOfItem(fParent); | |
385 | BreadcrumbItem parent= fParent.getViewer().getItem(index - 1); | |
386 | ||
387 | Shell shell= parent.getDropDownShell(); | |
388 | if (shell == null) { | |
389 | parent.openDropDownMenu(); | |
390 | shell= parent.getDropDownShell(); | |
391 | } | |
392 | shell.setFocus(); | |
393 | } | |
394 | ||
395 | public void keyReleased(KeyEvent e) { | |
396 | } | |
397 | }); | |
398 | ||
399 | composite.addFocusListener(new FocusListener() { | |
400 | public void focusGained(FocusEvent e) { | |
401 | if (!fHasFocus) { | |
402 | fHasFocus= true; | |
403 | updateSelection(); | |
404 | } | |
405 | } | |
406 | ||
407 | public void focusLost(FocusEvent e) { | |
408 | if (fHasFocus) { | |
409 | fHasFocus= false; | |
410 | updateSelection(); | |
411 | } | |
412 | } | |
413 | }); | |
414 | } | |
415 | ||
416 | /** | |
417 | * Add mouse listeners to the given control. | |
418 | * | |
419 | * @param control the control to which may be clicked | |
420 | */ | |
421 | private void addElementListener(Control control) { | |
422 | control.addMouseListener(new MouseListener() { | |
423 | public void mouseDoubleClick(MouseEvent e) { | |
424 | } | |
425 | ||
426 | public void mouseDown(MouseEvent e) { | |
427 | BreadcrumbViewer viewer= fParent.getViewer(); | |
428 | ||
429 | int parentIndex= viewer.getIndexOfItem(fParent) - 1; | |
430 | Shell shell= null; | |
431 | if (parentIndex >= 0) {//sanity check, must always hold | |
432 | BreadcrumbItem dropDownItem= viewer.getItem(parentIndex); | |
433 | shell= dropDownItem.getDropDownShell(); | |
434 | } | |
435 | ||
436 | viewer.selectItem(fParent); | |
437 | if (shell == null && e.button == 1 && e.stateMask == 0) { | |
438 | fParent.getViewer().fireDoubleClick(); | |
439 | } | |
440 | } | |
441 | ||
442 | public void mouseUp(MouseEvent e) { | |
443 | } | |
444 | }); | |
445 | control.addMenuDetectListener(new MenuDetectListener() { | |
446 | public void menuDetected(MenuDetectEvent e) { | |
447 | BreadcrumbViewer viewer= fParent.getViewer(); | |
448 | viewer.selectItem(fParent); | |
449 | fParent.getViewer().fireMenuDetect(e); | |
450 | } | |
451 | }); | |
452 | } | |
453 | ||
454 | public void generated_6397763314331257138(String text, Image image, String toolTip) { | |
455 | setText(text); | |
456 | setImage(image); | |
457 | setToolTip(toolTip); | |
458 | } | |
459 | } |