diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/.classpath b/OPTIMUS/eu.DECIDEh2020.optimus/.classpath new file mode 100644 index 0000000000000000000000000000000000000000..6a6b18835c2f4a065d562154d1b84be507e4253b --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/.classpath @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry exported="true" kind="lib" path="bin/"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="lib" path="D:/W/2017-Decide/workspace/json-simple-1.1.1.jar"> + <attributes> + <attribute name="org.eclipse.jst.component.dependency" value="../"/> + </attributes> + </classpathentry> + <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/.gitignore b/OPTIMUS/eu.DECIDEh2020.optimus/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..30263e535e10e346b59764f9680191bdfb9a8128 --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/.gitignore @@ -0,0 +1,2 @@ +bin/ +.settings/ \ No newline at end of file diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/.project b/OPTIMUS/eu.DECIDEh2020.optimus/.project new file mode 100644 index 0000000000000000000000000000000000000000..e63a7f50a2ccd818b12c9a02b2399627079992df --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/.project @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>eu.DECIDEh2020.optimus</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.wst.common.project.facet.core.builder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.wst.validation.validationbuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jem.workbench.JavaEMFNature</nature> + <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + <nature>org.eclipse.wst.common.project.facet.core.nature</nature> + </natures> +</projectDescription> diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/META-INF/MANIFEST.MF b/OPTIMUS/eu.DECIDEh2020.optimus/META-INF/MANIFEST.MF new file mode 100644 index 0000000000000000000000000000000000000000..e74e95b3f6f0b93577db14d59d7a9995bd25806a --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/META-INF/MANIFEST.MF @@ -0,0 +1,15 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Optimus +Bundle-SymbolicName: eu.DECIDEh2020.optimus;singleton:=true +Bundle-Version: 1.0.0.qualifier +Require-Bundle: org.eclipse.jface.text, + org.eclipse.core.resources, + org.eclipse.ui, + org.eclipse.ui.editors, + org.eclipse.ui.ide, + org.eclipse.core.runtime, + com.googlecode.json-simple;bundle-version="1.1.1", + org.eclipse.json;bundle-version="1.0.100" +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Bundle-ClassPath: bin/ diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/build.properties b/OPTIMUS/eu.DECIDEh2020.optimus/build.properties new file mode 100644 index 0000000000000000000000000000000000000000..daa203e5d18b51bc042eb4091c7c86c887911ae1 --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/build.properties @@ -0,0 +1,5 @@ +output.. = bin/ +bin.includes = plugin.xml,\ + META-INF/,\ + icons/,\ + bin/ diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/icons/decide.png b/OPTIMUS/eu.DECIDEh2020.optimus/icons/decide.png new file mode 100644 index 0000000000000000000000000000000000000000..def2a25b6b34ea7a6c0aff2be1fbbf42a20c6e28 Binary files /dev/null and b/OPTIMUS/eu.DECIDEh2020.optimus/icons/decide.png differ diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/icons/logoDecide_300ppp.jpg b/OPTIMUS/eu.DECIDEh2020.optimus/icons/logoDecide_300ppp.jpg new file mode 100644 index 0000000000000000000000000000000000000000..9b4c3f9bdc961c0c3f2f0032856e7a241c06d442 Binary files /dev/null and b/OPTIMUS/eu.DECIDEh2020.optimus/icons/logoDecide_300ppp.jpg differ diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/icons/sample@2x.png b/OPTIMUS/eu.DECIDEh2020.optimus/icons/sample@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..1705001c0e3a0bd3ab784a11fe5c44322d5adb6e Binary files /dev/null and b/OPTIMUS/eu.DECIDEh2020.optimus/icons/sample@2x.png differ diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/plugin.xml b/OPTIMUS/eu.DECIDEh2020.optimus/plugin.xml new file mode 100644 index 0000000000000000000000000000000000000000..c122ac602ea6d92d448f35c79e8ee0a5570d118e --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/plugin.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<?eclipse version="3.4"?> +<plugin> + + <extension + point="org.eclipse.ui.editors"> + <editor + name="Optimus Editor" + extensions="json" + filenames="*.json" + icon="icons/decide.png" + contributorClass="eu.DECIDEh2020.optimus.editors.OptimusContributor" + class="eu.DECIDEh2020.optimus.editors.optimus" + id="eu.DECIDEh2020.optimus.editors.optimus"> + </editor> + </extension> + <extension + point="org.eclipse.ui.newWizards"> + <category + name="Optimus Wizards" + id="eu.DECIDEh2020.optimus"> + </category> + <wizard + name="Optimus Editor file" + icon="icons/decide.png" + category="eu.DECIDEh2020.optimus" + class="eu.DECIDEh2020.optimus.wizards.OptimusWizard" + id="eu.DECIDEh2020.optimus.wizards.OptimusWizard"> + </wizard> + </extension> + +</plugin> diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/src/META-INF/MANIFEST.MF b/OPTIMUS/eu.DECIDEh2020.optimus/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000000000000000000000000000000000000..254272e1c0740423e25eb40be9a98ce1556474b1 --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/Classification.java b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/Classification.java new file mode 100644 index 0000000000000000000000000000000000000000..2fcafca73b6116043b4bfe6c4f7433d3551b81e9 --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/Classification.java @@ -0,0 +1,193 @@ + +/********************************************************************* +* Copyright (c) 2017 Tecnalia. +* +* This program and the accompanying materials are made +* available under the terms of the Eclipse Public License 2.0 +* which is available at https://www.eclipse.org/legal/epl-2.0/ +* +* SPDX-License-Identifier: EPL-2.0 +* Contributors (in alphabetical order): +* Gorka Benguria Tecnalia +* I�aki Etxaniz Tecnalia +* Juncal Alonso Tecnalia +* Leire Orue-Echevarria Tecnalia +* Maria Jose Lopez Tecnalia +* Marisa Escalante Tecnalia +* Initially developed in the context of DECIDE EU project www.DECIDE-h2020.eu +**********************************************************************/ +package eu.DECIDEh2020.optimus.editors; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.ScrolledComposite; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.events.ControlAdapter; +import org.eclipse.swt.events.ControlEvent; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.FocusListener; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; +import org.json.simple.JSONArray; +//import org.json.simple.JSONException; +import org.json.simple.JSONObject; + +import java.awt.Rectangle; +//import org.json.parser.JSONParser; +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; + +public class Classification extends Composite { + + /** + * Create the composite. + * @param parent + * @param style + */ + + /** The text widget used in page 2. */ + private StyledText text; + /** The font chosen in page 1. */ + private Font font; + private Text text_1; + private Composite text_2; + public int microservicesNumber = 0; + + public int y = 5; + public String newcontent; + + + + public Classification(Composite parent, int style) { + super(parent, style); + + + Label lblAppName = new Label(this, SWT.NONE); + lblAppName.setBounds(10, 10, 55, 15); + lblAppName.setText("App name"); + + text_1 = new Text(this, SWT.BORDER); + text_1.setBounds(74, 10, 76, 21); + + text_1.addFocusListener(new FocusListener() { + + @Override + public void focusGained(FocusEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void focusLost(FocusEvent arg0) { + newcontent = text_1.getText(); + optimus.appjson.put("id", (int) (Math.random()*1000)); + optimus.appjson.put("name", newcontent); + + }}); + + + + + + Button btnAddMicroservice = new Button(this, SWT.NONE); + btnAddMicroservice.setBounds(181, 10, 130, 25); + btnAddMicroservice.setText("Add microservice"); + + ScrolledComposite scrolledComposite = new ScrolledComposite(this, SWT.BORDER | SWT.V_SCROLL); + scrolledComposite.setExpandHorizontal(true); + scrolledComposite.setTouchEnabled(true); + scrolledComposite.setShowFocusedControl(true); + scrolledComposite.setAlwaysShowScrollBars(true); + scrolledComposite.setBounds(10, 48, 753, 596); + + scrolledComposite.setMinHeight(y); + + final StyledText styledText = new StyledText(scrolledComposite, SWT.BORDER | SWT.WRAP); + scrolledComposite.setContent(styledText); + scrolledComposite.setMinSize(styledText.computeSize(SWT.DEFAULT, SWT.DEFAULT)); + + Button btnSave = new Button(this, SWT.NONE); + btnSave.setBounds(688, 660, 75, 25); + btnSave.setText("Save"); + + btnSave.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + + optimus.appjson.put("microservices", optimus.appmicros); + final String FILENAME = optimus.workingDir; + + BufferedWriter bw = null; + FileWriter fw = null; + try { + String content = optimus.appjson.toString(); + fw = new FileWriter(FILENAME); + bw = new BufferedWriter(fw); + bw.write(content); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (bw != null) + bw.close(); + if (fw != null) + fw.close(); + + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } + }); + + + + + + btnAddMicroservice.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + microservicesNumber = microservicesNumber + 1; + //scrolledComposite.setMinHeight(y); + text_2 = new Microservice(scrolledComposite, SWT.BORDER, y); + text_2.setBounds(10, y, 500, 100); + y = (120* microservicesNumber); + + } + }); + + } + public void Create_microservices_from_file() { + if (!(optimus.appjson.isEmpty())) + { + text_1.setText(optimus.appjson.get("name").toString()); + ScrolledComposite scrolledComposite = new ScrolledComposite(this, SWT.BORDER | SWT.V_SCROLL); + scrolledComposite.setTouchEnabled(true); + scrolledComposite.setShowFocusedControl(true); + scrolledComposite.setAlwaysShowScrollBars(true); + scrolledComposite.setExpandVertical(true); + scrolledComposite.setBounds(10, 48, 753, 427); + + optimus.appjson.get("name").toString(); + optimus.appmicros.remove("microservices"); + optimus.appmicros.add(optimus.appjson.get("microservices")); + int numbermicr=optimus.appmicros.size(); + for (int i = 0; i <numbermicr; i++) + + { + microservicesNumber = microservicesNumber + 1; + text_2 = new Microservice(scrolledComposite, SWT.BORDER, y); + text_2.setBounds(10, y, 500, 100); + y = (120* microservicesNumber); + // } + } + } + } + @Override + protected void checkSubclass() { + // Disable the check that prevents subclassing of SWT components + } +} diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/Microservice.java b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/Microservice.java new file mode 100644 index 0000000000000000000000000000000000000000..4e7ada5e680bd1e929d02ab4e0ca1a6bd7682f8b --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/Microservice.java @@ -0,0 +1,223 @@ + +/********************************************************************* +* Copyright (c) 2017 Tecnalia. +* +* This program and the accompanying materials are made +* available under the terms of the Eclipse Public License 2.0 +* which is available at https://www.eclipse.org/legal/epl-2.0/ +* +* SPDX-License-Identifier: EPL-2.0 +* Contributors (in alphabetical order): +* Gorka Benguria Tecnalia +* I�aki Etxaniz Tecnalia +* Juncal Alonso Tecnalia +* Leire Orue-Echevarria Tecnalia +* Maria Jose Lopez Tecnalia +* Marisa Escalante Tecnalia +* Initially developed in the context of DECIDE EU project www.DECIDE-h2020.eu +**********************************************************************/ +package eu.DECIDEh2020.optimus.editors; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.FocusListener; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.FontDialog; +import org.eclipse.swt.custom.ScrolledComposite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; +import org.json.simple.JSONArray; + +import org.json.simple.JSONObject; +import org.eclipse.swt.widgets.Combo; + + +public class Microservice extends Composite { + + /** + * Create the composite. + * @param parent + * @param style + */ + /** The text widget used in page 2. */ + private StyledText text; + /** The font chosen in page 1. */ + private Font font; + private Text text_1; + private Text text_2; + private static JSONObject micros; + private JSONArray DetachR; + private Text Detach_text; + private JSONObject DRes; + + + + public Microservice(Composite parent, int style, int y) { + super(parent, style); + + Composite compositeMicro = new Composite(this, SWT.NONE); + compositeMicro.setBounds(1, 0, 437, 83); + DetachR = new JSONArray(); + + Label lblMicroserviceName = new Label(compositeMicro, SWT.NONE); + lblMicroserviceName.setBounds(10, 13, 50, 30); + lblMicroserviceName.setText("Name"); + text_2 = new Text(compositeMicro, SWT.BORDER); + text_2.setBounds(69, 13, 200, 21); + + Label microserviceID = new Label(compositeMicro, SWT.NONE); + microserviceID.setText("id"); + microserviceID.setBounds(275, 13, 50, 15); + microserviceID.setVisible(false); + + + + Combo ms_type = new Combo(compositeMicro, SWT.NONE); + ms_type.setItems(new String[] {"Computing", "Computing Public IP"}); + ms_type.setBounds(331, 10, 91, 23); + ms_type.setText("Computing"); + + Combo Detach_type = new Combo(compositeMicro, SWT.NONE); + Detach_type.setItems(new String[] {"Storage. DB", "Storage", "Queue System"}); + Detach_type.setBounds(322, 50, 91, 23); + Detach_type.setText("Storage"); + + Button Det_DB = new Button(compositeMicro, SWT.RADIO); + Det_DB.setSelection(false); + Det_DB.setBounds(277, 53, 90, 16); + Det_DB.setText("DB"); + Det_DB.addFocusListener(new FocusListener(){ + + + @Override + public void focusGained(FocusEvent arg0) { + // TODO Auto-generated method stub + + } + @Override + public void focusLost(FocusEvent arg0) { + if (Det_DB.getSelection()) { + Detach_type.setText("Storage. DB"); + } + if (!Det_DB.getSelection()) {Detach_type.setText("Storage");} + + if (DRes.get(Detach_type)!=Detach_type.getText()) + { + + DetachR.remove(DRes); + micros.remove(DetachR); + optimus.appmicros.remove(micros); + DRes.put("Det_type",Detach_type.getText()); + DetachR.add(DRes); + optimus.appmicros.add(micros); + } + + + + + } + } + ); + + Label lblDetachableResource = new Label(compositeMicro, SWT.NONE); + lblDetachableResource.setBounds(11, 53, 122, 15); + lblDetachableResource.setText("Detachable Resource"); + Detach_text = new Text(compositeMicro, SWT.BORDER); + Detach_text.setBounds(143, 47, 117, 21); + + Detach_text.addFocusListener(new FocusListener(){ + + + @Override + public void focusGained(FocusEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void focusLost(FocusEvent arg0) { + DRes = new JSONObject(); + for (int i=0; i < optimus.appmicros.size(); i++) + { + JSONObject itemArr = (JSONObject)optimus.appmicros.get(i); + if (itemArr.get("id").equals(microserviceID.getText())) + { + break; + } + } + + DRes.put("DetachableResource",Detach_text.getText()); + DRes.put("Det_type",Detach_type.getText()); + + DetachR.add(DRes); + micros.put("Detachable_resources",DetachR); + } + }); + + text_2.addFocusListener(new FocusListener() { + + + @Override + public void focusGained(FocusEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void focusLost(FocusEvent arg0) { + int id = (int) (Math.random()*1000); + String idS = String.valueOf(id); + micros = new JSONObject(); + + for (int i=0; i < optimus.appmicros.size(); i++) + { + JSONObject itemArr = (JSONObject)optimus.appmicros.get(i); + if (itemArr.get("id").equals(microserviceID.getText())) + { + optimus.appmicros.remove(itemArr); + idS = microserviceID.getText(); + break; + + } + } + + + micros.put("id",idS); + micros.put("microname", text_2.getText()); + micros.put("ms_type", ms_type.getText()); + optimus.appmicros.add(micros); + microserviceID.setText(idS); + microserviceID.setVisible(true); + } + + private boolean existmicroservice(String text) { + JSONObject microstemp = new JSONObject(); + microstemp.put("id",text); + microstemp.put("microname", text_2.getText()); + if (optimus.appmicros.contains(microstemp)) { + return true; + + } else { + return false; + } + }}); + } + //}); + + //} + + @Override + protected void checkSubclass() { + // Disable the check that prevents subclassing of SWT components + } +} diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/OptimusContributor.java b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/OptimusContributor.java new file mode 100644 index 0000000000000000000000000000000000000000..4638ee53d3411f43e3ccbda48b8db2f41280d968 --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/OptimusContributor.java @@ -0,0 +1,121 @@ + +/********************************************************************* +* Copyright (c) 2017 Tecnalia. +* +* This program and the accompanying materials are made +* available under the terms of the Eclipse Public License 2.0 +* which is available at https://www.eclipse.org/legal/epl-2.0/ +* +* SPDX-License-Identifier: EPL-2.0 +* Contributors (in alphabetical order): +* Gorka Benguria Tecnalia +* I�aki Etxaniz Tecnalia +* Juncal Alonso Tecnalia +* Leire Orue-Echevarria Tecnalia +* Maria Jose Lopez Tecnalia +* Marisa Escalante Tecnalia +* Initially developed in the context of DECIDE EU project www.DECIDE-h2020.eu +**********************************************************************/ +package eu.DECIDEh2020.optimus.editors; + +import org.eclipse.jface.action.*; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.ui.IActionBars; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchActionConstants; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.actions.ActionFactory; +import org.eclipse.ui.ide.IDE; +import org.eclipse.ui.ide.IDEActionFactory; +import org.eclipse.ui.part.MultiPageEditorActionBarContributor; +import org.eclipse.ui.texteditor.ITextEditor; +import org.eclipse.ui.texteditor.ITextEditorActionConstants; + +/** + * Manages the installation/deinstallation of global actions for multi-page editors. + * Responsible for the redirection of global actions to the active editor. + * Multi-page contributor replaces the contributors for the individual editors in the multi-page editor. + */ +public class OptimusContributor extends MultiPageEditorActionBarContributor { + private IEditorPart activeEditorPart; + private Action sampleAction; + /** + * Creates a multi-page contributor. + */ + public OptimusContributor() { + super(); + createActions(); + } + /** + * Returns the action registed with the given text editor. + * @return IAction or null if editor is null. + */ + protected IAction getAction(ITextEditor editor, String actionID) { + return (editor == null ? null : editor.getAction(actionID)); + } + /* (non-JavaDoc) + * Method declared in AbstractMultiPageEditorActionBarContributor. + */ + + public void setActivePage(IEditorPart part) { + if (activeEditorPart == part) + return; + + activeEditorPart = part; + + IActionBars actionBars = getActionBars(); + if (actionBars != null) { + + ITextEditor editor = (part instanceof ITextEditor) ? (ITextEditor) part : null; + + actionBars.setGlobalActionHandler( + ActionFactory.DELETE.getId(), + getAction(editor, ITextEditorActionConstants.DELETE)); + actionBars.setGlobalActionHandler( + ActionFactory.UNDO.getId(), + getAction(editor, ITextEditorActionConstants.UNDO)); + actionBars.setGlobalActionHandler( + ActionFactory.REDO.getId(), + getAction(editor, ITextEditorActionConstants.REDO)); + actionBars.setGlobalActionHandler( + ActionFactory.CUT.getId(), + getAction(editor, ITextEditorActionConstants.CUT)); + actionBars.setGlobalActionHandler( + ActionFactory.COPY.getId(), + getAction(editor, ITextEditorActionConstants.COPY)); + actionBars.setGlobalActionHandler( + ActionFactory.PASTE.getId(), + getAction(editor, ITextEditorActionConstants.PASTE)); + actionBars.setGlobalActionHandler( + ActionFactory.SELECT_ALL.getId(), + getAction(editor, ITextEditorActionConstants.SELECT_ALL)); + actionBars.setGlobalActionHandler( + ActionFactory.FIND.getId(), + getAction(editor, ITextEditorActionConstants.FIND)); + actionBars.setGlobalActionHandler( + IDEActionFactory.BOOKMARK.getId(), + getAction(editor, IDEActionFactory.BOOKMARK.getId())); + actionBars.updateActionBars(); + } + } + private void createActions() { + sampleAction = new Action() { + public void run() { + MessageDialog.openInformation(null, "Optimus", "Sample Action Executed"); + } + }; + sampleAction.setText("Sample Action"); + sampleAction.setToolTipText("Sample Action tool tip"); + sampleAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). + getImageDescriptor(IDE.SharedImages.IMG_OBJS_TASK_TSK)); + } + public void contributeToMenu(IMenuManager manager) { + IMenuManager menu = new MenuManager("Editor &Menu"); + manager.prependToGroup(IWorkbenchActionConstants.MB_ADDITIONS, menu); + menu.add(sampleAction); + } + public void contributeToToolBar(IToolBarManager manager) { + manager.add(new Separator()); + manager.add(sampleAction); + } +} diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/Simulation.java b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/Simulation.java new file mode 100644 index 0000000000000000000000000000000000000000..0cdb9388419de9803888cef935e19b9382e74d09 --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/Simulation.java @@ -0,0 +1,139 @@ + +/********************************************************************* +* Copyright (c) 2017 Tecnalia. +* +* This program and the accompanying materials are made +* available under the terms of the Eclipse Public License 2.0 +* which is available at https://www.eclipse.org/legal/epl-2.0/ +* +* SPDX-License-Identifier: EPL-2.0 +* Contributors (in alphabetical order): +* Gorka Benguria Tecnalia +* I�aki Etxaniz Tecnalia +* Juncal Alonso Tecnalia +* Leire Orue-Echevarria Tecnalia +* Maria Jose Lopez Tecnalia +* Marisa Escalante Tecnalia +* Initially developed in the context of DECIDE EU project www.DECIDE-h2020.eu +**********************************************************************/ +package eu.DECIDEh2020.optimus.editors; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.DisposeEvent; +import org.eclipse.swt.events.DisposeListener; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.widgets.Composite; +//import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Button; +//import org.eclipse.swt.widgets.Table; +import org.json.simple.JSONArray; +//import org.json.simple.JSONObject; +import org.eclipse.swt.widgets.ProgressBar; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.widgets.Group; +import org.eclipse.swt.custom.ScrolledComposite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.wb.swt.SWTResourceManager; + +public class Simulation extends Composite { + + + + /** + * Create the composite. + * @param parent + * @param style + */ + + private JSONArray NFRs; + public Simulation(Composite parent, int style) { + super(parent, SWT.NONE); + + ScrolledComposite scrolledComposite = new ScrolledComposite(this, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); + scrolledComposite.setAlwaysShowScrollBars(true); + scrolledComposite.setBounds(10, 43, 813, 453); + scrolledComposite.setExpandHorizontal(true); + scrolledComposite.setExpandVertical(true); + + Group group = new Group(scrolledComposite, SWT.NONE); + + Composite composite = new Composite(group, SWT.NONE); + composite.setBounds(10, 10, 135, 121); + + Label lblNfrEditor = new Label(composite, SWT.NONE); + lblNfrEditor.setBounds(10, 10, 55, 15); + lblNfrEditor.setText("NFR Editor"); + NFRs = new JSONArray(); + + + Button btnCost_1 = new Button(composite, SWT.CHECK); + btnCost_1.setBounds(30, 31, 93, 16); + btnCost_1.setText("Cost"); + + + Button btnAvailability_1 = new Button(composite, SWT.CHECK); + btnAvailability_1.setBounds(30, 53, 93, 16); + btnAvailability_1.setText("Availability"); + + + Button btnLocation_1 = new Button(composite, SWT.CHECK); + btnLocation_1.setBounds(30, 75, 93, 16); + btnLocation_1.setText("Location"); + + + Button btnSimulate = new Button(group, SWT.NONE); + btnSimulate.setBounds(185, 62, 75, 25); + btnSimulate.setText("Simulate"); + + Composite composite_1 = new Composite(group, SWT.NONE); + composite_1.setBounds(10, 137, 723, 259); + + Label lblNewLabel = new Label(composite_1, SWT.NONE); + lblNewLabel.setFont(SWTResourceManager.getFont("Calibri", 14, SWT.BOLD)); + lblNewLabel.setBounds(10, 10, 339, 21); + lblNewLabel.setText("ApplicationDescription JSON file:"); + StyledText simulationText = new StyledText(composite_1, SWT.BORDER); + simulationText.setBounds(10, 62, 671, 100); + simulationText.setAlwaysShowScrollBars(true); + simulationText.setJustify(true); + simulationText.setLineSpacing(3); + simulationText.setText(""); + + btnSimulate.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + NFRs.removeAll(NFRs); + if (btnCost_1.getSelection()) { + NFRs.add(btnCost_1.getText()); + } + if (btnAvailability_1.getSelection()) { + NFRs.add(btnAvailability_1.getText()); + } + if (btnLocation_1.getSelection()) { + NFRs.add(btnLocation_1.getText()); + } + + optimus.appjson.put("NFRs", NFRs); + //} + + + + ProgressBar progressBar = new ProgressBar(parent, SWT.NONE); + progressBar.setBounds(103, 18, 170, 17); + + + + progressBar.setVisible(true); + simulationText.setText(optimus.appjson.toString()); + addDisposeListener(new DisposeListener() { + public void widgetDisposed(DisposeEvent e) { + } + }); + + } + }); + scrolledComposite.setContent(group); + scrolledComposite.setMinSize(group.computeSize(SWT.DEFAULT, SWT.DEFAULT)); + + } +} diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/optimus.java b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/optimus.java new file mode 100644 index 0000000000000000000000000000000000000000..1739f14097673fd45bd06e5c671e2f2353f8e214 --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/editors/optimus.java @@ -0,0 +1,259 @@ + +/********************************************************************* +* Copyright (c) 2017 Tecnalia. +* +* This program and the accompanying materials are made +* available under the terms of the Eclipse Public License 2.0 +* which is available at https://www.eclipse.org/legal/epl-2.0/ +* +* SPDX-License-Identifier: EPL-2.0 +* Contributors (in alphabetical order): +* Gorka Benguria Tecnalia +* I�aki Etxaniz Tecnalia +* Juncal Alonso Tecnalia +* Leire Orue-Echevarria Tecnalia +* Maria Jose Lopez Tecnalia +* Marisa Escalante Tecnalia +* Initially developed in the context of DECIDE EU project www.DECIDE-h2020.eu +**********************************************************************/ +package eu.DECIDEh2020.optimus.editors; + + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringWriter; +import java.text.Collator; +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; +import java.util.StringTokenizer; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IResourceChangeEvent; +import org.eclipse.core.resources.IResourceChangeListener; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jface.dialogs.ErrorDialog; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.FontDialog; +import org.eclipse.ui.*; +import org.eclipse.ui.editors.text.TextEditor; +import org.eclipse.ui.part.FileEditorInput; +import org.eclipse.ui.part.MultiPageEditorPart; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; +import org.json.simple.JSONArray; +import org.json.*; +import org.eclipse.ui.ide.IDE; + +/** + * How to create a multi-page editor. + * This example has 3 pages: + * <ul> + * <li>page 0 contains a nested text editor. + * <li>page 1 shows the Classification tab + * <li>page 2 shows the Simulation tab + * </ul> + */ +public class optimus extends MultiPageEditorPart implements IResourceChangeListener{ + + /** The text editor used in page 0. */ + // Tecnalia20171024: I should find out how to assign a json editor + public static TextEditor editor; + private StyledText text; + // create a json object + public static JSONObject appjson; + public static JSONArray appmicros; + public static String newjson = ""; + public static String workingDir; + /** + * Creates a multi-page editor example. + */ + public optimus() { + super(); + + ResourcesPlugin.getWorkspace().addResourceChangeListener(this); + + appjson = new JSONObject(); + appmicros = new JSONArray(); + + } + /** + * Creates page 0 of the multi-page editor, + * which contains a text editor. + */ + void createPage0() { + try { + + editor = new TextEditor(); + int index = addPage(editor, getEditorInput()); + + setPageText(index, editor.getTitle()); + + } catch (PartInitException e) { + ErrorDialog.openError( + getSite().getShell(), + "Error creating nested text editor", + null, + e.getStatus()); + } + + } + // these lines of comments are a try of reading the JSON file and drawing the corresponding elements in classification tab + // I leave it to try again for the second version - Mar�a Jos� L�pe< - TECNALIA November 2017 + +// private JSONObject initialfile() throws IOException, ParseException { +// +// String line; +// FileReader f = new FileReader(workingDir); +// BufferedReader b = new BufferedReader(f); +// line = b.readLine(); +// JSONParser parser = new JSONParser(); +// JSONObject jsonfilecontent = (JSONObject) parser.parse(line); +// //JSONObject jsonfilecontent = new JSONObject(line); +// b.close(); +// return jsonfilecontent; +// } + /** + * Creates page 1 of the multi-page editor, + * which allows you to change the font used in page 2. + */ + // Tecnalia: The UI for the classification tool + void createPage1() { + + Composite composite = new Classification(getContainer(), SWT.NONE); + + int index = addPage(composite); + setPageText(index, "Classification"); + } + /** + * Creates page 2 of the multi-page editor, + * which shows the sorted text. + */ + void createPage2() { +// Classification tab + Composite composite = new Simulation (getContainer(), SWT.NONE); + + int index = addPage(composite); + setPageText(index, "Simulation"); + } + + + + + /** + * Creates the pages of the multi-page editor. + */ + protected void createPages() { + createPage0(); + createPage1(); + createPage2(); + } + /** + * The <code>MultiPageEditorPart</code> implementation of this + * <code>IWorkbenchPart</code> method disposes all nested editors. + * Subclasses may extend. + */ + public void dispose() { + ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); + super.dispose(); + } + /** + * Saves the multi-page editor's document. + */ + public void doSave(IProgressMonitor monitor) { + getEditor(0).doSave(monitor); + + + } + /** + * Saves the multi-page editor's document as another file. + * Also updates the text for page 0's tab, and updates this multi-page editor's input + * to correspond to the nested editor's. + */ + public void doSaveAs() { + IEditorPart editor = getEditor(0); + editor.doSaveAs(); + setPageText(0, editor.getTitle()); + setInput(editor.getEditorInput()); + } + /* (non-Javadoc) + * Method declared on IEditorPart + */ + public void gotoMarker(IMarker marker) { + setActivePage(0); + IDE.gotoMarker(getEditor(0), marker); + } + /** + * The <code>MultiPageEditorExample</code> implementation of this method + * checks that the input is an instance of <code>IFileEditorInput</code>. + */ + public void init(IEditorSite site, IEditorInput editorInput) + throws PartInitException { + + if (!(editorInput instanceof IFileEditorInput)) + throw new PartInitException("Invalid Input: Must be IFileEditorInput"); + workingDir = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString(); + Integer position = editorInput.toString().indexOf("/"); + Integer length = editorInput.toString().length(); + workingDir = workingDir+editorInput.toString().substring(position,length-1); + + super.init(site, editorInput); + } + /* (non-Javadoc) + * Method declared on IEditorPart. + */ + public boolean isSaveAsAllowed() { + return true; + } + /** + * Calculates the contents of page 0. + */ + protected void pageChange(int newPageIndex) { + super.pageChange(newPageIndex); + if (newPageIndex == 1) { + } + if (newPageIndex == 2) { + } + if (newPageIndex == 0) { + // I do not know why but I do not see the new content until the button save is selected. + // to be analyzed for next version + //IEditorPart editor = getEditor(0); + //setPageText(0, editor.getTitle()); + //setInput(editor.getEditorInput()); + } + } + + /** + * Closes all project files on project close. + */ + public void resourceChanged(final IResourceChangeEvent event){ + if(event.getType() == IResourceChangeEvent.PRE_CLOSE){ + Display.getDefault().asyncExec(new Runnable(){ + public void run(){ + IWorkbenchPage[] pages = getSite().getWorkbenchWindow().getPages(); + for (int i = 0; i<pages.length; i++){ + if(((FileEditorInput)editor.getEditorInput()).getFile().getProject().equals(event.getResource())){ + IEditorPart editorPart = pages[i].findEditor(editor.getEditorInput()); + pages[i].closeEditor(editorPart,true); + } + } + } + }); + } + } +} diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/wizards/OptimusWizard.java b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/wizards/OptimusWizard.java new file mode 100644 index 0000000000000000000000000000000000000000..6f1a9446404e5352a51c1753aa48916bcdd20024 --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/wizards/OptimusWizard.java @@ -0,0 +1,152 @@ +package eu.DECIDEh2020.optimus.wizards; + +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.wizard.Wizard; +import org.eclipse.ui.INewWizard; +import org.eclipse.ui.IWorkbench; +import org.eclipse.core.runtime.*; +import org.eclipse.jface.operation.*; +import java.lang.reflect.InvocationTargetException; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.core.resources.*; +import org.eclipse.core.runtime.CoreException; +import java.io.*; +import org.eclipse.ui.*; +import org.eclipse.ui.ide.IDE; + +/** + * This is a sample new wizard. Its role is to create a new file + * resource in the provided container. If the container resource + * (a folder or a project) is selected in the workspace + * when the wizard is opened, it will accept it as the target + * container. The wizard creates one file with the extension + * "json". If a sample multi-page editor (also available + * as a template) is registered for the same extension, it will + * be able to open it. + */ + +public class OptimusWizard extends Wizard implements INewWizard { + private OptimusWizardPage page; + private ISelection selection; + + /** + * Constructor for OptimusWizard. + */ + public OptimusWizard() { + super(); + setNeedsProgressMonitor(true); + } + + /** + * Adding the page to the wizard. + */ + @Override + public void addPages() { + page = new OptimusWizardPage(selection); + addPage(page); + } + + /** + * This method is called when 'Finish' button is pressed in + * the wizard. We will create an operation and run it + * using wizard as execution context. + */ + @Override + public boolean performFinish() { + final String containerName = page.getContainerName(); + final String fileName = page.getFileName(); + IRunnableWithProgress op = new IRunnableWithProgress() { + public void run(IProgressMonitor monitor) throws InvocationTargetException { + try { + doFinish(containerName, fileName, monitor); + } catch (CoreException e) { + throw new InvocationTargetException(e); + } finally { + monitor.done(); + } + } + }; + try { + getContainer().run(true, false, op); + } catch (InterruptedException e) { + return false; + } catch (InvocationTargetException e) { + Throwable realException = e.getTargetException(); + MessageDialog.openError(getShell(), "Error", realException.getMessage()); + return false; + } + return true; + } + + /** + * The worker method. It will find the container, create the + * file if missing or just replace its contents, and open + * the editor on the newly created file. + */ + + private void doFinish( + String containerName, + String fileName, + IProgressMonitor monitor) + throws CoreException { + // create a sample file + monitor.beginTask("Creating " + fileName, 2); + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + IResource resource = root.findMember(new Path(containerName)); + if (!resource.exists() || !(resource instanceof IContainer)) { + throwCoreException("Container \"" + containerName + "\" does not exist."); + } + IContainer container = (IContainer) resource; + final IFile file = container.getFile(new Path(fileName)); + try { + InputStream stream = openContentStream(); + if (file.exists()) { + file.setContents(stream, true, true, monitor); + } else { + file.create(stream, true, monitor); + } + stream.close(); + } catch (IOException e) { + } + monitor.worked(1); + monitor.setTaskName("Opening file for editing..."); + getShell().getDisplay().asyncExec(new Runnable() { + public void run() { + IWorkbenchPage page = + PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + try { + IDE.openEditor(page, file, true); + } catch (PartInitException e) { + } + } + }); + monitor.worked(1); + } + + /** + * We will initialize file contents with a sample text. + */ + + private InputStream openContentStream() { + String contents = + "This is the initial file contents for *.json file that should be word-sorted in the Preview page of the multi-page editor"; + return new ByteArrayInputStream(contents.getBytes()); + } + + private void throwCoreException(String message) throws CoreException { + IStatus status = + new Status(IStatus.ERROR, "eu.DECIDEh2020.optimus", IStatus.OK, message, null); + throw new CoreException(status); + } + + /** + * We will accept the selection in the workbench to see if + * we can initialize from it. + * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection) + */ + @Override + public void init(IWorkbench workbench, IStructuredSelection selection) { + this.selection = selection; + } +} \ No newline at end of file diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/wizards/OptimusWizardPage.java b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/wizards/OptimusWizardPage.java new file mode 100644 index 0000000000000000000000000000000000000000..0c0c8eeeb15f685f8f976c3342ddf0a55e5f26f5 --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/src/eu/DECIDEh2020/optimus/wizards/OptimusWizardPage.java @@ -0,0 +1,184 @@ +package eu.DECIDEh2020.optimus.wizards; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.Path; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.wizard.WizardPage; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; +import org.eclipse.ui.dialogs.ContainerSelectionDialog; + +/** + * The "New" wizard page allows setting the container for the new file as well + * as the file name. The page will only accept file name without the extension + * OR with the extension that matches the expected one (json). + */ + +public class OptimusWizardPage extends WizardPage { + private Text containerText; + + private Text fileText; + + private ISelection selection; + + /** + * Constructor for SampleNewWizardPage. + * + * @param pageName + */ + public OptimusWizardPage(ISelection selection) { + super("wizardPage"); + setTitle("Multi-page Editor File"); + setDescription("This wizard creates a new file with *.json extension that can be opened by a multi-page editor."); + this.selection = selection; + } + + @Override + public void createControl(Composite parent) { + Composite container = new Composite(parent, SWT.NULL); + GridLayout layout = new GridLayout(); + container.setLayout(layout); + layout.numColumns = 3; + layout.verticalSpacing = 9; + Label label = new Label(container, SWT.NULL); + label.setText("&Container:"); + + containerText = new Text(container, SWT.BORDER | SWT.SINGLE); + GridData gd = new GridData(GridData.FILL_HORIZONTAL); + containerText.setLayoutData(gd); + containerText.addModifyListener(new ModifyListener() { + @Override + public void modifyText(ModifyEvent e) { + dialogChanged(); + } + }); + + Button button = new Button(container, SWT.PUSH); + button.setText("Browse..."); + button.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + handleBrowse(); + } + }); + label = new Label(container, SWT.NULL); + label.setText("&File name:"); + + fileText = new Text(container, SWT.BORDER | SWT.SINGLE); + gd = new GridData(GridData.FILL_HORIZONTAL); + fileText.setLayoutData(gd); + fileText.addModifyListener(new ModifyListener() { + public void modifyText(ModifyEvent e) { + dialogChanged(); + } + }); + initialize(); + dialogChanged(); + setControl(container); + } + + /** + * Tests if the current workbench selection is a suitable container to use. + */ + + private void initialize() { + if (selection != null && selection.isEmpty() == false + && selection instanceof IStructuredSelection) { + IStructuredSelection ssel = (IStructuredSelection) selection; + if (ssel.size() > 1) + return; + Object obj = ssel.getFirstElement(); + if (obj instanceof IResource) { + IContainer container; + if (obj instanceof IContainer) + container = (IContainer) obj; + else + container = ((IResource) obj).getParent(); + containerText.setText(container.getFullPath().toString()); + } + } + fileText.setText("new_file.json"); + } + + /** + * Uses the standard container selection dialog to choose the new value for + * the container field. + */ + + private void handleBrowse() { + ContainerSelectionDialog dialog = new ContainerSelectionDialog( + getShell(), ResourcesPlugin.getWorkspace().getRoot(), false, + "Select new file container"); + if (dialog.open() == ContainerSelectionDialog.OK) { + Object[] result = dialog.getResult(); + if (result.length == 1) { + containerText.setText(((Path) result[0]).toString()); + } + } + } + + /** + * Ensures that both text fields are set. + */ + + private void dialogChanged() { + IResource container = ResourcesPlugin.getWorkspace().getRoot() + .findMember(new Path(getContainerName())); + String fileName = getFileName(); + + if (getContainerName().length() == 0) { + updateStatus("File container must be specified"); + return; + } + if (container == null + || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) { + updateStatus("File container must exist"); + return; + } + if (!container.isAccessible()) { + updateStatus("Project must be writable"); + return; + } + if (fileName.length() == 0) { + updateStatus("File name must be specified"); + return; + } + if (fileName.replace('\\', '/').indexOf('/', 1) > 0) { + updateStatus("File name must be valid"); + return; + } + int dotLoc = fileName.lastIndexOf('.'); + if (dotLoc != -1) { + String ext = fileName.substring(dotLoc + 1); + if (ext.equalsIgnoreCase("json") == false) { + updateStatus("File extension must be \"json\""); + return; + } + } + updateStatus(null); + } + + private void updateStatus(String message) { + setErrorMessage(message); + setPageComplete(message == null); + } + + public String getContainerName() { + return containerText.getText(); + } + + public String getFileName() { + return fileText.getText(); + } +} \ No newline at end of file diff --git a/OPTIMUS/eu.DECIDEh2020.optimus/src/org/eclipse/wb/swt/SWTResourceManager.java b/OPTIMUS/eu.DECIDEh2020.optimus/src/org/eclipse/wb/swt/SWTResourceManager.java new file mode 100644 index 0000000000000000000000000000000000000000..d8a285807adcefed53ca8b9d3af0f7d9881e1a13 --- /dev/null +++ b/OPTIMUS/eu.DECIDEh2020.optimus/src/org/eclipse/wb/swt/SWTResourceManager.java @@ -0,0 +1,447 @@ +/******************************************************************************* + * Copyright (c) 2011 Google, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Google, Inc. - initial API and implementation + *******************************************************************************/ +package org.eclipse.wb.swt; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Cursor; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.widgets.Display; + +/** + * Utility class for managing OS resources associated with SWT controls such as colors, fonts, images, etc. + * <p> + * !!! IMPORTANT !!! Application code must explicitly invoke the <code>dispose()</code> method to release the + * operating system resources managed by cached objects when those objects and OS resources are no longer + * needed (e.g. on application shutdown) + * <p> + * This class may be freely distributed as part of any application or plugin. + * <p> + * @author scheglov_ke + * @author Dan Rubel + */ +public class SWTResourceManager { + //////////////////////////////////////////////////////////////////////////// + // + // Color + // + //////////////////////////////////////////////////////////////////////////// + private static Map<RGB, Color> m_colorMap = new HashMap<RGB, Color>(); + /** + * Returns the system {@link Color} matching the specific ID. + * + * @param systemColorID + * the ID value for the color + * @return the system {@link Color} matching the specific ID + */ + public static Color getColor(int systemColorID) { + Display display = Display.getCurrent(); + return display.getSystemColor(systemColorID); + } + /** + * Returns a {@link Color} given its red, green and blue component values. + * + * @param r + * the red component of the color + * @param g + * the green component of the color + * @param b + * the blue component of the color + * @return the {@link Color} matching the given red, green and blue component values + */ + public static Color getColor(int r, int g, int b) { + return getColor(new RGB(r, g, b)); + } + /** + * Returns a {@link Color} given its RGB value. + * + * @param rgb + * the {@link RGB} value of the color + * @return the {@link Color} matching the RGB value + */ + public static Color getColor(RGB rgb) { + Color color = m_colorMap.get(rgb); + if (color == null) { + Display display = Display.getCurrent(); + color = new Color(display, rgb); + m_colorMap.put(rgb, color); + } + return color; + } + /** + * Dispose of all the cached {@link Color}'s. + */ + public static void disposeColors() { + for (Color color : m_colorMap.values()) { + color.dispose(); + } + m_colorMap.clear(); + } + //////////////////////////////////////////////////////////////////////////// + // + // Image + // + //////////////////////////////////////////////////////////////////////////// + /** + * Maps image paths to images. + */ + private static Map<String, Image> m_imageMap = new HashMap<String, Image>(); + /** + * Returns an {@link Image} encoded by the specified {@link InputStream}. + * + * @param stream + * the {@link InputStream} encoding the image data + * @return the {@link Image} encoded by the specified input stream + */ + protected static Image getImage(InputStream stream) throws IOException { + try { + Display display = Display.getCurrent(); + ImageData data = new ImageData(stream); + if (data.transparentPixel > 0) { + return new Image(display, data, data.getTransparencyMask()); + } + return new Image(display, data); + } finally { + stream.close(); + } + } + /** + * Returns an {@link Image} stored in the file at the specified path. + * + * @param path + * the path to the image file + * @return the {@link Image} stored in the file at the specified path + */ + public static Image getImage(String path) { + Image image = m_imageMap.get(path); + if (image == null) { + try { + image = getImage(new FileInputStream(path)); + m_imageMap.put(path, image); + } catch (Exception e) { + image = getMissingImage(); + m_imageMap.put(path, image); + } + } + return image; + } + /** + * Returns an {@link Image} stored in the file at the specified path relative to the specified class. + * + * @param clazz + * the {@link Class} relative to which to find the image + * @param path + * the path to the image file, if starts with <code>'/'</code> + * @return the {@link Image} stored in the file at the specified path + */ + public static Image getImage(Class<?> clazz, String path) { + String key = clazz.getName() + '|' + path; + Image image = m_imageMap.get(key); + if (image == null) { + try { + image = getImage(clazz.getResourceAsStream(path)); + m_imageMap.put(key, image); + } catch (Exception e) { + image = getMissingImage(); + m_imageMap.put(key, image); + } + } + return image; + } + private static final int MISSING_IMAGE_SIZE = 10; + /** + * @return the small {@link Image} that can be used as placeholder for missing image. + */ + private static Image getMissingImage() { + Image image = new Image(Display.getCurrent(), MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE); + // + GC gc = new GC(image); + gc.setBackground(getColor(SWT.COLOR_RED)); + gc.fillRectangle(0, 0, MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE); + gc.dispose(); + // + return image; + } + /** + * Style constant for placing decorator image in top left corner of base image. + */ + public static final int TOP_LEFT = 1; + /** + * Style constant for placing decorator image in top right corner of base image. + */ + public static final int TOP_RIGHT = 2; + /** + * Style constant for placing decorator image in bottom left corner of base image. + */ + public static final int BOTTOM_LEFT = 3; + /** + * Style constant for placing decorator image in bottom right corner of base image. + */ + public static final int BOTTOM_RIGHT = 4; + /** + * Internal value. + */ + protected static final int LAST_CORNER_KEY = 5; + /** + * Maps images to decorated images. + */ + @SuppressWarnings("unchecked") + private static Map<Image, Map<Image, Image>>[] m_decoratedImageMap = new Map[LAST_CORNER_KEY]; + /** + * Returns an {@link Image} composed of a base image decorated by another image. + * + * @param baseImage + * the base {@link Image} that should be decorated + * @param decorator + * the {@link Image} to decorate the base image + * @return {@link Image} The resulting decorated image + */ + public static Image decorateImage(Image baseImage, Image decorator) { + return decorateImage(baseImage, decorator, BOTTOM_RIGHT); + } + /** + * Returns an {@link Image} composed of a base image decorated by another image. + * + * @param baseImage + * the base {@link Image} that should be decorated + * @param decorator + * the {@link Image} to decorate the base image + * @param corner + * the corner to place decorator image + * @return the resulting decorated {@link Image} + */ + public static Image decorateImage(final Image baseImage, final Image decorator, final int corner) { + if (corner <= 0 || corner >= LAST_CORNER_KEY) { + throw new IllegalArgumentException("Wrong decorate corner"); + } + Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[corner]; + if (cornerDecoratedImageMap == null) { + cornerDecoratedImageMap = new HashMap<Image, Map<Image, Image>>(); + m_decoratedImageMap[corner] = cornerDecoratedImageMap; + } + Map<Image, Image> decoratedMap = cornerDecoratedImageMap.get(baseImage); + if (decoratedMap == null) { + decoratedMap = new HashMap<Image, Image>(); + cornerDecoratedImageMap.put(baseImage, decoratedMap); + } + // + Image result = decoratedMap.get(decorator); + if (result == null) { + Rectangle bib = baseImage.getBounds(); + Rectangle dib = decorator.getBounds(); + // + result = new Image(Display.getCurrent(), bib.width, bib.height); + // + GC gc = new GC(result); + gc.drawImage(baseImage, 0, 0); + if (corner == TOP_LEFT) { + gc.drawImage(decorator, 0, 0); + } else if (corner == TOP_RIGHT) { + gc.drawImage(decorator, bib.width - dib.width, 0); + } else if (corner == BOTTOM_LEFT) { + gc.drawImage(decorator, 0, bib.height - dib.height); + } else if (corner == BOTTOM_RIGHT) { + gc.drawImage(decorator, bib.width - dib.width, bib.height - dib.height); + } + gc.dispose(); + // + decoratedMap.put(decorator, result); + } + return result; + } + /** + * Dispose all of the cached {@link Image}'s. + */ + public static void disposeImages() { + // dispose loaded images + { + for (Image image : m_imageMap.values()) { + image.dispose(); + } + m_imageMap.clear(); + } + // dispose decorated images + for (int i = 0; i < m_decoratedImageMap.length; i++) { + Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[i]; + if (cornerDecoratedImageMap != null) { + for (Map<Image, Image> decoratedMap : cornerDecoratedImageMap.values()) { + for (Image image : decoratedMap.values()) { + image.dispose(); + } + decoratedMap.clear(); + } + cornerDecoratedImageMap.clear(); + } + } + } + //////////////////////////////////////////////////////////////////////////// + // + // Font + // + //////////////////////////////////////////////////////////////////////////// + /** + * Maps font names to fonts. + */ + private static Map<String, Font> m_fontMap = new HashMap<String, Font>(); + /** + * Maps fonts to their bold versions. + */ + private static Map<Font, Font> m_fontToBoldFontMap = new HashMap<Font, Font>(); + /** + * Returns a {@link Font} based on its name, height and style. + * + * @param name + * the name of the font + * @param height + * the height of the font + * @param style + * the style of the font + * @return {@link Font} The font matching the name, height and style + */ + public static Font getFont(String name, int height, int style) { + return getFont(name, height, style, false, false); + } + /** + * Returns a {@link Font} based on its name, height and style. Windows-specific strikeout and underline + * flags are also supported. + * + * @param name + * the name of the font + * @param size + * the size of the font + * @param style + * the style of the font + * @param strikeout + * the strikeout flag (warning: Windows only) + * @param underline + * the underline flag (warning: Windows only) + * @return {@link Font} The font matching the name, height, style, strikeout and underline + */ + public static Font getFont(String name, int size, int style, boolean strikeout, boolean underline) { + String fontName = name + '|' + size + '|' + style + '|' + strikeout + '|' + underline; + Font font = m_fontMap.get(fontName); + if (font == null) { + FontData fontData = new FontData(name, size, style); + if (strikeout || underline) { + try { + Class<?> logFontClass = Class.forName("org.eclipse.swt.internal.win32.LOGFONT"); //$NON-NLS-1$ + Object logFont = FontData.class.getField("data").get(fontData); //$NON-NLS-1$ + if (logFont != null && logFontClass != null) { + if (strikeout) { + logFontClass.getField("lfStrikeOut").set(logFont, Byte.valueOf((byte) 1)); //$NON-NLS-1$ + } + if (underline) { + logFontClass.getField("lfUnderline").set(logFont, Byte.valueOf((byte) 1)); //$NON-NLS-1$ + } + } + } catch (Throwable e) { + System.err.println("Unable to set underline or strikeout" + " (probably on a non-Windows platform). " + e); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + font = new Font(Display.getCurrent(), fontData); + m_fontMap.put(fontName, font); + } + return font; + } + /** + * Returns a bold version of the given {@link Font}. + * + * @param baseFont + * the {@link Font} for which a bold version is desired + * @return the bold version of the given {@link Font} + */ + public static Font getBoldFont(Font baseFont) { + Font font = m_fontToBoldFontMap.get(baseFont); + if (font == null) { + FontData fontDatas[] = baseFont.getFontData(); + FontData data = fontDatas[0]; + font = new Font(Display.getCurrent(), data.getName(), data.getHeight(), SWT.BOLD); + m_fontToBoldFontMap.put(baseFont, font); + } + return font; + } + /** + * Dispose all of the cached {@link Font}'s. + */ + public static void disposeFonts() { + // clear fonts + for (Font font : m_fontMap.values()) { + font.dispose(); + } + m_fontMap.clear(); + // clear bold fonts + for (Font font : m_fontToBoldFontMap.values()) { + font.dispose(); + } + m_fontToBoldFontMap.clear(); + } + //////////////////////////////////////////////////////////////////////////// + // + // Cursor + // + //////////////////////////////////////////////////////////////////////////// + /** + * Maps IDs to cursors. + */ + private static Map<Integer, Cursor> m_idToCursorMap = new HashMap<Integer, Cursor>(); + /** + * Returns the system cursor matching the specific ID. + * + * @param id + * int The ID value for the cursor + * @return Cursor The system cursor matching the specific ID + */ + public static Cursor getCursor(int id) { + Integer key = Integer.valueOf(id); + Cursor cursor = m_idToCursorMap.get(key); + if (cursor == null) { + cursor = new Cursor(Display.getDefault(), id); + m_idToCursorMap.put(key, cursor); + } + return cursor; + } + /** + * Dispose all of the cached cursors. + */ + public static void disposeCursors() { + for (Cursor cursor : m_idToCursorMap.values()) { + cursor.dispose(); + } + m_idToCursorMap.clear(); + } + //////////////////////////////////////////////////////////////////////////// + // + // General + // + //////////////////////////////////////////////////////////////////////////// + /** + * Dispose of cached objects and their underlying OS resources. This should only be called when the cached + * objects are no longer needed (e.g. on application shutdown). + */ + public static void dispose() { + disposeColors(); + disposeImages(); + disposeFonts(); + disposeCursors(); + } +} \ No newline at end of file