From: luukkainen Date: Mon, 24 Oct 2011 11:37:11 +0000 (+0000) Subject: ComparatorView's comparator can be configured. X-Git-Tag: v1.31.0~102 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=016859e93aa8f3fdb53f0b00179886fc4b561a3b;p=simantics%2Finterop.git ComparatorView's comparator can be configured. git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@22928 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.interop/plugin.xml b/org.simantics.interop/plugin.xml index 74fdd2d..8143ecf 100644 --- a/org.simantics.interop/plugin.xml +++ b/org.simantics.interop/plugin.xml @@ -44,12 +44,14 @@ diff --git a/org.simantics.interop/src/org/simantics/interop/test/GraphComparatorViewer.java b/org.simantics.interop/src/org/simantics/interop/test/GraphComparatorViewer.java index 5cbe652..2ca1a8f 100644 --- a/org.simantics.interop/src/org/simantics/interop/test/GraphComparatorViewer.java +++ b/org.simantics.interop/src/org/simantics/interop/test/GraphComparatorViewer.java @@ -11,15 +11,23 @@ *******************************************************************************/ package org.simantics.interop.test; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; +import java.io.PrintStream; +import java.io.Serializable; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Stack; + +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; import org.eclipse.jface.action.Action; -import org.eclipse.jface.action.IAction; -import org.eclipse.jface.action.IMenuCreator; import org.eclipse.jface.action.IMenuListener; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.MenuManager; @@ -27,9 +35,6 @@ import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.resource.ImageDescriptor; -import org.eclipse.jface.util.IPropertyChangeListener; -import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.LabelProvider; @@ -43,9 +48,6 @@ import org.eclipse.swt.dnd.DropTargetAdapter; import org.eclipse.swt.dnd.DropTargetEvent; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.dnd.Transfer; -import org.eclipse.swt.events.HelpListener; -import org.eclipse.swt.events.MenuDetectEvent; -import org.eclipse.swt.events.MenuDetectListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FillLayout; @@ -55,10 +57,9 @@ import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.part.ViewPart; @@ -84,6 +85,11 @@ import org.simantics.ui.utils.ResourceAdaptionUtils; import org.simantics.utils.datastructures.BijectionMap; import org.simantics.utils.datastructures.Pair; import org.simantics.utils.ui.ExceptionUtils; +import org.xml.sax.Attributes; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.DefaultHandler; /** * Simple multiline text viewer for seeing differences in two subgraphs. @@ -164,6 +170,7 @@ public class GraphComparatorViewer extends ViewPart{ Layer0 l0 = Layer0.getInstance(graph); traversed.add(l0.ConsistsOf); tested.add(l0.IsWeaklyRelatedTo); + nonTraversed.add(l0.InstanceOf); } }); } catch (Exception e) { @@ -399,10 +406,58 @@ public class GraphComparatorViewer extends ViewPart{ @Override protected void createButtonsForButtonBar(Composite parent) { - createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, - true); + createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); + createButton(parent, IDialogConstants.CLIENT_ID+1, "Load", false); + createButton(parent, IDialogConstants.CLIENT_ID+2, "Save", false); } + @Override + protected void buttonPressed(int buttonId) { + super.buttonPressed(buttonId); + if (buttonId == IDialogConstants.CLIENT_ID+1) { + //load + FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.OPEN); + dialog.setFilterExtensions(new String[]{"*.xml"}); + String filename = dialog.open(); + if (filename == null) + return; + try { + load(filename); + traversedViewer.refresh(); + testedViewer.refresh(); + strongViewer.refresh(); + nonTestedViewer.refresh(); + nonTraversedViewer.refresh(); + } catch (Exception e1) { + MessageDialog.openError(Display.getCurrent().getActiveShell(), "Cannot load file" , e1.getMessage()); + } + } else if (buttonId == IDialogConstants.CLIENT_ID+2) { + //save + FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.SAVE); + dialog.setFilterExtensions(new String[]{"*.xml"}); + String filename = dialog.open(); + if (filename == null) + return; + try { + saveConf(filename); + if (!filename.endsWith("xml")) + filename += ".xml"; + } catch (FileNotFoundException e1) { + MessageDialog.openError(Display.getCurrent().getActiveShell(), "Cannot save file" , e1.getMessage()); + } catch (IOException e1) { + MessageDialog.openError(Display.getCurrent().getActiveShell(), "Cannot save file" , e1.getMessage()); + } catch (DatabaseException e1) { + MessageDialog.openError(Display.getCurrent().getActiveShell(), "Cannot save file" , e1.getMessage()); + } + } + } + + private ListViewer traversedViewer; + private ListViewer testedViewer; + private ListViewer strongViewer; + private ListViewer nonTraversedViewer; + private ListViewer nonTestedViewer; + @Override protected Control createDialogArea(Composite parent) { Composite composite = (Composite) super.createDialogArea(parent); @@ -411,27 +466,27 @@ public class GraphComparatorViewer extends ViewPart{ Group group = new Group(composite, SWT.NONE); group.setText("Traversed"); group.setLayout(new FillLayout()); - ListViewer traversedViewer = new ListViewer(group); + traversedViewer = new ListViewer(group); group = new Group(composite, SWT.NONE); group.setText("Tested"); group.setLayout(new FillLayout()); - ListViewer testedViewer = new ListViewer(group); + testedViewer = new ListViewer(group); group = new Group(composite, SWT.NONE); group.setText("Strong"); group.setLayout(new FillLayout()); - ListViewer strongViewer = new ListViewer(group); + strongViewer = new ListViewer(group); group = new Group(composite, SWT.NONE); group.setText("NonTraversed"); group.setLayout(new FillLayout()); - ListViewer nonTraversedViewer = new ListViewer(group); + nonTraversedViewer = new ListViewer(group); group = new Group(composite, SWT.NONE); group.setText("NonTested"); group.setLayout(new FillLayout()); - ListViewer nonTestedViewer = new ListViewer(group); + nonTestedViewer = new ListViewer(group); traversedViewer.setContentProvider(new ResourceListContentProvider()); traversedViewer.setLabelProvider(new ResourceLabelProvider()); @@ -640,4 +695,336 @@ public class GraphComparatorViewer extends ViewPart{ } + private boolean saveConf(String filename) throws IOException, DatabaseException { + File file = new File(filename); + if (file.exists()) { + if(!MessageDialog.openQuestion(Display.getCurrent().getActiveShell(), "Saving compare configuration", "Do you want to overwrite file'" + filename + "'?")) + return false; + } else { + if(!file.createNewFile()) + return false; + } + final PrintStream ps = new PrintStream(file); + ps.println(""); + ps.println(""); + session.syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + for (Resource r : traversed) { + ps.println(""); + } + for (Resource r : tested) { + ps.println(""); + } + for (Resource r : strong) { + ps.println(""); + } + for (Resource r : nonTraversed) { + ps.println(""); + } + for (Resource r : nonTested) { + ps.println(""); + } + } + }); + + ps.println(""); + ps.flush(); + ps.close(); + return true; + } + + private void load(String filename) throws Exception { + final ImportListener listener = new ImportListener(); + XMLParser parser = new XMLParser(listener); + parser.parse(filename); + + if (listener.getNonTested().size() + + listener.getNonTraversed().size() + + listener.getStrong().size() + + listener.getTested().size() + + listener.getTraversed().size() == 0) + return; + session.syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + tested.clear(); + traversed.clear(); + strong.clear(); + nonTested.clear(); + nonTraversed.clear(); + for (String uri : listener.getTested()) { + tested.add(graph.getResource(uri)); + } + for (String uri : listener.getTraversed()) { + traversed.add(graph.getResource(uri)); + } + for (String uri : listener.getStrong()) { + strong.add(graph.getResource(uri)); + } + + for (String uri : listener.getNonTested()) { + nonTested.add(graph.getResource(uri)); + } + for (String uri : listener.getNonTraversed()) { + nonTraversed.add(graph.getResource(uri)); + } + + } + }); + + + + } + + public static class ImportListener { + + + private Collection traversed = new ArrayList(); + private Collection tested = new ArrayList(); + private Collection strong = new ArrayList(); + private Collection nonTraversed = new ArrayList(); + private Collection nonTested = new ArrayList(); + + + public void done() { + + } + + + public void addTraversed(String uri) { + traversed.add(uri); + } + + public void addTested(String uri) { + tested.add(uri); + } + + public void addStrong(String uri) { + strong.add(uri); + } + + public void addNonTraversed(String uri) { + nonTraversed.add(uri); + } + + public void addNonTested(String uri) { + nonTested.add(uri); + } + + public Collection getTraversed() { + return traversed; + } + + public Collection getNonTested() { + return nonTested; + } + + public Collection getNonTraversed() { + return nonTraversed; + } + + public Collection getStrong() { + return strong; + } + + public Collection getTested() { + return tested; + } + + } + + + + public static class XMLParser { + + private ImportListener listener; + + public XMLParser(ImportListener listener) { + this.listener = listener; + } + + public void parse(String filename) throws Exception{ + ParserBean p = new ParserBean(); + p.parse(filename); + } + + private void handleElement(Stack parents, Element element) throws SAXException{ + String name = element.qName; + if(name.equals("traversed")) { + for (Attribute a : element.attributes) { + String propertyName = a.localName; + String value = a.value; + if (propertyName.equals("uri")) { + listener.addTraversed(value); + + } + } + } else if(name.equals("tested")) { + for (Attribute a : element.attributes) { + String propertyName = a.localName; + String value = a.value; + if (propertyName.equals("uri")) { + listener.addTested(value); + + } + } + } else if(name.equals("strong")) { + for (Attribute a : element.attributes) { + String propertyName = a.localName; + String value = a.value; + if (propertyName.equals("uri")) { + listener.addStrong(value); + + } + } + } else if(name.equals("nontraversed")) { + for (Attribute a : element.attributes) { + String propertyName = a.localName; + String value = a.value; + if (propertyName.equals("uri")) { + listener.addNonTraversed(value); + + } + } + } else if(name.equals("nontested")) { + for (Attribute a : element.attributes) { + String propertyName = a.localName; + String value = a.value; + if (propertyName.equals("uri")) { + listener.addNonTested(value); + + } + } + } else if (name.equals("compare")) { + + } else { + throw new SAXException("Unknown element '" + name + "'. The file does not contain ExtIO configuration."); + + } + } + + + public class ParserBean extends DefaultHandler implements Serializable { + + private static final long serialVersionUID = -4995836637014958966L; + + private Stack current = new Stack(); + private Stack texts = new Stack(); + + public ParserBean() { + super(); + } + + public void parse(String filename) throws Exception { + SAXParserFactory spf = SAXParserFactory.newInstance(); + SAXParser saxParser = spf.newSAXParser(); + + XMLReader reader = saxParser.getXMLReader(); + FileReader file = new FileReader(filename); + reader.setContentHandler(this); + reader.parse(new InputSource(file)); + listener.done(); + } + + @Override + public void startElement(String uri, String localName, String name, + Attributes attributes) throws SAXException { + current.push(new Element(uri,localName,name,attributes)); + texts.push(new String()); + } + + @Override + public void endElement(String uri, String localName, String name) + throws SAXException { + Element e = null; + String text = null; + if (!current.empty()) { + e = current.pop(); + text = texts.pop(); + } + if (e != null) { + e.setValue(text.trim()); + } + if (e != null) { + handleElement(current,e); + } + } + + @Override + public void characters(char[] ch, int start, int length) + throws SAXException { + if (!current.empty()) { + + String text = texts.pop(); + + String value = new String( + ch, start, length); + text += value; + texts.push(text); + } + } + } + + public class Attribute { + public String localName; + public String qName; + public String uri; + public String value; + + public Attribute(String localName, String qName, String uri, String value) { + this.localName = localName; + this.qName = qName; + this.uri = uri; + this.value = value; + } + } + + public class Element implements Serializable { + private static final long serialVersionUID = -5207502156942818875L; + String uri; + String localName; + String qName; + String value = null; + List attributes = new ArrayList(); + + public Element(String uri, String localName, String qName, Attributes attributes) { + this.uri = uri; + this.localName = localName; + this.qName = qName; + for (int i = 0; i < attributes.getLength(); i++) { + this.attributes.add(new Attribute(attributes.getLocalName(i),attributes.getQName(i),attributes.getURI(i),attributes.getValue(i))); + } + } + + public String getUri() { + return uri; + } + + public String getLocalName() { + return localName; + } + + public String getQName() { + return qName; + } + + public String getValue() { + return value; + } + + public List getAttributes() { + return attributes; + } + + public void setValue(String value) { + this.value = value; + } + + } + + } + }