author | dadubrow |
Mon, 20 Sep 2010 15:24:33 -0500 | |
changeset 2068 | 8db6518f7141 |
parent 2062 | 4d032dc346bf |
child 2099 | 22d901cba60a |
permissions | -rw-r--r-- |
1609 | 1 |
/* |
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
* All rights reserved. |
|
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of the License "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: |
|
15 |
* |
|
16 |
*/ |
|
17 |
package com.nokia.carbide.internal.discovery.ui.editor; |
|
18 |
||
19 |
import java.util.LinkedHashMap; |
|
20 |
import java.util.Map; |
|
21 |
||
1618 | 22 |
import org.eclipse.jface.resource.FontDescriptor; |
23 |
import org.eclipse.jface.resource.JFaceResources; |
|
1609 | 24 |
import org.eclipse.swt.SWT; |
2062 | 25 |
import org.eclipse.swt.custom.CLabel; |
26 |
import org.eclipse.swt.events.MouseAdapter; |
|
27 |
import org.eclipse.swt.events.MouseEvent; |
|
28 |
import org.eclipse.swt.events.MouseListener; |
|
29 |
import org.eclipse.swt.events.MouseTrackAdapter; |
|
30 |
import org.eclipse.swt.events.MouseTrackListener; |
|
1609 | 31 |
import org.eclipse.swt.events.SelectionAdapter; |
32 |
import org.eclipse.swt.events.SelectionEvent; |
|
33 |
import org.eclipse.swt.events.SelectionListener; |
|
34 |
import org.eclipse.swt.graphics.Font; |
|
1618 | 35 |
import org.eclipse.swt.graphics.FontData; |
2068 | 36 |
import org.eclipse.swt.graphics.Point; |
37 |
import org.eclipse.swt.graphics.Rectangle; |
|
2035 | 38 |
import org.eclipse.swt.layout.FillLayout; |
1609 | 39 |
import org.eclipse.swt.widgets.Composite; |
2062 | 40 |
import org.eclipse.swt.widgets.Control; |
41 |
import org.eclipse.swt.widgets.Event; |
|
42 |
||
43 |
import com.nokia.cpp.internal.api.utils.core.ListenerList; |
|
1609 | 44 |
|
45 |
class NavigationBar extends RoundedCornerComposite { |
|
2062 | 46 |
private class Button extends CLabel { |
47 |
private ListenerList<SelectionListener> listeners; |
|
48 |
private boolean selection; |
|
49 |
private MouseTrackListener mouseTrackListener; |
|
50 |
private MouseListener mouseListener; |
|
51 |
||
52 |
public Button(Composite parent) { |
|
53 |
super(parent, SWT.CENTER); |
|
54 |
listeners = new ListenerList<SelectionListener>(); |
|
55 |
setBackground(getDisplay().getSystemColor(SWT.COLOR_WHITE)); |
|
56 |
addMouseTrackListener(mouseTrackListener = new MouseTrackAdapter() { |
|
57 |
@Override |
|
58 |
public void mouseExit(MouseEvent e) { |
|
59 |
if (!selection) |
|
60 |
setBackgroundColor(); |
|
61 |
||
62 |
} |
|
63 |
||
64 |
@Override |
|
65 |
public void mouseEnter(MouseEvent e) { |
|
66 |
if (!selection) |
|
67 |
setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); |
|
68 |
} |
|
69 |
}); |
|
70 |
||
71 |
addMouseListener(mouseListener = new MouseAdapter() { |
|
72 |
@Override |
|
73 |
public void mouseUp(MouseEvent e) { |
|
2068 | 74 |
Point size = Button.this.getSize(); |
75 |
if (new Rectangle(0, 0, size.x, size.y).contains(e.x, e.y)) |
|
76 |
setSelection(true); |
|
2062 | 77 |
} |
78 |
}); |
|
79 |
} |
|
80 |
||
81 |
@Override |
|
82 |
protected void checkSubclass() { |
|
83 |
} |
|
84 |
||
85 |
public void addSelectionListener(SelectionListener listener) { |
|
86 |
listeners.add(listener); |
|
87 |
} |
|
88 |
||
89 |
public void setSelection(boolean selection) { |
|
90 |
if (this.selection != selection) { |
|
91 |
this.selection = selection; |
|
92 |
setBackgroundColor(); |
|
93 |
if (selection) { |
|
94 |
for (Control control : getParent().getChildren()) { |
|
95 |
if (control instanceof Button) { |
|
96 |
if (!control.equals(this)) { |
|
97 |
((Button) control).setSelection(false); |
|
98 |
} |
|
99 |
} |
|
100 |
} |
|
101 |
fireSelection(); |
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
private void fireSelection() { |
|
107 |
Event e = new Event(); |
|
108 |
e.widget = this; |
|
109 |
e.display = getDisplay(); |
|
110 |
for (SelectionListener listener : listeners) { |
|
111 |
listener.widgetSelected(new SelectionEvent(e)); |
|
112 |
} |
|
113 |
||
114 |
} |
|
115 |
||
116 |
private void setBackgroundColor() { |
|
117 |
setBackground(getDisplay().getSystemColor(selection ? SWT.COLOR_WIDGET_NORMAL_SHADOW : SWT.COLOR_WHITE)); |
|
118 |
} |
|
119 |
||
120 |
@Override |
|
121 |
public void dispose() { |
|
122 |
removeMouseTrackListener(mouseTrackListener); |
|
123 |
removeMouseListener(mouseListener); |
|
124 |
super.dispose(); |
|
125 |
} |
|
126 |
} |
|
1609 | 127 |
|
128 |
private final class ButtonListener extends SelectionAdapter { |
|
129 |
@Override |
|
130 |
public void widgetSelected(SelectionEvent e) { |
|
131 |
Button button = (Button) e.getSource(); |
|
132 |
selectNavButton(button); |
|
133 |
} |
|
134 |
} |
|
135 |
||
2062 | 136 |
private static final int BUTTON_SPACING = 0; |
2047
c984a53977bd
More tweaks - scaling icons will be replaced with actual 32x32 icons
dadubrow
parents:
2035
diff
changeset
|
137 |
private PortalEditor portalEditor; |
1684
c2eba6dabd38
refactor to allow better support for multiple layers
dadubrow
parents:
1634
diff
changeset
|
138 |
private Map<Button, PortalPage> buttonToPageMap; |
1609 | 139 |
private SelectionListener listener; |
140 |
private Font buttonFont; |
|
141 |
private Font selectedButtonFont; |
|
142 |
||
143 |
NavigationBar(PortalEditor portalEditor, Composite parent) { |
|
2047
c984a53977bd
More tweaks - scaling icons will be replaced with actual 32x32 icons
dadubrow
parents:
2035
diff
changeset
|
144 |
super(parent, portalEditor.getBackgroundParent(), null, null); |
1609 | 145 |
this.portalEditor = portalEditor; |
1684
c2eba6dabd38
refactor to allow better support for multiple layers
dadubrow
parents:
1634
diff
changeset
|
146 |
buttonToPageMap = new LinkedHashMap<Button, PortalPage>(); |
2035 | 147 |
FillLayout layout = new FillLayout(SWT.HORIZONTAL); |
2047
c984a53977bd
More tweaks - scaling icons will be replaced with actual 32x32 icons
dadubrow
parents:
2035
diff
changeset
|
148 |
layout.spacing = BUTTON_SPACING; |
c984a53977bd
More tweaks - scaling icons will be replaced with actual 32x32 icons
dadubrow
parents:
2035
diff
changeset
|
149 |
layout.marginHeight = BUTTON_SPACING; |
c984a53977bd
More tweaks - scaling icons will be replaced with actual 32x32 icons
dadubrow
parents:
2035
diff
changeset
|
150 |
layout.marginWidth = BUTTON_SPACING; |
2035 | 151 |
setLayout(layout); |
1609 | 152 |
listener = new ButtonListener(); |
1618 | 153 |
selectedButtonFont = JFaceResources.getHeaderFont(); |
154 |
FontData[] fontData = selectedButtonFont.getFontData(); |
|
155 |
for (FontData fd : fontData) { |
|
156 |
fd.setStyle(SWT.NORMAL); |
|
157 |
} |
|
158 |
FontDescriptor desc = FontDescriptor.createFrom(fontData); |
|
159 |
buttonFont = portalEditor.createFont(desc); |
|
1609 | 160 |
} |
161 |
||
162 |
public void initUI() { |
|
163 |
if (buttonToPageMap.isEmpty()) |
|
164 |
return; |
|
2062 | 165 |
buttonToPageMap.keySet().iterator().next().setSelection(true); |
1609 | 166 |
} |
167 |
||
1684
c2eba6dabd38
refactor to allow better support for multiple layers
dadubrow
parents:
1634
diff
changeset
|
168 |
public void addNavButton(NavigationBar bar, PortalPage page) { |
2062 | 169 |
Button b = new Button(bar); |
1609 | 170 |
b.setFont(buttonFont); |
1705 | 171 |
b.setText(page.getTitle()); |
2062 | 172 |
b.setToolTipText(page.getTooltip()); |
2047
c984a53977bd
More tweaks - scaling icons will be replaced with actual 32x32 icons
dadubrow
parents:
2035
diff
changeset
|
173 |
b.setImage(portalEditor.createImage(page.getImageDescriptor(), 32, 32)); |
1609 | 174 |
b.addSelectionListener(listener); |
175 |
buttonToPageMap.put(b, page); |
|
176 |
} |
|
177 |
||
178 |
||
179 |
public void selectNavButton(Button button) { |
|
1618 | 180 |
portalEditor.showPage(buttonToPageMap.get(button)); |
1609 | 181 |
button.setFont(selectedButtonFont); |
1618 | 182 |
layout(); |
183 |
} |
|
1609 | 184 |
} |