Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 93982015b1d86d7c4b6913e7d440003b090df070 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*******************************************************************************
 * Copyright (c) 2014 THALES GLOBAL SERVICES and others.
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.diagram.ui.internal.refresh.listeners;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.sirius.common.tools.api.resource.FileProvider;
import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin;

import com.google.common.collect.Lists;

/**
 * An {@link IResourceChangeListener} that removes the resource from a cache of
 * files if existing in it.
 * 
 * @author <a href="mailto:steve.monnier@obeo.fr">Steve Monnier</a>
 */
public class WorkspaceFileResourceChangeListener implements IResourceChangeListener {

    /**
     * Map associating a {@link File} uri, as a Sring, to the {@link File}.
     */
    private Map<String, File> uriToFileMap = new HashMap<String, File>();

    /**
     * Map associating a {@link File} to its read status (file != null &&
     * file.exists() && file.canRead()).
     */
    private Map<File, Boolean> readStatusFileMap = new HashMap<File, Boolean>();

    /**
     * Map associating a {@link File} to its {@link URL}, in order to avoid recomputing it
     * multiple times.
     */
    private Map<File, URL> fileURLMap = new HashMap<File, URL>();

    /**
     * Returns the shared instance.
     * 
     * @return the {@link WorkspaceFileResourceChangeListener} instance.
     */
    public static WorkspaceFileResourceChangeListener getInstance() {
        return WorkspaceResourceChangeListenerHolder.instance;
    }

    private static class WorkspaceResourceChangeListenerHolder {
        private static WorkspaceFileResourceChangeListener instance;
        static {
            instance = new WorkspaceResourceChangeListenerImpl();
            instance.init();
        }

    }

    /**
     * Init the {@link IResourceChangeListener}.
     * 
     */
    public void init() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void resourceChanged(IResourceChangeEvent event) {
        // Do nothing.
    }

    /**
     * Returns the {@link ResourceDeltaVisitor} to use for visiting
     * changes.
     * 
     * @return the {@link ResourceDeltaVisitor} to use for visiting changes
     */
    protected ResourceDeltaVisitor getResourceDeltaVisitor() {
        return new ResourceDeltaVisitor();
    }

    protected class ResourceDeltaVisitor implements IResourceDeltaVisitor {

        List<String> changedFilesURI = Lists.<String> newArrayList();

        public ResourceDeltaVisitor() {
        }

        /**
         * {@inheritDoc}
         * 
         * This method has been overridden to check if the file is contained in
         * a cache of files.
         */
        protected boolean visitFile(IResourceDelta delta, IFile file) {
            String uri = file.getFullPath().makeRelative().toString();
            if (hasURIOfFile(uri)) {
                changedFilesURI.add(uri);
            }
            return false;
        }

        @Override
        public boolean visit(IResourceDelta delta) throws CoreException {
            boolean visitChildren = true;
            IResource res = delta.getResource();
            if (res.getType() == IResource.FILE) {
                visitChildren = visitFile(delta, (IFile) res);
            }
            return visitChildren;
        }

    }

    /**
     * Finds a {@link File} from a file URI using a cache. If it is not found,
     * the {@link File} is loaded and added to the cache.
     * 
     * @param uri
     *            the path of the file
     * @return the cached {@link File}.
     */
    public File getFileFromURI(String uri) {
        if (uriToFileMap.containsKey(uri)) {
            return uriToFileMap.get(uri);
        } else {
            final File file = FileProvider.getDefault().getFile(new Path(uri));
            uriToFileMap.put(uri, file);
            return file;
        }
    }

    /**
     * Checks if an uri exists in the cache of files.
     * 
     * @param uri
     *            the uri of the file to check.
     * @return if an uri exists in the cache of files.
     */
    public boolean hasURIOfFile(String uri) {
        return uriToFileMap.containsKey(uri);
    }

    /**
     * 
     * Finds the read status (exists() && canRead()) of a {@link File} using a
     * cache. If it is not found, the status is computed and added to the cache.
     * 
     * @param file
     *            {@link File} to request its read status.
     * @return the read status of the {@link File}.
     */
    public boolean getReadStatusOfFile(File file) {
        if (readStatusFileMap.containsKey(file)) {
            return readStatusFileMap.get(file);
        } else {
            boolean fileStatus = file != null && file.exists() && file.canRead();
            readStatusFileMap.put(file, fileStatus);
            try {
                // Addition to the file to url cache as well
                fileURLMap.put(file, file.toURI().toURL());
            } catch (MalformedURLException e) {
                DiagramUIPlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, DiagramUIPlugin.ID, e.getMessage()));
            }
            return fileStatus;
        }
    }

    /**
     * Clears the files caches.
     */
    public void dispose() {
        final IWorkspace workspace = ResourcesPlugin.getWorkspace();
        workspace.removeResourceChangeListener(this);
        uriToFileMap.clear();
        readStatusFileMap.clear();
        fileURLMap.clear();
    }

    /**
     * Removes a files from the caches.
     * 
     * @param uri
     *            of the {@link File} to remove from the caches.
     */
    public void removeFileStatusAndURIFromMaps(String uri) {
        readStatusFileMap.remove(uriToFileMap.get(uri));
        fileURLMap.remove(uriToFileMap.get(uri));
        uriToFileMap.remove(uri);
    }

    /**
     * Returns an image descriptor for the given image file. <br/>
     * Note that this method uses a cache of {@link File} to {@link URL}, that
     * can improve performances on diagrams with a lot of instance of (the same)
     * image.
     * 
     * @param file
     *            the image file.
     * @return the image descriptor.
     */
    public ImageDescriptor findImageDescriptor(File file) throws MalformedURLException {
        if (fileURLMap.containsKey(file)) {
            return DiagramUIPlugin.Implementation.getURLImageDescriptor(fileURLMap.get(file));
        }
        return DiagramUIPlugin.Implementation.getURLImageDescriptor(file.toURI().toURL());
    }

}

Back to the top