Source Code Cross Referenced for PerspectiveSwitcher.java in  » IDE-Eclipse » ui-workbench » org » eclipse » ui » internal » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » ui workbench » org.eclipse.ui.internal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*******************************************************************************
0002:         * Copyright (c) 2004, 2007 IBM Corporation and others.
0003:         * All rights reserved. This program and the accompanying materials
0004:         * are made available under the terms of the Eclipse Public License v1.0
0005:         * which accompanies this distribution, and is available at
0006:         * http://www.eclipse.org/legal/epl-v10.html
0007:         *
0008:         * Contributors:
0009:         *     IBM Corporation - initial API and implementation
0010:         *     Chris Grindstaff <chris@gstaff.org> - Fix for bug 158016     
0011:         *     Tonny Madsen, RCP Company - bug 201055
0012:         *******************************************************************************/package org.eclipse.ui.internal;
0013:
0014:        import java.util.Arrays;
0015:        import java.util.StringTokenizer;
0016:
0017:        import org.eclipse.core.runtime.Assert;
0018:        import org.eclipse.jface.action.IContributionItem;
0019:        import org.eclipse.jface.preference.IPreferenceStore;
0020:        import org.eclipse.jface.util.IPropertyChangeListener;
0021:        import org.eclipse.jface.util.PropertyChangeEvent;
0022:        import org.eclipse.swt.SWT;
0023:        import org.eclipse.swt.custom.CBanner;
0024:        import org.eclipse.swt.events.ControlAdapter;
0025:        import org.eclipse.swt.events.ControlEvent;
0026:        import org.eclipse.swt.events.DisposeEvent;
0027:        import org.eclipse.swt.events.DisposeListener;
0028:        import org.eclipse.swt.events.SelectionAdapter;
0029:        import org.eclipse.swt.events.SelectionEvent;
0030:        import org.eclipse.swt.events.SelectionListener;
0031:        import org.eclipse.swt.graphics.Cursor;
0032:        import org.eclipse.swt.graphics.Point;
0033:        import org.eclipse.swt.graphics.Rectangle;
0034:        import org.eclipse.swt.layout.GridData;
0035:        import org.eclipse.swt.widgets.Composite;
0036:        import org.eclipse.swt.widgets.Control;
0037:        import org.eclipse.swt.widgets.CoolBar;
0038:        import org.eclipse.swt.widgets.CoolItem;
0039:        import org.eclipse.swt.widgets.Event;
0040:        import org.eclipse.swt.widgets.Label;
0041:        import org.eclipse.swt.widgets.Listener;
0042:        import org.eclipse.swt.widgets.Menu;
0043:        import org.eclipse.swt.widgets.MenuItem;
0044:        import org.eclipse.swt.widgets.ToolBar;
0045:        import org.eclipse.swt.widgets.ToolItem;
0046:        import org.eclipse.ui.IMemento;
0047:        import org.eclipse.ui.IPageListener;
0048:        import org.eclipse.ui.IPerspectiveDescriptor;
0049:        import org.eclipse.ui.IWorkbenchPage;
0050:        import org.eclipse.ui.IWorkbenchPreferenceConstants;
0051:        import org.eclipse.ui.IWorkbenchWindow;
0052:        import org.eclipse.ui.PerspectiveAdapter;
0053:        import org.eclipse.ui.internal.StartupThreading.StartupRunnable;
0054:        import org.eclipse.ui.internal.dnd.AbstractDropTarget;
0055:        import org.eclipse.ui.internal.dnd.DragUtil;
0056:        import org.eclipse.ui.internal.dnd.IDragOverListener;
0057:        import org.eclipse.ui.internal.dnd.IDropTarget;
0058:        import org.eclipse.ui.internal.layout.CacheWrapper;
0059:        import org.eclipse.ui.internal.layout.CellLayout;
0060:        import org.eclipse.ui.internal.layout.ITrimManager;
0061:        import org.eclipse.ui.internal.layout.IWindowTrim;
0062:        import org.eclipse.ui.internal.layout.LayoutUtil;
0063:        import org.eclipse.ui.internal.layout.Row;
0064:        import org.eclipse.ui.internal.util.PrefUtil;
0065:        import org.eclipse.ui.presentations.PresentationUtil;
0066:
0067:        /**
0068:         * A utility class to manage the perspective switcher.  At some point, it might be nice to
0069:         * move all this into PerspectiveViewBar.
0070:         * 
0071:         * @since 3.0
0072:         */
0073:        public class PerspectiveSwitcher implements  IWindowTrim {
0074:
0075:            /**
0076:             * The minimal width for the switcher (i.e. for the open button and chevron).
0077:             */
0078:            private static final int MIN_WIDTH = 45;
0079:
0080:            /**
0081:             * The average width for each perspective button.
0082:             */
0083:            private static final int ITEM_WIDTH = 80;
0084:
0085:            /**
0086:             * The minimum default width.
0087:             */
0088:            private static final int MIN_DEFAULT_WIDTH = 160;
0089:
0090:            private IWorkbenchWindow window;
0091:
0092:            private CBanner topBar;
0093:
0094:            private int style;
0095:
0096:            private Composite parent;
0097:
0098:            private Composite trimControl;
0099:
0100:            private Label trimSeparator;
0101:
0102:            private GridData trimLayoutData;
0103:
0104:            private boolean trimVisible = false;
0105:
0106:            private int trimOldLength = 0;
0107:
0108:            private PerspectiveBarManager perspectiveBar;
0109:
0110:            private CoolBar perspectiveCoolBar;
0111:
0112:            private CacheWrapper perspectiveCoolBarWrapper;
0113:
0114:            private CoolItem coolItem;
0115:
0116:            private CacheWrapper toolbarWrapper;
0117:
0118:            // The menus are cached, so the radio buttons should not be disposed until
0119:            // the switcher is disposed.
0120:            private Menu popupMenu;
0121:
0122:            private Menu genericMenu;
0123:
0124:            private static final int INITIAL = -1;
0125:
0126:            private static final int TOP_RIGHT = 1;
0127:
0128:            private static final int TOP_LEFT = 2;
0129:
0130:            private static final int LEFT = 3;
0131:
0132:            private int currentLocation = INITIAL;
0133:
0134:            private IPreferenceStore apiPreferenceStore = PrefUtil
0135:                    .getAPIPreferenceStore();
0136:
0137:            private IPropertyChangeListener propertyChangeListener;
0138:
0139:            private Listener popupListener = new Listener() {
0140:                public void handleEvent(Event event) {
0141:                    if (event.type == SWT.MenuDetect) {
0142:                        showPerspectiveBarPopup(new Point(event.x, event.y));
0143:                    }
0144:                }
0145:            };
0146:
0147:            class ChangeListener extends PerspectiveAdapter implements 
0148:                    IPageListener {
0149:                public void perspectiveOpened(IWorkbenchPage page,
0150:                        IPerspectiveDescriptor perspective) {
0151:                    if (findPerspectiveShortcut(perspective, page) == null) {
0152:                        addPerspectiveShortcut(perspective, page);
0153:                    }
0154:                }
0155:
0156:                public void perspectiveClosed(IWorkbenchPage page,
0157:                        IPerspectiveDescriptor perspective) {
0158:                    // Don't remove the shortcut if the workbench is
0159:                    // closing. This causes a spurious 'layout' on the
0160:                    // shell during close, leading to possible life-cycle issues
0161:                    if (page != null
0162:                            && !page.getWorkbenchWindow().getWorkbench()
0163:                                    .isClosing()) {
0164:                        removePerspectiveShortcut(perspective, page);
0165:                    }
0166:                }
0167:
0168:                public void perspectiveActivated(IWorkbenchPage page,
0169:                        IPerspectiveDescriptor perspective) {
0170:                    selectPerspectiveShortcut(perspective, page, true);
0171:                }
0172:
0173:                public void perspectiveDeactivated(IWorkbenchPage page,
0174:                        IPerspectiveDescriptor perspective) {
0175:                    selectPerspectiveShortcut(perspective, page, false);
0176:                }
0177:
0178:                public void perspectiveSavedAs(IWorkbenchPage page,
0179:                        IPerspectiveDescriptor oldPerspective,
0180:                        IPerspectiveDescriptor newPerspective) {
0181:                    updatePerspectiveShortcut(oldPerspective, newPerspective,
0182:                            page);
0183:                }
0184:
0185:                public void pageActivated(IWorkbenchPage page) {
0186:                }
0187:
0188:                public void pageClosed(IWorkbenchPage page) {
0189:                }
0190:
0191:                public void pageOpened(IWorkbenchPage page) {
0192:                }
0193:            }
0194:
0195:            private ChangeListener changeListener = new ChangeListener();
0196:
0197:            private Listener dragListener;
0198:
0199:            private IDragOverListener dragTarget;
0200:
0201:            private DisposeListener toolBarListener;
0202:
0203:            private IReorderListener reorderListener;
0204:
0205:            /**
0206:             * Creates an instance of the perspective switcher.
0207:             * @param window  it's window
0208:             * @param topBar  the CBanner to place this widget in
0209:             * @param style   the widget style to use
0210:             */
0211:            public PerspectiveSwitcher(IWorkbenchWindow window, CBanner topBar,
0212:                    int style) {
0213:                this .window = window;
0214:                this .topBar = topBar;
0215:                this .style = style;
0216:                setPropertyChangeListener();
0217:                // this listener will only be run when the Shell is being disposed
0218:                // and each WorkbenchWindow has its own PerspectiveSwitcher
0219:                toolBarListener = new DisposeListener() {
0220:                    public void widgetDisposed(DisposeEvent e) {
0221:                        dispose();
0222:                    }
0223:                };
0224:                window.addPerspectiveListener(changeListener);
0225:                window.addPageListener(changeListener);
0226:            }
0227:
0228:            private static int convertLocation(String preference) {
0229:                if (IWorkbenchPreferenceConstants.TOP_RIGHT.equals(preference)) {
0230:                    return TOP_RIGHT;
0231:                }
0232:                if (IWorkbenchPreferenceConstants.TOP_LEFT.equals(preference)) {
0233:                    return TOP_LEFT;
0234:                }
0235:                if (IWorkbenchPreferenceConstants.LEFT.equals(preference)) {
0236:                    return LEFT;
0237:                }
0238:
0239:                return TOP_RIGHT;
0240:            }
0241:
0242:            /**
0243:             * Create the contents of the receiver
0244:             * @param parent
0245:             */
0246:            public void createControl(Composite parent) {
0247:                Assert.isTrue(this .parent == null);
0248:                this .parent = parent;
0249:                // set the initial location read from the preference
0250:                setPerspectiveBarLocation(PrefUtil
0251:                        .getAPIPreferenceStore()
0252:                        .getString(
0253:                                IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR));
0254:            }
0255:
0256:            private void addPerspectiveShortcut(
0257:                    IPerspectiveDescriptor perspective,
0258:                    IWorkbenchPage workbenchPage) {
0259:                if (perspectiveBar == null) {
0260:                    return;
0261:                }
0262:
0263:                PerspectiveBarContributionItem item = new PerspectiveBarContributionItem(
0264:                        perspective, workbenchPage);
0265:                perspectiveBar.addItem(item);
0266:                setCoolItemSize(coolItem);
0267:                // This is need to update the vertical size of the tool bar on GTK+ when
0268:                // using large fonts.
0269:                if (perspectiveBar != null) {
0270:                    perspectiveBar.update(true);
0271:                }
0272:            }
0273:
0274:            /**
0275:             * Find a contribution item that matches the perspective provided.
0276:             * 
0277:             * @param perspective
0278:             * @param page
0279:             * @return the <code>IContributionItem</code> or null if no matches were found
0280:             */
0281:            public IContributionItem findPerspectiveShortcut(
0282:                    IPerspectiveDescriptor perspective, IWorkbenchPage page) {
0283:                if (perspectiveBar == null) {
0284:                    return null;
0285:                }
0286:
0287:                IContributionItem[] items = perspectiveBar.getItems();
0288:                int length = items.length;
0289:                for (int i = 0; i < length; i++) {
0290:                    IContributionItem item = items[i];
0291:                    if (item instanceof  PerspectiveBarContributionItem
0292:                            && ((PerspectiveBarContributionItem) item).handles(
0293:                                    perspective, page)) {
0294:                        return item;
0295:                    }
0296:                }
0297:                return null;
0298:            }
0299:
0300:            private void removePerspectiveShortcut(
0301:                    IPerspectiveDescriptor perspective, IWorkbenchPage page) {
0302:                if (perspectiveBar == null) {
0303:                    return;
0304:                }
0305:
0306:                IContributionItem item = findPerspectiveShortcut(perspective,
0307:                        page);
0308:                if (item != null) {
0309:                    if (item instanceof  PerspectiveBarContributionItem) {
0310:                        perspectiveBar
0311:                                .removeItem((PerspectiveBarContributionItem) item);
0312:                    }
0313:                    item.dispose();
0314:                    perspectiveBar.update(false);
0315:                    setCoolItemSize(coolItem);
0316:                }
0317:            }
0318:
0319:            /**
0320:             * Locate the perspective bar according to the provided location
0321:             * @param preference the location to put the perspective bar at
0322:             */
0323:            public void setPerspectiveBarLocation(String preference) {
0324:                // return if the control has not been created. createControl(...) will
0325:                // handle updating the state in that case
0326:                if (parent == null) {
0327:                    return;
0328:                }
0329:                int newLocation = convertLocation(preference);
0330:                if (newLocation == currentLocation) {
0331:                    return;
0332:                }
0333:                createControlForLocation(newLocation);
0334:                currentLocation = newLocation;
0335:                showPerspectiveBar();
0336:                if (newLocation == TOP_LEFT || newLocation == TOP_RIGHT) {
0337:                    updatePerspectiveBar();
0338:                    updateBarParent();
0339:                }
0340:            }
0341:
0342:            /**
0343:             * Make the perspective bar visible in its current location. This method
0344:             * should not be used unless the control has been successfully created.
0345:             */
0346:            private void showPerspectiveBar() {
0347:                switch (currentLocation) {
0348:                case TOP_LEFT:
0349:                    topBar.setRight(null);
0350:                    topBar.setBottom(perspectiveCoolBarWrapper.getControl());
0351:                    break;
0352:                case TOP_RIGHT:
0353:                    topBar.setBottom(null);
0354:                    topBar.setRight(perspectiveCoolBarWrapper.getControl());
0355:                    topBar.setRightWidth(getDefaultWidth());
0356:                    break;
0357:                case LEFT:
0358:                    topBar.setBottom(null);
0359:                    topBar.setRight(null);
0360:                    LayoutUtil.resize(topBar);
0361:                    getTrimManager().addTrim(SWT.LEFT, this );
0362:                    break;
0363:                default:
0364:                    return;
0365:                }
0366:
0367:                LayoutUtil.resize(perspectiveBar.getControl());
0368:            }
0369:
0370:            /**
0371:             * Returns the default width for the switcher.
0372:             */
0373:            private int getDefaultWidth() {
0374:                String extras = PrefUtil.getAPIPreferenceStore().getString(
0375:                        IWorkbenchPreferenceConstants.PERSPECTIVE_BAR_EXTRAS);
0376:                StringTokenizer tok = new StringTokenizer(extras, ", "); //$NON-NLS-1$
0377:                int numExtras = tok.countTokens();
0378:                int numPersps = Math.max(numExtras, 1); // assume initial perspective is also listed in extras
0379:                return Math.max(MIN_DEFAULT_WIDTH, MIN_WIDTH
0380:                        + (numPersps * ITEM_WIDTH));
0381:            }
0382:
0383:            /**
0384:             * Get the trim manager from the default workbench window. If the current
0385:             * workbench window is -not- the <code>WorkbenchWindow</code> then return null.
0386:             *  
0387:             * @return The trim manager for the current workbench window
0388:             */
0389:            private ITrimManager getTrimManager() {
0390:                if (window instanceof  WorkbenchWindow)
0391:                    return ((WorkbenchWindow) window).getTrimManager();
0392:
0393:                return null; // not using the default workbench window
0394:            }
0395:
0396:            /**
0397:             * Update the receiver
0398:             * @param force
0399:             */
0400:            public void update(boolean force) {
0401:                if (perspectiveBar == null) {
0402:                    return;
0403:                }
0404:
0405:                perspectiveBar.update(force);
0406:
0407:                if (currentLocation == LEFT) {
0408:                    ToolItem[] items = perspectiveBar.getControl().getItems();
0409:                    boolean shouldExpand = items.length > 0;
0410:                    if (shouldExpand != trimVisible) {
0411:                        perspectiveBar.getControl().setVisible(true);
0412:                        trimVisible = shouldExpand;
0413:                    }
0414:
0415:                    if (items.length != trimOldLength) {
0416:                        LayoutUtil.resize(trimControl);
0417:                        trimOldLength = items.length;
0418:                    }
0419:                }
0420:            }
0421:
0422:            private void selectPerspectiveShortcut(
0423:                    IPerspectiveDescriptor perspective, IWorkbenchPage page,
0424:                    boolean selected) {
0425:                IContributionItem item = findPerspectiveShortcut(perspective,
0426:                        page);
0427:                if (item != null
0428:                        && (item instanceof  PerspectiveBarContributionItem)) {
0429:                    if (selected) {
0430:                        // check if not visible and ensure visible
0431:                        PerspectiveBarContributionItem contribItem = (PerspectiveBarContributionItem) item;
0432:                        perspectiveBar.select(contribItem);
0433:                    }
0434:                    // select or de-select
0435:                    ((PerspectiveBarContributionItem) item)
0436:                            .setSelection(selected);
0437:                }
0438:            }
0439:
0440:            private void updatePerspectiveShortcut(
0441:                    IPerspectiveDescriptor oldDesc,
0442:                    IPerspectiveDescriptor newDesc, IWorkbenchPage page) {
0443:                IContributionItem item = findPerspectiveShortcut(oldDesc, page);
0444:                if (item != null
0445:                        && (item instanceof  PerspectiveBarContributionItem)) {
0446:                    ((PerspectiveBarContributionItem) item).update(newDesc);
0447:                }
0448:            }
0449:
0450:            /**
0451:             * Answer the perspective bar manager
0452:             * @return the manager
0453:             */
0454:            public PerspectiveBarManager getPerspectiveBar() {
0455:                return perspectiveBar;
0456:            }
0457:
0458:            /**
0459:             * Dispose resources being held by the receiver
0460:             */
0461:            public void dispose() {
0462:                window.removePerspectiveListener(changeListener);
0463:                window.removePageListener(changeListener);
0464:                if (propertyChangeListener != null) {
0465:                    apiPreferenceStore
0466:                            .removePropertyChangeListener(propertyChangeListener);
0467:                    propertyChangeListener = null;
0468:                }
0469:                unhookDragSupport();
0470:                disposeChildControls();
0471:                toolBarListener = null;
0472:            }
0473:
0474:            private void disposeChildControls() {
0475:
0476:                if (trimControl != null) {
0477:                    trimControl.dispose();
0478:                    trimControl = null;
0479:                }
0480:
0481:                if (trimSeparator != null) {
0482:                    trimSeparator.dispose();
0483:                    trimSeparator = null;
0484:                }
0485:
0486:                if (perspectiveCoolBar != null) {
0487:                    perspectiveCoolBar.dispose();
0488:                    perspectiveCoolBar = null;
0489:                }
0490:
0491:                if (toolbarWrapper != null) {
0492:                    toolbarWrapper.dispose();
0493:                    toolbarWrapper = null;
0494:                }
0495:
0496:                if (perspectiveBar != null) {
0497:                    perspectiveBar.dispose();
0498:                    perspectiveBar = null;
0499:                }
0500:
0501:                perspectiveCoolBarWrapper = null;
0502:            }
0503:
0504:            /**
0505:             * Ensures the control has been set for the argument location. If the
0506:             * control already exists and can be used the argument location, nothing
0507:             * happens. Updates the location attribute.
0508:             * 
0509:             * @param newLocation
0510:             */
0511:            private void createControlForLocation(int newLocation) {
0512:                // if there is a control, then perhaps it can be reused
0513:                if (perspectiveBar != null
0514:                        && perspectiveBar.getControl() != null
0515:                        && !perspectiveBar.getControl().isDisposed()) {
0516:                    if (newLocation == LEFT && currentLocation == LEFT) {
0517:                        return;
0518:                    }
0519:                    if ((newLocation == TOP_LEFT || newLocation == TOP_RIGHT)
0520:                            && (currentLocation == TOP_LEFT || currentLocation == TOP_RIGHT)) {
0521:                        return;
0522:                    }
0523:                }
0524:
0525:                if (perspectiveBar != null) {
0526:                    perspectiveBar.getControl().removeDisposeListener(
0527:                            toolBarListener);
0528:                    unhookDragSupport();
0529:                }
0530:                // otherwise dispose the current controls and make new ones
0531:
0532:                // First, make sure that the existing verion is removed from the trim layout
0533:                getTrimManager().removeTrim(this );
0534:
0535:                disposeChildControls();
0536:                if (newLocation == LEFT) {
0537:                    createControlForLeft();
0538:                } else {
0539:                    createControlForTop();
0540:                }
0541:                hookDragSupport();
0542:
0543:                perspectiveBar.getControl().addDisposeListener(toolBarListener);
0544:            }
0545:
0546:            /**
0547:             * Remove any drag and drop support and associated listeners hooked for the
0548:             * perspective switcher.
0549:             */
0550:            private void unhookDragSupport() {
0551:                ToolBar bar = perspectiveBar.getControl();
0552:
0553:                if (bar == null || bar.isDisposed() || dragListener == null) {
0554:                    return;
0555:                }
0556:                PresentationUtil.removeDragListener(bar, dragListener);
0557:                DragUtil.removeDragTarget(perspectiveBar.getControl(),
0558:                        dragTarget);
0559:                dragListener = null;
0560:                dragTarget = null;
0561:            }
0562:
0563:            /**
0564:             * Attach drag and drop support and associated listeners hooked for 
0565:             * the perspective switcher.
0566:             */
0567:            private void hookDragSupport() {
0568:                dragListener = new Listener() {
0569:                    /* (non-Javadoc)
0570:                     * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
0571:                     */
0572:                    public void handleEvent(Event event) {
0573:                        ToolBar toolbar = perspectiveBar.getControl();
0574:                        ToolItem item = toolbar.getItem(new Point(event.x,
0575:                                event.y));
0576:
0577:                        if (item != null) {
0578:                            //ignore the first item, which remains in position Zero
0579:                            if (item.getData() instanceof  PerspectiveBarNewContributionItem) {
0580:                                return;
0581:                            }
0582:
0583:                            Rectangle bounds = item.getBounds();
0584:                            Rectangle parentBounds = toolbar.getBounds();
0585:                            bounds.x += parentBounds.x;
0586:                            bounds.y += parentBounds.y;
0587:                            startDragging(item.getData(), toolbar.getDisplay()
0588:                                    .map(toolbar, null, bounds));
0589:                        } else {
0590:                            //startDragging(toolbar, toolbar.getDisplay().map(toolbar, null, toolbar.getBounds()));
0591:                        }
0592:                    }
0593:
0594:                    private void startDragging(Object widget, Rectangle bounds) {
0595:                        if (!DragUtil.performDrag(widget, bounds, new Point(
0596:                                bounds.x, bounds.y), true)) {
0597:                            //currently do nothing on a failed drag 
0598:                        }
0599:                    }
0600:                };
0601:
0602:                dragTarget = new IDragOverListener() {
0603:                    protected PerspectiveDropTarget perspectiveDropTarget;
0604:
0605:                    class PerspectiveDropTarget extends AbstractDropTarget {
0606:
0607:                        private PerspectiveBarContributionItem perspective;
0608:
0609:                        private Point location;
0610:
0611:                        /**
0612:                         * @param location
0613:                         * @param draggedObject
0614:                         */
0615:                        public PerspectiveDropTarget(Object draggedObject,
0616:                                Point location) {
0617:                            update(draggedObject, location);
0618:                        }
0619:
0620:                        /**
0621:                         * 
0622:                         * @param draggedObject
0623:                         * @param location
0624:                         */
0625:                        private void update(Object draggedObject, Point location) {
0626:                            this .location = location;
0627:                            this .perspective = (PerspectiveBarContributionItem) draggedObject;
0628:                        }
0629:
0630:                        /*
0631:                         * (non-Javadoc)
0632:                         * 
0633:                         * @see org.eclipse.ui.internal.dnd.IDropTarget#drop()
0634:                         */
0635:                        public void drop() {
0636:                            ToolBar toolBar = perspectiveBar.getControl();
0637:                            ToolItem item = toolBar.getItem(toolBar
0638:                                    .getDisplay().map(null, toolBar, location));
0639:                            if (toolBar.getItem(0) == item) {
0640:                                return;
0641:                            }
0642:                            ToolItem[] items = toolBar.getItems();
0643:                            ToolItem droppedItem = null;
0644:                            int dropIndex = -1;
0645:                            for (int i = 0; i < items.length; i++) {
0646:                                if (item == items[i]) {
0647:                                    dropIndex = i;
0648:                                }
0649:                                if (items[i].getData() == perspective) {
0650:                                    droppedItem = items[i];
0651:                                }
0652:                            }
0653:                            if (dropIndex != -1 && droppedItem != null
0654:                                    && (droppedItem != item)) {
0655:                                PerspectiveBarContributionItem barItem = (PerspectiveBarContributionItem) droppedItem
0656:                                        .getData();
0657:                                // policy is to insert at the beginning so mirror the value when indicating a 
0658:                                // new position for the perspective
0659:                                if (reorderListener != null) {
0660:                                    reorderListener.reorder(barItem
0661:                                            .getPerspective(),
0662:                                            Math.abs(dropIndex
0663:                                                    - (items.length - 1)));
0664:                                }
0665:
0666:                                perspectiveBar.relocate(barItem, dropIndex);
0667:                            }
0668:                        }
0669:
0670:                        /*
0671:                         * (non-Javadoc)
0672:                         * 
0673:                         * @see org.eclipse.ui.internal.dnd.IDropTarget#getCursor()
0674:                         */
0675:                        public Cursor getCursor() {
0676:                            return DragCursors.getCursor(DragCursors.CENTER);
0677:                        }
0678:
0679:                        boolean sameShell() {
0680:                            return perspective.getToolItem().getParent()
0681:                                    .getShell().equals(
0682:                                            perspectiveBar.getControl()
0683:                                                    .getShell());
0684:                        }
0685:
0686:                        public Rectangle getSnapRectangle() {
0687:                            ToolBar toolBar = perspectiveBar.getControl();
0688:                            ToolItem item = toolBar.getItem(toolBar
0689:                                    .getDisplay().map(null, toolBar, location));
0690:                            Rectangle bounds;
0691:                            if (item != null && item != toolBar.getItem(0)) {
0692:                                bounds = item.getBounds();
0693:                            } else {
0694:                                // it should not be possible to start a drag with item 0
0695:                                return null;
0696:                            }
0697:                            return toolBar.getDisplay().map(toolBar, null,
0698:                                    bounds);
0699:                        }
0700:                    }
0701:
0702:                    public IDropTarget drag(Control currentControl,
0703:                            Object draggedObject, Point position,
0704:                            Rectangle dragRectangle) {
0705:                        if (draggedObject instanceof  PerspectiveBarContributionItem) {
0706:                            if (perspectiveDropTarget == null) {
0707:                                perspectiveDropTarget = new PerspectiveDropTarget(
0708:                                        draggedObject, position);
0709:                            } else {
0710:                                perspectiveDropTarget.update(draggedObject,
0711:                                        position);
0712:                            }
0713:                            // do not support drag to perspective bars between shells.
0714:                            if (!perspectiveDropTarget.sameShell()) {
0715:                                return null;
0716:                            }
0717:
0718:                            return perspectiveDropTarget;
0719:                        }// else if (draggedObject instanceof IPerspectiveBar) {
0720:                        //	return new PerspectiveBarDropTarget();
0721:                        //}
0722:
0723:                        return null;
0724:                    }
0725:
0726:                };
0727:
0728:                PresentationUtil.addDragListener(perspectiveBar.getControl(),
0729:                        dragListener);
0730:                DragUtil.addDragTarget(perspectiveBar.getControl(), dragTarget);
0731:            }
0732:
0733:            private void setPropertyChangeListener() {
0734:                propertyChangeListener = new IPropertyChangeListener() {
0735:
0736:                    public void propertyChange(
0737:                            PropertyChangeEvent propertyChangeEvent) {
0738:                        if (IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR
0739:                                .equals(propertyChangeEvent.getProperty())) {
0740:                            if (perspectiveBar == null) {
0741:                                return;
0742:                            }
0743:                            updatePerspectiveBar();
0744:                            updateBarParent();
0745:                        }
0746:                    }
0747:                };
0748:                apiPreferenceStore
0749:                        .addPropertyChangeListener(propertyChangeListener);
0750:            }
0751:
0752:            private void createControlForLeft() {
0753:                trimControl = new Composite(parent, SWT.NONE);
0754:
0755:                trimControl.setLayout(new CellLayout(1).setMargins(0, 0)
0756:                        .setSpacing(3, 3).setDefaultRow(Row.fixed())
0757:                        .setDefaultColumn(Row.growing()));
0758:
0759:                perspectiveBar = createBarManager(SWT.VERTICAL);
0760:
0761:                perspectiveBar.createControl(trimControl);
0762:                perspectiveBar.getControl().addListener(SWT.MenuDetect,
0763:                        popupListener);
0764:
0765:                //        trimSeparator = new Label(trimControl, SWT.SEPARATOR | SWT.HORIZONTAL);
0766:                //        GridData sepData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING
0767:                //                | GridData.HORIZONTAL_ALIGN_CENTER);
0768:                //        sepData.widthHint = SEPARATOR_LENGTH;
0769:                //        trimSeparator.setLayoutData(sepData);
0770:                //        trimSeparator.setVisible(false);
0771:
0772:                trimLayoutData = new GridData(GridData.FILL_BOTH);
0773:                trimVisible = false;
0774:                perspectiveBar.getControl().setLayoutData(trimLayoutData);
0775:            }
0776:
0777:            private void createControlForTop() {
0778:                perspectiveBar = createBarManager(SWT.HORIZONTAL);
0779:
0780:                perspectiveCoolBarWrapper = new CacheWrapper(topBar);
0781:                perspectiveCoolBar = new CoolBar(perspectiveCoolBarWrapper
0782:                        .getControl(), SWT.FLAT);
0783:                coolItem = new CoolItem(perspectiveCoolBar, SWT.DROP_DOWN);
0784:                toolbarWrapper = new CacheWrapper(perspectiveCoolBar);
0785:                perspectiveBar.createControl(toolbarWrapper.getControl());
0786:                coolItem.setControl(toolbarWrapper.getControl());
0787:                perspectiveCoolBar.setLocked(true);
0788:                perspectiveBar.setParent(perspectiveCoolBar);
0789:                perspectiveBar.update(true);
0790:
0791:                // adjust the toolbar size to display as many items as possible
0792:                perspectiveCoolBar.addControlListener(new ControlAdapter() {
0793:                    public void controlResized(ControlEvent e) {
0794:                        setCoolItemSize(coolItem);
0795:                    }
0796:                });
0797:
0798:                coolItem.addSelectionListener(new SelectionAdapter() {
0799:                    public void widgetSelected(SelectionEvent e) {
0800:                        if (e.detail == SWT.ARROW) {
0801:                            if (perspectiveBar != null) {
0802:                                perspectiveBar.handleChevron(e);
0803:                            }
0804:                        }
0805:                    }
0806:                });
0807:                coolItem.setMinimumSize(0, 0);
0808:                perspectiveBar.getControl().addListener(SWT.MenuDetect,
0809:                        popupListener);
0810:            }
0811:
0812:            /**
0813:             * @param coolItem
0814:             */
0815:            private void setCoolItemSize(final CoolItem coolItem) {
0816:                // there is no coolItem when the bar is on the left
0817:                if (currentLocation == LEFT) {
0818:                    return;
0819:                }
0820:
0821:                ToolBar toolbar = perspectiveBar.getControl();
0822:                if (toolbar == null) {
0823:                    return;
0824:                }
0825:
0826:                int rowHeight = 0;
0827:                ToolItem[] toolItems = toolbar.getItems();
0828:                for (int i = 0; i < toolItems.length; i++) {
0829:                    rowHeight = Math.max(rowHeight,
0830:                            toolItems[i].getBounds().height);
0831:                }
0832:
0833:                Rectangle area = perspectiveCoolBar.getClientArea();
0834:                int rows = rowHeight <= 0 ? 1 : (int) Math.max(1, Math
0835:                        .floor(area.height / rowHeight));
0836:                if (rows == 1 || (toolbar.getStyle() & SWT.WRAP) == 0
0837:                        || currentLocation == TOP_LEFT) {
0838:                    Point p = toolbar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
0839:                    coolItem.setSize(coolItem.computeSize(p.x, p.y));
0840:                    return;
0841:                }
0842:                Point offset = coolItem.computeSize(0, 0);
0843:                Point wrappedSize = toolbar.computeSize(area.width - offset.x,
0844:                        SWT.DEFAULT);
0845:                int h = rows * rowHeight;
0846:                int w = wrappedSize.y <= h ? wrappedSize.x : wrappedSize.x + 1;
0847:                coolItem.setSize(coolItem.computeSize(w, h));
0848:            }
0849:
0850:            private void showPerspectiveBarPopup(Point pt) {
0851:                if (perspectiveBar == null) {
0852:                    return;
0853:                }
0854:
0855:                // Get the tool item under the mouse.
0856:                ToolBar toolBar = perspectiveBar.getControl();
0857:                ToolItem toolItem = toolBar.getItem(toolBar.toControl(pt));
0858:
0859:                // Get the action for the tool item.
0860:                Object data = null;
0861:                if (toolItem != null) {
0862:                    data = toolItem.getData();
0863:                }
0864:                if (toolItem == null
0865:                        || !(data instanceof  PerspectiveBarContributionItem)) {
0866:                    if (genericMenu == null) {
0867:                        Menu menu = new Menu(toolBar);
0868:                        addDockOnSubMenu(menu);
0869:                        addShowTextItem(menu);
0870:                        genericMenu = menu;
0871:                    }
0872:
0873:                    // set the state of the menu items to match the preferences
0874:                    genericMenu
0875:                            .getItem(1)
0876:                            .setSelection(
0877:                                    PrefUtil
0878:                                            .getAPIPreferenceStore()
0879:                                            .getBoolean(
0880:                                                    IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR));
0881:                    updateLocationItems(genericMenu.getItem(0).getMenu(),
0882:                            currentLocation);
0883:
0884:                    // Show popup menu.
0885:                    genericMenu.setLocation(pt.x, pt.y);
0886:                    genericMenu.setVisible(true);
0887:                    return;
0888:                }
0889:
0890:                if (data == null
0891:                        || !(data instanceof  PerspectiveBarContributionItem)) {
0892:                    return;
0893:                }
0894:
0895:                PerspectiveBarContributionItem pbci = (PerspectiveBarContributionItem) data;
0896:                IPerspectiveDescriptor selectedPerspective = pbci
0897:                        .getPerspective();
0898:
0899:                // The perspective bar menu is created lazily here.
0900:                // Its data is set (each time) to the tool item, which refers to the SetPagePerspectiveAction
0901:                // which in turn refers to the page and perspective.
0902:                // It is important not to refer to the action, the page or the perspective directly
0903:                // since otherwise the menu hangs on to them after they are closed.
0904:                // By hanging onto the tool item instead, these references are cleared when the
0905:                // corresponding page or perspective is closed.
0906:                // See bug 11282 for more details on why it is done this way.
0907:                if (popupMenu != null) {
0908:                    popupMenu.dispose();
0909:                    popupMenu = null;
0910:                }
0911:                popupMenu = createPopup(toolBar, selectedPerspective);
0912:                popupMenu.setData(toolItem);
0913:
0914:                // Show popup menu.
0915:                popupMenu.setLocation(pt.x, pt.y);
0916:                popupMenu.setVisible(true);
0917:            }
0918:
0919:            /**
0920:             * @param persp the perspective
0921:             * @return <code>true</code> if the perspective is active in the active page 
0922:             */
0923:            private boolean perspectiveIsActive(IPerspectiveDescriptor persp) {
0924:                IWorkbenchPage page = window.getActivePage();
0925:                return page != null && persp.equals(page.getPerspective());
0926:            }
0927:
0928:            /**
0929:             * @param persp the perspective
0930:             * @return <code>true</code> if the perspective is open in the active page 
0931:             */
0932:            private boolean perspectiveIsOpen(IPerspectiveDescriptor persp) {
0933:                IWorkbenchPage page = window.getActivePage();
0934:                return page != null
0935:                        && Arrays.asList(page.getOpenPerspectives()).contains(
0936:                                persp);
0937:            }
0938:
0939:            private Menu createPopup(ToolBar toolBar,
0940:                    IPerspectiveDescriptor persp) {
0941:                Menu menu = new Menu(toolBar);
0942:                if (perspectiveIsActive(persp)) {
0943:                    addCustomizeItem(menu);
0944:                    addSaveAsItem(menu);
0945:                    addResetItem(menu);
0946:                }
0947:                if (perspectiveIsOpen(persp)) {
0948:                    addCloseItem(menu);
0949:                }
0950:
0951:                new MenuItem(menu, SWT.SEPARATOR);
0952:                addDockOnSubMenu(menu);
0953:                addShowTextItem(menu);
0954:                return menu;
0955:            }
0956:
0957:            private void addCloseItem(Menu menu) {
0958:                MenuItem menuItem = new MenuItem(menu, SWT.NONE);
0959:                menuItem.setText(WorkbenchMessages.WorkbenchWindow_close);
0960:                window.getWorkbench().getHelpSystem().setHelp(menuItem,
0961:                        IWorkbenchHelpContextIds.CLOSE_PAGE_ACTION);
0962:                menuItem.addSelectionListener(new SelectionAdapter() {
0963:                    public void widgetSelected(SelectionEvent e) {
0964:                        ToolItem perspectiveToolItem = (ToolItem) popupMenu
0965:                                .getData();
0966:                        if (perspectiveToolItem != null
0967:                                && !perspectiveToolItem.isDisposed()) {
0968:                            PerspectiveBarContributionItem item = (PerspectiveBarContributionItem) perspectiveToolItem
0969:                                    .getData();
0970:                            item.getPage().closePerspective(
0971:                                    item.getPerspective(), true, true);
0972:                        }
0973:                    }
0974:                });
0975:            }
0976:
0977:            /**
0978:             * @param direction one of <code>SWT.HORIZONTAL</code> or <code>SWT.VERTICAL</code>
0979:             */
0980:            private PerspectiveBarManager createBarManager(int direction) {
0981:                PerspectiveBarManager barManager = new PerspectiveBarManager(
0982:                        style | direction);
0983:                if (apiPreferenceStore
0984:                        .getBoolean(IWorkbenchPreferenceConstants.SHOW_OPEN_ON_PERSPECTIVE_BAR)) {
0985:                    barManager
0986:                            .add(new PerspectiveBarNewContributionItem(window));
0987:                }
0988:
0989:                // add an item for all open perspectives
0990:                IWorkbenchPage page = window.getActivePage();
0991:                if (page != null) {
0992:                    // these are returned with the most recently opened one first
0993:                    IPerspectiveDescriptor[] perspectives = page
0994:                            .getOpenPerspectives();
0995:                    for (int i = 0; i < perspectives.length; i++) {
0996:                        barManager.insert(1,
0997:                                new PerspectiveBarContributionItem(
0998:                                        perspectives[i], page));
0999:                    }
1000:                }
1001:
1002:                return barManager;
1003:            }
1004:
1005:            private void updateLocationItems(Menu parent, int newLocation) {
1006:                MenuItem left;
1007:                MenuItem topLeft;
1008:                MenuItem topRight;
1009:
1010:                topRight = parent.getItem(0);
1011:                topLeft = parent.getItem(1);
1012:                left = parent.getItem(2);
1013:
1014:                if (newLocation == LEFT) {
1015:                    left.setSelection(true);
1016:                    topRight.setSelection(false);
1017:                    topLeft.setSelection(false);
1018:                } else if (newLocation == TOP_LEFT) {
1019:                    topLeft.setSelection(true);
1020:                    left.setSelection(false);
1021:                    topRight.setSelection(false);
1022:                } else {
1023:                    topRight.setSelection(true);
1024:                    left.setSelection(false);
1025:                    topLeft.setSelection(false);
1026:                }
1027:            }
1028:
1029:            private void addDockOnSubMenu(Menu menu) {
1030:                MenuItem item = new MenuItem(menu, SWT.CASCADE);
1031:                item.setText(WorkbenchMessages.PerspectiveSwitcher_dockOn);
1032:
1033:                final Menu subMenu = new Menu(item);
1034:
1035:                final MenuItem menuItemTopRight = new MenuItem(subMenu,
1036:                        SWT.RADIO);
1037:                menuItemTopRight
1038:                        .setText(WorkbenchMessages.PerspectiveSwitcher_topRight);
1039:
1040:                window.getWorkbench().getHelpSystem().setHelp(menuItemTopRight,
1041:                        IWorkbenchHelpContextIds.DOCK_ON_PERSPECTIVE_ACTION);
1042:
1043:                final MenuItem menuItemTopLeft = new MenuItem(subMenu,
1044:                        SWT.RADIO);
1045:                menuItemTopLeft
1046:                        .setText(WorkbenchMessages.PerspectiveSwitcher_topLeft);
1047:
1048:                window.getWorkbench().getHelpSystem().setHelp(menuItemTopLeft,
1049:                        IWorkbenchHelpContextIds.DOCK_ON_PERSPECTIVE_ACTION);
1050:
1051:                final MenuItem menuItemLeft = new MenuItem(subMenu, SWT.RADIO);
1052:                menuItemLeft
1053:                        .setText(WorkbenchMessages.PerspectiveSwitcher_left);
1054:
1055:                window.getWorkbench().getHelpSystem().setHelp(menuItemLeft,
1056:                        IWorkbenchHelpContextIds.DOCK_ON_PERSPECTIVE_ACTION);
1057:
1058:                SelectionListener listener = new SelectionAdapter() {
1059:                    public void widgetSelected(SelectionEvent e) {
1060:                        MenuItem item = (MenuItem) e.widget;
1061:                        String pref = null;
1062:                        if (item.equals(menuItemLeft)) {
1063:                            updateLocationItems(subMenu, LEFT);
1064:                            pref = IWorkbenchPreferenceConstants.LEFT;
1065:                        } else if (item.equals(menuItemTopLeft)) {
1066:                            updateLocationItems(subMenu, TOP_LEFT);
1067:                            pref = IWorkbenchPreferenceConstants.TOP_LEFT;
1068:                        } else {
1069:                            updateLocationItems(subMenu, TOP_RIGHT);
1070:                            pref = IWorkbenchPreferenceConstants.TOP_RIGHT;
1071:                        }
1072:                        IPreferenceStore apiStore = PrefUtil
1073:                                .getAPIPreferenceStore();
1074:                        if (!pref
1075:                                .equals(apiStore
1076:                                        .getDefaultString(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR))) {
1077:                            PrefUtil.getInternalPreferenceStore().setValue(
1078:                                    IPreferenceConstants.OVERRIDE_PRESENTATION,
1079:                                    true);
1080:                        }
1081:                        apiStore
1082:                                .setValue(
1083:                                        IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR,
1084:                                        pref);
1085:                    }
1086:                };
1087:
1088:                menuItemTopRight.addSelectionListener(listener);
1089:                menuItemTopLeft.addSelectionListener(listener);
1090:                menuItemLeft.addSelectionListener(listener);
1091:                item.setMenu(subMenu);
1092:                updateLocationItems(subMenu, currentLocation);
1093:            }
1094:
1095:            private void addShowTextItem(Menu menu) {
1096:                final MenuItem showtextMenuItem = new MenuItem(menu, SWT.CHECK);
1097:                showtextMenuItem
1098:                        .setText(WorkbenchMessages.PerspectiveBar_showText);
1099:                window.getWorkbench().getHelpSystem().setHelp(showtextMenuItem,
1100:                        IWorkbenchHelpContextIds.SHOW_TEXT_PERSPECTIVE_ACTION);
1101:
1102:                showtextMenuItem.addSelectionListener(new SelectionAdapter() {
1103:                    public void widgetSelected(SelectionEvent e) {
1104:                        if (perspectiveBar == null) {
1105:                            return;
1106:                        }
1107:
1108:                        boolean preference = showtextMenuItem.getSelection();
1109:                        if (preference != PrefUtil
1110:                                .getAPIPreferenceStore()
1111:                                .getDefaultBoolean(
1112:                                        IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR)) {
1113:                            PrefUtil.getInternalPreferenceStore().setValue(
1114:                                    IPreferenceConstants.OVERRIDE_PRESENTATION,
1115:                                    true);
1116:                        }
1117:                        PrefUtil
1118:                                .getAPIPreferenceStore()
1119:                                .setValue(
1120:                                        IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR,
1121:                                        preference);
1122:                    }
1123:                });
1124:                showtextMenuItem
1125:                        .setSelection(PrefUtil
1126:                                .getAPIPreferenceStore()
1127:                                .getBoolean(
1128:                                        IWorkbenchPreferenceConstants.SHOW_TEXT_ON_PERSPECTIVE_BAR));
1129:            }
1130:
1131:            private void addCustomizeItem(Menu menu) {
1132:                final MenuItem customizeMenuItem = new MenuItem(menu,
1133:                        SWT.Activate);
1134:                customizeMenuItem
1135:                        .setText(WorkbenchMessages.PerspectiveBar_customize);
1136:                window.getWorkbench().getHelpSystem().setHelp(
1137:                        customizeMenuItem,
1138:                        IWorkbenchHelpContextIds.EDIT_ACTION_SETS_ACTION);
1139:                customizeMenuItem.addSelectionListener(new SelectionAdapter() {
1140:                    public void widgetSelected(SelectionEvent e) {
1141:                        if (perspectiveBar == null) {
1142:                            return;
1143:                        }
1144:                        EditActionSetsAction editAction = new EditActionSetsAction(
1145:                                window);
1146:                        editAction.setEnabled(true);
1147:                        editAction.run();
1148:                    }
1149:                });
1150:            }
1151:
1152:            private void addSaveAsItem(Menu menu) {
1153:                final MenuItem saveasMenuItem = new MenuItem(menu, SWT.Activate);
1154:                saveasMenuItem.setText(WorkbenchMessages.PerspectiveBar_saveAs);
1155:                window.getWorkbench().getHelpSystem().setHelp(saveasMenuItem,
1156:                        IWorkbenchHelpContextIds.SAVE_PERSPECTIVE_ACTION);
1157:                saveasMenuItem.addSelectionListener(new SelectionAdapter() {
1158:                    public void widgetSelected(SelectionEvent e) {
1159:                        if (perspectiveBar == null) {
1160:                            return;
1161:                        }
1162:                        SavePerspectiveAction saveAction = new SavePerspectiveAction(
1163:                                window);
1164:                        saveAction.setEnabled(true);
1165:                        saveAction.run();
1166:                    }
1167:                });
1168:            }
1169:
1170:            private void addResetItem(Menu menu) {
1171:                final MenuItem resetMenuItem = new MenuItem(menu, SWT.Activate);
1172:                resetMenuItem.setText(WorkbenchMessages.PerspectiveBar_reset);
1173:                window.getWorkbench().getHelpSystem().setHelp(resetMenuItem,
1174:                        IWorkbenchHelpContextIds.RESET_PERSPECTIVE_ACTION);
1175:                resetMenuItem.addSelectionListener(new SelectionAdapter() {
1176:                    public void widgetSelected(SelectionEvent e) {
1177:                        if (perspectiveBar == null) {
1178:                            return;
1179:                        }
1180:                        ResetPerspectiveAction resetAction = new ResetPerspectiveAction(
1181:                                window);
1182:                        resetAction.setEnabled(true);
1183:                        resetAction.run();
1184:                    }
1185:                });
1186:            }
1187:
1188:            /**
1189:             * Method to save the width of the perspective bar in the 
1190:             * @param persBarMem 
1191:             */
1192:            public void saveState(IMemento persBarMem) {
1193:                // save the width of the perspective bar
1194:                IMemento childMem = persBarMem
1195:                        .createChild(IWorkbenchConstants.TAG_ITEM_SIZE);
1196:
1197:                int x;
1198:                if (currentLocation == TOP_RIGHT && topBar != null) {
1199:                    x = topBar.getRightWidth();
1200:                } else {
1201:                    x = getDefaultWidth();
1202:                }
1203:
1204:                childMem.putString(IWorkbenchConstants.TAG_X, Integer
1205:                        .toString(x));
1206:            }
1207:
1208:            /**
1209:             * Method to restore the width of the perspective bar
1210:             * @param memento 
1211:             */
1212:            public void restoreState(IMemento memento) {
1213:                if (memento == null) {
1214:                    return;
1215:                }
1216:                // restore the width of the perspective bar
1217:                IMemento attributes = memento
1218:                        .getChild(IWorkbenchConstants.TAG_PERSPECTIVE_BAR);
1219:                IMemento size = null;
1220:                if (attributes != null) {
1221:                    size = attributes
1222:                            .getChild(IWorkbenchConstants.TAG_ITEM_SIZE);
1223:                }
1224:                if (size != null && currentLocation == TOP_RIGHT
1225:                        && topBar != null) {
1226:                    final Integer x = size
1227:                            .getInteger(IWorkbenchConstants.TAG_X);
1228:                    StartupThreading
1229:                            .runWithoutExceptions(new StartupRunnable() {
1230:
1231:                                public void runWithException() {
1232:                                    if (x != null) {
1233:                                        topBar.setRightWidth(x.intValue());
1234:                                    } else {
1235:                                        topBar.setRightWidth(getDefaultWidth());
1236:                                    }
1237:                                }
1238:                            });
1239:                }
1240:            }
1241:
1242:            /**
1243:             * Method to rebuild and update the toolbar when necessary
1244:             */
1245:            void updatePerspectiveBar() {
1246:                // Update each item as the text may have to be shortened.
1247:                IContributionItem[] items = perspectiveBar.getItems();
1248:                for (int i = 0; i < items.length; i++) {
1249:                    items[i].update();
1250:                }
1251:                // make sure the selected item is visible
1252:                perspectiveBar.arrangeToolbar();
1253:                setCoolItemSize(coolItem);
1254:                perspectiveBar.getControl().redraw();
1255:            }
1256:
1257:            /**
1258:             * Updates the height of the CBanner if the perspective bar
1259:             * is docked on the top right
1260:             */
1261:            public void updateBarParent() {
1262:                if (perspectiveBar == null
1263:                        || perspectiveBar.getControl() == null) {
1264:                    return;
1265:                }
1266:
1267:                // TOP_LEFT and LEFT need only relayout in this case, however TOP_RIGHT
1268:                // will need to set the minimum height of the CBanner as it might have changed.
1269:                if (currentLocation == TOP_RIGHT && topBar != null) {
1270:                    // This gets the height of the tallest tool item.
1271:                    int maxRowHeight = 0;
1272:                    ToolItem[] toolItems = perspectiveBar.getControl()
1273:                            .getItems();
1274:                    for (int i = 0; i < toolItems.length; i++) {
1275:                        maxRowHeight = Math.max(maxRowHeight, toolItems[i]
1276:                                .getBounds().height);
1277:                    }
1278:                    // This sets the CBanner's minimum height to support large fonts
1279:                    // TODO: Actually calculate the correct 'min' size for the right side
1280:                    topBar.setRightMinimumSize(new Point(MIN_WIDTH,
1281:                            maxRowHeight));
1282:                }
1283:
1284:                LayoutUtil.resize(perspectiveBar.getControl());
1285:            }
1286:
1287:            /**
1288:             * Add a listener for reordering of perspectives (usually done through drag
1289:             * and drop).
1290:             * 
1291:             * @param listener
1292:             */
1293:            public void addReorderListener(IReorderListener listener) {
1294:                reorderListener = listener;
1295:            }
1296:
1297:            /* (non-Javadoc)
1298:             * @see org.eclipse.ui.internal.IWindowTrim#dock(int)
1299:             */
1300:            public void dock(int dropSide) {
1301:            }
1302:
1303:            /* (non-Javadoc)
1304:             * @see org.eclipse.ui.internal.IWindowTrim#getControl()
1305:             */
1306:            public Control getControl() {
1307:                return trimControl;
1308:            }
1309:
1310:            /* (non-Javadoc)
1311:             * @see org.eclipse.ui.internal.IWindowTrim#getId()
1312:             */
1313:            public String getId() {
1314:                return "org.eclipse.ui.internal.PerspectiveSwitcher"; //$NON-NLS-1$
1315:            }
1316:
1317:            /* (non-Javadoc)
1318:             * @see org.eclipse.ui.internal.IWindowTrim#getDisplayName()
1319:             */
1320:            public String getDisplayName() {
1321:                return WorkbenchMessages.TrimCommon_PerspectiveSwitcher_TrimName;
1322:            }
1323:
1324:            /* (non-Javadoc)
1325:             * @see org.eclipse.ui.internal.IWindowTrim#getValidSides()
1326:             */
1327:            public int getValidSides() {
1328:                return SWT.NONE;
1329:            }
1330:
1331:            /* (non-Javadoc)
1332:             * @see org.eclipse.ui.internal.IWindowTrim#isCloseable()
1333:             */
1334:            public boolean isCloseable() {
1335:                return false;
1336:            }
1337:
1338:            /* (non-Javadoc)
1339:             * @see org.eclipse.ui.internal.IWindowTrim#handleClose()
1340:             */
1341:            public void handleClose() {
1342:                // nothing to do...		
1343:            }
1344:
1345:            /* (non-Javadoc)
1346:             * @see org.eclipse.ui.IWindowTrim#getWidthHint()
1347:             */
1348:            public int getWidthHint() {
1349:                return SWT.DEFAULT;
1350:            }
1351:
1352:            /* (non-Javadoc)
1353:             * @see org.eclipse.ui.IWindowTrim#getHeightHint()
1354:             */
1355:            public int getHeightHint() {
1356:                return SWT.DEFAULT;
1357:            }
1358:
1359:            /* (non-Javadoc)
1360:             * @see org.eclipse.ui.IWindowTrim#isResizeable()
1361:             */
1362:            public boolean isResizeable() {
1363:                return false;
1364:            }
1365:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.