KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > FindReplaceAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Max Weninger <max.weninger@windriver.com> - https://bugs.eclipse.org/bugs/show_bug.cgi?id=148898
11  *******************************************************************************/

12
13 package org.eclipse.ui.texteditor;
14
15
16 import java.util.ResourceBundle JavaDoc;
17
18 import org.eclipse.swt.events.DisposeEvent;
19 import org.eclipse.swt.events.DisposeListener;
20 import org.eclipse.swt.widgets.Shell;
21
22 import org.eclipse.core.runtime.Assert;
23
24 import org.eclipse.jface.text.IFindReplaceTarget;
25
26 import org.eclipse.ui.IPartListener;
27 import org.eclipse.ui.IPartService;
28 import org.eclipse.ui.IWorkbenchPart;
29 import org.eclipse.ui.IWorkbenchPartSite;
30 import org.eclipse.ui.IWorkbenchWindow;
31
32
33 /**
34  * An action which opens a Find/Replace dialog.
35  * The dialog while open, tracks the active workbench part
36  * and retargets itself to the active find/replace target.
37  * <p>
38  * It can also be used without having an IWorkbenchPart e.g. for
39  * dialogs or wizards by just providing a {@link Shell} and an {@link IFindReplaceTarget}.
40  * <em>In this case the dialog won't be shared with the one
41  * used for the active workbench part.</em>
42  * </p>
43  * <p>
44  * This class may be instantiated; it is not intended to be subclassed.
45  * </p>
46  *
47  * @see IFindReplaceTarget
48  */

