001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 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.part;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.core.resources.IResourceChangeEvent;
014: import org.eclipse.core.resources.IResourceChangeListener;
015: import org.eclipse.core.resources.IResourceDelta;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IPath;
018: import org.eclipse.ui.IInPlaceEditor;
019: import org.eclipse.ui.IInPlaceEditorInput;
020:
021: /**
022: * Adapter for making a file resource a suitable input for an in-place editor.
023: * <p>
024: * This class may be instantiated; it is not intended to be subclassed.
025: * </p>
026: *
027: * @since 3.0
028: */
029: public class FileInPlaceEditorInput extends FileEditorInput implements
030: IInPlaceEditorInput {
031: IInPlaceEditor embeddedEditor;
032:
033: /**
034: * A resource listener to update the input and in-place
035: * editor if the input's file resource changes.
036: */
037: private IResourceChangeListener resourceListener = new IResourceChangeListener() {
038: public void resourceChanged(IResourceChangeEvent event) {
039: IResourceDelta mainDelta = event.getDelta();
040: if (mainDelta != null && embeddedEditor != null) {
041: IResourceDelta affectedElement = mainDelta
042: .findMember(getFile().getFullPath());
043: if (affectedElement != null) {
044: try {
045: processDelta(affectedElement);
046: } catch (CoreException exception) {
047: // Failed so close the receiver
048: if (embeddedEditor != null) {
049: embeddedEditor.getSite().getPage()
050: .closeEditor(embeddedEditor, true);
051: }
052: }
053: }
054: }
055: }
056:
057: private boolean processDelta(final IResourceDelta delta)
058: throws CoreException {
059: Runnable changeRunnable = null;
060:
061: switch (delta.getKind()) {
062: case IResourceDelta.REMOVED:
063: if ((IResourceDelta.MOVED_TO & delta.getFlags()) != 0) {
064: changeRunnable = new Runnable() {
065: public void run() {
066: IPath path = delta.getMovedToPath();
067: IFile newFile = delta.getResource()
068: .getWorkspace().getRoot().getFile(
069: path);
070: if (newFile != null
071: && embeddedEditor != null) {
072: embeddedEditor
073: .sourceChanged(new FileInPlaceEditorInput(
074: newFile));
075: }
076: }
077: };
078: } else {
079: changeRunnable = new Runnable() {
080: public void run() {
081: if (embeddedEditor != null) {
082: embeddedEditor.sourceDeleted();
083: embeddedEditor.getSite().getPage()
084: .closeEditor(embeddedEditor,
085: true);
086: }
087: }
088: };
089:
090: }
091:
092: break;
093: }
094:
095: if (changeRunnable != null && embeddedEditor != null) {
096: embeddedEditor.getSite().getShell().getDisplay()
097: .asyncExec(changeRunnable);
098: }
099:
100: return true; // because we are sitting on files anyway
101: }
102: };
103:
104: /**
105: * Creates an in-place editor input based on a file resource.
106: *
107: * @param file the file resource
108: */
109: public FileInPlaceEditorInput(IFile file) {
110: super (file);
111: }
112:
113: /* (non-Javadoc)
114: * @see org.eclipse.ui.IInPlaceEditorInput#setInPlaceEditor(org.eclipse.ui.IInPlaceEditor)
115: */
116: public void setInPlaceEditor(IInPlaceEditor editor) {
117: if (embeddedEditor != editor) {
118: if (embeddedEditor != null) {
119: getFile().getWorkspace().removeResourceChangeListener(
120: resourceListener);
121: }
122:
123: embeddedEditor = editor;
124:
125: if (embeddedEditor != null) {
126: getFile().getWorkspace().addResourceChangeListener(
127: resourceListener);
128: }
129: }
130: }
131: }
|