upload tizen1.0 source
[sdk/ide/common-eplugin.git] / org.tizen.common / src / org / tizen / common / swt / ClosableTray.java
1 /*
2 *  Common
3 *
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: 
7 * Kangho Kim <kh5325.kim@samsung.com>
8
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * Contributors:
22 * - S-Core Co., Ltd
23 *
24 */
25
26 package org.tizen.common.swt;
27
28 import org.eclipse.jface.action.ContributionItem;
29 import org.eclipse.jface.action.ToolBarManager;
30 import org.eclipse.jface.dialogs.DialogTray;
31 import org.eclipse.jface.dialogs.TrayDialog;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.graphics.Color;
34 import org.eclipse.swt.graphics.GC;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.graphics.ImageData;
37 import org.eclipse.swt.graphics.PaletteData;
38 import org.eclipse.swt.graphics.RGB;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Control;
43 import org.eclipse.swt.widgets.Display;
44 import org.eclipse.swt.widgets.Event;
45 import org.eclipse.swt.widgets.Label;
46 import org.eclipse.swt.widgets.Listener;
47 import org.eclipse.swt.widgets.Shell;
48 import org.eclipse.swt.widgets.ToolBar;
49 import org.eclipse.swt.widgets.ToolItem;
50
51
52 /**
53  * a extended DialogTray class that user can be closing
54  * 
55  * @author Jihoon Song {@literal <jihoon80.song@samsung.com>} (S-Core)
56  */
57 public abstract class ClosableTray extends DialogTray {
58         private Image closeNormal;
59         private Image closeHover;
60         private Shell shell;
61
62         private void createImages() {
63                 Display display = Display.getCurrent();
64                 int[] shape = new int[] {
65                                 3, 3, 5, 3, 7, 5, 8, 5, 10, 3, 12, 3, 12, 5, 10, 7, 10, 8, 12, 10,
66                                 12, 12, 10, 12, 8, 10, 7, 10, 5, 12, 3, 12, 3, 10, 5, 8, 5, 7, 3, 5
67                 };
68
69                 // create image data
70                 Color border = display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW);
71                 Color background = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);
72                 Color backgroundHot = new Color(display, new RGB(252, 160, 160));
73                 Color transparent = display.getSystemColor(SWT.COLOR_MAGENTA);
74
75                 PaletteData palette = new PaletteData(new RGB[] {
76                                 transparent.getRGB(), border.getRGB(), background.getRGB(), backgroundHot.getRGB()
77                 });
78                 ImageData data = new ImageData(16, 16, 8, palette);
79                 data.transparentPixel = 0;
80
81                 // create close image
82                 closeNormal = new Image(display, data);
83                 closeNormal.setBackground(transparent);
84                 GC gc = new GC(closeNormal);
85                 gc.setBackground(background);
86                 gc.fillPolygon(shape);
87                 gc.setForeground(border);
88                 gc.drawPolygon(shape);
89                 gc.dispose();
90
91                 // create hovered close image
92                 closeHover = new Image(display, data);
93                 closeHover.setBackground(transparent);
94                 gc = new GC(closeHover);
95                 gc.setBackground(backgroundHot);
96                 gc.fillPolygon(shape);
97                 gc.setForeground(border);
98                 gc.drawPolygon(shape);
99                 gc.dispose();
100
101                 backgroundHot.dispose();
102         }
103
104         @Override
105         protected Control createContents(Composite parent) {
106                 shell = parent.getShell();
107
108                 // appearence composite
109                 Composite container = new Composite(parent, SWT.NONE);
110                 GridLayout layout = new GridLayout();
111                 layout.marginWidth = layout.marginHeight = 0;
112                 layout.verticalSpacing = 0;
113                 container.setLayout(layout);
114                 container.addListener(SWT.Dispose, new Listener() {
115                         @Override
116                         public void handleEvent(Event event) {
117                                 dispose();
118                         }
119                 });
120
121                 // top toolbar composite
122                 ToolBarManager tbm = new ToolBarManager(SWT.FLAT);
123                 tbm.createControl(container);
124                 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
125                 gd.grabExcessHorizontalSpace = true;
126                 tbm.getControl().setLayoutData(gd);
127
128                 // horizontal separator
129                 Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
130                 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
131                 gd.heightHint = 1;
132                 separator.setLayoutData(gd);
133
134                 // create external composite
135                 createExternalControl(container);
136
137                 // create close button image
138                 createImages();
139
140                 // add close button
141                 tbm.add(new ContributionItem() {
142                         @Override
143                         public void fill(ToolBar parent, int index) {
144                                 final ToolItem item = new ToolItem(parent, SWT.PUSH);
145                                 item.setImage(closeNormal);
146                                 item.setHotImage(closeHover);
147                                 item.addListener(SWT.Selection, new Listener() {
148                                         @Override
149                                         public void handleEvent(Event event) {
150                                                 TrayDialog dialog = (TrayDialog) shell.getData();
151                                                 dialog.closeTray();
152                                                 shell.setFocus();
153                                         }
154                                 });
155                         }
156                 });
157                 tbm.update(true);
158
159                 return container;
160         }
161
162         public abstract void createExternalControl(Composite parent);
163
164         protected void dispose() {
165                 if (closeNormal != null) {
166                         closeNormal.dispose();
167                 }
168                 if (closeHover != null) {
169                         closeHover.dispose();
170                 }
171         }
172 }