*******************************************************************************/\r
package org.simantics.interop.test;\r
\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileReader;\r
import java.io.IOException;\r
+import java.io.PrintStream;\r
+import java.io.Serializable;\r
import java.util.ArrayList;\r
+import java.util.Collection;\r
import java.util.HashMap;\r
import java.util.List;\r
import java.util.Map;\r
+import java.util.Stack;\r
+\r
+import javax.xml.parsers.SAXParser;\r
+import javax.xml.parsers.SAXParserFactory;\r
\r
import org.eclipse.jface.action.Action;\r
-import org.eclipse.jface.action.IAction;\r
-import org.eclipse.jface.action.IMenuCreator;\r
import org.eclipse.jface.action.IMenuListener;\r
import org.eclipse.jface.action.IMenuManager;\r
import org.eclipse.jface.action.MenuManager;\r
import org.eclipse.jface.dialogs.IDialogConstants;\r
import org.eclipse.jface.dialogs.MessageDialog;\r
import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.resource.ImageDescriptor;\r
-import org.eclipse.jface.util.IPropertyChangeListener;\r
-import org.eclipse.jface.viewers.ILabelProvider;\r
import org.eclipse.jface.viewers.IStructuredContentProvider;\r
import org.eclipse.jface.viewers.IStructuredSelection;\r
import org.eclipse.jface.viewers.LabelProvider;\r
import org.eclipse.swt.dnd.DropTargetEvent;\r
import org.eclipse.swt.dnd.TextTransfer;\r
import org.eclipse.swt.dnd.Transfer;\r
-import org.eclipse.swt.events.HelpListener;\r
-import org.eclipse.swt.events.MenuDetectEvent;\r
-import org.eclipse.swt.events.MenuDetectListener;\r
import org.eclipse.swt.events.SelectionAdapter;\r
import org.eclipse.swt.events.SelectionEvent;\r
import org.eclipse.swt.layout.FillLayout;\r
import org.eclipse.swt.widgets.Composite;\r
import org.eclipse.swt.widgets.Control;\r
import org.eclipse.swt.widgets.Display;\r
-import org.eclipse.swt.widgets.Event;\r
+import org.eclipse.swt.widgets.FileDialog;\r
import org.eclipse.swt.widgets.Group;\r
import org.eclipse.swt.widgets.Label;\r
-import org.eclipse.swt.widgets.Menu;\r
import org.eclipse.swt.widgets.Shell;\r
import org.eclipse.swt.widgets.Text;\r
import org.eclipse.ui.part.ViewPart;\r
import org.simantics.utils.datastructures.BijectionMap;\r
import org.simantics.utils.datastructures.Pair;\r
import org.simantics.utils.ui.ExceptionUtils;\r
+import org.xml.sax.Attributes;\r
+import org.xml.sax.InputSource;\r
+import org.xml.sax.SAXException;\r
+import org.xml.sax.XMLReader;\r
+import org.xml.sax.helpers.DefaultHandler;\r
\r
/**\r
* Simple multiline text viewer for seeing differences in two subgraphs. \r
Layer0 l0 = Layer0.getInstance(graph);\r
traversed.add(l0.ConsistsOf);\r
tested.add(l0.IsWeaklyRelatedTo);\r
+ nonTraversed.add(l0.InstanceOf);\r
}\r
});\r
} catch (Exception e) {\r
\r
@Override\r
protected void createButtonsForButtonBar(Composite parent) {\r
- createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,\r
- true);\r
+ createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);\r
+ createButton(parent, IDialogConstants.CLIENT_ID+1, "Load", false);\r
+ createButton(parent, IDialogConstants.CLIENT_ID+2, "Save", false);\r
}\r
\r
+ @Override\r
+ protected void buttonPressed(int buttonId) {\r
+ super.buttonPressed(buttonId);\r
+ if (buttonId == IDialogConstants.CLIENT_ID+1) {\r
+ //load\r
+ FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.OPEN);\r
+ dialog.setFilterExtensions(new String[]{"*.xml"});\r
+ String filename = dialog.open();\r
+ if (filename == null)\r
+ return;\r
+ try {\r
+ load(filename);\r
+ traversedViewer.refresh();\r
+ testedViewer.refresh();\r
+ strongViewer.refresh();\r
+ nonTestedViewer.refresh();\r
+ nonTraversedViewer.refresh();\r
+ } catch (Exception e1) {\r
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), "Cannot load file" , e1.getMessage());\r
+ }\r
+ } else if (buttonId == IDialogConstants.CLIENT_ID+2) {\r
+ //save\r
+ FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.SAVE);\r
+ dialog.setFilterExtensions(new String[]{"*.xml"});\r
+ String filename = dialog.open();\r
+ if (filename == null)\r
+ return;\r
+ try {\r
+ saveConf(filename);\r
+ if (!filename.endsWith("xml")) \r
+ filename += ".xml";\r
+ } catch (FileNotFoundException e1) {\r
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), "Cannot save file" , e1.getMessage());\r
+ } catch (IOException e1) {\r
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), "Cannot save file" , e1.getMessage());\r
+ } catch (DatabaseException e1) {\r
+ MessageDialog.openError(Display.getCurrent().getActiveShell(), "Cannot save file" , e1.getMessage());\r
+ }\r
+ }\r
+ }\r
+ \r
+ private ListViewer traversedViewer;\r
+ private ListViewer testedViewer;\r
+ private ListViewer strongViewer;\r
+ private ListViewer nonTraversedViewer;\r
+ private ListViewer nonTestedViewer;\r
+ \r
@Override\r
protected Control createDialogArea(Composite parent) {\r
Composite composite = (Composite) super.createDialogArea(parent);\r
Group group = new Group(composite, SWT.NONE);\r
group.setText("Traversed");\r
group.setLayout(new FillLayout());\r
- ListViewer traversedViewer = new ListViewer(group);\r
+ traversedViewer = new ListViewer(group);\r
\r
group = new Group(composite, SWT.NONE);\r
group.setText("Tested");\r
group.setLayout(new FillLayout());\r
- ListViewer testedViewer = new ListViewer(group);\r
+ testedViewer = new ListViewer(group);\r
\r
group = new Group(composite, SWT.NONE);\r
group.setText("Strong");\r
group.setLayout(new FillLayout());\r
- ListViewer strongViewer = new ListViewer(group);\r
+ strongViewer = new ListViewer(group);\r
\r
group = new Group(composite, SWT.NONE);\r
group.setText("NonTraversed");\r
group.setLayout(new FillLayout());\r
- ListViewer nonTraversedViewer = new ListViewer(group);\r
+ nonTraversedViewer = new ListViewer(group);\r
\r
group = new Group(composite, SWT.NONE);\r
group.setText("NonTested");\r
group.setLayout(new FillLayout());\r
- ListViewer nonTestedViewer = new ListViewer(group);\r
+ nonTestedViewer = new ListViewer(group);\r
\r
traversedViewer.setContentProvider(new ResourceListContentProvider());\r
traversedViewer.setLabelProvider(new ResourceLabelProvider());\r
\r
}\r
\r
+ private boolean saveConf(String filename) throws IOException, DatabaseException {\r
+ File file = new File(filename);\r
+ if (file.exists()) {\r
+ if(!MessageDialog.openQuestion(Display.getCurrent().getActiveShell(), "Saving compare configuration", "Do you want to overwrite file'" + filename + "'?"))\r
+ return false; \r
+ } else {\r
+ if(!file.createNewFile())\r
+ return false;\r
+ }\r
+ final PrintStream ps = new PrintStream(file);\r
+ ps.println("<?xml version=\"1.0\"?>");\r
+ ps.println("<compare>");\r
+ session.syncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ for (Resource r : traversed) {\r
+ ps.println("<traversed uri=\"" + graph.getURI(r) + "\"/>");\r
+ }\r
+ for (Resource r : tested) {\r
+ ps.println("<tested uri=\"" + graph.getURI(r) + "\"/>");\r
+ }\r
+ for (Resource r : strong) {\r
+ ps.println("<strong uri=\"" + graph.getURI(r) + "\"/>");\r
+ }\r
+ for (Resource r : nonTraversed) {\r
+ ps.println("<nontraversed uri=\"" + graph.getURI(r) + "\"/>");\r
+ }\r
+ for (Resource r : nonTested) {\r
+ ps.println("<nontested uri=\"" + graph.getURI(r) + "\"/>");\r
+ }\r
+ }\r
+ });\r
+ \r
+ ps.println("</compare>");\r
+ ps.flush();\r
+ ps.close();\r
+ return true;\r
+ }\r
+ \r
+ private void load(String filename) throws Exception {\r
+ final ImportListener listener = new ImportListener();\r
+ XMLParser parser = new XMLParser(listener);\r
+ parser.parse(filename);\r
+ \r
+ if (listener.getNonTested().size() +\r
+ listener.getNonTraversed().size() +\r
+ listener.getStrong().size() +\r
+ listener.getTested().size() +\r
+ listener.getTraversed().size() == 0)\r
+ return;\r
+ session.syncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ tested.clear();\r
+ traversed.clear();\r
+ strong.clear();\r
+ nonTested.clear();\r
+ nonTraversed.clear();\r
+ for (String uri : listener.getTested()) {\r
+ tested.add(graph.getResource(uri));\r
+ }\r
+ for (String uri : listener.getTraversed()) {\r
+ traversed.add(graph.getResource(uri));\r
+ }\r
+ for (String uri : listener.getStrong()) {\r
+ strong.add(graph.getResource(uri));\r
+ }\r
+ \r
+ for (String uri : listener.getNonTested()) {\r
+ nonTested.add(graph.getResource(uri));\r
+ }\r
+ for (String uri : listener.getNonTraversed()) {\r
+ nonTraversed.add(graph.getResource(uri));\r
+ }\r
+ \r
+ }\r
+ });\r
+ \r
+ \r
+ \r
+ }\r
+ \r
+ public static class ImportListener {\r
+ \r
+ \r
+ private Collection<String> traversed = new ArrayList<String>();\r
+ private Collection<String> tested = new ArrayList<String>();\r
+ private Collection<String> strong = new ArrayList<String>();\r
+ private Collection<String> nonTraversed = new ArrayList<String>();\r
+ private Collection<String> nonTested = new ArrayList<String>();\r
+ \r
+\r
+ public void done() {\r
+\r
+ }\r
+ \r
+ \r
+ public void addTraversed(String uri) {\r
+ traversed.add(uri);\r
+ }\r
+ \r
+ public void addTested(String uri) {\r
+ tested.add(uri);\r
+ }\r
+ \r
+ public void addStrong(String uri) {\r
+ strong.add(uri);\r
+ }\r
+ \r
+ public void addNonTraversed(String uri) {\r
+ nonTraversed.add(uri);\r
+ }\r
+ \r
+ public void addNonTested(String uri) {\r
+ nonTested.add(uri);\r
+ }\r
+ \r
+ public Collection<String> getTraversed() {\r
+ return traversed;\r
+ }\r
+ \r
+ public Collection<String> getNonTested() {\r
+ return nonTested;\r
+ }\r
+ \r
+ public Collection<String> getNonTraversed() {\r
+ return nonTraversed;\r
+ }\r
+ \r
+ public Collection<String> getStrong() {\r
+ return strong;\r
+ }\r
+ \r
+ public Collection<String> getTested() {\r
+ return tested;\r
+ }\r
+ \r
+ }\r
+ \r
+ \r
+\r
+ public static class XMLParser {\r
+ \r
+ private ImportListener listener;\r
+ \r
+ public XMLParser(ImportListener listener) {\r
+ this.listener = listener;\r
+ }\r
+ \r
+ public void parse(String filename) throws Exception{\r
+ ParserBean p = new ParserBean();\r
+ p.parse(filename);\r
+ }\r
+ \r
+ private void handleElement(Stack<Element> parents, Element element) throws SAXException{\r
+ String name = element.qName;\r
+ if(name.equals("traversed")) {\r
+ for (Attribute a : element.attributes) {\r
+ String propertyName = a.localName;\r
+ String value = a.value;\r
+ if (propertyName.equals("uri")) {\r
+ listener.addTraversed(value);\r
+ \r
+ } \r
+ }\r
+ } else if(name.equals("tested")) {\r
+ for (Attribute a : element.attributes) {\r
+ String propertyName = a.localName;\r
+ String value = a.value;\r
+ if (propertyName.equals("uri")) {\r
+ listener.addTested(value);\r
+ \r
+ } \r
+ }\r
+ } else if(name.equals("strong")) {\r
+ for (Attribute a : element.attributes) {\r
+ String propertyName = a.localName;\r
+ String value = a.value;\r
+ if (propertyName.equals("uri")) {\r
+ listener.addStrong(value);\r
+ \r
+ } \r
+ }\r
+ } else if(name.equals("nontraversed")) {\r
+ for (Attribute a : element.attributes) {\r
+ String propertyName = a.localName;\r
+ String value = a.value;\r
+ if (propertyName.equals("uri")) {\r
+ listener.addNonTraversed(value);\r
+ \r
+ } \r
+ }\r
+ } else if(name.equals("nontested")) {\r
+ for (Attribute a : element.attributes) {\r
+ String propertyName = a.localName;\r
+ String value = a.value;\r
+ if (propertyName.equals("uri")) {\r
+ listener.addNonTested(value);\r
+ \r
+ } \r
+ }\r
+ } else if (name.equals("compare")) {\r
+ \r
+ } else {\r
+ throw new SAXException("Unknown element '" + name + "'. The file does not contain ExtIO configuration.");\r
+ \r
+ }\r
+ }\r
+\r
+ \r
+ public class ParserBean extends DefaultHandler implements Serializable {\r
+\r
+ private static final long serialVersionUID = -4995836637014958966L;\r
+ \r
+ private Stack<Element> current = new Stack<Element>();\r
+ private Stack<String> texts = new Stack<String>();\r
+ \r
+ public ParserBean() {\r
+ super();\r
+ }\r
+ \r
+ public void parse(String filename) throws Exception {\r
+ SAXParserFactory spf = SAXParserFactory.newInstance();\r
+ SAXParser saxParser = spf.newSAXParser();\r
+ \r
+ XMLReader reader = saxParser.getXMLReader();\r
+ FileReader file = new FileReader(filename);\r
+ reader.setContentHandler(this);\r
+ reader.parse(new InputSource(file));\r
+ listener.done();\r
+ }\r
+ \r
+ @Override\r
+ public void startElement(String uri, String localName, String name,\r
+ Attributes attributes) throws SAXException {\r
+ current.push(new Element(uri,localName,name,attributes));\r
+ texts.push(new String());\r
+ }\r
+ \r
+ @Override\r
+ public void endElement(String uri, String localName, String name)\r
+ throws SAXException {\r
+ Element e = null;\r
+ String text = null;\r
+ if (!current.empty()) {\r
+ e = current.pop();\r
+ text = texts.pop();\r
+ }\r
+ if (e != null) {\r
+ e.setValue(text.trim());\r
+ }\r
+ if (e != null) {\r
+ handleElement(current,e);\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public void characters(char[] ch, int start, int length)\r
+ throws SAXException {\r
+ if (!current.empty()) {\r
+ \r
+ String text = texts.pop();\r
+ \r
+ String value = new String(\r
+ ch, start, length);\r
+ text += value;\r
+ texts.push(text);\r
+ }\r
+ }\r
+ }\r
+ \r
+ public class Attribute {\r
+ public String localName;\r
+ public String qName;\r
+ public String uri;\r
+ public String value;\r
+ \r
+ public Attribute(String localName, String qName, String uri, String value) {\r
+ this.localName = localName;\r
+ this.qName = qName;\r
+ this.uri = uri;\r
+ this.value = value;\r
+ }\r
+ }\r
+ \r
+ public class Element implements Serializable {\r
+ private static final long serialVersionUID = -5207502156942818875L;\r
+ String uri;\r
+ String localName;\r
+ String qName;\r
+ String value = null;\r
+ List<Attribute> attributes = new ArrayList<Attribute>();\r
+ \r
+ public Element(String uri, String localName, String qName, Attributes attributes) {\r
+ this.uri = uri;\r
+ this.localName = localName;\r
+ this.qName = qName;\r
+ for (int i = 0; i < attributes.getLength(); i++) {\r
+ this.attributes.add(new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i)));\r
+ }\r
+ }\r
+\r
+ public String getUri() {\r
+ return uri;\r
+ }\r
+\r
+ public String getLocalName() {\r
+ return localName;\r
+ }\r
+\r
+ public String getQName() {\r
+ return qName;\r
+ }\r
+\r
+ public String getValue() {\r
+ return value;\r
+ }\r
+\r
+ public List<Attribute> getAttributes() {\r
+ return attributes;\r
+ }\r
+ \r
+ public void setValue(String value) {\r
+ this.value = value;\r
+ }\r
+\r
+ }\r
+\r
+ }\r
+ \r
}\r