1 11 package org.eclipse.ui.internal.editorsupport.win32; 12 13 import java.io.File ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.Vector ; 16 17 import org.eclipse.core.resources.IFile; 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.IResourceChangeEvent; 21 import org.eclipse.core.resources.IResourceChangeListener; 22 import org.eclipse.core.resources.IResourceDelta; 23 import org.eclipse.core.resources.ResourcesPlugin; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.core.runtime.IProgressMonitor; 27 import org.eclipse.core.runtime.IStatus; 28 import org.eclipse.core.runtime.Status; 29 import org.eclipse.jface.action.IMenuManager; 30 import org.eclipse.jface.dialogs.ErrorDialog; 31 import org.eclipse.jface.dialogs.MessageDialog; 32 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 33 import org.eclipse.jface.resource.ImageDescriptor; 34 import org.eclipse.jface.resource.JFaceColors; 35 import org.eclipse.jface.window.Window; 36 import org.eclipse.swt.SWT; 37 import org.eclipse.swt.SWTException; 38 import org.eclipse.swt.custom.BusyIndicator; 39 import org.eclipse.swt.graphics.Image; 40 import org.eclipse.swt.ole.win32.OLE; 41 import org.eclipse.swt.ole.win32.OleAutomation; 42 import org.eclipse.swt.ole.win32.OleClientSite; 43 import org.eclipse.swt.ole.win32.OleFrame; 44 import org.eclipse.swt.ole.win32.Variant; 45 import org.eclipse.swt.widgets.Composite; 46 import org.eclipse.swt.widgets.Display; 47 import org.eclipse.swt.widgets.Menu; 48 import org.eclipse.swt.widgets.MenuItem; 49 import org.eclipse.swt.widgets.Shell; 50 import org.eclipse.ui.IEditorInput; 51 import org.eclipse.ui.IEditorSite; 52 import org.eclipse.ui.IFileEditorInput; 53 import org.eclipse.ui.IPartListener; 54 import org.eclipse.ui.IPathEditorInput; 55 import org.eclipse.ui.IWorkbench; 56 import org.eclipse.ui.IWorkbenchActionConstants; 57 import org.eclipse.ui.IWorkbenchPart; 58 import org.eclipse.ui.IWorkbenchWindow; 59 import org.eclipse.ui.PartInitException; 60 import org.eclipse.ui.PlatformUI; 61 import org.eclipse.ui.actions.WorkspaceModifyOperation; 62 import org.eclipse.ui.dialogs.SaveAsDialog; 63 import org.eclipse.ui.ide.ResourceUtil; 64 import org.eclipse.ui.internal.WorkbenchPlugin; 65 import org.eclipse.ui.part.EditorPart; 66 import org.eclipse.ui.part.FileEditorInput; 67 68 70 public class OleEditor extends EditorPart { 71 72 76 private IResourceChangeListener resourceListener = new IResourceChangeListener() { 77 78 81 public void resourceChanged(IResourceChangeEvent event) { 82 IResourceDelta mainDelta = event.getDelta(); 83 if (mainDelta == null) 84 return; 85 IResourceDelta affectedElement = mainDelta.findMember(resource 86 .getFullPath()); 87 if (affectedElement != null) 88 processDelta(affectedElement); 89 } 90 91 94 private boolean processDelta(final IResourceDelta delta) { 95 96 Runnable changeRunnable = null; 97 98 switch (delta.getKind()) { 99 case IResourceDelta.REMOVED: 100 if ((IResourceDelta.MOVED_TO & delta.getFlags()) != 0) { 101 changeRunnable = new Runnable () { 102 public void run() { 103 IPath path = delta.getMovedToPath(); 104 IFile newFile = delta.getResource().getWorkspace() 105 .getRoot().getFile(path); 106 if (newFile != null) { 107 sourceChanged(newFile); 108 } 109 } 110 }; 111 } else { 112 changeRunnable = new Runnable () { 113 public void run() { 114 sourceDeleted = true; 115 getSite().getPage().closeEditor(OleEditor.this, 116 true); 117 } 118 }; 119 120 } 121 122 break; 123 } 124 125 if (changeRunnable != null) 126 update(changeRunnable); 127 128 return true; } 130 131 }; 132 133 private OleFrame clientFrame; 134 135 private OleClientSite clientSite; 136 137 private File source; 138 139 private IFile resource; 140 141 private Image oleTitleImage; 142 143 boolean sourceDeleted = false; 146 147 boolean sourceChanged = false; 150 151 155 private boolean clientActive = false; 156 157 161 private boolean oleActivated = false; 162 163 private IPartListener partListener = new IPartListener() { 164 public void partActivated(IWorkbenchPart part) { 165 activateClient(part); 166 } 167 168 public void partBroughtToTop(IWorkbenchPart part) { 169 } 170 171 public void partClosed(IWorkbenchPart part) { 172 } 173 174 public void partOpened(IWorkbenchPart part) { 175 } 176 177 public void partDeactivated(IWorkbenchPart part) { 178 deactivateClient(part); 179 } 180 }; 181 182 private static final String RENAME_ERROR_TITLE = OleMessages 183 .getString("OleEditor.errorSaving"); 185 private static final String OLE_EXCEPTION_TITLE = OleMessages 186 .getString("OleEditor.oleExceptionTitle"); 188 private static final String OLE_EXCEPTION_MESSAGE = OleMessages 189 .getString("OleEditor.oleExceptionMessage"); 191 private static final String OLE_CREATE_EXCEPTION_MESSAGE = OleMessages 192 .getString("OleEditor.oleCreationExceptionMessage"); 194 private static final String OLE_CREATE_EXCEPTION_REASON = OleMessages 195 .getString("OleEditor.oleCreationExceptionReason"); 197 private static final String SAVE_ERROR_TITLE = OleMessages 198 .getString("OleEditor.savingTitle"); 200 private static final String SAVE_ERROR_MESSAGE = OleMessages 201 .getString("OleEditor.savingMessage"); 203 206 public OleEditor() { 207 } 209 210 private void activateClient(IWorkbenchPart part) { 211 if (part == this) { 212 oleActivate(); 213 this.clientActive = true; 214 } 215 } 216 217 220 public void createPartControl(Composite parent) { 221 222 clientFrame = new OleFrame(parent, SWT.CLIP_CHILDREN); 224 clientFrame.setBackground(JFaceColors.getBannerBackground(clientFrame 225 .getDisplay())); 226 227 initializeWorkbenchMenus(); 228 229 createClientSite(); 230 } 231 232 235 236 private void createClientSite() { 237 if (clientFrame == null || clientFrame.isDisposed()) 239 return; 240 try { 242 clientSite = new OleClientSite(clientFrame, SWT.NONE, source); 243 } catch (SWTException exception) { 244 245 IStatus errorStatus = new Status(IStatus.ERROR, 246 WorkbenchPlugin.PI_WORKBENCH, IStatus.ERROR, 247 OLE_CREATE_EXCEPTION_REASON, exception); 248 ErrorDialog.openError(null, OLE_EXCEPTION_TITLE, OLE_CREATE_EXCEPTION_MESSAGE, errorStatus); 250 return; 251 } 252 clientSite.setBackground(JFaceColors.getBannerBackground(clientFrame 253 .getDisplay())); 254 255 } 256 257 private void deactivateClient(IWorkbenchPart part) { 258 if (part == this && clientActive) { 261 if (clientSite != null) 262 clientSite.deactivateInPlaceClient(); 263 this.clientActive = false; 264 this.oleActivated = false; 265 } 266 } 267 268 273 private void displayErrorDialog(String title, String message) { 274 Shell parent = null; 275 if (getClientSite() != null) 276 parent = getClientSite().getShell(); 277 MessageDialog.openError(parent, title, message); 278 } 279 280 283 public void dispose() { 284 if (resource != null) { 285 ResourcesPlugin.getWorkspace().removeResourceChangeListener( 286 resourceListener); 287 resource = null; 288 } 289 290 if (oleTitleImage != null) { 292 oleTitleImage.dispose(); 293 oleTitleImage = null; 294 } 295 296 if (getSite() != null && getSite().getPage() != null) 297 getSite().getPage().removePartListener(partListener); 298 299 } 300 301 304 public void doPrint() { 305 if (clientSite == null) 306 return; 307 BusyIndicator.showWhile(clientSite.getDisplay(), new Runnable () { 308 public void run() { 309 clientSite.exec(OLE.OLECMDID_PRINT, 310 OLE.OLECMDEXECOPT_PROMPTUSER, null, null); 311 } 313 }); 314 } 315 316 319 public void doSave(final IProgressMonitor monitor) { 320 if (clientSite == null) 321 return; 322 BusyIndicator.showWhile(clientSite.getDisplay(), new Runnable () { 323 324 328 public void run() { 329 330 if (!sourceChanged) { 333 int result = clientSite.queryStatus(OLE.OLECMDID_SAVE); 334 if ((result & OLE.OLECMDF_ENABLED) != 0) { 335 result = clientSite.exec(OLE.OLECMDID_SAVE, 336 OLE.OLECMDEXECOPT_PROMPTUSER, null, null); 337 if (result == OLE.S_OK) { 338 try { 339 resource.refreshLocal(IResource.DEPTH_ZERO, 340 monitor); 341 } catch (CoreException ex) { 342 } 344 return; 345 } 346 displayErrorDialog(OLE_EXCEPTION_TITLE, 347 OLE_EXCEPTION_MESSAGE + String.valueOf(result)); 348 return; 349 } 350 } 351 if (saveFile(source)) { 352 try { 353 resource.refreshLocal(IResource.DEPTH_ZERO, monitor); 354 } catch (CoreException ex) { 355 } 357 } else 358 displayErrorDialog(SAVE_ERROR_TITLE, SAVE_ERROR_MESSAGE 359 + source.getName()); 360 } 361 }); 362 } 363 364 367 public void doSaveAs() { 368 if (clientSite == null) 369 return; 370 WorkspaceModifyOperation op = saveNewFileOperation(); 371 Shell shell = clientSite.getShell(); 372 try { 373 new ProgressMonitorDialog(shell).run(false, true, op); 374 } catch (InterruptedException interrupt) { 375 } catch (InvocationTargetException invocationException) { 377 MessageDialog.openError(shell, RENAME_ERROR_TITLE, 378 invocationException.getTargetException().getMessage()); 379 } 380 381 } 382 383 388 public OleClientSite getClientSite() { 389 return clientSite; 390 } 391 392 397 public File getSourceFile() { 398 return source; 399 } 400 401 private void handleWord() { 402 OleAutomation dispInterface = new OleAutomation(clientSite); 403 int[] appId = dispInterface 405 .getIDsOfNames(new String [] { "Application" }); if (appId != null) { 407 Variant pVarResult = dispInterface.getProperty(appId[0]); 408 if (pVarResult != null) { 409 OleAutomation application = pVarResult.getAutomation(); 410 int[] dispid = application 411 .getIDsOfNames(new String [] { "DisplayScrollBars" }); if (dispid != null) { 413 Variant rgvarg = new Variant(true); 414 application.setProperty(dispid[0], rgvarg); 415 } 416 application.dispose(); 417 } 418 } 419 dispInterface.dispose(); 420 } 421 422 435 public void init(IEditorSite site, IEditorInput input) 436 throws PartInitException { 437 438 validatePathEditorInput(input); 439 440 setSite(site); 442 setInputWithNotify(input); 443 444 setPartName(input.getName()); 446 setTitleToolTip(input.getToolTipText()); 447 ImageDescriptor desc = input.getImageDescriptor(); 448 if (desc != null) { 449 oleTitleImage = desc.createImage(); 450 setTitleImage(oleTitleImage); 451 } 452 453 site.getPage().addPartListener(partListener); 455 456 } 457 458 464 private boolean validatePathEditorInput(IEditorInput input) throws PartInitException { 465 if (!(input instanceof IPathEditorInput)) 467 throw new PartInitException(OleMessages.format( 468 "OleEditor.invalidInput", new Object [] { input })); 470 IPath path= ((IPathEditorInput)input).getPath(); 471 472 if (path == null 474 || !(new File (path.toOSString()).exists())) 475 throw new PartInitException( 476 OleMessages 477 .format( 478 "OleEditor.noFileInput", new Object [] { path.toOSString() })); return true; 480 } 481 482 485 protected void initializeWorkbenchMenus() { 486 if (clientFrame == null || clientFrame.isDisposed()) 488 return; 489 Shell shell = clientFrame.getShell(); 492 Menu menuBar = shell.getMenuBar(); 493 if (menuBar == null) { 494 menuBar = new Menu(shell, SWT.BAR); 495 shell.setMenuBar(menuBar); 496 } 497 498 MenuItem[] windowMenu = new MenuItem[1]; 500 MenuItem[] fileMenu = new MenuItem[1]; 501 Vector containerItems = new Vector (); 502 503 IWorkbenchWindow window = getSite().getWorkbenchWindow(); 504 505 for (int i = 0; i < menuBar.getItemCount(); i++) { 506 MenuItem item = menuBar.getItem(i); 507 String id = ""; if (item.getData() instanceof IMenuManager) 509 id = ((IMenuManager) item.getData()).getId(); 510 if (id.equals(IWorkbenchActionConstants.M_FILE)) 511 fileMenu[0] = item; 512 else if (id.equals(IWorkbenchActionConstants.M_WINDOW)) 513 windowMenu[0] = item; 514 else { 515 if (window.isApplicationMenu(id)) { 516 containerItems.addElement(item); 517 } 518 } 519 } 520 MenuItem[] containerMenu = new MenuItem[containerItems.size()]; 521 containerItems.copyInto(containerMenu); 522 clientFrame.setFileMenus(fileMenu); 523 clientFrame.setContainerMenus(containerMenu); 524 clientFrame.setWindowMenus(windowMenu); 525 } 526 527 531 public boolean isDirty() { 532 534 return this.clientSite != null; 535 } 536 537 541 public boolean isSaveAsAllowed() { 542 return true; 543 } 544 545 550 public boolean isSaveNeeded() { 551 return getClientSite() != null && isDirty(); 552 } 553 554 559 private boolean saveFile(File file) { 560 561 File tempFile = new File (file.getAbsolutePath() + ".tmp"); file.renameTo(tempFile); 563 boolean saved = false; 564 if (OLE.isOleFile(file) || usesStorageFiles(clientSite.getProgramID())) { 565 saved = clientSite.save(file, true); 566 } else { 567 saved = clientSite.save(file, false); 568 } 569 570 if (saved) { 571 tempFile.delete(); 573 return true; 574 } 575 tempFile.renameTo(file); 577 return false; 578 } 579 580 584 private WorkspaceModifyOperation saveNewFileOperation() { 585 586 return new WorkspaceModifyOperation() { 587 public void execute(final IProgressMonitor monitor) 588 throws CoreException { 589 SaveAsDialog dialog = new SaveAsDialog(clientFrame.getShell()); 590 IFile sFile = ResourceUtil.getFile(getEditorInput()); 591 if (sFile != null) { 592 dialog.setOriginalFile(sFile); 593 dialog.open(); 594 595 IPath newPath = dialog.getResult(); 596 if (newPath == null) 597 return; 598 599 if (dialog.getReturnCode() == Window.OK) { 600 String projectName = newPath.segment(0); 601 newPath = newPath.removeFirstSegments(1); 602 IProject project = resource.getWorkspace().getRoot() 603 .getProject(projectName); 604 newPath = project.getLocation().append(newPath); 605 File newFile = newPath.toFile(); 606 if (saveFile(newFile)) { 607 IFile newResource = resource.getWorkspace().getRoot() 608 .getFileForLocation(newPath); 609 if (newResource != null) { 610 sourceChanged(newResource); 611 newResource.refreshLocal(IResource.DEPTH_ZERO, 612 monitor); 613 } 614 } else { 615 displayErrorDialog(SAVE_ERROR_TITLE, SAVE_ERROR_MESSAGE 616 + newFile.getName()); 617 return; 618 } 619 } 620 } 621 } 622 }; 623 624 } 625 626 630 public void setFocus() { 631 } 633 634 637 private void oleActivate() { 638 if (clientSite == null || clientFrame == null 640 || clientFrame.isDisposed()) 641 return; 642 643 if (!oleActivated) { 644 clientSite.doVerb(OLE.OLEIVERB_SHOW); 645 oleActivated = true; 646 String progId = clientSite.getProgramID(); 647 if (progId != null && progId.startsWith("Word.Document")) { handleWord(); 649 } 650 } 651 } 652 653 656 protected void setInputWithNotify(IEditorInput input) { 657 if (input instanceof IPathEditorInput) 658 source = new File (((IPathEditorInput)input).getPath().toOSString()); 659 660 if (input instanceof IFileEditorInput) { 661 if (resource == null) 662 ResourcesPlugin.getWorkspace().addResourceChangeListener(resourceListener); 663 resource = ((IFileEditorInput)input).getFile(); 664 } else if (resource != null) { 665 ResourcesPlugin.getWorkspace().removeResourceChangeListener(resourceListener); 666 resource = null; 667 } 668 669 super.setInputWithNotify(input); 670 } 671 672 677 private static boolean usesStorageFiles(String progID) { 678 return (progID != null && (progID.startsWith("Word.", 0) || progID.startsWith("MSGraph", 0) || progID.startsWith("PowerPoint", 0) || progID.startsWith("Excel", 0))); } 683 684 689 private void sourceChanged(IFile newFile) { 690 691 FileEditorInput newInput = new FileEditorInput(newFile); 692 setInputWithNotify(newInput); 693 sourceChanged = true; 694 setPartName(newInput.getName()); 695 696 } 697 698 701 public boolean isSaveOnCloseNeeded() { 702 return !sourceDeleted && super.isSaveOnCloseNeeded(); 703 } 704 705 710 private void update(Runnable runnable) { 711 IWorkbench workbench = PlatformUI.getWorkbench(); 712 IWorkbenchWindow[] windows = workbench.getWorkbenchWindows(); 713 if (windows != null && windows.length > 0) { 714 Display display = windows[0].getShell().getDisplay(); 715 display.asyncExec(runnable); 716 } else 717 runnable.run(); 718 } 719 720 } 721 | Popular Tags |