1 /*******************************************************************************
2  * Copyright (c) 2008, 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.api.tools.ui.internal.markers;
15 
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.pde.api.tools.internal.problems.ApiProblemFactory;
22 import org.eclipse.pde.api.tools.internal.provisional.IApiMarkerConstants;
23 import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblem;
24 import org.eclipse.pde.api.tools.ui.internal.ApiUIPlugin;
25 import org.eclipse.pde.api.tools.ui.internal.IApiToolsConstants;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.ui.IMarkerResolution2;
28 
29 /**
30  * This resolution helps users to pick a default API profile when the tooling
31  * has been set up but there is no default profile
32  *
33  * @since 1.0.0
34  */
35 public class VersionNumberingResolution implements IMarkerResolution2 {
36 	String newVersionValue;
37 	// major or minor version
38 	private int kind;
39 	private String description;
40 
VersionNumberingResolution(IMarker marker)41 	public VersionNumberingResolution(IMarker marker) {
42 		this.newVersionValue = marker.getAttribute(IApiMarkerConstants.MARKER_ATTR_VERSION, null);
43 		this.kind = ApiProblemFactory.getProblemKind(marker.getAttribute(IApiMarkerConstants.MARKER_ATTR_PROBLEM_ID, 0));
44 		this.description = marker.getAttribute(IApiMarkerConstants.VERSION_NUMBERING_ATTR_DESCRIPTION, null);
45 	}
46 
47 	@Override
getDescription()48 	public String getDescription() {
49 		description = description.replace(System.lineSeparator(), "<br>");//$NON-NLS-1$
50 		switch (this.kind) {
51 			case IApiProblem.MAJOR_VERSION_CHANGE:
52 				return NLS.bind(MarkerMessages.VersionNumberingResolution_major0, new String[] { this.description });
53 			case IApiProblem.MINOR_VERSION_CHANGE:
54 				return NLS.bind(MarkerMessages.VersionNumberingResolution_minor0, new String[] { this.description });
55 			case IApiProblem.MAJOR_VERSION_CHANGE_NO_BREAKAGE:
56 				return NLS.bind(MarkerMessages.VersionNumberingResolution_major0, new String[] { this.description });
57 			case IApiProblem.MINOR_VERSION_CHANGE_NO_NEW_API:
58 				return MarkerMessages.VersionNumberingResolution_minorNoNewAPI0;
59 			case IApiProblem.REEXPORTED_MAJOR_VERSION_CHANGE:
60 				return MarkerMessages.VersionNumberingResolution_reexportedMajor0;
61 			case IApiProblem.MINOR_VERSION_CHANGE_EXECUTION_ENV_CHANGED:
62 				return MarkerMessages.VersionNumberingResolution_breeMinor;
63 			case IApiProblem.MICRO_VERSION_CHANGE_UNNECESSARILY:
64 				return MarkerMessages.VersionNumberingResolution_unnecessaryMicroIncrease;
65 			case IApiProblem.MINOR_VERSION_CHANGE_UNNECESSARILY:
66 				return MarkerMessages.VersionNumberingResolution_unnecessaryMinorIncrease;
67 			default:
68 				// reexported minor
69 				return MarkerMessages.VersionNumberingResolution_reexportedMinor0;
70 		}
71 	}
72 
73 	@Override
getImage()74 	public Image getImage() {
75 		return ApiUIPlugin.getSharedImage(IApiToolsConstants.IMG_OBJ_BUNDLE_VERSION);
76 	}
77 
78 	@Override
getLabel()79 	public String getLabel() {
80 		switch (this.kind) {
81 			case IApiProblem.MAJOR_VERSION_CHANGE:
82 				return NLS.bind(MarkerMessages.VersionNumberingResolution_major1, this.newVersionValue);
83 			case IApiProblem.MINOR_VERSION_CHANGE:
84 				return NLS.bind(MarkerMessages.VersionNumberingResolution_minor1, this.newVersionValue);
85 			case IApiProblem.MAJOR_VERSION_CHANGE_NO_BREAKAGE:
86 				return NLS.bind(MarkerMessages.VersionNumberingResolution_major1, this.newVersionValue);
87 			case IApiProblem.MINOR_VERSION_CHANGE_NO_NEW_API:
88 				return NLS.bind(MarkerMessages.VersionNumberingResolution_minorNoNewAPI1, this.newVersionValue);
89 			case IApiProblem.REEXPORTED_MAJOR_VERSION_CHANGE:
90 				return NLS.bind(MarkerMessages.VersionNumberingResolution_major1, this.newVersionValue);
91 			case IApiProblem.MICRO_VERSION_CHANGE_UNNECESSARILY:
92 				return MarkerMessages.VersionNumberingResolution_unnecessaryMicroIncrease;
93 			case IApiProblem.MINOR_VERSION_CHANGE_UNNECESSARILY:
94 				return MarkerMessages.VersionNumberingResolution_unnecessaryMinorIncrease;
95 
96 			default:
97 				// IApiProblem.REEXPORTED_MINOR_VERSION_CHANGE
98 				return NLS.bind(MarkerMessages.VersionNumberingResolution_minor1, this.newVersionValue);
99 		}
100 	}
101 
102 	@Override
run(final IMarker marker)103 	public void run(final IMarker marker) {
104 		String title = null;
105 		switch (this.kind) {
106 			case IApiProblem.MAJOR_VERSION_CHANGE:
107 				title = NLS.bind(MarkerMessages.VersionNumberingResolution_major1, this.newVersionValue);
108 				break;
109 			case IApiProblem.MINOR_VERSION_CHANGE:
110 				title = NLS.bind(MarkerMessages.VersionNumberingResolution_minor1, this.newVersionValue);
111 				break;
112 			case IApiProblem.MAJOR_VERSION_CHANGE_NO_BREAKAGE:
113 				title = NLS.bind(MarkerMessages.VersionNumberingResolution_major1, this.newVersionValue);
114 				break;
115 			case IApiProblem.MINOR_VERSION_CHANGE_NO_NEW_API:
116 				title = NLS.bind(MarkerMessages.VersionNumberingResolution_minorNoNewAPI2, this.newVersionValue);
117 				break;
118 			case IApiProblem.REEXPORTED_MAJOR_VERSION_CHANGE:
119 				title = NLS.bind(MarkerMessages.VersionNumberingResolution_major1, this.newVersionValue);
120 				break;
121 			case IApiProblem.MICRO_VERSION_CHANGE_UNNECESSARILY:
122 				title = MarkerMessages.VersionNumberingResolution_unnecessaryMicroIncrease;
123 				break;
124 			case IApiProblem.MINOR_VERSION_CHANGE_UNNECESSARILY:
125 				title = MarkerMessages.VersionNumberingResolution_unnecessaryMinorIncrease;
126 				break;
127 			default:
128 				// IApiProblem.REEXPORTED_MINOR_VERSION_CHANGE
129 				title = NLS.bind(MarkerMessages.VersionNumberingResolution_minor1, this.newVersionValue);
130 		}
131 		Job job = new Job(title) {
132 			@Override
133 			public IStatus run(IProgressMonitor monitor) {
134 				UpdateBundleVersionOperation updateBundleVersionOperation = new UpdateBundleVersionOperation(marker, VersionNumberingResolution.this.newVersionValue);
135 				return updateBundleVersionOperation.run(monitor);
136 			}
137 		};
138 		job.setSystem(true);
139 		job.schedule();
140 	}
141 }
142