KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > actions > LinkEditorAction


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.navigator.actions;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.ISafeRunnable;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.SafeRunner;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.ISelectionChangedListener;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.SelectionChangedEvent;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IPartListener;
27 import org.eclipse.ui.IPropertyListener;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.IWorkbenchPart;
30 import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
31 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
32 import org.eclipse.ui.internal.navigator.extensions.LinkHelperService;
33 import org.eclipse.ui.navigator.CommonNavigator;
34 import org.eclipse.ui.navigator.CommonViewer;
35 import org.eclipse.ui.navigator.ILinkHelper;
36 import org.eclipse.ui.navigator.INavigatorContentService;
37 import org.eclipse.ui.part.ISetSelectionTarget;
38 import org.eclipse.ui.progress.UIJob;
39
40 /**
41  * This action links the activate editor with the Navigator selection.
42  *
43  * @since 3.2
44  */

45 public class LinkEditorAction extends Action implements
46         ISelectionChangedListener, IAction, IPropertyListener {
47
48     private static final long BRIEF_DELAY = 100;
49
50     private IPartListener partListener;
51
52     private final CommonNavigator commonNavigator;
53
54     private final CommonViewer commonViewer;
55
56     private final LinkHelperService linkService;
57
58     private UIJob activateEditorJob = new UIJob(
59             CommonNavigatorMessages.Link_With_Editor_Job_) {
60         public IStatus runInUIThread(IProgressMonitor monitor) {
61
62             if (!commonViewer.getControl().isDisposed()) {
63                 ISelection selection = commonViewer.getSelection();
64                 if (selection != null && !selection.isEmpty()
65                         && selection instanceof IStructuredSelection) {
66
67                     IStructuredSelection sSelection = (IStructuredSelection) selection;
68                     if (sSelection.size() == 1) {
69                         ILinkHelper[] helpers = linkService
70                                 .getLinkHelpersFor(sSelection.getFirstElement());
71                         if (helpers.length > 0) {
72                             helpers[0].activateEditor(commonNavigator.getSite()
73                                     .getPage(), sSelection);
74                         }
75                     }
76                 }
77             }
78             return Status.OK_STATUS;
79         }
80     };
81
82     private UIJob updateSelectionJob = new UIJob(
83             CommonNavigatorMessages.Link_With_Editor_Job_) {
84         public IStatus runInUIThread(IProgressMonitor monitor) {
85
86             if (!commonNavigator.getCommonViewer().getControl().isDisposed()) {
87                 SafeRunner.run(new ISafeRunnable() {
88
89                     public void run() throws Exception JavaDoc {
90                         IWorkbenchPage page = commonNavigator.getSite()
91                                 .getPage();
92                         if (page != null) {
93                             IEditorPart editor = page.getActiveEditor();
94                             if (editor != null) {
95                                 IEditorInput input = editor.getEditorInput();
96                                 IStructuredSelection newSelection = linkService
97                                         .getSelectionFor(input);
98                                 if (!newSelection.isEmpty()) {
99                                     commonNavigator.selectReveal(newSelection);
100                                 }
101                             }
102                         }
103                     }
104
105                     public void handleException(Throwable JavaDoc e) {
106                         String JavaDoc msg = e.getMessage() != null ? e.getMessage()
107                                 : e.toString();
108                         NavigatorPlugin.logError(0, msg, e);
109                     }
110                 });
111
112             }
113
114             return Status.OK_STATUS;
115         }
116     };
117
118     /**
119      * Create a LinkEditorAction for the given navigator and viewer.
120      *
121      * @param aNavigator
122      * The navigator which defines whether linking is enabled and
123      * implements {@link ISetSelectionTarget}.
124      * @param aViewer
125      * The common viewer instance with a
126      * {@link INavigatorContentService}.
127      * @param linkHelperService
128      */

129     public LinkEditorAction(CommonNavigator aNavigator, CommonViewer aViewer,
130             LinkHelperService linkHelperService) {
131         super(CommonNavigatorMessages.LinkEditorActionDelegate_0);
132         linkService = linkHelperService;
133         setToolTipText(CommonNavigatorMessages.LinkEditorActionDelegate_1);
134         commonNavigator = aNavigator;
135         commonViewer = aViewer;
136         init();
137     }
138
139     /**
140      * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
141      */

142     protected void init() {
143         partListener = new IPartListener() {
144
145             public void partActivated(IWorkbenchPart part) {
146                 if (part instanceof IEditorPart) {
147                     updateSelectionJob.schedule(BRIEF_DELAY);
148                 }
149             }
150
151             public void partBroughtToTop(IWorkbenchPart part) {
152                 if (part instanceof IEditorPart) {
153                     updateSelectionJob.schedule(BRIEF_DELAY);
154                 }
155             }
156
157             public void partClosed(IWorkbenchPart part) {
158
159             }
160
161             public void partDeactivated(IWorkbenchPart part) {
162             }
163
164             public void partOpened(IWorkbenchPart part) {
165             }
166         };
167
168         updateLinkingEnabled(commonNavigator.isLinkingEnabled());
169
170         commonNavigator.addPropertyListener(this);
171
172         // linkHelperRegistry = new
173
// LinkHelperManager(commonViewer.getNavigatorContentService());
174
}
175
176     /**
177      *
178      */

179     public void dispose() {
180         commonNavigator.removePropertyListener(this);
181         if (isChecked()) {
182             commonViewer.removePostSelectionChangedListener(this);
183             commonNavigator.getSite().getPage()
184                     .removePartListener(partListener);
185         }
186
187     }
188
189     /**
190      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
191      */

192     public void run() {
193         commonNavigator.setLinkingEnabled(!commonNavigator.isLinkingEnabled());
194     }
195
196     /*
197      * (non-Javadoc)
198      *
199      * @see org.eclipse.jface.viewers.ISelectionChangedList
200      */

201     public void selectionChanged(SelectionChangedEvent event) {
202         if (commonNavigator.isLinkingEnabled()) {
203             activateEditor();
204         }
205
206     }
207
208     /**
209      * Update the active editor based on the current selection in the Navigator.
210      */

211     protected void activateEditor() {
212         ISelection selection = commonViewer.getSelection();
213         if (selection != null && !selection.isEmpty()
214                 && selection instanceof IStructuredSelection) {
215             /*
216              * Create and schedule a UI Job to activate the editor in a valid
217              * Display thread
218              */

219             activateEditorJob.schedule(BRIEF_DELAY);
220         }
221     }
222
223     /*
224      * (non-Javadoc)
225      *
226      * @see org.eclipse.ui.IPropertyListener#propertyChanged(java.lang.Object,
227      * int)
228      */

229     public void propertyChanged(Object JavaDoc aSource, int aPropertyId) {
230         switch (aPropertyId) {
231         case CommonNavigator.IS_LINKING_ENABLED_PROPERTY:
232             updateLinkingEnabled(((CommonNavigator) aSource).isLinkingEnabled());
233         }
234     }
235
236     /**
237      * @param toEnableLinking
238      */

239     private void updateLinkingEnabled(boolean toEnableLinking) {
240         setChecked(toEnableLinking);
241
242         if (toEnableLinking) {
243
244             updateSelectionJob.schedule(BRIEF_DELAY);
245
246             commonViewer.addPostSelectionChangedListener(this);
247             commonNavigator.getSite().getPage().addPartListener(partListener);
248         } else {
249             commonViewer.removePostSelectionChangedListener(this);
250             commonNavigator.getSite().getPage()
251                     .removePartListener(partListener);
252         }
253     }
254
255 }
256
Popular Tags