1 /*******************************************************************************
2  *  Copyright (c) 2005, 2019 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.pde.internal.ui.correction;
15 
16 import java.util.HashSet;
17 import org.eclipse.core.resources.*;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.pde.core.IBaseModel;
21 import org.eclipse.pde.internal.core.builders.PDEMarkerFactory;
22 import org.eclipse.pde.internal.ui.PDEPluginImages;
23 import org.eclipse.pde.internal.ui.util.ModelModification;
24 import org.eclipse.pde.internal.ui.util.PDEModelUtility;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.ui.views.markers.WorkbenchMarkerResolution;
27 
28 public abstract class AbstractPDEMarkerResolution extends WorkbenchMarkerResolution {
29 
30 	public static final int CREATE_TYPE = 1;
31 	public static final int RENAME_TYPE = 2;
32 	public static final int REMOVE_TYPE = 3;
33 	public static final int CONFIGURE_TYPE = 4;
34 
35 	protected Image image = null;
36 	protected int fType;
37 	/**
38 	 * This variable will only be available after run() is called.
39 	 * ie. its not the be used in getImage()/getType()/getDesciption()
40 	 */
41 	protected IResource fResource;
42 	protected IMarker marker = null;
43 
AbstractPDEMarkerResolution(int type)44 	public AbstractPDEMarkerResolution(int type) {
45 		fType = type;
46 	}
47 
AbstractPDEMarkerResolution(int type, IMarker marker2)48 	public AbstractPDEMarkerResolution(int type, IMarker marker2) {
49 		fType = type;
50 		marker = marker2;
51 	}
52 
53 	@Override
findOtherMarkers(IMarker[] markers)54 	public IMarker[] findOtherMarkers(IMarker[] markers) {
55 		HashSet<IMarker> mset = new HashSet<>(markers.length);
56 		for (IMarker iMarker : markers) {
57 			if (iMarker.equals(marker))
58 				continue;
59 			String str = iMarker.getAttribute(PDEMarkerFactory.compilerKey, ""); //$NON-NLS-1$
60 			if (str.equals(marker.getAttribute(PDEMarkerFactory.compilerKey, ""))) //$NON-NLS-1$
61 				mset.add(iMarker);
62 		}
63 		int size = mset.size();
64 		return mset.toArray(new IMarker[size]);
65 	}
66 
67 	@Override
getImage()68 	public Image getImage() {
69 		if (image == null) {
70 			switch (this.getType()) {
71 			case AbstractPDEMarkerResolution.CREATE_TYPE:
72 				image = PDEPluginImages.DESC_ADD_ATT.createImage();
73 				break;
74 			case AbstractPDEMarkerResolution.REMOVE_TYPE:
75 				image = PDEPluginImages.DESC_DELETE.createImage();
76 				break;
77 			case AbstractPDEMarkerResolution.RENAME_TYPE:
78 				image = PDEPluginImages.DESC_REFRESH.createImage();
79 				break;
80 			case AbstractPDEMarkerResolution.CONFIGURE_TYPE:
81 				image = PDEPluginImages.DESC_CON_SEV.createImage();
82 				break;
83 			}
84 		}
85 		return image;
86 	}
87 
getType()88 	public int getType() {
89 		return fType;
90 	}
91 
92 	@Override
getDescription()93 	public String getDescription() {
94 		return getLabel();
95 	}
96 
97 	@Override
run(IMarker marker)98 	public void run(IMarker marker) {
99 		this.marker = marker;
100 		fResource = marker.getResource();
101 		ModelModification modification = new ModelModification((IFile) marker.getResource()) {
102 			@Override
103 			protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
104 				createChange(model);
105 			}
106 		};
107 		PDEModelUtility.modifyModel(modification, null);
108 
109 	}
110 
111 
112 	@Override
finalize()113 	protected void finalize() throws Throwable {
114 		super.finalize();
115 		if (image != null)
116 			image.dispose();
117 	}
118 
createChange(IBaseModel model)119 	protected abstract void createChange(IBaseModel model);
120 
121 }
122