KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > util > StandardSystemToolbar


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.ui.internal.presentations.util;
12
13 import org.eclipse.core.commands.common.EventManager;
14 import org.eclipse.jface.action.ContributionItem;
15 import org.eclipse.jface.action.ToolBarManager;
16 import org.eclipse.jface.util.Geometry;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.MouseAdapter;
19 import org.eclipse.swt.events.MouseEvent;
20 import org.eclipse.swt.events.MouseListener;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.swt.graphics.Rectangle;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.ToolBar;
29 import org.eclipse.swt.widgets.ToolItem;
30 import org.eclipse.ui.IPropertyListener;
31 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
32 import org.eclipse.ui.internal.WorkbenchImages;
33 import org.eclipse.ui.internal.WorkbenchMessages;
34 import org.eclipse.ui.presentations.IStackPresentationSite;
35
36 /**
37  * @since 3.1
38  */

39 public class StandardSystemToolbar extends EventManager {
40     private ToolBarManager toolbarManager;
41     private Composite control;
42     
43     private class SystemMenuContribution extends ContributionItem {
44         ToolItem item;
45         Image img;
46         String JavaDoc text;
47         int flags;
48         
49         public SystemMenuContribution() {
50             this(SWT.PUSH);
51         }
52         
53         public SystemMenuContribution(int flags) {
54             this.flags = flags;
55         }
56         
57         public void setToolTipText(String JavaDoc text) {
58             this.text = text;
59             if (item != null) {
60                 item.setToolTipText(text);
61             }
62         }
63         
64         public void setImage(Image img) {
65             this.img = img;
66             
67             if (item != null) {
68                 item.setImage(img);
69             }
70         }
71         
72         /* (non-Javadoc)
73          * @see org.eclipse.jface.action.ContributionItem#setVisible(boolean)
74          */

75         public void setVisible(boolean visible) {
76             if (visible != isVisible()) {
77                 toolbarManager.markDirty();
78             }
79             
80             super.setVisible(visible);
81         }
82         
83         /* (non-Javadoc)
84          * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.ToolBar, int)
85          */

86         public void fill(ToolBar parent, int index) {
87             if (!isVisible()) {
88                 return;
89             }
90             
91             item = new ToolItem(parent, flags, index);
92             if (img != null) {
93                 item.setImage(img);
94             }
95             if (text != null) {
96                 item.setToolTipText(text);
97             }
98             item.addSelectionListener(selectionListener);
99         }
100     }
101     
102     private class PaneMenu extends SystemMenuContribution {
103         
104         public PaneMenu() {
105             super(SWT.NONE);
106         }
107                 
108         public void setToolTipText(String JavaDoc text) {
109             super.setToolTipText(text);
110         }
111         
112         public void setImage(Image img) {
113             
114             super.setImage(img);
115         }
116         
117         /* (non-Javadoc)
118          * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.ToolBar, int)
119          */

120         public void fill(ToolBar parent, int index) {
121             if (!isVisible()) {
122                 return;
123             }
124             super.fill(parent, index);
125         }
126     }
127     
128     
129     private MouseListener mouseListener = new MouseAdapter() {
130         /* (non-Javadoc)
131          * @see org.eclipse.swt.events.MouseAdapter#mouseDown(org.eclipse.swt.events.MouseEvent)
132          */

133         public void mouseDown(MouseEvent e) {
134             ToolItem item = toolbarManager.getControl().getItem(new Point(e.x, e.y));
135             
136             if (item == paneMenu.item && e.button == 1) {
137                 fireEvent(TabFolderEvent.EVENT_PANE_MENU);
138             }
139         }
140     };
141     
142     private SystemMenuContribution paneMenu = new PaneMenu();
143     private SystemMenuContribution showToolbar = new SystemMenuContribution();
144     private SystemMenuContribution min = new SystemMenuContribution();
145     private SystemMenuContribution max = new SystemMenuContribution();
146     private SystemMenuContribution close = new SystemMenuContribution();
147     
148     private int state = IStackPresentationSite.STATE_RESTORED;
149     private boolean showingToolbar = true;
150     
151     private SelectionAdapter selectionListener = new SelectionAdapter() {
152         public void widgetSelected(SelectionEvent e) {
153             if (e.widget == paneMenu.item) {
154                 //fireEvent(TabFolderEvent.EVENT_PANE_MENU);
155
} else if (e.widget == showToolbar.item) {
156                 if (showingToolbar) {
157                     fireEvent(TabFolderEvent.EVENT_HIDE_TOOLBAR);
158                 } else {
159                     fireEvent(TabFolderEvent.EVENT_SHOW_TOOLBAR);
160                 }
161             } else if (e.widget == min.item) {
162                 if (state == IStackPresentationSite.STATE_MINIMIZED) {
163                     fireEvent(TabFolderEvent.EVENT_RESTORE);
164                 } else {
165                     fireEvent(TabFolderEvent.EVENT_MINIMIZE);
166                 }
167             } else if (e.widget == max.item) {
168                 if (state == IStackPresentationSite.STATE_MAXIMIZED) {
169                     fireEvent(TabFolderEvent.EVENT_RESTORE);
170                 } else {
171                     fireEvent(TabFolderEvent.EVENT_MAXIMIZE);
172                 }
173             } else if (e.widget == close.item) {
174                 fireEvent(TabFolderEvent.EVENT_CLOSE);
175             }
176         }
177     };
178     
179     public StandardSystemToolbar(Composite parent, boolean showPaneMenu,
180             boolean showHideToolbar, boolean showMinimize, boolean showMaximize, boolean enableClose) {
181         
182         control = new Composite(parent, SWT.NONE);
183         control.setLayout(new EnhancedFillLayout());
184         
185         toolbarManager = new ToolBarManager(SWT.FLAT);
186         toolbarManager.createControl(control);
187         toolbarManager.getControl().addMouseListener(mouseListener);
188         
189         toolbarManager.add(paneMenu);
190         paneMenu.setImage(WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_LCL_VIEW_MENU_THIN));
191         paneMenu.setVisible(showPaneMenu);
192         paneMenu.setToolTipText(WorkbenchMessages.Menu);
193
194         toolbarManager.add(showToolbar);
195         showToolbar.setImage(WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_LCL_HIDE_TOOLBAR_THIN));
196         showToolbar.setVisible(showHideToolbar);
197
198         toolbarManager.add(min);
199         min.setVisible(showMinimize);
200
201         toolbarManager.add(max);
202         max.setVisible(showMaximize);
203
204         toolbarManager.add(close);
205         close.setImage(WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_LCL_CLOSE_VIEW_THIN));
206         close.setVisible(enableClose);
207         
208         setState(IStackPresentationSite.STATE_RESTORED);
209         
210         toolbarManager.update(true);
211     }
212     
213     public Point getPaneMenuLocation() {
214         
215         Rectangle bounds = Geometry.toDisplay(paneMenu.item.getParent(), paneMenu.item.getBounds());
216         return new Point(bounds.x, bounds.y + bounds.height);
217     }
218     
219     public void enableClose(boolean enabled) {
220         close.setVisible(enabled);
221         toolbarManager.update(false);
222     }
223     
224     public void enableMinimize(boolean enabled) {
225         min.setVisible(enabled);
226         toolbarManager.update(false);
227     }
228     
229     public void enableMaximize(boolean enabled) {
230         max.setVisible(enabled);
231         toolbarManager.update(false);
232     }
233     
234    public void enableShowToolbar(boolean enabled) {
235         showToolbar.setVisible(enabled);
236         toolbarManager.update(false);
237     }
238     
239     public void enablePaneMenu(boolean enabled) {
240         paneMenu.setVisible(enabled);
241         toolbarManager.update(false);
242     }
243     
244     /**
245      * Updates the icons on the state buttons to match the given state
246      * @param newState
247      * @since 3.1
248      */

249     public void setState(int newState) {
250         if (min != null) {
251             if (newState == IStackPresentationSite.STATE_MINIMIZED) {
252                 min.setToolTipText(WorkbenchMessages.StandardSystemToolbar_Restore);
253                 min.setImage(WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_LCL_RESTORE_VIEW_THIN));
254             } else {
255                 min.setToolTipText(WorkbenchMessages.StandardSystemToolbar_Minimize);
256                 min.setImage(WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_LCL_MIN_VIEW_THIN));
257             }
258         }
259         if (max != null) {
260             if (newState == IStackPresentationSite.STATE_MAXIMIZED) {
261                 max.setToolTipText(WorkbenchMessages.StandardSystemToolbar_Restore);
262                 max.setImage(WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_LCL_RESTORE_VIEW_THIN));
263             } else {
264                 max.setToolTipText(WorkbenchMessages.StandardSystemToolbar_Maximize);
265                 max.setImage(WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_LCL_MAX_VIEW_THIN));
266             }
267         }
268         
269         state = newState;
270     }
271     
272     public void setToolbarShowing(boolean isShowing) {
273         showingToolbar = isShowing;
274         if (showToolbar != null) {
275             if (isShowing) {
276                 showToolbar.setImage(WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_LCL_HIDE_TOOLBAR_THIN));
277             } else {
278                 showToolbar.setImage(WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_LCL_SHOW_TOOLBAR_THIN));
279             }
280         }
281     }
282     
283     public void addListener(IPropertyListener propertyListener) {
284         addListenerObject(propertyListener);
285     }
286     
287     public void removeListener(IPropertyListener propertyListener) {
288         removeListenerObject(propertyListener);
289     }
290     
291     public Control getControl() {
292         return control;
293     }
294     
295     private void fireEvent(int event) {
296         Object JavaDoc[] list = getListeners();
297         
298         for (int i = 0; i < list.length; i++) {
299             IPropertyListener listener = (IPropertyListener)list[i];
300             
301             listener.propertyChanged(this, event);
302         }
303     }
304 }
305
Popular Tags