Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6f5fc20f31cadadf8d2f7d5a2005f68cb8b48d3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*******************************************************************************
 * Copyright (c) 2003, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.part;

import org.eclipse.core.runtime.IPath;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;

import org.eclipse.ui.IInPlaceEditor;
import org.eclipse.ui.IInPlaceEditorInput;

/**
 * Adapter for making a file resource a suitable input for an in-place editor.
 * <p>
 * This class may be instantiated; it is not intended to be subclassed.
 * </p>
 *
 * @since 3.0
 * @noextend This class is not intended to be subclassed by clients.
 */
public class FileInPlaceEditorInput extends FileEditorInput implements
        IInPlaceEditorInput {
    IInPlaceEditor embeddedEditor;

    /**
     * A resource listener to update the input and in-place
     * editor if the input's file resource changes.
     */
    private IResourceChangeListener resourceListener = new IResourceChangeListener() {
        @Override
		public void resourceChanged(IResourceChangeEvent event) {
            IResourceDelta mainDelta = event.getDelta();
            if (mainDelta != null && embeddedEditor != null) {
                IResourceDelta affectedElement = mainDelta.findMember(getFile()
                        .getFullPath());
                if (affectedElement != null) {
                	processDelta(affectedElement);
                }
            }
        }

        private boolean processDelta(final IResourceDelta delta) {
            Runnable changeRunnable = null;

            switch (delta.getKind()) {
            case IResourceDelta.REMOVED:
                if ((IResourceDelta.MOVED_TO & delta.getFlags()) != 0) {
                    changeRunnable = () -> {
					    IPath path = delta.getMovedToPath();
					    IFile newFile = delta.getResource().getWorkspace()
					            .getRoot().getFile(path);
					    if (newFile != null && embeddedEditor != null) {
					        embeddedEditor
					                .sourceChanged(new FileInPlaceEditorInput(
					                        newFile));
					    }
					};
                } else {
                    changeRunnable = () -> {
					    if (embeddedEditor != null) {
					        embeddedEditor.sourceDeleted();
					        embeddedEditor.getSite().getPage().closeEditor(
					                embeddedEditor, true);
					    }
					};

                }

                break;
            }

            if (changeRunnable != null && embeddedEditor != null) {
                embeddedEditor.getSite().getShell().getDisplay().asyncExec(
                        changeRunnable);
            }

            return true; // because we are sitting on files anyway
        }
    };

    /**
     * Creates an in-place editor input based on a file resource.
     *
     * @param file the file resource
     */
    public FileInPlaceEditorInput(IFile file) {
        super(file);
    }

    @Override
	public void setInPlaceEditor(IInPlaceEditor editor) {
        if (embeddedEditor != editor) {
            if (embeddedEditor != null) {
                getFile().getWorkspace().removeResourceChangeListener(
                        resourceListener);
            }

            embeddedEditor = editor;

            if (embeddedEditor != null) {
                getFile().getWorkspace().addResourceChangeListener(
                        resourceListener);
            }
        }
    }
}

Back to the top