49 public class FindReplaceAction extends ResourceAction implements IUpdate {
50
51     /**
52      * Represents the "global" find/replace dialog. It tracks the active
53      * part and retargets the find/replace dialog accordingly. The find/replace
54      * target is retrieved from the active part using
55      * <code>getAdapter(IFindReplaceTarget.class)</code>.
56      * <p>
57      * The stub has the same life cycle as the find/replace dialog.</p>
58      * <p>
59      * If no IWorkbenchPart is available a Shell must be provided
60      * In this case the IFindReplaceTarget will never change.</p>
61      */

62     static class FindReplaceDialogStub implements IPartListener, DisposeListener {
63
64         /** The workbench part */
65         private IWorkbenchPart fPart;
66         /** The previous workbench part */
67         private IWorkbenchPart fPreviousPart;
68         /** The previous find/replace target */
69         private IFindReplaceTarget fPreviousTarget;
70
71         /** The workbench window */
72         private IWorkbenchWindow fWindow;
73         /** The find/replace dialog */
74         private FindReplaceDialog fDialog;
75
76         /**
77          * Creates a new find/replace dialog accessor anchored at the given part site.
78          *
79          * @param site the part site
80          */

81         public FindReplaceDialogStub(IWorkbenchPartSite site) {
82             this(site.getShell());
83             fWindow= site.getWorkbenchWindow();
84             IPartService service= fWindow.getPartService();
85             service.addPartListener(this);
86             partActivated(service.getActivePart());
87         }
88
89         /**
90          * Creates a new find/replace dialog accessor anchored at the given shell.
91          *
92          * @param shell the shell if no site is used
93          * @since 3.3
94          */

95         public FindReplaceDialogStub(Shell shell) {
96             fDialog= new FindReplaceDialog(shell);
97             fDialog.create();
98             fDialog.getShell().addDisposeListener(this);
99         }
100
101         /**
102          * Returns the find/replace dialog.
103          * @return the find/replace dialog
104          */

105         public FindReplaceDialog getDialog() {
106             return fDialog;
107         }
108
109         /*
110          * @see IPartListener#partActivated(IWorkbenchPart)
111          */

112         public void partActivated(IWorkbenchPart part) {
113
114             IFindReplaceTarget target= part == null ? null : (IFindReplaceTarget) part.getAdapter(IFindReplaceTarget.class);
115             fPreviousPart= fPart;
116             fPart= target == null ? null : part;
117
118             if (fPreviousTarget != target) {
119                 fPreviousTarget= target;
120                 if (fDialog != null) {
121                     boolean isEditable= false;
122                     if (fPart instanceof ITextEditorExtension2) {
123                         ITextEditorExtension2 extension= (ITextEditorExtension2) fPart;
124                         isEditable= extension.isEditorInputModifiable();
125                     } else if (target != null)
126                         isEditable= target.isEditable();
127                     fDialog.updateTarget(target, isEditable, false);
128                 }
129             }
130         }
131
132         /*
133          * @see IPartListener#partClosed(IWorkbenchPart)
134          */

135         public void partClosed(IWorkbenchPart part) {
136
137             if (part == fPreviousPart) {
138                 fPreviousPart= null;
139                 fPreviousTarget= null;
140             }
141
142             if (part == fPart)
143                 partActivated(null);
144         }
145
146         /*
147          * @see DisposeListener#widgetDisposed(DisposeEvent)
148          */

149         public void widgetDisposed(DisposeEvent event) {
150
151             if (fgFindReplaceDialogStub == this)
152                 fgFindReplaceDialogStub= null;
153
154             if(fgFindReplaceDialogStubShell == this)
155                 fgFindReplaceDialogStubShell= null;
156             
157             if (fWindow != null) {
158                 fWindow.getPartService().removePartListener(this);
159                 fWindow= null;
160             }
161             fDialog= null;
162             fPart= null;
163             fPreviousPart= null;
164             fPreviousTarget= null;
165         }
166
167         /*
168          * @see IPartListener#partOpened(IWorkbenchPart)
169          */

170         public void partOpened(IWorkbenchPart part) {}
171
172         /*
173          * @see IPartListener#partDeactivated(IWorkbenchPart)
174          */

175         public void partDeactivated(IWorkbenchPart part) {}
176
177         /*
178          * @see IPartListener#partBroughtToTop(IWorkbenchPart)
179          */

180         public void partBroughtToTop(IWorkbenchPart part) {}
181         
182         /**
183          * Checks if the dialogs shell is the same as the
184          * given <code>shell</code> and if not clears the stub
185          * and closes the dialog.
186          *
187          * @param shell the shell check
188          * @since 3.3
189          */

190         public void checkShell(Shell shell) {
191             if (fDialog != null && shell != fDialog.getParentShell()) {
192                 if (fgFindReplaceDialogStub == this)
193                     fgFindReplaceDialogStub= null;
194
195                 if(fgFindReplaceDialogStubShell == this)
196                     fgFindReplaceDialogStubShell= null;
197                 
198                 fDialog.close();
199             }
200         }
201     }
202
203
204     /**
205      * Listener for disabling the dialog on shell close.
206      * <p>
207      * This stub is shared amongst <code>IWorkbenchPart</code>s.</p>
208      */

209     private static FindReplaceDialogStub fgFindReplaceDialogStub;
210     
211     /** Listener for disabling the dialog on shell close.
212      * <p>
213      * This stub is shared amongst <code>Shell</code>s.</p>
214      * @since 3.3
215      */

216     private static FindReplaceDialogStub fgFindReplaceDialogStubShell;
217
218     /** The action's target */
219     private IFindReplaceTarget fTarget;
220     /** The part to use if the action is created with a part. */
221     private IWorkbenchPart fWorkbenchPart;
222     /** The workbench window */
223     private IWorkbenchWindow fWorkbenchWindow;
224     /**
225      * The shell to use if the action is created with a shell.
226      * @since 3.3
227      */

228     private Shell fShell;
229
230     /**
231      * Creates a new find/replace action for the given workbench part.
232      * <p>
233      * The action configures its visual representation from the given
234      * resource bundle.</p>
235      *
236      * @param bundle the resource bundle
237      * @param prefix a prefix to be prepended to the various resource keys
238      * (described in <code>ResourceAction</code> constructor), or
239      * <code>null</code> if none
240      * @param workbenchPart the workbench part
241      * @see ResourceAction#ResourceAction(ResourceBundle, String)
242      */

243     public FindReplaceAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, IWorkbenchPart workbenchPart) {
244         super(bundle, prefix);
245         Assert.isLegal(workbenchPart != null);
246         fWorkbenchPart= workbenchPart;
247         update();
248     }
249
250     /**
251      * Creates a new find/replace action for the given target and shell.
252      * <p>
253      * This can be used without having an IWorkbenchPart e.g. for
254      * dialogs or wizards.</p>
255      * <p>
256      * The action configures its visual representation from the given
257      * resource bundle.</p>
258      *
259      * @param bundle the resource bundle
260      * @param prefix a prefix to be prepended to the various resource keys
261      * (described in <code>ResourceAction</code> constructor), or
262      * <code>null</code> if none
263      * @param target the IFindReplaceTarget to use
264      * @param shell the shell
265      * @see ResourceAction#ResourceAction(ResourceBundle, String)
266      *
267      * @since 3.3
268      */

