summaryrefslogtreecommitdiffstats
path: root/rba.tool.core/src/rba/tool/core/marker/MarkerManager.java
blob: b04809f90e87b2a5b32e81fa009a0400cd0d4e27 (plain)
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
package rba.tool.core.marker;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
import org.eclipse.xtext.resource.ILocationInFileProvider;
import org.eclipse.xtext.util.TextRegionWithLineInformation;

import com.google.inject.Injector;

import rba.tool.editor.ui.activator.ExtensionEditorActivator;
import rba.tool.editor.util.RBAModelEditorNameUtil;

public class MarkerManager {
    public static MarkerManager INSTANCE = new MarkerManager();

    public static String MARKER_ID_MODEL_EX = "rba.tool.core.model.ex"; //$NON-NLS-1$

    public static String MARKER_ID_SORT_VALUE = "rba.tool.core.sort"; //$NON-NLS-1$

    public static String MARKER_ID_CONSTRAINT = "rba.tool.core.constraint"; //$NON-NLS-1$

    public void createMarker(EObject element, String message, int errorLevel, String id) {
        Resource resource = element.eResource();
        IFile file = WorkspaceSynchronizer.getFile(resource);

        if (existInBuildFolder(file.getParent())) {
            IFolder fol = file.getProject().getFolder(RBAModelEditorNameUtil.MODEL_FOLDER_NAME);
            String path = getPath(file.getParent(), "");
            if (!path.equals("")) {
                file = fol.getFile(path + "//" + file.getName());
            } else {
                file = fol.getFile(file.getName());
            }
        }
        if (file != null && file.exists()) {
            String location = file.getFullPath().toString();

            Injector injector = ExtensionEditorActivator.getInstance().getInjector("rba.tool.editor.RBAModel");
            ILocationInFileProvider locationInFileProvider = injector.getInstance(ILocationInFileProvider.class);

            TextRegionWithLineInformation fullTextRegion = (TextRegionWithLineInformation) locationInFileProvider.getFullTextRegion(element);
            int lineNumber = fullTextRegion.getLineNumber() + 1;

            Map<String, Object> attributes = new HashMap<String, Object>();
            attributes.put(IMarker.LINE_NUMBER, lineNumber);

            attributes.put(IMarker.SEVERITY, errorLevel);
            attributes.put(IMarker.MESSAGE, message);
            attributes.put(IMarker.LOCATION, location);

            try {
                IMarker marker = file.createMarker(id);
                marker.setAttributes(attributes);
            } catch (CoreException e) {
                e.printStackTrace();
            }
        }
    }

    public void clearMarkers(Resource resource, String id) {
        IFile ifile = WorkspaceSynchronizer.getFile(resource);
        if (ifile == null) {
            return;
        }
        if (existInBuildFolder(ifile.getParent())) {
            IFolder fol = ifile.getProject().getFolder(RBAModelEditorNameUtil.MODEL_FOLDER_NAME);
            String path = getPath(ifile.getParent(), "");
            if (!path.equals("")) {
                ifile = fol.getFile(path + "//" + ifile.getName());
            } else {
                ifile = fol.getFile(ifile.getName());
            }
        }
        if (ifile != null && ifile.exists()) {
        try {
            for (IMarker marker : ResourcesPlugin.getWorkspace().getRoot().findMarkers(id, true, IResource.DEPTH_INFINITE)) {
                if (ifile.equals(marker.getResource())) {
                    marker.delete();
                }
            }
        } catch (CoreException e) {
            throw new RuntimeException(e);
        }
        }
    }

    public void clearMarkers(IProject project, String id) {

        try {
            for (IMarker marker : ResourcesPlugin.getWorkspace().getRoot().findMarkers(id, true, IResource.DEPTH_INFINITE)) {

                IResource resource = marker.getResource();
                if (project == resource.getProject()) {
                    marker.delete();
                }
            }
        } catch (CoreException e) {
            throw new RuntimeException(e);
        }
    }

    private boolean existInBuildFolder(IResource res) {
        if (res instanceof IFolder) {
            if (res.getName().equals(RBAModelEditorNameUtil.BUILD_FOLDER_NAME)) {
                return true;
            } else {
                return existInBuildFolder(res.getParent());
            }
        }
        return false;
    }

    private String getPath(IResource res, String path) {
        if (res instanceof IFolder) {
            if (res.getName().equals(RBAModelEditorNameUtil.BUILD_FOLDER_NAME)) {
                return path;
            } else {
                String p;
                if (!path.equals("")) {
                    p = res.getName() + "//" + path;
                } else {
                    p = res.getName();
                }
                return getPath(res.getParent(), p);
            }
        }
        return "";
    }
}