1 12 13 package org.eclipse.ui.texteditor; 14 15 16 import java.util.ResourceBundle ; 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 49 public class FindReplaceAction extends ResourceAction implements IUpdate { 50 51 62 static class FindReplaceDialogStub implements IPartListener, DisposeListener { 63 64 65 private IWorkbenchPart fPart; 66 67 private IWorkbenchPart fPreviousPart; 68 69 private IFindReplaceTarget fPreviousTarget; 70 71 72 private IWorkbenchWindow fWindow; 73 74 private FindReplaceDialog fDialog; 75 76 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 95 public FindReplaceDialogStub(Shell shell) { 96 fDialog= new FindReplaceDialog(shell); 97 fDialog.create(); 98 fDialog.getShell().addDisposeListener(this); 99 } 100 101 105 public FindReplaceDialog getDialog() { 106 return fDialog; 107 } 108 109 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 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 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 170 public void partOpened(IWorkbenchPart part) {} 171 172 175 public void partDeactivated(IWorkbenchPart part) {} 176 177 180 public void partBroughtToTop(IWorkbenchPart part) {} 181 182 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 209 private static FindReplaceDialogStub fgFindReplaceDialogStub; 210 211 216 private static FindReplaceDialogStub fgFindReplaceDialogStubShell; 217 218 219 private IFindReplaceTarget fTarget; 220 221 private IWorkbenchPart fWorkbenchPart; 222 223 private IWorkbenchWindow fWorkbenchWindow; 224 228 private Shell fShell; 229 230 243 public FindReplaceAction(ResourceBundle bundle, String prefix, IWorkbenchPart workbenchPart) { 244 super(bundle, prefix); 245 Assert.isLegal(workbenchPart != null); 246 fWorkbenchPart= workbenchPart; 247 update(); 248 } 249 250 269 public FindReplaceAction(ResourceBundle bundle, String 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 291 public FindReplaceAction(ResourceBundle bundle, String prefix, IWorkbenchWindow workbenchWindow) { 292 super(bundle, prefix); 293 fWorkbenchWindow= workbenchWindow; 294 update(); 295 } 296 297 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 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 |