Source Code Cross Referenced for ActionDelegateHandlerProxy.java in  » IDE-Eclipse » ui-workbench » org » eclipse » ui » internal » handlers » 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.handlers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2005, 2007 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.handlers;
011:
012:        import org.eclipse.core.commands.ExecutionEvent;
013:        import org.eclipse.core.commands.IHandler;
014:        import org.eclipse.core.commands.IHandlerListener;
015:        import org.eclipse.core.commands.IObjectWithState;
016:        import org.eclipse.core.commands.ParameterizedCommand;
017:        import org.eclipse.core.commands.State;
018:        import org.eclipse.core.expressions.EvaluationResult;
019:        import org.eclipse.core.expressions.Expression;
020:        import org.eclipse.core.expressions.IEvaluationContext;
021:        import org.eclipse.core.runtime.CoreException;
022:        import org.eclipse.core.runtime.IConfigurationElement;
023:        import org.eclipse.core.runtime.ISafeRunnable;
024:        import org.eclipse.core.runtime.IStatus;
025:        import org.eclipse.core.runtime.InvalidRegistryObjectException;
026:        import org.eclipse.core.runtime.ListenerList;
027:        import org.eclipse.core.runtime.SafeRunner;
028:        import org.eclipse.core.runtime.Status;
029:        import org.eclipse.jface.action.IAction;
030:        import org.eclipse.jface.util.IPropertyChangeListener;
031:        import org.eclipse.jface.util.PropertyChangeEvent;
032:        import org.eclipse.jface.viewers.ISelection;
033:        import org.eclipse.jface.viewers.ISelectionChangedListener;
034:        import org.eclipse.jface.viewers.SelectionChangedEvent;
035:        import org.eclipse.jface.viewers.StructuredSelection;
036:        import org.eclipse.swt.widgets.Event;
037:        import org.eclipse.ui.IActionDelegate;
038:        import org.eclipse.ui.IActionDelegate2;
039:        import org.eclipse.ui.IActionDelegateWithEvent;
040:        import org.eclipse.ui.IEditorActionDelegate;
041:        import org.eclipse.ui.IEditorPart;
042:        import org.eclipse.ui.INullSelectionListener;
043:        import org.eclipse.ui.IObjectActionDelegate;
044:        import org.eclipse.ui.IPartListener2;
045:        import org.eclipse.ui.ISelectionListener;
046:        import org.eclipse.ui.ISources;
047:        import org.eclipse.ui.IViewActionDelegate;
048:        import org.eclipse.ui.IViewPart;
049:        import org.eclipse.ui.IWorkbenchPage;
050:        import org.eclipse.ui.IWorkbenchPart;
051:        import org.eclipse.ui.IWorkbenchPartReference;
052:        import org.eclipse.ui.IWorkbenchWindow;
053:        import org.eclipse.ui.IWorkbenchWindowActionDelegate;
054:        import org.eclipse.ui.handlers.IHandlerService;
055:        import org.eclipse.ui.internal.WorkbenchPlugin;
056:
057:        /**
058:         * <p>
059:         * This proxies an {@link IActionDelegate} so that it can impersonate an
060:         * {@link IHandler}.
061:         * </p>
062:         * <p>
063:         * This class is not intended for use outside of the
064:         * <code>org.eclipse.ui.workbench</code> plug-in.
065:         * </p>
066:         * 
067:         * @since 3.2
068:         */
069:        public final class ActionDelegateHandlerProxy implements 
070:                ISelectionListener, ISelectionChangedListener,
071:                INullSelectionListener, IHandler, IObjectWithState,
072:                IPartListener2 {
073:
074:            /**
075:             * The fake action that proxies all of the command-based services. This
076:             * value is never <code>null</code>.
077:             */
078:            private CommandLegacyActionWrapper action;
079:
080:            /**
081:             * The identifier of the actions to create as a wrapper to the command
082:             * architecture. This value may be <code>null</code>.
083:             */
084:            private String actionId;
085:
086:            /**
087:             * The command that will back the dummy actions exposed to this delegate.
088:             * This value is never <code>null</code>.
089:             */
090:            private ParameterizedCommand command;
091:
092:            /**
093:             * This is the current selection, as seen by this proxy.
094:             */
095:            private ISelection currentSelection;
096:
097:            /**
098:             * The delegate, if it has been created yet.
099:             */
100:            private IActionDelegate delegate;
101:
102:            //
103:            // instead of casting, which is unreliable, pick
104:            // a delegate type based on the IConfigurationElement
105:            //
106:            private IEditorActionDelegate editorDelegate = null;
107:            private IViewActionDelegate viewDelegate = null;
108:            private IObjectActionDelegate objectDelegate = null;
109:            private IWorkbenchWindowActionDelegate windowDelegate = null;
110:
111:            private IWorkbenchPart currentPart = null;
112:
113:            /**
114:             * The name of the configuration element attribute which contains the
115:             * information necessary to instantiate the real handler.
116:             */
117:            private String delegateAttributeName;
118:
119:            /**
120:             * The configuration element from which the handler can be created. This
121:             * value will exist until the element is converted into a real class -- at
122:             * which point this value will be set to <code>null</code>.
123:             */
124:            private IConfigurationElement element;
125:
126:            /**
127:             * The <code>enabledWhen</code> expression for the handler. Only if this
128:             * expression evaluates to <code>true</code> (or the value is
129:             * <code>null</code>) should we consult the handler.
130:             */
131:            private final Expression enabledWhenExpression;
132:
133:            /**
134:             * A collection of objects listening to changes to this manager. This
135:             * collection is <code>null</code> if there are no listeners.
136:             */
137:            private transient ListenerList listenerList = null;
138:
139:            /**
140:             * The image style to use when selecting the images to display for this
141:             * delegate. This value may be <code>null</code>, if the default style
142:             * should be used.
143:             */
144:            private final String style;
145:
146:            /**
147:             * The identifier of the view with which this delegate must be associated.
148:             * This value is not <code>null</code> iff the delegate is an
149:             * {@link IViewActionDelegate}.
150:             */
151:            private final String viewId;
152:
153:            /**
154:             * The workbench window in which this delegate is active. This value is
155:             * never <code>null</code>.
156:             */
157:            private final IWorkbenchWindow window;
158:
159:            /**
160:             * Constructs a new instance of <code>ActionDelegateHandlerProxy</code>
161:             * with all the information it needs to try to avoid loading until it is
162:             * needed.
163:             * 
164:             * @param element
165:             *            The configuration element from which the real class can be
166:             *            loaded at run-time; must not be <code>null</code>.
167:             * @param delegateAttributeName
168:             *            The name of the attibute or element containing the action
169:             *            delegate; must not be <code>null</code>.
170:             * @param actionId
171:             *            The identifier of the underlying action; may be
172:             *            <code>null</code>.
173:             * @param command
174:             *            The command with which the action delegate will be associated;
175:             *            must not be <code>null</code>.
176:             * @param window
177:             *            The workbench window in which this delegate will be active;
178:             *            must not be <code>null</code>.
179:             * @param style
180:             *            The image style with which the icons are associated; may be
181:             *            <code>null</code>.
182:             * @param enabledWhenExpression
183:             *            The name of the element containing the enabledWhen expression.
184:             *            This should be a child of the
185:             *            <code>configurationElement</code>. If this value is
186:             *            <code>null</code>, then there is no enablement expression
187:             *            (i.e., enablement will be delegated to the handler when
188:             *            possible).
189:             * @param viewId
190:             *            The identifier of the view to which this proxy is bound; may
191:             *            be <code>null</code> if this proxy is not for an
192:             *            {@link IViewActionDelegate}.
193:             */
194:            public ActionDelegateHandlerProxy(
195:                    final IConfigurationElement element,
196:                    final String delegateAttributeName, final String actionId,
197:                    final ParameterizedCommand command,
198:                    final IWorkbenchWindow window, final String style,
199:                    final Expression enabledWhenExpression, final String viewId) {
200:                if (element == null) {
201:                    throw new NullPointerException(
202:                            "The configuration element backing a handler proxy cannot be null"); //$NON-NLS-1$
203:                }
204:
205:                if (delegateAttributeName == null) {
206:                    throw new NullPointerException(
207:                            "The attribute containing the action delegate must be known"); //$NON-NLS-1$
208:                }
209:
210:                if (window == null) {
211:                    throw new NullPointerException(
212:                            "The workbench window for a delegate must not be null"); //$NON-NLS-1$
213:                }
214:
215:                this .element = element;
216:                this .enabledWhenExpression = enabledWhenExpression;
217:                this .delegateAttributeName = delegateAttributeName;
218:                this .window = window;
219:                this .command = command;
220:                this .actionId = actionId;
221:                this .style = style;
222:                this .viewId = viewId;
223:            }
224:
225:            public final void addHandlerListener(
226:                    final IHandlerListener handlerListener) {
227:                if (listenerList == null) {
228:                    listenerList = new ListenerList(ListenerList.IDENTITY);
229:                }
230:
231:                listenerList.add(handlerListener);
232:            }
233:
234:            public void addState(String id, State state) {
235:                // TODO Auto-generated method stub
236:
237:            }
238:
239:            public final void dispose() {
240:                disposeDelegate();
241:            }
242:
243:            /**
244:             * 
245:             */
246:            private void disposeDelegate() {
247:                final IActionDelegate actDel = getDelegate();
248:                if (actDel instanceof  IWorkbenchWindowActionDelegate) {
249:                    final IWorkbenchWindowActionDelegate workbenchWindowDelegate = (IWorkbenchWindowActionDelegate) actDel;
250:                    workbenchWindowDelegate.dispose();
251:                } else if (actDel instanceof  IActionDelegate2) {
252:                    final IActionDelegate2 delegate2 = (IActionDelegate2) actDel;
253:                    delegate2.dispose();
254:                }
255:                delegate = null;
256:                editorDelegate = null;
257:                objectDelegate = null;
258:                viewDelegate = null;
259:                windowDelegate = null;
260:                currentSelection = null;
261:            }
262:
263:            public final Object execute(final ExecutionEvent event) {
264:                final IAction action = getAction();
265:                if (loadDelegate() && (action != null)) {
266:                    final Object trigger = event.getTrigger();
267:
268:                    // Attempt to update the selection.
269:                    final Object applicationContext = event
270:                            .getApplicationContext();
271:                    if (applicationContext instanceof  IEvaluationContext) {
272:                        final IEvaluationContext context = (IEvaluationContext) applicationContext;
273:                        updateDelegate(action, context);
274:                    }
275:
276:                    // Decide what type of delegate we have.
277:                    if ((delegate instanceof  IActionDelegate2)
278:                            && (trigger instanceof  Event)) {
279:                        // This supports Eclipse 2.1 to Eclipse 3.1.
280:                        final IActionDelegate2 delegate2 = (IActionDelegate2) delegate;
281:                        final Event triggeringEvent = (Event) trigger;
282:                        delegate2.runWithEvent(action, triggeringEvent);
283:                    } else if ((delegate instanceof  IActionDelegateWithEvent)
284:                            && (trigger instanceof  Event)) {
285:                        // This supports Eclipse 2.0
286:                        final IActionDelegateWithEvent delegateWithEvent = (IActionDelegateWithEvent) delegate;
287:                        final Event triggeringEvent = (Event) trigger;
288:                        delegateWithEvent.runWithEvent(action, triggeringEvent);
289:                    } else {
290:                        delegate.run(action);
291:                    }
292:                }
293:
294:                return null;
295:            }
296:
297:            /**
298:             * @param action
299:             * @param context
300:             */
301:            private void updateDelegate(final IAction action,
302:                    final IEvaluationContext context) {
303:                if (action == null || delegate == null) {
304:                    return;
305:                }
306:
307:                if (editorDelegate != null) {
308:                    final Object activeEditor = context
309:                            .getVariable(ISources.ACTIVE_EDITOR_NAME);
310:                    if (activeEditor != null) {
311:                        editorDelegate.setActiveEditor(action,
312:                                (IEditorPart) activeEditor);
313:                    }
314:                    updateActivePart((IWorkbenchPart) activeEditor);
315:                } else if (objectDelegate != null) {
316:                    final Object activePart = context
317:                            .getVariable(ISources.ACTIVE_PART_NAME);
318:                    if (activePart != null) {
319:                        objectDelegate.setActivePart(action,
320:                                (IWorkbenchPart) activePart);
321:                    }
322:                    updateActivePart((IWorkbenchPart) activePart);
323:                }
324:
325:                final Object selectionObject = getCurrentSelection(context);
326:                if (selectionObject instanceof  ISelection) {
327:                    currentSelection = (ISelection) selectionObject;
328:                    delegate.selectionChanged(action, currentSelection);
329:                } else {
330:                    currentSelection = null;
331:                    delegate.selectionChanged(action, null);
332:                }
333:            }
334:
335:            /**
336:             * @param activePart
337:             */
338:            private void updateActivePart(IWorkbenchPart activePart) {
339:                if (currentPart == activePart) {
340:                    return;
341:                }
342:                if (currentPart != null) {
343:                    currentPart.getSite().getPage().removePartListener(this );
344:                }
345:                if (activePart != null) {
346:                    activePart.getSite().getPage().addPartListener(this );
347:                }
348:                currentPart = activePart;
349:            }
350:
351:            /**
352:             * @param context
353:             * @return
354:             */
355:            private Object getCurrentSelection(final IEvaluationContext context) {
356:                Object obj = context
357:                        .getVariable(ISources.ACTIVE_MENU_EDITOR_INPUT_NAME);
358:                if (obj == null) {
359:                    obj = context
360:                            .getVariable(ISources.ACTIVE_MENU_SELECTION_NAME);
361:                    if (obj == null) {
362:                        obj = context
363:                                .getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
364:                    }
365:                }
366:                return obj;
367:            }
368:
369:            /**
370:             * Retrieves the action corresponding to the currently active workbench
371:             * window, if any.
372:             * 
373:             * @return The current action; <code>null</code> if there is no currently
374:             *         active workbench window.
375:             */
376:            private final CommandLegacyActionWrapper getAction() {
377:                if (action == null) {
378:                    action = new CommandLegacyActionWrapper(actionId, command,
379:                            style, window);
380:                    action
381:                            .addPropertyChangeListener(new IPropertyChangeListener() {
382:                                public final void propertyChange(
383:                                        final PropertyChangeEvent event) {
384:                                    // TODO Update the state somehow.
385:                                }
386:                            });
387:                }
388:                return action;
389:            }
390:
391:            /**
392:             * Retrieves the delegate corresponding to the currently active workbench
393:             * window, if any. This does not trigger loading of the delegate.
394:             * 
395:             * @return The current delegate; or <code>null</code> if none.
396:             */
397:            private final IActionDelegate getDelegate() {
398:                return delegate;
399:            }
400:
401:            public State getState(String stateId) {
402:                // TODO Auto-generated method stub
403:                return null;
404:            }
405:
406:            public String[] getStateIds() {
407:                // TODO Auto-generated method stub
408:                return null;
409:            }
410:
411:            public final void handleStateChange(final State state,
412:                    final Object oldValue) {
413:                // TODO What should we do here?
414:            }
415:
416:            /**
417:             * Initialize the action delegate by calling its lifecycle method.
418:             */
419:            private final boolean initDelegate() {
420:                final IWorkbenchPage page = window.getActivePage();
421:                final IWorkbenchPart activePart;
422:                final IEditorPart activeEditor;
423:                if (page == null) {
424:                    activePart = null;
425:                    activeEditor = null;
426:                } else {
427:                    activePart = page.getActivePart();
428:                    activeEditor = page.getActiveEditor();
429:                }
430:                final IActionDelegate delegate = getDelegate();
431:                final IAction action = getAction();
432:
433:                // Check to see if the view delegate should be initialized.
434:                if ((viewId != null) && (page != null)
435:                        && (viewDelegate != null)) {
436:                    final IViewPart viewPart = page.findView(viewId);
437:                    if (viewPart == null) {
438:                        return false;
439:                    }
440:                }
441:
442:                // Initialize the delegate.
443:                final ISafeRunnable runnable = new ISafeRunnable() {
444:                    public final void handleException(final Throwable exception) {
445:                        // Do nothing.
446:                    }
447:
448:                    public final void run() {
449:                        // Handle IActionDelegate2
450:                        if (delegate instanceof  IActionDelegate2) {
451:                            final IActionDelegate2 delegate2 = (IActionDelegate2) delegate;
452:                            delegate2.init(action);
453:                        }
454:
455:                        // Handle IObjectActionDelegates
456:                        if ((objectDelegate != null) && (activePart != null)) {
457:                            objectDelegate.setActivePart(action, activePart);
458:                            updateActivePart(activePart);
459:                        } else if (editorDelegate != null) {
460:                            editorDelegate
461:                                    .setActiveEditor(action, activeEditor);
462:                            updateActivePart(activeEditor);
463:                        } else if ((viewId != null) && (page != null)
464:                                && (viewDelegate != null)) {
465:                            final IViewPart viewPart = page.findView(viewId);
466:                            viewDelegate.init(viewPart);
467:                        } else if (windowDelegate != null) {
468:                            windowDelegate.init(window);
469:                        }
470:                    }
471:                };
472:                SafeRunner.run(runnable);
473:                return true;
474:            }
475:
476:            public final boolean isEnabled() {
477:                final IHandlerService service = (IHandlerService) window
478:                        .getService(IHandlerService.class);
479:                IEvaluationContext context = service.getCurrentState();
480:                return isEnabled(context);
481:            }
482:
483:            public final boolean isEnabled(IEvaluationContext context) {
484:                final CommandLegacyActionWrapper action = getAction();
485:                if (enabledWhenExpression != null) {
486:                    try {
487:                        final EvaluationResult result = enabledWhenExpression
488:                                .evaluate(context);
489:                        if (result == EvaluationResult.TRUE) {
490:                            updateDelegate(action, context);
491:                            return (action == null)
492:                                    || action.isEnabledDisregardingCommand();
493:                        }
494:                    } catch (final CoreException e) {
495:                        // We will just fall through an let it return false.
496:                        final StringBuffer message = new StringBuffer(
497:                                "An exception occurred while evaluating the enabledWhen expression for "); //$NON-NLS-1$
498:                        if (delegate != null) {
499:                            message.append(delegate);
500:                        } else {
501:                            message.append(element
502:                                    .getAttribute(delegateAttributeName));
503:                        }
504:                        message.append("' could not be loaded"); //$NON-NLS-1$
505:                        final IStatus status = new Status(IStatus.WARNING,
506:                                WorkbenchPlugin.PI_WORKBENCH, 0,
507:                                e.getMessage(), e);
508:                        WorkbenchPlugin.log(message.toString(), status);
509:                    }
510:
511:                    return false;
512:                }
513:
514:                updateDelegate(action, context);
515:                return (action == null)
516:                        || action.isEnabledDisregardingCommand();
517:            }
518:
519:            public final boolean isHandled() {
520:                return true;
521:            }
522:
523:            /**
524:             * Checks if the declaring plugin has been loaded. This means that there
525:             * will be no need to delay creating the delegate.
526:             * 
527:             * @return <code>true</code> if the bundle containing the delegate is
528:             *         already loaded -- making it safe to load the delegate.
529:             */
530:            private final boolean isSafeToLoadDelegate() {
531:                return false;
532:                // TODO This causes problem because some people expect their selections
533:                // to be a particular class.
534:                // final String bundleId = element.getNamespace();
535:                // return BundleUtility.isActive(bundleId);
536:            }
537:
538:            /**
539:             * Loads the delegate, if possible. If the delegate is loaded, then the
540:             * member variables are updated accordingly.
541:             * 
542:             * @return <code>true</code> if the delegate is now non-null;
543:             *         <code>false</code> otherwise.
544:             */
545:            private final boolean loadDelegate() {
546:                // Try to load the delegate, if it hasn't been loaded already.
547:                if (delegate == null) {
548:                    /*
549:                     * If this is an IViewActionDelegate, then check to see if we have a
550:                     * view ready yet. If not, then we'll have to wait.
551:                     */
552:                    if (viewId != null) {
553:                        final IWorkbenchPage activePage = window
554:                                .getActivePage();
555:                        if (activePage != null) {
556:                            final IViewPart part = activePage.findView(viewId);
557:                            if (part == null) {
558:                                return false;
559:                            }
560:                        } else {
561:                            return false;
562:                        }
563:                    }
564:
565:                    // Load the delegate.
566:                    try {
567:                        delegate = (IActionDelegate) element
568:                                .createExecutableExtension(delegateAttributeName);
569:                        String name = element.getDeclaringExtension()
570:                                .getExtensionPointUniqueIdentifier();
571:                        if ("org.eclipse.ui.actionSets".equals(name) //$NON-NLS-1$
572:                                && delegate instanceof  IWorkbenchWindowActionDelegate) {
573:                            windowDelegate = (IWorkbenchWindowActionDelegate) delegate;
574:                        } else if ("org.eclipse.ui.editorActions".equals(name) //$NON-NLS-1$
575:                                && delegate instanceof  IEditorActionDelegate) {
576:                            editorDelegate = (IEditorActionDelegate) delegate;
577:                        } else if ("org.eclipse.ui.viewActions".equals(name) //$NON-NLS-1$
578:                                && delegate instanceof  IViewActionDelegate) {
579:                            viewDelegate = (IViewActionDelegate) delegate;
580:                        } else if ("org.eclipse.ui.popupMenus".equals(name)) { //$NON-NLS-1$
581:                            IConfigurationElement parent = (IConfigurationElement) element
582:                                    .getParent();
583:                            if ("objectContribution".equals(parent.getName()) //$NON-NLS-1$
584:                                    && delegate instanceof  IObjectActionDelegate) {
585:                                objectDelegate = (IObjectActionDelegate) delegate;
586:                            } else if (viewId == null
587:                                    && delegate instanceof  IEditorActionDelegate) {
588:                                editorDelegate = (IEditorActionDelegate) delegate;
589:                            } else if (viewId != null
590:                                    && delegate instanceof  IViewActionDelegate) {
591:                                viewDelegate = (IViewActionDelegate) delegate;
592:                            }
593:                        }
594:                        if (initDelegate()) {
595:                            return true;
596:                        }
597:
598:                        delegate = null;
599:                        objectDelegate = null;
600:                        viewDelegate = null;
601:                        editorDelegate = null;
602:                        windowDelegate = null;
603:                        return false;
604:
605:                    } catch (final ClassCastException e) {
606:                        final String message = "The proxied delegate was the wrong class"; //$NON-NLS-1$
607:                        final IStatus status = new Status(IStatus.ERROR,
608:                                WorkbenchPlugin.PI_WORKBENCH, 0, message, e);
609:                        WorkbenchPlugin.log(message, status);
610:                        return false;
611:
612:                    } catch (final CoreException e) {
613:                        final String message = "The proxied delegate for '" //$NON-NLS-1$
614:                                + element.getAttribute(delegateAttributeName)
615:                                + "' could not be loaded"; //$NON-NLS-1$
616:                        IStatus status = new Status(IStatus.ERROR,
617:                                WorkbenchPlugin.PI_WORKBENCH, 0, message, e);
618:                        WorkbenchPlugin.log(message, status);
619:                        return false;
620:                    }
621:                }
622:
623:                return true;
624:            }
625:
626:            /**
627:             * Refresh the action enablement.
628:             */
629:            private final void refreshEnablement() {
630:                final IActionDelegate delegate = getDelegate();
631:                final IAction action = getAction();
632:                if ((delegate != null) && (action != null)) {
633:                    delegate.selectionChanged(action, currentSelection);
634:                }
635:            }
636:
637:            public void removeHandlerListener(IHandlerListener handlerListener) {
638:                if (listenerList != null) {
639:                    listenerList.remove(handlerListener);
640:
641:                    if (listenerList.isEmpty()) {
642:                        listenerList = null;
643:                    }
644:                }
645:            }
646:
647:            public void removeState(String stateId) {
648:                // TODO Auto-generated method stub
649:
650:            }
651:
652:            private final void selectionChanged(final ISelection selection) {
653:                // Update selection.
654:                currentSelection = selection;
655:                if (currentSelection == null) {
656:                    currentSelection = StructuredSelection.EMPTY;
657:                }
658:
659:                // The selection is passed to the delegate as-is without
660:                // modification. If the selection needs to be modified
661:                // the action contributors should do so.
662:
663:                // If the delegate can be loaded, do so.
664:                // Otherwise, just update the enablement.
665:                final IActionDelegate delegate = getDelegate();
666:                if (delegate == null && isSafeToLoadDelegate()) {
667:                    loadDelegate();
668:                }
669:                refreshEnablement();
670:            }
671:
672:            public final void selectionChanged(final IWorkbenchPart part,
673:                    final ISelection selection) {
674:                selectionChanged(selection);
675:
676:            }
677:
678:            public final void selectionChanged(final SelectionChangedEvent event) {
679:                final ISelection selection = event.getSelection();
680:                selectionChanged(selection);
681:            }
682:
683:            public final String toString() {
684:                final StringBuffer buffer = new StringBuffer();
685:                buffer.append("ActionDelegateHandlerProxy("); //$NON-NLS-1$
686:                buffer.append(getDelegate());
687:                if (element != null) {
688:                    buffer.append(',');
689:                    try {
690:                        final String className = element
691:                                .getAttribute(delegateAttributeName);
692:                        buffer.append(className);
693:                    } catch (InvalidRegistryObjectException e) {
694:                        buffer.append(actionId);
695:                    }
696:                }
697:                buffer.append(')');
698:                return buffer.toString();
699:            }
700:
701:            /* (non-Javadoc)
702:             * @see org.eclipse.ui.IPartListener2#partActivated(org.eclipse.ui.IWorkbenchPartReference)
703:             */
704:            public void partActivated(IWorkbenchPartReference partRef) {
705:            }
706:
707:            /* (non-Javadoc)
708:             * @see org.eclipse.ui.IPartListener2#partBroughtToTop(org.eclipse.ui.IWorkbenchPartReference)
709:             */
710:            public void partBroughtToTop(IWorkbenchPartReference partRef) {
711:            }
712:
713:            /* (non-Javadoc)
714:             * @see org.eclipse.ui.IPartListener2#partClosed(org.eclipse.ui.IWorkbenchPartReference)
715:             */
716:            public void partClosed(IWorkbenchPartReference partRef) {
717:                if (currentPart != null
718:                        && partRef.getPart(false) == currentPart) {
719:                    selectionChanged(StructuredSelection.EMPTY);
720:                    disposeDelegate();
721:                    currentPart = null;
722:                }
723:            }
724:
725:            /* (non-Javadoc)
726:             * @see org.eclipse.ui.IPartListener2#partDeactivated(org.eclipse.ui.IWorkbenchPartReference)
727:             */
728:            public void partDeactivated(IWorkbenchPartReference partRef) {
729:            }
730:
731:            /* (non-Javadoc)
732:             * @see org.eclipse.ui.IPartListener2#partHidden(org.eclipse.ui.IWorkbenchPartReference)
733:             */
734:            public void partHidden(IWorkbenchPartReference partRef) {
735:            }
736:
737:            /* (non-Javadoc)
738:             * @see org.eclipse.ui.IPartListener2#partInputChanged(org.eclipse.ui.IWorkbenchPartReference)
739:             */
740:            public void partInputChanged(IWorkbenchPartReference partRef) {
741:            }
742:
743:            /* (non-Javadoc)
744:             * @see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
745:             */
746:            public void partOpened(IWorkbenchPartReference partRef) {
747:            }
748:
749:            /* (non-Javadoc)
750:             * @see org.eclipse.ui.IPartListener2#partVisible(org.eclipse.ui.IWorkbenchPartReference)
751:             */
752:            public void partVisible(IWorkbenchPartReference partRef) {
753:            }
754:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.