KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > part > FileInPlaceEditorInput


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 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.part;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IResourceChangeEvent;
15 import org.eclipse.core.resources.IResourceChangeListener;
16 import org.eclipse.core.resources.IResourceDelta;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.ui.IInPlaceEditor;
20 import org.eclipse.ui.IInPlaceEditorInput;
21
22 /**
23  * Adapter for making a file resource a suitable input for an in-place editor.
24  * <p>
25  * This class may be instantiated; it is not intended to be subclassed.
26  * </p>
27  *
28  * @since 3.0
29  */

30 public class FileInPlaceEditorInput extends FileEditorInput implements
31         IInPlaceEditorInput {
32     IInPlaceEditor embeddedEditor;
33
34     /**
35      * A resource listener to update the input and in-place
36      * editor if the input's file resource changes.
37      */

38     private IResourceChangeListener resourceListener = new IResourceChangeListener() {
39         public void resourceChanged(IResourceChangeEvent event) {
40             IResourceDelta mainDelta = event.getDelta();
41             if (mainDelta != null && embeddedEditor != null) {
42                 IResourceDelta affectedElement = mainDelta.findMember(getFile()
43                         .getFullPath());
44                 if (affectedElement != null) {
45                     try {
46                         processDelta(affectedElement);
47                     } catch (CoreException exception) {
48                         // Failed so close the receiver
49
if (embeddedEditor != null) {
50                             embeddedEditor.getSite().getPage().closeEditor(
51                                     embeddedEditor, true);
52                         }
53                     }
54                 }
55             }
56         }
57
58         private boolean processDelta(final IResourceDelta delta)
59                 throws CoreException {
60             Runnable JavaDoc changeRunnable = null;
61
62             switch (delta.getKind()) {
63             case IResourceDelta.REMOVED:
64                 if ((IResourceDelta.MOVED_TO & delta.getFlags()) != 0) {
65                     changeRunnable = new Runnable JavaDoc() {
66                         public void run() {
67                             IPath path = delta.getMovedToPath();
68                             IFile newFile = delta.getResource().getWorkspace()
69                                     .getRoot().getFile(path);
70                             if (newFile != null && embeddedEditor != null) {
71                                 embeddedEditor
72                                         .sourceChanged(new FileInPlaceEditorInput(
73                                                 newFile));
74                             }
75                         }
76                     };
77                 } else {
78                     changeRunnable = new Runnable JavaDoc() {
79                         public void run() {
80                             if (embeddedEditor != null) {
81                                 embeddedEditor.sourceDeleted();
82                                 embeddedEditor.getSite().getPage().closeEditor(
83                                         embeddedEditor, true);
84                             }
85                         }
86                     };
87
88                 }
89
90                 break;
91             }
92
93             if (changeRunnable != null && embeddedEditor != null) {
94                 embeddedEditor.getSite().getShell().getDisplay().asyncExec(
95                         changeRunnable);
96             }
97
98             return true; // because we are sitting on files anyway
99
}
100     };
101
102     /**
103      * Creates an in-place editor input based on a file resource.
104      *
105      * @param file the file resource
106      */

107     public FileInPlaceEditorInput(IFile file) {
108         super(file);
109     }
110
111     /* (non-Javadoc)
112      * @see org.eclipse.ui.IInPlaceEditorInput#setInPlaceEditor(org.eclipse.ui.IInPlaceEditor)
113      */

114     public void setInPlaceEditor(IInPlaceEditor editor) {
115         if (embeddedEditor != editor) {
116             if (embeddedEditor != null) {
117                 getFile().getWorkspace().removeResourceChangeListener(
118                         resourceListener);
119             }
120
121             embeddedEditor = editor;
122
123             if (embeddedEditor != null) {
124                 getFile().getWorkspace().addResourceChangeListener(
125                         resourceListener);
126             }
127         }
128     }
129 }
130
Popular Tags