3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.jsweet.plugin.preferences;
17 |
18 | import java.lang.reflect.InvocationTargetException;
19 | import java.util.ArrayList;
20 | import java.util.HashMap;
21 | import java.util.List;
22 | import java.util.Map;
23 |
24 | import org.eclipse.core.resources.IProject;
25 | import org.eclipse.core.runtime.IAdaptable;
26 | import org.eclipse.jface.preference.FieldEditor;
27 | import org.eclipse.jface.preference.FieldEditorPreferencePage;
28 | import org.eclipse.jface.preference.IPreferenceNode;
29 | import org.eclipse.jface.preference.IPreferencePage;
30 | import org.eclipse.jface.preference.IPreferenceStore;
31 | import org.eclipse.jface.preference.PreferenceDialog;
32 | import org.eclipse.jface.preference.PreferenceManager;
33 | import org.eclipse.jface.preference.PreferenceNode;
34 | import org.eclipse.swt.SWT;
35 | import org.eclipse.swt.custom.BusyIndicator;
36 | import org.eclipse.swt.events.SelectionAdapter;
37 | import org.eclipse.swt.events.SelectionEvent;
38 | import org.eclipse.swt.layout.GridData;
39 | import org.eclipse.swt.layout.GridLayout;
40 | import org.eclipse.swt.widgets.Button;
41 | import org.eclipse.swt.widgets.Composite;
42 | import org.eclipse.swt.widgets.Control;
43 | import org.eclipse.swt.widgets.Label;
44 | import org.eclipse.swt.widgets.Link;
45 | import org.eclipse.ui.IWorkbenchPropertyPage;
46 |
47 | /**
48 | * A field editor preference page that can also be used as a property page.
49 | *
50 | * Adapted from
51 | * http://www.eclipse.org/articles/Article-Mutatis-mutandis/overlay-pages.html.
52 | *
53 | * Adapted from the TypeScript Eclipse plugin (author: dcicerone).
54 | */
55 | abstract class FieldEditorProjectPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPropertyPage {
56 |
57 | private final List fields = new ArrayList<>();
58 | protected final Map fieldMap = new HashMap<>();
59 | private Link configureWorkspaceLink;
60 | private IAdaptable element;
61 | private ProjectPreferenceStore projectPreferenceStore;
62 | private Button projectSpecificCheckbox;
63 |
64 | protected FieldEditorProjectPreferencePage(final int style) {
65 | super(style);
66 | }
67 |
68 | @Override
69 | public IAdaptable getElement() {
70 | return element;
71 | }
72 |
73 | @Override
74 | public void setElement(final IAdaptable element) {
75 | this.element = element;
76 | }
77 |
78 | protected abstract String getPreferenceNodeId();
79 |
80 | protected abstract String getSentinelPropertyName();
81 |
82 | @Override
83 | protected Control createContents(final Composite parent) {
84 | if (isPropertyPage()) { // properties page
85 | final Composite composite = new Composite(parent, SWT.NONE);
86 | final GridLayout layout = new GridLayout(2, false);
87 | layout.marginWidth = 0;
88 | layout.marginHeight = 0;
89 | composite.setLayout(layout);
90 | composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
91 | // enable project specific settings
92 | projectSpecificCheckbox = new Button(composite, SWT.CHECK);
93 | projectSpecificCheckbox.setFont(parent.getFont());
94 | projectSpecificCheckbox.setSelection(projectPreferenceStore.getProjectSpecificSettings());
95 | projectSpecificCheckbox.setText("Enable project specific settings");
96 | projectSpecificCheckbox.addSelectionListener(new SelectionAdapter() {
97 | @Override
98 | public void widgetSelected(final SelectionEvent e) {
99 | final boolean projectSpecific = isProjectSpecific();
100 | projectPreferenceStore.setProjectSpecificSettings(projectSpecific);
101 | configureWorkspaceLink.setEnabled(!projectSpecific);
102 | updateFieldEditors();
103 | }
104 | });
105 | // configure workspace settings
106 | configureWorkspaceLink = new Link(composite, SWT.NONE);
107 | configureWorkspaceLink.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
108 | configureWorkspaceLink.setText("Configure Workspace Settings...");
109 | configureWorkspaceLink.addSelectionListener(new SelectionAdapter() {
110 | @Override
111 | public void widgetSelected(final SelectionEvent e) {
112 | configureWorkspaceSettings();
113 | }
114 | });
115 | // horizontal separator
116 | final Label horizontalSeparator = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
117 | horizontalSeparator.setLayoutData(new GridData(GridData.FILL, SWT.TOP, true, false, 2, 1));
118 | }
119 | return super.createContents(parent);
120 | }
121 |
122 | @Override
123 | public void createControl(final Composite parent) {
124 | if (isPropertyPage()) {
125 | final IProject project = element.getAdapter(IProject.class);
126 | final String sentinelPropertyName = getSentinelPropertyName();
127 | projectPreferenceStore = new ProjectPreferenceStore(project, super.getPreferenceStore(),
128 | sentinelPropertyName);
129 | }
130 | super.createControl(parent);
131 | if (isPropertyPage()) {
132 | updateFieldEditors();
133 | }
134 | }
135 |
136 | @Override
137 | public IPreferenceStore getPreferenceStore() {
138 | if (projectPreferenceStore != null)
139 | return projectPreferenceStore;
140 | return super.getPreferenceStore();
141 | }
142 |
143 | @Override
144 | protected void addField(final FieldEditor editor) {
145 | fields.add(editor);
146 | fieldMap.put(editor.getPreferenceName(), editor);
147 | super.addField(editor);
148 | }
149 |
150 | protected final boolean isPageEnabled() {
151 | return !isPropertyPage() || isProjectSpecific();
152 | }
153 |
154 | protected final boolean isPropertyPage() {
155 | return element != null;
156 | }
157 |
158 | protected final boolean isProjectSpecific() {
159 | return isPropertyPage() && projectSpecificCheckbox.getSelection();
160 | }
161 |
162 | @Override
163 | protected void performDefaults() {
164 | if (isPropertyPage()) {
165 | projectSpecificCheckbox.setSelection(false);
166 | configureWorkspaceLink.setEnabled(false);
167 | updateFieldEditors();
168 | }
169 | super.performDefaults();
170 | }
171 |
172 | protected void updateFieldEditors() {
173 | final boolean pageEnabled = isPageEnabled();
174 | final Composite parent = getFieldEditorParent();
175 | for (final FieldEditor field : fields) {
176 | field.setEnabled(pageEnabled, parent);
177 | }
178 | }
179 |
180 | private void configureWorkspaceSettings() {
181 | final String preferenceNodeId = getPreferenceNodeId();
182 | final IPreferencePage preferencePage = newPreferencePage();
183 | final IPreferenceNode preferenceNode = new PreferenceNode(preferenceNodeId, preferencePage);
184 | final PreferenceManager manager = new PreferenceManager();
185 | manager.addToRoot(preferenceNode);
186 | final PreferenceDialog dialog = new PreferenceDialog(getControl().getShell(), manager);
187 | BusyIndicator.showWhile(this.getControl().getDisplay(), new Runnable() {
188 | @Override
189 | public void run() {
190 | dialog.create();
191 | dialog.setMessage(preferenceNode.getLabelText());
192 | dialog.open();
193 | }
194 | });
195 | }
196 |
197 | private IPreferencePage newPreferencePage() {
198 | try {
199 | final IPreferencePage preferencePage = this.getClass().getConstructor().newInstance();
200 | preferencePage.setTitle(getTitle());
201 | return preferencePage;
202 | } catch (IllegalAccessException e) {
203 | throw new RuntimeException(e);
204 | } catch (InstantiationException e) {
205 | throw new RuntimeException(e);
206 | } catch (IllegalArgumentException e) {
207 | throw new RuntimeException(e);
208 | } catch (InvocationTargetException e) {
209 | throw new RuntimeException(e);
210 | } catch (NoSuchMethodException e) {
211 | throw new RuntimeException(e);
212 | } catch (SecurityException e) {
213 | throw new RuntimeException(e);
214 | }
215 | }
216 |
217 | public List getFields() {
218 | return fields;
219 | }
220 |
221 | @SuppressWarnings("unchecked")
222 | public T getField(final String preferenceName) {
223 | return (T) fieldMap.get(preferenceName);
224 | }
225 |
226 | }
227 |
--------------------------------------------------------------------------------
/org.jsweet.plugin/src/org/jsweet/plugin/preferences/JSweetPreferencePage.java:
--------------------------------------------------------------------------------
1 | package org.jsweet.plugin.preferences;
2 |
3 | import static org.jsweet.plugin.preferences.Preferences.DEFAULT_PROFILE_NAME;
4 |
5 | import java.util.Map.Entry;
6 |
7 | import org.apache.commons.lang3.StringUtils;
8 | import org.eclipse.core.resources.IProject;
9 | import org.eclipse.jface.dialogs.IDialogConstants;
10 | import org.eclipse.jface.dialogs.IInputValidator;
11 | import org.eclipse.jface.dialogs.InputDialog;
12 | import org.eclipse.jface.dialogs.MessageDialog;
13 | import org.eclipse.jface.preference.BooleanFieldEditor;
14 | import org.eclipse.jface.preference.ComboFieldEditor;
15 | import org.eclipse.jface.preference.FieldEditor;
16 | import org.eclipse.jface.preference.FieldEditorPreferencePage;
17 | import org.eclipse.jface.preference.IPreferenceStore;
18 | import org.eclipse.jface.preference.StringFieldEditor;
19 | import org.eclipse.jface.util.PropertyChangeEvent;
20 | import org.eclipse.jface.window.Window;
21 | import org.eclipse.swt.SWT;
22 | import org.eclipse.swt.events.SelectionAdapter;
23 | import org.eclipse.swt.events.SelectionEvent;
24 | import org.eclipse.swt.layout.GridData;
25 | import org.eclipse.swt.layout.RowLayout;
26 | import org.eclipse.swt.widgets.Button;
27 | import org.eclipse.swt.widgets.Composite;
28 | import org.eclipse.swt.widgets.Label;
29 | import org.eclipse.ui.IWorkbench;
30 | import org.eclipse.ui.IWorkbenchPreferencePage;
31 | import org.jsweet.plugin.JSweetPlugin;
32 | import org.jsweet.plugin.builder.JSweetBuilder;
33 |
34 | public final class JSweetPreferencePage extends FieldEditorProjectPreferencePage implements IWorkbenchPreferencePage {
35 | private boolean compilerPreferencesModified;
36 | private Button createProfile;
37 | private Button deleteProfile;
38 | private String currentProfile = DEFAULT_PROFILE_NAME;
39 | ListSelectorFieldEditor profileSelector;
40 |
41 | public JSweetPreferencePage() {
42 | super(FieldEditorPreferencePage.GRID);
43 | }
44 |
45 | @Override
46 | public void init(IWorkbench workbench) {
47 | }
48 |
49 | @Override
50 | public boolean performOk() {
51 | final boolean process;
52 | // offer to rebuild the workspace if the compiler preferences were
53 | // modified
54 | if (this.compilerPreferencesModified) {
55 | String title = "JSweet";
56 | String message = "Changing configuration requires a full build. Do you want to start a build now?";
57 | String[] buttonLabels = new String[] { IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL,
58 | IDialogConstants.YES_LABEL };
59 | MessageDialog dialog = new MessageDialog(this.getShell(), title, null, message, MessageDialog.QUESTION,
60 | buttonLabels, 2);
61 | int result = dialog.open();
62 | if (result == 1) { // cancel
63 | process = false;
64 | } else {
65 | // yes/no
66 | process = super.performOk();
67 | // rebuild the workspace
68 | if (result == 2) {
69 | if (this.isPropertyPage()) {
70 | IProject project = (IProject) this.getElement().getAdapter(IProject.class);
71 | JSweetBuilder.cleanProject(project);
72 | } else {
73 | JSweetBuilder.cleanWorkspace();
74 | }
75 | }
76 | }
77 | this.compilerPreferencesModified = false;
78 | } else {
79 | process = super.performOk();
80 | }
81 | return process;
82 | }
83 |
84 | @Override
85 | public void propertyChange(PropertyChangeEvent event) {
86 | super.propertyChange(event);
87 | Object source = event.getSource();
88 | if (event.getProperty().equals(FieldEditor.VALUE)) {
89 | this.updateFieldEditors();
90 | }
91 | if (getFields().contains(source)) {
92 | this.compilerPreferencesModified = true;
93 | }
94 | }
95 |
96 | private void applyProfile(String profile) {
97 | currentProfile = profile;
98 | for (Entry entry : fieldMap.entrySet()) {
99 | if (Preferences.profiles().equals(entry.getKey())) {
100 | continue;
101 | }
102 | entry.getValue().setPreferenceName(Preferences.getProfilePrefix(currentProfile) + entry.getKey());
103 | entry.getValue().load();
104 | }
105 | }
106 |
107 | @Override
108 | protected void createFieldEditors() {
109 | if (this.isPropertyPage()) {
110 | this.addField(profileSelector = new ListSelectorFieldEditor(Preferences.profiles(), "Profile",
111 | getFieldEditorParent()));
112 | profileSelector.getCombo().addSelectionListener(new SelectionAdapter() {
113 | @Override
114 | public void widgetSelected(SelectionEvent e) {
115 | applyProfile(profileSelector.getCombo().getText());
116 | updateFieldEditors();
117 | }
118 | });
119 | Composite composite = new Composite(getFieldEditorParent(), SWT.NONE);
120 | composite.setLayoutData(new GridData(GridData.END, SWT.TOP, true, true, 2, 1));
121 | RowLayout rl = new RowLayout();
122 | rl.marginBottom = rl.marginHeight = rl.marginLeft = rl.marginRight = 0;
123 | composite.setLayout(rl);
124 | createProfile = new Button(composite, SWT.PUSH);
125 | createProfile.setFont(getFieldEditorParent().getFont());
126 | createProfile.setText("Create new profile");
127 | createProfile.addSelectionListener(new SelectionAdapter() {
128 | @Override
129 | public void widgetSelected(SelectionEvent e) {
130 | InputDialog d = new InputDialog(null, "Create new profile", "Enter a profile name", "",
131 | new IInputValidator() {
132 | @Override
133 | public String isValid(String name) {
134 | if (name.matches("[a-zA-Z0-9]")) {
135 | return name;
136 | } else {
137 | return null;
138 | }
139 | }
140 | });
141 | if (d.open() == Window.OK) {
142 | if (!StringUtils.isBlank(d.getValue())) {
143 | profileSelector.addValue(d.getValue());
144 | applyProfile(d.getValue());
145 | updateFieldEditors();
146 | }
147 | }
148 | }
149 | });
150 | deleteProfile = new Button(composite, SWT.PUSH);
151 | deleteProfile.setFont(getFieldEditorParent().getFont());
152 | deleteProfile.setText("Delete current profile");
153 | deleteProfile.addSelectionListener(new SelectionAdapter() {
154 | @Override
155 | public void widgetSelected(SelectionEvent e) {
156 | if (MessageDialog.openConfirm(null, "Delete current profile",
157 | "Do you really want to delete '" + currentProfile + "'?")) {
158 | profileSelector.removeValue(currentProfile);
159 | applyProfile(DEFAULT_PROFILE_NAME);
160 | updateFieldEditors();
161 | }
162 | }
163 | });
164 |
165 | Label horizontalSeparator = new Label(getFieldEditorParent(), SWT.SEPARATOR | SWT.HORIZONTAL);
166 | horizontalSeparator.setLayoutData(new GridData(GridData.FILL, SWT.TOP, true, false, 2, 1));
167 |
168 | }
169 |
170 | addField(new StringFieldEditor(Preferences.srcDirs(DEFAULT_PROFILE_NAME),
171 | "Source folders (project ones if empty)", getFieldEditorParent()));
172 | addField(new StringFieldEditor(Preferences.srcIncludeFilter(DEFAULT_PROFILE_NAME), //
173 | "Include filter (regexp)", getFieldEditorParent()));
174 | addField(new StringFieldEditor(Preferences.srcExcludeFilter(DEFAULT_PROFILE_NAME), //
175 | "Exclude filter (regexp)", getFieldEditorParent()));
176 | addField(new StringFieldEditor(Preferences.tsOutputDir(DEFAULT_PROFILE_NAME), //
177 | "Generated TypeScript folder", getFieldEditorParent()));
178 | addField(new BooleanFieldEditor(Preferences.noJs(DEFAULT_PROFILE_NAME), //
179 | "Do not generate JavaScript", getFieldEditorParent()));
180 | addField(new StringFieldEditor(Preferences.jsOutputDir(DEFAULT_PROFILE_NAME), //
181 | "Generated Javascript folder", getFieldEditorParent()));
182 | addField(new StringFieldEditor(Preferences.candyJsOutputDir(DEFAULT_PROFILE_NAME),
183 | "Candies-extracted JavaScript folder", getFieldEditorParent()));
184 | addField(new BooleanFieldEditor(Preferences.bundle(DEFAULT_PROFILE_NAME), //
185 | "Create browser bundle", getFieldEditorParent()));
186 | addField(new StringFieldEditor(Preferences.bundlesDir(DEFAULT_PROFILE_NAME),
187 | "Bundle folder (in-place bundle if empty)", getFieldEditorParent()));
188 | addField(new BooleanFieldEditor(Preferences.declaration(DEFAULT_PROFILE_NAME),
189 | "Generate TypeScript definitions", getFieldEditorParent()));
190 | addField(new StringFieldEditor(Preferences.declarationDir(DEFAULT_PROFILE_NAME),
191 | "Definitions folder (in-place if empty)", getFieldEditorParent()));
192 | addField(new BooleanFieldEditor(Preferences.debugMode(DEFAULT_PROFILE_NAME),
193 | "Debug mode (generate '.js.map' files)", getFieldEditorParent()));
194 | addField(new ComboFieldEditor(Preferences.moduleKind(DEFAULT_PROFILE_NAME), //
195 | "Module kind", new String[][] { //
196 | new String[] { "none", "none" }, //
197 | new String[] { "es2015", "es2015" }, //
198 | new String[] { "commonjs", "commonjs" }, //
199 | new String[] { "amd", "amd" }, //
200 | new String[] { "system", "system" }, //
201 | new String[] { "umd", "umd" } }, //
202 | getFieldEditorParent()));
203 |
204 | addField(new ComboFieldEditor(Preferences.ecmaTargetVersion(DEFAULT_PROFILE_NAME), //
205 | "Target ECMA version", new String[][] { //
206 | new String[] { "ES6", "ES6" }, //
207 | new String[] { "ES5", "ES5" }, //
208 | new String[] { "ES3", "ES3" } //
209 | }, getFieldEditorParent()));
210 | }
211 |
212 | @Override
213 | protected IPreferenceStore doGetPreferenceStore() {
214 | return JSweetPlugin.getDefault().getPreferenceStore();
215 | }
216 |
217 | @Override
218 | protected String getPreferenceNodeId() {
219 | return "org.jsweet.jsweetPreferencePage";
220 | }
221 |
222 | @Override
223 | protected String getSentinelPropertyName() {
224 | return Preferences.tsOutputDir(DEFAULT_PROFILE_NAME);
225 | }
226 |
227 | @Override
228 | protected void initialize() {
229 | super.initialize();
230 | this.updateFieldEditors();
231 | }
232 |
233 | @Override
234 | protected void performDefaults() {
235 | super.performDefaults();
236 | this.updateFieldEditors();
237 | }
238 |
239 | @Override
240 | protected void updateFieldEditors() {
241 | super.updateFieldEditors();
242 | // update editor states if needed
243 | if (isPageEnabled()) {
244 | Composite parent = getFieldEditorParent();
245 | BooleanFieldEditor bundle = this.getField(Preferences.bundle(DEFAULT_PROFILE_NAME));
246 | StringFieldEditor bundlesDirectory = this.getField(Preferences.bundlesDir(DEFAULT_PROFILE_NAME));
247 | ComboFieldEditor module = this.getField(Preferences.moduleKind(DEFAULT_PROFILE_NAME));
248 | if (bundle.getBooleanValue()) {
249 | getPreferenceStore().setValue(Preferences.moduleKind(currentProfile), "none");
250 | module.load();
251 | }
252 | module.setEnabled(!bundle.getBooleanValue(), parent);
253 | bundlesDirectory.setEnabled(bundle.getBooleanValue(), parent);
254 |
255 | BooleanFieldEditor declaration = this.getField(Preferences.declaration(DEFAULT_PROFILE_NAME));
256 | StringFieldEditor declarationDirectory = this.getField(Preferences.declarationDir(DEFAULT_PROFILE_NAME));
257 | declarationDirectory.setEnabled(declaration.getBooleanValue(), parent);
258 |
259 | }
260 |
261 | }
262 |
263 | }
264 |
--------------------------------------------------------------------------------
/org.jsweet.plugin/src/org/jsweet/plugin/preferences/ListSelectorFieldEditor.java:
--------------------------------------------------------------------------------
1 | package org.jsweet.plugin.preferences;
2 |
3 | import org.apache.commons.lang3.ArrayUtils;
4 | import org.apache.commons.lang3.StringUtils;
5 | import org.eclipse.jface.preference.FieldEditor;
6 | import org.eclipse.swt.SWT;
7 | import org.eclipse.swt.layout.GridData;
8 | import org.eclipse.swt.widgets.Combo;
9 | import org.eclipse.swt.widgets.Composite;
10 | import org.eclipse.swt.widgets.Control;
11 |
12 | /**
13 | * A field editor for a list of semicolon-separated strings, which allows for a
14 | * combo box selection.
15 | */
16 | public class ListSelectorFieldEditor extends FieldEditor {
17 |
18 | private Combo combo;
19 |
20 | private String[] values;
21 |
22 | /**
23 | * Create the field editor.
24 | *
25 | * @param name
26 | * the name of the preference this field editor works on
27 | * @param labelText
28 | * the label text of the field editor
29 | * @param parent
30 | * the parent composite
31 | */
32 | public ListSelectorFieldEditor(String name, String labelText, Composite parent) {
33 | init(name, labelText);
34 | createControl(parent);
35 | }
36 |
37 | /*
38 | * (non-Javadoc)
39 | *
40 | * @see org.eclipse.jface.preference.FieldEditor#adjustForNumColumns(int)
41 | */
42 | @Override
43 | protected void adjustForNumColumns(int numColumns) {
44 | if (numColumns > 1) {
45 | Control control = getLabelControl();
46 | int left = numColumns;
47 | if (control != null) {
48 | ((GridData) control.getLayoutData()).horizontalSpan = 1;
49 | left = left - 1;
50 | }
51 | ((GridData) combo.getLayoutData()).horizontalSpan = left;
52 | } else {
53 | Control control = getLabelControl();
54 | if (control != null) {
55 | ((GridData) control.getLayoutData()).horizontalSpan = 1;
56 | }
57 | ((GridData) combo.getLayoutData()).horizontalSpan = 1;
58 | }
59 | }
60 |
61 | /*
62 | * (non-Javadoc)
63 | *
64 | * @see org.eclipse.jface.preference.FieldEditor#doFillIntoGrid(org.eclipse.swt. widgets.Composite, int)
65 | */
66 | @Override
67 | protected void doFillIntoGrid(Composite parent, int numColumns) {
68 | int comboC = 1;
69 | if (numColumns > 1) {
70 | comboC = numColumns - 1;
71 | }
72 | Control control = getLabelControl(parent);
73 | GridData gd = new GridData();
74 | gd.horizontalSpan = 1;
75 | control.setLayoutData(gd);
76 | control = getComboBoxControl(parent);
77 | gd = new GridData();
78 | gd.horizontalSpan = comboC;
79 | gd.horizontalAlignment = GridData.FILL;
80 | control.setLayoutData(gd);
81 | control.setFont(parent.getFont());
82 | }
83 |
84 | /*
85 | * (non-Javadoc)
86 | *
87 | * @see org.eclipse.jface.preference.FieldEditor#doLoad()
88 | */
89 | @Override
90 | protected void doLoad() {
91 | updateComboForValue(getPreferenceStore().getString(getPreferenceName()));
92 | }
93 |
94 | /*
95 | * (non-Javadoc)
96 | *
97 | * @see org.eclipse.jface.preference.FieldEditor#doLoadDefault()
98 | */
99 | @Override
100 | protected void doLoadDefault() {
101 | updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName()));
102 | }
103 |
104 | /*
105 | * (non-Javadoc)
106 | *
107 | * @see org.eclipse.jface.preference.FieldEditor#doStore()
108 | */
109 | @Override
110 | protected void doStore() {
111 | if (values == null) {
112 | getPreferenceStore().setToDefault(getPreferenceName());
113 | return;
114 | }
115 | getPreferenceStore().setValue(getPreferenceName(), StringUtils.join(values, ";"));
116 | }
117 |
118 | /*
119 | * (non-Javadoc)
120 | *
121 | * @see org.eclipse.jface.preference.FieldEditor#getNumberOfControls()
122 | */
123 | @Override
124 | public int getNumberOfControls() {
125 | return 2;
126 | }
127 |
128 | /*
129 | * Lazily create and return the Combo control.
130 | */
131 | private Combo getComboBoxControl(Composite parent) {
132 | if (combo == null) {
133 | combo = new Combo(parent, SWT.READ_ONLY);
134 | combo.setFont(parent.getFont());
135 | fillComboFromValues();
136 | }
137 | return combo;
138 | }
139 |
140 | private void fillComboFromValues() {
141 | combo.removeAll();
142 | if (values != null) {
143 | for (int i = 0; i < values.length; i++) {
144 | combo.add(values[i], i);
145 | }
146 | combo.select(0);
147 | combo.setText(values[0]);
148 | }
149 | }
150 |
151 | public void addValue(String value) {
152 | String oldValue = StringUtils.join(values);
153 | values = ArrayUtils.add(values, value);
154 | combo.add(value);
155 | combo.select(values.length - 1);
156 | combo.setText(values[values.length - 1]);
157 | fireValueChanged(VALUE, oldValue, StringUtils.join(values));
158 | }
159 |
160 | public void removeValue(String value) {
161 | int index = ArrayUtils.indexOf(values, value);
162 | if (index != -1) {
163 | String oldValue = StringUtils.join(values);
164 | values = ArrayUtils.remove(values, index);
165 | combo.remove(index);
166 | combo.select(0);
167 | combo.setText(values[0]);
168 | fireValueChanged(VALUE, oldValue, StringUtils.join(values));
169 | }
170 | }
171 |
172 | /*
173 | * Set the name in the combo widget to match the specified value.
174 | */
175 | private void updateComboForValue(String value) {
176 | values = value.split(";");
177 | fillComboFromValues();
178 | }
179 |
180 | /*
181 | * (non-Javadoc)
182 | *
183 | * @see org.eclipse.jface.preference.FieldEditor#setEnabled(boolean,
184 | * org.eclipse.swt.widgets.Composite)
185 | */
186 | @Override
187 | public void setEnabled(boolean enabled, Composite parent) {
188 | super.setEnabled(enabled, parent);
189 | getComboBoxControl(parent).setEnabled(enabled);
190 | }
191 |
192 | public final Combo getCombo() {
193 | return combo;
194 | }
195 |
196 | }
197 |
--------------------------------------------------------------------------------
/org.jsweet.plugin/src/org/jsweet/plugin/preferences/PreferenceInitializer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 CINCHEO SAS
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.jsweet.plugin.preferences;
17 |
18 | import static org.jsweet.plugin.preferences.Preferences.DEFAULT_PROFILE_NAME;
19 |
20 | import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
21 | import org.eclipse.jface.preference.IPreferenceStore;
22 | import org.jsweet.plugin.JSweetPlugin;
23 |
24 | /**
25 | * Class used to initialize default preference values.
26 | */
27 | public class PreferenceInitializer extends AbstractPreferenceInitializer {
28 |
29 | @Override
30 | public void initializeDefaultPreferences() {
31 | IPreferenceStore store = JSweetPlugin.getDefault().getPreferenceStore();
32 | store.setDefault(Preferences.profiles(), "default");
33 | store.setDefault(Preferences.tsOutputDir(DEFAULT_PROFILE_NAME), ".generated");
34 | store.setDefault(Preferences.jsOutputDir(DEFAULT_PROFILE_NAME), "js");
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/org.jsweet.plugin/src/org/jsweet/plugin/preferences/Preferences.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 CINCHEO SAS
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.jsweet.plugin.preferences;
17 |
18 | import org.apache.commons.lang3.StringUtils;
19 | import org.eclipse.core.resources.IProject;
20 | import org.eclipse.jface.preference.IPreferenceStore;
21 |
22 | /**
23 | * Constant definitions for plug-in preferences
24 | */
25 | public class Preferences {
26 |
27 | public static final String DEFAULT_PROFILE_NAME = "default";
28 |
29 | private static final String COMPILER_PROFILES = "compiler.profiles";
30 |
31 | private static final String COMPILER_SOURCE_FOLDERS = "compiler.sourceFolders";
32 |
33 | private static final String COMPILER_SOURCE_INCLUDE_FILTER = "compiler.sourceIncludeFilter";
34 |
35 | private static final String COMPILER_SOURCE_EXCLUDE_FILTER = "compiler.sourceExcludeFilter";
36 |
37 | private static final String COMPILER_TYPESCRIPT_FOLDER = "compiler.typescriptFolder";
38 |
39 | private static final String COMPILER_JAVASCRIPT_FOLDER = "compiler.javascriptFolder";
40 |
41 | private static final String COMPILER_CANDY_JS_FOLDER = "compiler.candyJsFolder";
42 |
43 | private static final String COMPILER_BUNDLES_DIRECTORY = "compiler.bundlesDirectory";
44 |
45 | private static final String COMPILER_BUNDLE = "compiler.bundle";
46 |
47 | private static final String COMPILER_DECLARATION_DIRECTORY = "compiler.declarationDirectory";
48 |
49 | private static final String COMPILER_DECLARATION = "compiler.declaration";
50 |
51 | private static final String COMPILER_NO_JS = "compiler.nojs";
52 |
53 | private static final String COMPILER_DEBUG_MODE = "compiler.debugMode";
54 |
55 | private static final String COMPILER_MODULE_KIND = "compiler.moduleKind";
56 |
57 | private static final String COMPILER_ECMA_TARGET_VERSION = "compiler.target";
58 |
59 | public static String getProfilePrefix(String profile) {
60 | return StringUtils.isBlank(profile) || DEFAULT_PROFILE_NAME.equals(profile) ? "" : profile + ".";
61 | }
62 |
63 | public static String profiles() {
64 | return Preferences.COMPILER_PROFILES;
65 | }
66 |
67 | public static String getProfiles(IProject project) {
68 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
69 | return projectPreferenceStore.getString(Preferences.COMPILER_PROFILES);
70 | }
71 |
72 | public static String[] parseProfiles(IProject project) {
73 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
74 | String profiles = projectPreferenceStore.getString(Preferences.COMPILER_PROFILES);
75 | return (profiles == null ? "" : profiles).split(";");
76 | }
77 |
78 | public static String srcDirs(final String profile) {
79 | return getProfilePrefix(profile) + Preferences.COMPILER_SOURCE_FOLDERS;
80 | }
81 |
82 | public static String getSourceFolders(IProject project, String profile) {
83 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
84 | return projectPreferenceStore.getString(getProfilePrefix(profile) + Preferences.COMPILER_SOURCE_FOLDERS);
85 | }
86 |
87 | public static String srcIncludeFilter(final String profile) {
88 | return getProfilePrefix(profile) + Preferences.COMPILER_SOURCE_INCLUDE_FILTER;
89 | }
90 |
91 | public static String getSourceIncludeFilter(IProject project, String profile) {
92 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
93 | return projectPreferenceStore.getString(getProfilePrefix(profile) + Preferences.COMPILER_SOURCE_INCLUDE_FILTER);
94 | }
95 |
96 | public static String srcExcludeFilter(final String profile) {
97 | return getProfilePrefix(profile) + Preferences.COMPILER_SOURCE_EXCLUDE_FILTER;
98 | }
99 |
100 | public static String getSourceExcludeFilter(IProject project, String profile) {
101 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
102 | return projectPreferenceStore.getString(getProfilePrefix(profile) + Preferences.COMPILER_SOURCE_EXCLUDE_FILTER);
103 | }
104 |
105 | public static String tsOutputDir(final String profile) {
106 | return getProfilePrefix(profile) + Preferences.COMPILER_TYPESCRIPT_FOLDER;
107 | }
108 |
109 | public static String getTsOutputFolder(IProject project, String profile) {
110 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
111 | return projectPreferenceStore.getString(getProfilePrefix(profile) + Preferences.COMPILER_TYPESCRIPT_FOLDER);
112 | }
113 |
114 | public static String candyJsOutputDir(final String profile) {
115 | return getProfilePrefix(profile) + Preferences.COMPILER_CANDY_JS_FOLDER;
116 | }
117 |
118 | public static String getCandyJsOutputFolder(IProject project, String profile) {
119 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
120 | return projectPreferenceStore.getString(getProfilePrefix(profile) + Preferences.COMPILER_CANDY_JS_FOLDER);
121 | }
122 |
123 | public static String moduleKind(String profile) {
124 | return getProfilePrefix(profile) + Preferences.COMPILER_MODULE_KIND;
125 | }
126 |
127 | public static String getModuleKind(IProject project, String profile) {
128 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
129 | return projectPreferenceStore.getString(getProfilePrefix(profile) + Preferences.COMPILER_MODULE_KIND);
130 | }
131 |
132 | public static String ecmaTargetVersion(String profile) {
133 | return getProfilePrefix(profile) + Preferences.COMPILER_ECMA_TARGET_VERSION;
134 | }
135 |
136 | public static String getEcmaTargetVersion(IProject project, String profile) {
137 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
138 | return projectPreferenceStore.getString(getProfilePrefix(profile) + Preferences.COMPILER_ECMA_TARGET_VERSION);
139 | }
140 |
141 | public static String jsOutputDir(final String profile) {
142 | return getProfilePrefix(profile) + Preferences.COMPILER_JAVASCRIPT_FOLDER;
143 | }
144 |
145 | public static String getJsOutputFolder(IProject project, String profile) {
146 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
147 | return projectPreferenceStore.getString(getProfilePrefix(profile) + Preferences.COMPILER_JAVASCRIPT_FOLDER);
148 | }
149 |
150 | public static String bundlesDir(final String profile) {
151 | return getProfilePrefix(profile) + Preferences.COMPILER_BUNDLES_DIRECTORY;
152 | }
153 |
154 | public static String getBundlesDirectory(IProject project, String profile) {
155 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
156 | return projectPreferenceStore.getString(getProfilePrefix(profile) + Preferences.COMPILER_BUNDLES_DIRECTORY);
157 | }
158 |
159 | public static String bundle(String profile) {
160 | return getProfilePrefix(profile) + Preferences.COMPILER_BUNDLE;
161 | }
162 |
163 | public static boolean getBundle(IProject project, String profile) {
164 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
165 | return projectPreferenceStore.getBoolean(getProfilePrefix(profile) + Preferences.COMPILER_BUNDLE);
166 | }
167 |
168 | public static String declaration(String profile) {
169 | return getProfilePrefix(profile) + Preferences.COMPILER_DECLARATION;
170 | }
171 |
172 | public static boolean getDeclaration(IProject project, String profile) {
173 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
174 | return projectPreferenceStore.getBoolean(getProfilePrefix(profile) + Preferences.COMPILER_DECLARATION);
175 | }
176 |
177 | public static String declarationDir(final String profile) {
178 | return getProfilePrefix(profile) + Preferences.COMPILER_DECLARATION_DIRECTORY;
179 | }
180 |
181 | public static String getDeclarationDirectory(IProject project, String profile) {
182 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
183 | return projectPreferenceStore.getString(getProfilePrefix(profile) + Preferences.COMPILER_DECLARATION_DIRECTORY);
184 | }
185 |
186 | public static String debugMode(String profile) {
187 | return getProfilePrefix(profile) + Preferences.COMPILER_DEBUG_MODE;
188 | }
189 |
190 | public static boolean isDebugMode(IProject project, String profile) {
191 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
192 | return projectPreferenceStore.getBoolean(getProfilePrefix(profile) + Preferences.COMPILER_DEBUG_MODE);
193 | }
194 |
195 | public static String noJs(String profile) {
196 | return getProfilePrefix(profile) + Preferences.COMPILER_NO_JS;
197 | }
198 |
199 | public static boolean getNoJs(IProject project, String profile) {
200 | IPreferenceStore projectPreferenceStore = new ProjectPreferenceStore(project);
201 | return projectPreferenceStore.getBoolean(getProfilePrefix(profile) + Preferences.COMPILER_NO_JS);
202 | }
203 |
204 | }
205 |
--------------------------------------------------------------------------------
/org.jsweet.plugin/src/org/jsweet/plugin/preferences/ProjectPreferenceStore.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 CINCHEO SAS
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.jsweet.plugin.preferences;
17 |
18 | import java.io.IOException;
19 | import java.io.OutputStream;
20 |
21 | import org.eclipse.core.resources.IProject;
22 | import org.eclipse.core.resources.ProjectScope;
23 | import org.eclipse.core.runtime.preferences.IEclipsePreferences;
24 | import org.eclipse.core.runtime.preferences.IScopeContext;
25 | import org.eclipse.jface.preference.IPreferenceStore;
26 | import org.eclipse.jface.preference.PreferenceStore;
27 | import org.jsweet.plugin.JSweetPlugin;
28 | import org.osgi.service.prefs.BackingStoreException;
29 |
30 | /**
31 | * An adapter for {@link PreferenceStore} to allow a preference page to be used
32 | * as a property page.
33 | *
34 | * Adapted from
35 | * http://www.eclipse.org/articles/Article-Mutatis-mutandis/overlay-pages.html.
36 | *
37 | * Adapted from the Typescript Eclipse plugin (author: dcicerone).
38 | */
39 | public final class ProjectPreferenceStore extends PreferenceStore {
40 | private final IProject project;
41 | private final IPreferenceStore preferenceStore;
42 | private boolean projectSpecificSettings;
43 | private transient boolean inserting;
44 |
45 | public ProjectPreferenceStore(IProject project) {
46 | this(project, JSweetPlugin.getDefault().getPreferenceStore(), "");
47 | }
48 |
49 | public ProjectPreferenceStore(IProject project, IPreferenceStore preferenceStore, String sentinelPropertyName) {
50 | assert project != null;
51 | assert preferenceStore != null;
52 | assert sentinelPropertyName != null;
53 | this.project = project;
54 | this.preferenceStore = preferenceStore;
55 | IEclipsePreferences projectPreferences = this.getProjectPreferences();
56 | this.projectSpecificSettings = (projectPreferences.get(sentinelPropertyName, null) != null);
57 | }
58 |
59 | @Override
60 | public boolean contains(String name) {
61 | return this.preferenceStore.contains(name);
62 | }
63 |
64 | @Override
65 | public boolean getDefaultBoolean(String name) {
66 | return this.preferenceStore.getDefaultBoolean(name);
67 | }
68 |
69 | @Override
70 | public double getDefaultDouble(String name) {
71 | return this.preferenceStore.getDefaultDouble(name);
72 | }
73 |
74 | @Override
75 | public float getDefaultFloat(String name) {
76 | return this.preferenceStore.getDefaultFloat(name);
77 | }
78 |
79 | @Override
80 | public int getDefaultInt(String name) {
81 | return this.preferenceStore.getDefaultInt(name);
82 | }
83 |
84 | @Override
85 | public long getDefaultLong(String name) {
86 | return this.preferenceStore.getDefaultLong(name);
87 | }
88 |
89 | @Override
90 | public String getDefaultString(String name) {
91 | return this.preferenceStore.getDefaultString(name);
92 | }
93 |
94 | @Override
95 | public boolean getBoolean(String name) {
96 | this.insertValue(name);
97 | return super.getBoolean(name);
98 | }
99 |
100 | @Override
101 | public double getDouble(String name) {
102 | this.insertValue(name);
103 | return super.getDouble(name);
104 | }
105 |
106 | @Override
107 | public float getFloat(String name) {
108 | this.insertValue(name);
109 | return super.getFloat(name);
110 | }
111 |
112 | @Override
113 | public int getInt(String name) {
114 | this.insertValue(name);
115 | return super.getInt(name);
116 | }
117 |
118 | @Override
119 | public long getLong(String name) {
120 | this.insertValue(name);
121 | return super.getLong(name);
122 | }
123 |
124 | @Override
125 | public String getString(String name) {
126 | this.insertValue(name);
127 | return super.getString(name);
128 | }
129 |
130 | @Override
131 | public boolean isDefault(String name) {
132 | String defaultValue = this.getDefaultString(name);
133 | if (defaultValue == null) {
134 | return false;
135 | }
136 | return defaultValue.equals(this.getString(name));
137 | }
138 |
139 | public boolean getProjectSpecificSettings() {
140 | return this.projectSpecificSettings;
141 | }
142 |
143 | public void setProjectSpecificSettings(boolean projectSpecificSettings) {
144 | this.projectSpecificSettings = projectSpecificSettings;
145 | }
146 |
147 | @Override
148 | public void save() throws IOException {
149 | this.writeProperties();
150 | }
151 |
152 | @Override
153 | public void save(OutputStream out, String header) throws IOException {
154 | this.writeProperties();
155 | }
156 |
157 | @Override
158 | public void setToDefault(String name) {
159 | String defaultValue = this.getDefaultString(name);
160 | this.setValue(name, defaultValue);
161 | }
162 |
163 | private synchronized void insertValue(String name) {
164 | // check if an insertion is already in-progress
165 | if (this.inserting) {
166 | return;
167 | }
168 | if (this.projectSpecificSettings && super.contains(name)) {
169 | return;
170 | }
171 | this.inserting = true;
172 | try {
173 | IEclipsePreferences projectPreferences = this.getProjectPreferences();
174 | String value = projectPreferences.get(name, null);
175 | if (value == null) {
176 | value = this.preferenceStore.getString(name);
177 | }
178 | if (value != null) {
179 | this.setValue(name, value);
180 | }
181 | } finally {
182 | this.inserting = false;
183 | }
184 | }
185 |
186 | private void writeProperties() throws IOException {
187 | IEclipsePreferences projectPreferences = this.getProjectPreferences();
188 | for (String name : super.preferenceNames()) {
189 | if (this.projectSpecificSettings) {
190 | String value = this.getString(name);
191 | projectPreferences.put(name, value);
192 | } else {
193 | projectPreferences.remove(name);
194 | }
195 | }
196 | try {
197 | projectPreferences.flush();
198 | } catch (BackingStoreException e) {
199 | throw new IOException(e);
200 | }
201 | }
202 |
203 | private IEclipsePreferences getProjectPreferences() {
204 | IScopeContext projectScope = new ProjectScope(this.project);
205 | return projectScope.getNode(JSweetPlugin.ID);
206 | }
207 | }
--------------------------------------------------------------------------------
/org.jsweet.update/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | org.jsweet.update
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.pde.UpdateSiteBuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.pde.UpdateSiteNature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/org.jsweet.update/site.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | JSweet transpiler update site
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 | The JSweet transpiler Eclipse plugin
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------