/
SREInstallWizard.java
153 lines (137 loc) · 4.66 KB
/
SREInstallWizard.java
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
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2019 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sarl.eclipse.wizards.sreinstall;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Strings;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.wizard.Wizard;
import io.sarl.eclipse.SARLEclipseConfig;
import io.sarl.eclipse.SARLEclipsePlugin;
import io.sarl.eclipse.runtime.ISREInstall;
import io.sarl.eclipse.runtime.ManifestBasedSREInstall;
import io.sarl.eclipse.runtime.SREException;
/**
* Wiazrd for SRE installation.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
public abstract class SREInstallWizard extends Wizard {
private ISREInstall sre;
private String[] names;
/**
* Constructs a new wizard to add/edit a SRE.
*
* @param editSRE the SRE being edited, or <code>null</code> if none.
* @param currentInstalls current SRE installs used to validate name changes.
*/
public SREInstallWizard(ISREInstall editSRE, ISREInstall[] currentInstalls) {
this.sre = editSRE;
final List<String> names = new ArrayList<>(currentInstalls.length);
for (int i = 0; i < currentInstalls.length; i++) {
final ISREInstall install = currentInstalls[i];
if (this.sre == null || !install.getId().equals(this.sre.getId())) {
final String name = install.getNameNoDefault();
if (!Strings.isNullOrEmpty(name) && !name.equals(install.getId())) {
names.add(name);
}
}
}
this.names = names.toArray(new String[names.size()]);
}
/**
* Returns the original SRE to edit, or <code>null</code> if creating a SRE.
*
* @return SRE to edit or <code>null</code>
*/
protected ISREInstall getOriginalSRE() {
return this.sre;
}
@Override
public boolean performFinish() {
return true;
}
/**
* Returns a page to use for editing a SRE install type.
*
* @param sre the edited SRE.
* @return the wizard page.
*/
public AbstractSREInstallPage getPage(ISREInstall sre) {
final IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(
SARLEclipsePlugin.PLUGIN_ID,
SARLEclipseConfig.EXTENSION_POINT_SRE_INSTALL_PAGES);
if (sre != null && extensionPoint != null) {
IConfigurationElement firstTypeMatching = null;
for (final IConfigurationElement info : extensionPoint.getConfigurationElements()) {
final String id = info.getAttribute("sreInstallId"); //$NON-NLS-1$
if (sre.getId().equals(Strings.nullToEmpty(id))) {
try {
final AbstractSREInstallPage page = (AbstractSREInstallPage)
info.createExecutableExtension("class"); //$NON-NLS-1$
page.setExistingNames(this.names);
return page;
} catch (CoreException e) {
SARLEclipsePlugin.getDefault().log(e);
}
} else if (firstTypeMatching == null
&& isInstance(info.getAttribute("sreInstallType"), sre)) { //$NON-NLS-1$
firstTypeMatching = info;
}
}
if (firstTypeMatching != null) {
try {
final AbstractSREInstallPage page = (AbstractSREInstallPage)
firstTypeMatching.createExecutableExtension("class"); //$NON-NLS-1$
page.setExistingNames(this.names);
return page;
} catch (CoreException e) {
SARLEclipsePlugin.getDefault().log(e);
}
}
}
if (sre == null || sre instanceof ManifestBasedSREInstall) {
final StandardSREPage standardVMPage = new StandardSREPage();
standardVMPage.setExistingNames(this.names);
return standardVMPage;
}
throw new SREException(MessageFormat.format(
Messages.SREInstallWizard_5, sre.getName()));
}
private static boolean isInstance(String classname, ISREInstall sre) {
if (!Strings.isNullOrEmpty(classname)) {
try {
final Class<?> type = Class.forName(classname);
return type.isInstance(sre);
} catch (Throwable e) {
SARLEclipsePlugin.getDefault().log(e);
}
}
return false;
}
}