Commit 7d05a1d9 authored by aziz's avatar aziz
Browse files

emoved utils package


Signed-off-by: default avataraziz <aziz@fortiss.org>
parent c56b32cd
EditorUtils.java 0aa5f7e5806c49991663d080c12b149783e35d03 YELLOW
/*-------------------------------------------------------------------------+
| Copyright 2018 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.cosimulation.ui.utils;
import static org.eclipse.ui.PlatformUI.getWorkbench;
import static org.fortiss.af3.component.utils.ComponentModelElementFactory.createInputPort;
import static org.fortiss.af3.component.utils.ComponentModelElementFactory.createOutputPort;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.boolType;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.createNoVal;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.doubleType;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.intType;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.fortiss.af3.component.model.Component;
import org.fortiss.af3.component.model.InputPort;
import org.fortiss.af3.component.model.Port;
import org.fortiss.af3.cosimulation.ui.editor.ExternalSpecificationEditor;
import org.fortiss.af3.project.model.typesystem.IType;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
/**
* Utility methods for {@link ExternalSpecificationEditor}.
*
* @author aziz
*/
public class EditorUtils {
/** Open the file dialog and ask to provide the path for the file. */
public static String fileDialog(String fileExtenstion) {
Shell shell = getWorkbench().getActiveWorkbenchWindow().getShell();
FileDialog fd = new FileDialog(shell, SWT.OPEN);
fd.setText("Provide " + fileExtenstion + " file");
fd.setFilterPath("C:/");
String[] filterExt = {fileExtenstion};
fd.setFilterExtensions(filterExt);
return fd.open();
}
/** Unzip and extract xml file */
public static void
extractXMLFile(String xmlDestinationDirectory, String zipPath, String xmlName)
throws IOException {
String xmlPath = xmlDestinationDirectory + File.separator + xmlName;
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipPath));
ZipEntry entry = zipIn.getNextEntry();
while(entry != null) {
if(!entry.isDirectory() && entry.getName().contains(xmlName)) {
extractFile(zipIn, xmlPath);
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
/** Extract XML file in the temporary build directory. */
public static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[4096];
int read = 0;
while((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
/** Parse XML and create or modify ports. */
public static DefaultHandler parseXMLAndCreateOrModifyPorts(Component container,
ArrayList<Port> newOutputPorts, ArrayList<Port> newInputPorts) {
DefaultHandler handler = new DefaultHandler() {
String portName;
boolean isInput = false;
boolean isPort = false;
IType type = null;
@Override
public void startElement(String uri, String localName, String startElementName,
Attributes attributes) {
if(startElementName.equals("Inport")) {
portName = attributes.getValue("Name");
isInput = true;
isPort = true;
type = doubleType();
}
if(startElementName.equals("Outport")) {
portName = attributes.getValue("Name");
isInput = false;
isPort = true;
type = doubleType();
}
if(startElementName.equals("ScalarVariable")) {
portName = attributes.getValue("name");
isInput = attributes.getValue("causality").equals("input");
}
if(startElementName.equals("Real")) {
type = doubleType();
isPort = true;
}
if(startElementName.equals("Integer")) {
type = intType();
isPort = true;
}
if(startElementName.equals("Boolean")) {
type = boolType();
isPort = true;
}
}
@Override
public void characters(char ch[], int start, int length) {
if(isPort) {
Port existingPort =
isInput ? container.findInputPort(portName) : container
.findOutputPort(portName);
if(existingPort != null) {
existingPort.getPortSpecification().setType(type);
newInputPorts.add(isInput ? existingPort : null);
newOutputPorts.add(isInput ? null : existingPort);
} else {
Port port;
if(isInput) {
port = createInputPort(portName, "", type, createNoVal());
} else {
port = createOutputPort(portName, "", type, createNoVal());
}
container.getConnectors().add(port);
newInputPorts.add(isInput ? port : null);
newOutputPorts.add(isInput ? null : port);
}
isPort = false;
}
}
};
return handler;
}
/** Delete extra input and output ports. */
public static void deleteExtraPorts(ArrayList<Port> newInputPorts,
ArrayList<Port> newOutputPorts, Component container) {
ArrayList<Port> ports = new ArrayList<>(container.getInputPorts());
ports.addAll(container.getOutputPorts());
for(Port p : ports) {
if(p instanceof InputPort ? !newInputPorts.contains(p) : !newOutputPorts.contains(p)) {
container.getConnectors().remove(p);
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment