001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.presentations.util;
011:
012: import org.eclipse.core.commands.common.EventManager;
013: import org.eclipse.jface.action.ContributionItem;
014: import org.eclipse.jface.action.ToolBarManager;
015: import org.eclipse.jface.util.Geometry;
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.events.MouseAdapter;
018: import org.eclipse.swt.events.MouseEvent;
019: import org.eclipse.swt.events.MouseListener;
020: import org.eclipse.swt.events.SelectionAdapter;
021: import org.eclipse.swt.events.SelectionEvent;
022: import org.eclipse.swt.graphics.Image;
023: import org.eclipse.swt.graphics.Point;
024: import org.eclipse.swt.graphics.Rectangle;
025: import org.eclipse.swt.widgets.Composite;
026: import org.eclipse.swt.widgets.Control;
027: import org.eclipse.swt.widgets.ToolBar;
028: import org.eclipse.swt.widgets.ToolItem;
029: import org.eclipse.ui.IPropertyListener;
030: import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
031: import org.eclipse.ui.internal.WorkbenchImages;
032: import org.eclipse.ui.internal.WorkbenchMessages;
033: import org.eclipse.ui.presentations.IStackPresentationSite;
034:
035: /**
036: * @since 3.1
037: */
038: public class StandardSystemToolbar extends EventManager {
039: private ToolBarManager toolbarManager;
040: private Composite control;
041:
042: private class SystemMenuContribution extends ContributionItem {
043: ToolItem item;
044: Image img;
045: String text;
046: int flags;
047:
048: public SystemMenuContribution() {
049: this (SWT.PUSH);
050: }
051:
052: public SystemMenuContribution(int flags) {
053: this .flags = flags;
054: }
055:
056: public void setToolTipText(String text) {
057: this .text = text;
058: if (item != null) {
059: item.setToolTipText(text);
060: }
061: }
062:
063: public void setImage(Image img) {
064: this .img = img;
065:
066: if (item != null) {
067: item.setImage(img);
068: }
069: }
070:
071: /* (non-Javadoc)
072: * @see org.eclipse.jface.action.ContributionItem#setVisible(boolean)
073: */
074: public void setVisible(boolean visible) {
075: if (visible != isVisible()) {
076: toolbarManager.markDirty();
077: }
078:
079: super .setVisible(visible);
080: }
081:
082: /* (non-Javadoc)
083: * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.ToolBar, int)
084: */
085: public void fill(ToolBar parent, int index) {
086: if (!isVisible()) {
087: return;
088: }
089:
090: item = new ToolItem(parent, flags, index);
091: if (img != null) {
092: item.setImage(img);
093: }
094: if (text != null) {
095: item.setToolTipText(text);
096: }
097: item.addSelectionListener(selectionListener);
098: }
099: }
100:
101: private class PaneMenu extends SystemMenuContribution {
102:
103: public PaneMenu() {
104: super (SWT.NONE);
105: }
106:
107: public void setToolTipText(String text) {
108: super .setToolTipText(text);
109: }
110:
111: public void setImage(Image img) {
112:
113: super .setImage(img);
114: }
115:
116: /* (non-Javadoc)
117: * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.ToolBar, int)
118: */
119: public void fill(ToolBar parent, int index) {
120: if (!isVisible()) {
121: return;
122: }
123: super .fill(parent, index);
124: }
125: }
126:
127: private MouseListener mouseListener = new MouseAdapter() {
128: /* (non-Javadoc)
129: * @see org.eclipse.swt.events.MouseAdapter#mouseDown(org.eclipse.swt.events.MouseEvent)
130: */
131: public void mouseDown(MouseEvent e) {
132: ToolItem item = toolbarManager.getControl().getItem(
133: new Point(e.x, e.y));
134:
135: if (item == paneMenu.item && e.button == 1) {
136: fireEvent(TabFolderEvent.EVENT_PANE_MENU);
137: }
138: }
139: };
140:
141: private SystemMenuContribution paneMenu = new PaneMenu();
142: private SystemMenuContribution showToolbar = new SystemMenuContribution();
143: private SystemMenuContribution min = new SystemMenuContribution();
144: private SystemMenuContribution max = new SystemMenuContribution();
145: private SystemMenuContribution close = new SystemMenuContribution();
146:
147: private int state = IStackPresentationSite.STATE_RESTORED;
148: private boolean showingToolbar = true;
149:
150: private SelectionAdapter selectionListener = new SelectionAdapter() {
151: public void widgetSelected(SelectionEvent e) {
152: if (e.widget == paneMenu.item) {
153: //fireEvent(TabFolderEvent.EVENT_PANE_MENU);
154: } else if (e.widget == showToolbar.item) {
155: if (showingToolbar) {
156: fireEvent(TabFolderEvent.EVENT_HIDE_TOOLBAR);
157: } else {
158: fireEvent(TabFolderEvent.EVENT_SHOW_TOOLBAR);
159: }
160: } else if (e.widget == min.item) {
161: if (state == IStackPresentationSite.STATE_MINIMIZED) {
162: fireEvent(TabFolderEvent.EVENT_RESTORE);
163: } else {
164: fireEvent(TabFolderEvent.EVENT_MINIMIZE);
165: }
166: } else if (e.widget == max.item) {
167: if (state == IStackPresentationSite.STATE_MAXIMIZED) {
168: fireEvent(TabFolderEvent.EVENT_RESTORE);
169: } else {
170: fireEvent(TabFolderEvent.EVENT_MAXIMIZE);
171: }
172: } else if (e.widget == close.item) {
173: fireEvent(TabFolderEvent.EVENT_CLOSE);
174: }
175: }
176: };
177:
178: public StandardSystemToolbar(Composite parent,
179: boolean showPaneMenu, boolean showHideToolbar,
180: boolean showMinimize, boolean showMaximize,
181: boolean enableClose) {
182:
183: control = new Composite(parent, SWT.NONE);
184: control.setLayout(new EnhancedFillLayout());
185:
186: toolbarManager = new ToolBarManager(SWT.FLAT);
187: toolbarManager.createControl(control);
188: toolbarManager.getControl().addMouseListener(mouseListener);
189:
190: toolbarManager.add(paneMenu);
191: paneMenu
192: .setImage(WorkbenchImages
193: .getImage(IWorkbenchGraphicConstants.IMG_LCL_VIEW_MENU_THIN));
194: paneMenu.setVisible(showPaneMenu);
195: paneMenu.setToolTipText(WorkbenchMessages.Menu);
196:
197: toolbarManager.add(showToolbar);
198: showToolbar
199: .setImage(WorkbenchImages
200: .getImage(IWorkbenchGraphicConstants.IMG_LCL_HIDE_TOOLBAR_THIN));
201: showToolbar.setVisible(showHideToolbar);
202:
203: toolbarManager.add(min);
204: min.setVisible(showMinimize);
205:
206: toolbarManager.add(max);
207: max.setVisible(showMaximize);
208:
209: toolbarManager.add(close);
210: close
211: .setImage(WorkbenchImages
212: .getImage(IWorkbenchGraphicConstants.IMG_LCL_CLOSE_VIEW_THIN));
213: close.setVisible(enableClose);
214:
215: setState(IStackPresentationSite.STATE_RESTORED);
216:
217: toolbarManager.update(true);
218: }
219:
220: public Point getPaneMenuLocation() {
221:
222: Rectangle bounds = Geometry.toDisplay(
223: paneMenu.item.getParent(), paneMenu.item.getBounds());
224: return new Point(bounds.x, bounds.y + bounds.height);
225: }
226:
227: public void enableClose(boolean enabled) {
228: close.setVisible(enabled);
229: toolbarManager.update(false);
230: }
231:
232: public void enableMinimize(boolean enabled) {
233: min.setVisible(enabled);
234: toolbarManager.update(false);
235: }
236:
237: public void enableMaximize(boolean enabled) {
238: max.setVisible(enabled);
239: toolbarManager.update(false);
240: }
241:
242: public void enableShowToolbar(boolean enabled) {
243: showToolbar.setVisible(enabled);
244: toolbarManager.update(false);
245: }
246:
247: public void enablePaneMenu(boolean enabled) {
248: paneMenu.setVisible(enabled);
249: toolbarManager.update(false);
250: }
251:
252: /**
253: * Updates the icons on the state buttons to match the given state
254: * @param newState
255: * @since 3.1
256: */
257: public void setState(int newState) {
258: if (min != null) {
259: if (newState == IStackPresentationSite.STATE_MINIMIZED) {
260: min
261: .setToolTipText(WorkbenchMessages.StandardSystemToolbar_Restore);
262: min
263: .setImage(WorkbenchImages
264: .getImage(IWorkbenchGraphicConstants.IMG_LCL_RESTORE_VIEW_THIN));
265: } else {
266: min
267: .setToolTipText(WorkbenchMessages.StandardSystemToolbar_Minimize);
268: min
269: .setImage(WorkbenchImages
270: .getImage(IWorkbenchGraphicConstants.IMG_LCL_MIN_VIEW_THIN));
271: }
272: }
273: if (max != null) {
274: if (newState == IStackPresentationSite.STATE_MAXIMIZED) {
275: max
276: .setToolTipText(WorkbenchMessages.StandardSystemToolbar_Restore);
277: max
278: .setImage(WorkbenchImages
279: .getImage(IWorkbenchGraphicConstants.IMG_LCL_RESTORE_VIEW_THIN));
280: } else {
281: max
282: .setToolTipText(WorkbenchMessages.StandardSystemToolbar_Maximize);
283: max
284: .setImage(WorkbenchImages
285: .getImage(IWorkbenchGraphicConstants.IMG_LCL_MAX_VIEW_THIN));
286: }
287: }
288:
289: state = newState;
290: }
291:
292: public void setToolbarShowing(boolean isShowing) {
293: showingToolbar = isShowing;
294: if (showToolbar != null) {
295: if (isShowing) {
296: showToolbar
297: .setImage(WorkbenchImages
298: .getImage(IWorkbenchGraphicConstants.IMG_LCL_HIDE_TOOLBAR_THIN));
299: } else {
300: showToolbar
301: .setImage(WorkbenchImages
302: .getImage(IWorkbenchGraphicConstants.IMG_LCL_SHOW_TOOLBAR_THIN));
303: }
304: }
305: }
306:
307: public void addListener(IPropertyListener propertyListener) {
308: addListenerObject(propertyListener);
309: }
310:
311: public void removeListener(IPropertyListener propertyListener) {
312: removeListenerObject(propertyListener);
313: }
314:
315: public Control getControl() {
316: return control;
317: }
318:
319: private void fireEvent(int event) {
320: Object[] list = getListeners();
321:
322: for (int i = 0; i < list.length; i++) {
323: IPropertyListener listener = (IPropertyListener) list[i];
324:
325: listener.propertyChanged(this, event);
326: }
327: }
328: }
|