1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * Foster Wheeler Energia Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.interop.test;
\r
14 import java.io.IOException;
\r
15 import java.util.HashMap;
\r
16 import java.util.Map;
\r
18 import org.eclipse.jface.layout.GridDataFactory;
\r
19 import org.eclipse.swt.SWT;
\r
20 import org.eclipse.swt.dnd.DND;
\r
21 import org.eclipse.swt.dnd.DropTarget;
\r
22 import org.eclipse.swt.dnd.DropTargetAdapter;
\r
23 import org.eclipse.swt.dnd.DropTargetEvent;
\r
24 import org.eclipse.swt.dnd.TextTransfer;
\r
25 import org.eclipse.swt.dnd.Transfer;
\r
26 import org.eclipse.swt.events.SelectionAdapter;
\r
27 import org.eclipse.swt.events.SelectionEvent;
\r
28 import org.eclipse.swt.layout.GridData;
\r
29 import org.eclipse.swt.layout.GridLayout;
\r
30 import org.eclipse.swt.widgets.Button;
\r
31 import org.eclipse.swt.widgets.Composite;
\r
32 import org.eclipse.swt.widgets.Display;
\r
33 import org.eclipse.swt.widgets.Label;
\r
34 import org.eclipse.swt.widgets.Text;
\r
35 import org.eclipse.ui.part.ViewPart;
\r
36 import org.simantics.db.ReadGraph;
\r
37 import org.simantics.db.Resource;
\r
38 import org.simantics.db.Session;
\r
39 import org.simantics.db.Statement;
\r
40 import org.simantics.db.common.ResourceArray;
\r
41 import org.simantics.db.common.request.ReadRequest;
\r
42 import org.simantics.db.exception.DatabaseException;
\r
43 import org.simantics.db.exception.InvalidResourceReferenceException;
\r
44 import org.simantics.db.exception.ServiceException;
\r
45 import org.simantics.db.exception.ValidationException;
\r
46 import org.simantics.db.request.Read;
\r
47 import org.simantics.db.service.SerialisationSupport;
\r
48 import org.simantics.layer0.utils.direct.GraphUtils;
\r
49 import org.simantics.ui.SimanticsUI;
\r
50 import org.simantics.ui.dnd.LocalObjectTransfer;
\r
51 import org.simantics.ui.dnd.ResourceReferenceTransfer;
\r
52 import org.simantics.ui.dnd.ResourceTransferUtils;
\r
53 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
54 import org.simantics.utils.datastructures.BijectionMap;
\r
57 * Simple multi line text viewer for seeing differences in two subgraphs.
\r
59 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
62 public class GraphComparatorViewer extends ViewPart{
\r
64 private Session session;
\r
66 private Composite composite;
\r
68 private Label resourceText1;
\r
69 private Label resourceText2;
\r
74 private GraphComparator comparator = new GraphComparator();
\r
77 public void createPartControl(Composite parent) {
\r
78 composite = new Composite(parent, SWT.NONE);
\r
79 composite.setLayout(new GridLayout(2, false));
\r
81 session = SimanticsUI.getSession();
\r
83 Composite topComposite = new Composite(composite, SWT.BORDER);
\r
84 topComposite.setLayout(new GridLayout(3, false));
\r
85 text1 = new Text(composite, SWT.MULTI|SWT.V_SCROLL);
\r
86 text2 = new Text(composite, SWT.MULTI|SWT.V_SCROLL);
\r
88 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.TOP).grab(true, false).span(2, 1).applyTo(topComposite);
\r
89 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(text1);
\r
90 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(text2);
\r
92 resourceText1 = createDropLabel(topComposite);
\r
93 resourceText2 = createDropLabel(topComposite);
\r
95 Button button = new Button(topComposite, SWT.PUSH);
\r
96 button.setText("Compare");
\r
97 button.addSelectionListener(new SelectionAdapter() {
\r
99 public void widgetSelected(SelectionEvent e) {
\r
108 private void compare() {
\r
111 final Resource r1 = (Resource)resourceText1.getData();
\r
112 final Resource r2 = (Resource)resourceText2.getData();
\r
113 if (r1 == null || r2 == null) {
\r
115 text1.setText("Missing input!");
\r
117 text2.setText("Missing input!");
\r
121 session.asyncRequest(new ReadRequest() {
\r
124 public void run(final ReadGraph graph) throws DatabaseException {
\r
125 comparator.clearRels();
\r
126 comparator.addTraversed(graph.getBuiltins().ConsistsOf);
\r
127 comparator.addTested(graph.getBuiltins().IsWeaklyRelatedTo);
\r
128 comparator.test(graph, r1, r2);
\r
129 BijectionMap<Statement, Statement> map = comparator.getComparable();
\r
130 Map<Statement, Integer> indices = new HashMap<Statement, Integer>();
\r
131 final StringBuilder sb1 = new StringBuilder();
\r
133 for (Statement s : comparator.getChanges1()) {
\r
137 sub = GraphUtils.getReadableName(graph, s.getSubject());
\r
138 String pre = GraphUtils.getReadableName(graph, s.getPredicate());
\r
139 String obj = GraphUtils.getReadableName(graph, s.getObject());
\r
140 if (map.containsLeft(s)) {
\r
142 indices.put(s, index);
\r
143 sb1.append("["+index + "] ");
\r
145 sb1.append(sub + " - " + pre + " - " + obj);
\r
146 sb1.append(" ["+s.getSubject().getResourceId() + " " + s.getPredicate().getResourceId() + " " + s.getObject().getResourceId()+"]");
\r
148 } catch (ValidationException e) {
\r
149 e.printStackTrace();
\r
150 } catch (ServiceException e) {
\r
151 e.printStackTrace();
\r
155 final StringBuilder sb2 = new StringBuilder();
\r
156 for (Statement s : comparator.getChanges2()) {
\r
159 sub = GraphUtils.getReadableName(graph, s.getSubject());
\r
160 String pre = GraphUtils.getReadableName(graph, s.getPredicate());
\r
161 String obj = GraphUtils.getReadableName(graph, s.getObject());
\r
162 if (map.containsRight(s)) {
\r
163 index = indices.get(map.getLeft(s));
\r
164 sb2.append("["+index + "] ");
\r
166 sb2.append(sub + " - " + pre + " - " + obj);
\r
167 sb2.append(" ["+s.getSubject().getResourceId() + " " + s.getPredicate().getResourceId() + " " + s.getObject().getResourceId()+"]");
\r
169 } catch (ValidationException e) {
\r
170 e.printStackTrace();
\r
171 } catch (ServiceException e) {
\r
172 e.printStackTrace();
\r
176 Display.getDefault().asyncExec(new Runnable() {
\r
178 public void run() {
\r
179 text1.setText(sb1.toString());
\r
180 text2.setText(sb2.toString());
\r
189 public void setFocus() {
\r
190 composite.setFocus();
\r
194 public void dispose() {
\r
200 // copy-paste from GraphDebugger
\r
201 public Label createDropLabel(Composite parent) {
\r
202 final Label label = new Label(parent, SWT.BORDER);
\r
203 label.setAlignment(SWT.CENTER);
\r
204 label.setText("Drag a resource here to examine it in this debugger!");
\r
205 label.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
\r
206 GridData data = new GridData(SWT.FILL, SWT.TOP, true, false);
\r
207 data.heightHint = 16;
\r
208 label.setLayoutData(data);
\r
210 // Add resource id drop support to the drop-area.
\r
211 DropTarget dropTarget = new DropTarget(label, DND.DROP_LINK | DND.DROP_COPY);
\r
212 dropTarget.setTransfer(new Transfer[] { TextTransfer.getInstance(), ResourceReferenceTransfer.getInstance(), LocalObjectTransfer.getTransfer() });
\r
213 dropTarget.addDropListener(new DropTargetAdapter() {
\r
215 public void dragEnter(DropTargetEvent event) {
\r
216 event.detail = DND.DROP_LINK;
\r
217 //label.setBackground(green);
\r
221 public void dragLeave(DropTargetEvent event) {
\r
222 label.setBackground(null);
\r
226 public void drop(DropTargetEvent event) {
\r
227 label.setBackground(null);
\r
228 ResourceArray[] data = parseEventData(event);
\r
229 if (data == null || data.length != 1) {
\r
230 event.detail = DND.DROP_NONE;
\r
233 final ResourceArray array = data[0];
\r
234 final Resource r = array.resources[array.resources.length - 1];
\r
238 label.setText(session.syncRequest(new Read<String>() {
\r
240 public String perform(ReadGraph graph)
\r
241 throws DatabaseException {
\r
242 return GraphUtils.getReadableName(graph, r);
\r
245 } catch (DatabaseException e) {
\r
246 e.printStackTrace();
\r
250 private ResourceArray[] parseEventData(DropTargetEvent event) {
\r
251 //System.out.println("DATA: " + event.data);
\r
252 if (event.data instanceof String) {
\r
254 SerialisationSupport support = session.getService(SerialisationSupport.class);
\r
255 return ResourceTransferUtils.readStringTransferable(support.getResourceSerializer(), (String) event.data).toResourceArrayArray();
\r
256 } catch (IllegalArgumentException e) {
\r
257 e.printStackTrace();
\r
258 } catch (IOException e) {
\r
259 e.printStackTrace();
\r
260 } catch (InvalidResourceReferenceException e) {
\r
261 e.printStackTrace();
\r
264 ResourceArray[] ret = ResourceAdaptionUtils.toResourceArrays(event.data);
\r
265 if (ret.length > 0)
\r