Skip to main content
summaryrefslogtreecommitdiffstats
blob: e3745ca918b02837b1bda0d46236cc2cfc1b61d7 (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
/*
 * <copyright>
 *
 * Copyright (c) 2005-2006 Sven Efftinge (http://www.efftinge.de) 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:
 *     Sven Efftinge (http://www.efftinge.de) - Initial API and implementation
 *
 * </copyright>
 */
package org.eclipse.xtend.shared.ui.core.internal;

import java.util.Iterator;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.internal.xtend.util.Cache;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.xtend.shared.ui.core.IModelManager;
import org.eclipse.xtend.shared.ui.core.IXtendXpandProject;
import org.eclipse.xtend.shared.ui.core.IXtendXpandResource;
import org.eclipse.xtend.shared.ui.core.builder.XtendXpandNature;
import org.eclipse.xtend.shared.ui.internal.XtendLog;

public class XtendXpandModelManager implements IModelManager {

    public XtendXpandModelManager() {

    }

    public final Cache<IJavaProject, XtendXpandProject> projects = new Cache<IJavaProject, XtendXpandProject>() {
        @Override
        protected XtendXpandProject createNew(IJavaProject ele) {
            return new XtendXpandProject(ele);
        }
    };

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.internal.xtend.core.IXpandModelManager#findProject(org.eclipse.core.runtime.IPath)
     */
    public IXtendXpandProject findProject(final IPath path) {
        return findProject(ResourcesPlugin.getWorkspace().getRoot().findMember(path));
    }

    public IXtendXpandProject findProject(final IResource res) {
        if (res == null)
            return null;
        final IJavaProject ele = JavaCore.create(res.getProject());
        try {
            if (ele != null && res.getProject().isAccessible() && res.getProject().isNatureEnabled(XtendXpandNature.NATURE_ID))
                return (IXtendXpandProject) projects.get(ele);
        } catch (final CoreException e) {
            XtendLog.logError(e);
        }
        return null;
    }

    public void analyze(final IProgressMonitor monitor) {
        monitor.beginTask("Analyzing oAW4 projects...", computeAmoutOfWork());
        for (final Iterator<?> iter = projects.getValues().iterator(); iter.hasNext();) {
            if (monitor.isCanceled())
                return;
            ((IXtendXpandProject) iter.next()).analyze(monitor);
        }
        monitor.done();
    }

    /**
     * Computes the amount of work that has to be done during a build.
     * @return the number of resources registered within all oAW projects.
     */
    private int computeAmoutOfWork() {
        int i = 0;
        for (final Iterator<?> iter = projects.getValues().iterator(); iter.hasNext();) {
            final IXtendXpandProject element = (IXtendXpandProject) iter.next();
            i += element.getRegisteredResources().length;
        }
        return i;
    }

    /**
     * Tries to locate an oAW resource by its underlying file.
     * @param underlying IStorage
     */
    public IXtendXpandResource findExtXptResource(IStorage file) {
    	// it can be that the resource is located within a jar, than scan the projects for the resource
    	if (!(file instanceof IFile)) {
    		for (Iterator<?> it=projects.getValues().iterator(); it.hasNext(); ) {
    			IXtendXpandProject p = (IXtendXpandProject) it.next();
    			IXtendXpandResource res = p.findOawResource(file);
    			if (res!=null) {
    				return res;
    			}
    		}
    	} else {
	        final IXtendXpandProject project = findProject((IFile)file);
	        if (project != null) {
	            return project.findOawResource(file);
	        }
        }
        return null;
    }

	public IXtendXpandResource findOawResource(String oawNamespace, String extension) {
		for (IXtendXpandProject p : projects.getValues()) {
			IXtendXpandResource res = p.findExtXptResource(oawNamespace, extension);
			if (res!=null)
				return res;
		}
		return null;
	}

}

Back to the top