269     public FindReplaceAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, Shell shell, IFindReplaceTarget target) {
270         super(bundle, prefix);
271         Assert.isLegal(target != null && shell != null);
272         fTarget= target;
273         fShell= shell;
274         update();
275     }
276     
277     /**
278      * Creates a new find/replace action for the given workbench window.
279      * The action configures its visual representation from the given
280      * resource bundle.
281      *
282      * @param bundle the resource bundle
283      * @param prefix a prefix to be prepended to the various resource keys
284      * (described in <code>ResourceAction</code> constructor), or
285      * <code>null</code> if none
286      * @param workbenchWindow the workbench window
287      * @see ResourceAction#ResourceAction(ResourceBundle, String)
288      *
289      * @deprecated use FindReplaceAction(ResourceBundle, String, IWorkbenchPart) instead
290      */

291     public FindReplaceAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, IWorkbenchWindow workbenchWindow) {
292         super(bundle, prefix);
293         fWorkbenchWindow= workbenchWindow;
294         update();
295     }
296     
297     /*
298      * @see IAction#run()
299      */

300     public void run() {
301         if (fTarget == null)
302             return;
303
304         final FindReplaceDialog dialog;
305         final boolean isEditable;
306         
307         if(fShell == null) {
308             if (fgFindReplaceDialogStub != null) {
309                 Shell shell= fWorkbenchPart.getSite().getShell();
310                 fgFindReplaceDialogStub.checkShell(shell);
311             }
312             if (fgFindReplaceDialogStub == null)
313                 fgFindReplaceDialogStub= new FindReplaceDialogStub(fWorkbenchPart.getSite());
314
315             if (fWorkbenchPart instanceof ITextEditorExtension2)
316                 isEditable= ((ITextEditorExtension2) fWorkbenchPart).isEditorInputModifiable();
317             else
318                 isEditable= fTarget.isEditable();
319                 
320             dialog= fgFindReplaceDialogStub.getDialog();
321
322         } else {
323             if (fgFindReplaceDialogStubShell != null) {
324                 fgFindReplaceDialogStubShell.checkShell(fShell);
325             }
326             if (fgFindReplaceDialogStubShell == null)
327                 fgFindReplaceDialogStubShell= new FindReplaceDialogStub(fShell);
328
329             isEditable= fTarget.isEditable();
330             dialog= fgFindReplaceDialogStubShell.getDialog();
331         }
332         
333         dialog.updateTarget(fTarget, isEditable, true);
334         dialog.open();
335     }
336
337     /*
338      * @see IUpdate#update()
339      */

340     public void update() {
341
342         if(fShell == null){
343             if (fWorkbenchPart == null && fWorkbenchWindow != null)
344                 fWorkbenchPart= fWorkbenchWindow.getPartService().getActivePart();
345
346             if (fWorkbenchPart != null)
347                 fTarget= (IFindReplaceTarget) fWorkbenchPart.getAdapter(IFindReplaceTarget.class);
348             else
349                 fTarget= null;
350         }
351         setEnabled(fTarget != null && fTarget.canPerformFind());
352     }
353 }
354
Popular Tags