From 176c9a07805082054296461fc13d5ca8a222adfd Mon Sep 17 00:00:00 2001 From: luukkainen Date: Thu, 26 Jan 2012 16:17:55 +0000 Subject: [PATCH] refs #3169 git-svn-id: https://www.simantics.org/svn/simantics/interoperability/trunk@24062 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.interop/META-INF/MANIFEST.MF | 1 - org.simantics.interop/plugin.xml | 7 - .../interop/issues/AbstractIssue.java | 70 ------ .../org/simantics/interop/issues/AndRead.java | 30 --- .../simantics/interop/issues/BasicIssue.java | 26 --- .../simantics/interop/issues/GraphIssue.java | 76 ------- .../org/simantics/interop/issues/Issue.java | 38 ---- .../interop/issues/IssueListener.java | 22 -- .../interop/issues/IssueReporter.java | 107 --------- .../interop/issues/IssueReporterListener.java | 9 - .../simantics/interop/ui/IssuesViewPart.java | 166 -------------- .../interop/utils/LinkedBijectionMap.java | 212 ++++++++++++++++++ 12 files changed, 212 insertions(+), 552 deletions(-) delete mode 100644 org.simantics.interop/src/org/simantics/interop/issues/AbstractIssue.java delete mode 100644 org.simantics.interop/src/org/simantics/interop/issues/AndRead.java delete mode 100644 org.simantics.interop/src/org/simantics/interop/issues/BasicIssue.java delete mode 100644 org.simantics.interop/src/org/simantics/interop/issues/GraphIssue.java delete mode 100644 org.simantics.interop/src/org/simantics/interop/issues/Issue.java delete mode 100644 org.simantics.interop/src/org/simantics/interop/issues/IssueListener.java delete mode 100644 org.simantics.interop/src/org/simantics/interop/issues/IssueReporter.java delete mode 100644 org.simantics.interop/src/org/simantics/interop/issues/IssueReporterListener.java delete mode 100644 org.simantics.interop/src/org/simantics/interop/ui/IssuesViewPart.java create mode 100644 org.simantics.interop/src/org/simantics/interop/utils/LinkedBijectionMap.java diff --git a/org.simantics.interop/META-INF/MANIFEST.MF b/org.simantics.interop/META-INF/MANIFEST.MF index cc325d7..677e9e4 100644 --- a/org.simantics.interop/META-INF/MANIFEST.MF +++ b/org.simantics.interop/META-INF/MANIFEST.MF @@ -25,7 +25,6 @@ Require-Bundle: org.eclipse.ui, Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Bundle-ActivationPolicy: lazy Export-Package: org.simantics.interop.browsing, - org.simantics.interop.issues, org.simantics.interop.stubs, org.simantics.interop.test, org.simantics.interop.utils diff --git a/org.simantics.interop/plugin.xml b/org.simantics.interop/plugin.xml index 729df96..17ac122 100644 --- a/org.simantics.interop/plugin.xml +++ b/org.simantics.interop/plugin.xml @@ -24,13 +24,6 @@ name="Comparator" restorable="true"> - - diff --git a/org.simantics.interop/src/org/simantics/interop/issues/AbstractIssue.java b/org.simantics.interop/src/org/simantics/interop/issues/AbstractIssue.java deleted file mode 100644 index 01ebb02..0000000 --- a/org.simantics.interop/src/org/simantics/interop/issues/AbstractIssue.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.simantics.interop.issues; - -import java.util.ArrayList; -import java.util.List; - -import org.eclipse.swt.widgets.Display; - -public abstract class AbstractIssue implements Issue{ - - private String description; - private List listeners = new ArrayList(); - - @Override - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - fireUpdated(); - } - - protected void fireUpdated() { - Display.getDefault().asyncExec(new Runnable() { - @Override - public void run() { - synchronized (listeners) { - for (IssueListener l : listeners) { - l.updated(AbstractIssue.this); - } - } - - } - }); - } - - protected void fireDisposed() { - Display.getDefault().asyncExec(new Runnable() { - @Override - public void run() { - List list = new ArrayList(); - list.addAll(listeners); - for (IssueListener l : list) { - l.disposed(AbstractIssue.this); - } - listeners.clear(); - } - }); - } - - @Override - public void addListener(IssueListener listener) { - synchronized(listeners) { - listeners.add(listener); - } - } - - @Override - public void removeListener(IssueListener listener) { - synchronized(listeners) { - listeners.remove(listener); - } - } - - @Override - public void dispose() { - fireDisposed(); - } - -} diff --git a/org.simantics.interop/src/org/simantics/interop/issues/AndRead.java b/org.simantics.interop/src/org/simantics/interop/issues/AndRead.java deleted file mode 100644 index e8a28c0..0000000 --- a/org.simantics.interop/src/org/simantics/interop/issues/AndRead.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.simantics.interop.issues; - -import org.simantics.db.ReadGraph; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.request.Read; - -/** - * AndRead can be used to combine multiple requests into single one. If all reads return true or null, this returns true. - * @author Marko Luukkainen - * - */ -public class AndRead implements Read{ - - private Read reads[]; - - public AndRead(Read... reads) { - this.reads = reads; - } - - @Override - public Boolean perform(ReadGraph graph) throws DatabaseException { - Boolean b = null; - for (Read r : reads) { - b = r.perform(graph); - if(b != null && !b) - return false; - } - return true; - } -} diff --git a/org.simantics.interop/src/org/simantics/interop/issues/BasicIssue.java b/org.simantics.interop/src/org/simantics/interop/issues/BasicIssue.java deleted file mode 100644 index 12020ab..0000000 --- a/org.simantics.interop/src/org/simantics/interop/issues/BasicIssue.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.simantics.interop.issues; - -/** - * Basic, static issue. - * @author Marko Luukkainen - * - */ -public class BasicIssue extends AbstractIssue { - - - public BasicIssue(String description) { - setDescription(description); - } - - @Override - public void showInEditor() { - - } - - @Override - public boolean supportsEditor() { - return false; - } - - -} diff --git a/org.simantics.interop/src/org/simantics/interop/issues/GraphIssue.java b/org.simantics.interop/src/org/simantics/interop/issues/GraphIssue.java deleted file mode 100644 index 2a37603..0000000 --- a/org.simantics.interop/src/org/simantics/interop/issues/GraphIssue.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.simantics.interop.issues; - -import org.simantics.db.AsyncReadGraph; -import org.simantics.db.ReadGraph; -import org.simantics.db.procedure.AsyncListener; -import org.simantics.db.request.Read; -import org.simantics.utils.ui.ErrorLogger; - -/** - * An issue whose state can be updated with an query - * @author Marko Luukkainen - * - */ -public class GraphIssue extends AbstractIssue { - - private boolean disposed = false; - - /** - * Creates an new Graph Issue - * @param g access to graph - * @param request Request that returns true if issue is still valid. If the request returns false, the issue is disposed. - * @param description - */ - public GraphIssue(ReadGraph g, Read request, String description) { - setDescription(description); - IssueReporter.getInstance().addIssue(this); - setRequest(g, request); - } - - public GraphIssue(String description) { - setDescription(description); - IssueReporter.getInstance().addIssue(this); - } - - public void setRequest(ReadGraph g, Read request) { - g.asyncRequest(request,new AsyncListener() { - @Override - public void execute(AsyncReadGraph graph, Boolean result) { - if (result == null) - return; - - if (!result) { - dispose(); - } - } - - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - ErrorLogger.defaultLogError("Error in GraphIssue handling, disposing issue \"" + getDescription()+"\"", throwable); - dispose(); - } - - @Override - public boolean isDisposed() { - return disposed; - } - }); - } - - @Override - public void showInEditor() { - - } - - @Override - public boolean supportsEditor() { - return false; - } - - @Override - public void dispose() { - this.disposed = true; - super.dispose(); - } - -} diff --git a/org.simantics.interop/src/org/simantics/interop/issues/Issue.java b/org.simantics.interop/src/org/simantics/interop/issues/Issue.java deleted file mode 100644 index 8d7a072..0000000 --- a/org.simantics.interop/src/org/simantics/interop/issues/Issue.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.simantics.interop.issues; - -/** - * Interface for an issue - * - * TODO: grouping, severity? - * - * @author Marko Luukkainen - * - */ -public interface Issue { - - /** - * Prints description - * @return - */ - public String getDescription(); - - /** - * Returns true if this issue can be highlighted in an editor - * @return - */ - public boolean supportsEditor(); - - /** - * Opens and highlights the reason of the issue in an editor - */ - public void showInEditor(); - - /** - * Disposes the issue. - */ - public void dispose(); - - public void addListener(IssueListener listenr); - public void removeListener(IssueListener listener); - -} diff --git a/org.simantics.interop/src/org/simantics/interop/issues/IssueListener.java b/org.simantics.interop/src/org/simantics/interop/issues/IssueListener.java deleted file mode 100644 index 0750afd..0000000 --- a/org.simantics.interop/src/org/simantics/interop/issues/IssueListener.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.simantics.interop.issues; - -/** - * Interface for listening changes in issues. - * @author Marko Luukkainen - * - */ -public interface IssueListener { - - /** - * Issues is updated - * @param issue - */ - public void updated(Issue issue); - - /** - * Issue is disposed (not valid anymore); - * @param issue - */ - public void disposed(Issue issue); - -} diff --git a/org.simantics.interop/src/org/simantics/interop/issues/IssueReporter.java b/org.simantics.interop/src/org/simantics/interop/issues/IssueReporter.java deleted file mode 100644 index f1ab4a7..0000000 --- a/org.simantics.interop/src/org/simantics/interop/issues/IssueReporter.java +++ /dev/null @@ -1,107 +0,0 @@ -package org.simantics.interop.issues; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import org.eclipse.swt.widgets.Display; - -/** - * IssueReporter is singleton class that handles collecting issues. - * - * TODO: how to store issues when the application is closed? Memento-based? - * - * @author Marko Luukkainen - * - */ -public class IssueReporter implements IssueListener{ - - private static IssueReporter INSTANCE; - - private List issues = new ArrayList(); - private List listeners = new ArrayList(); - - - private IssueReporter() { - - } - - public static IssueReporter getInstance() { - if (INSTANCE == null) - INSTANCE = new IssueReporter(); - return INSTANCE; - } - - /** - * Retuns list of issues. - * @return - */ - public List getIssues() { - return Collections.unmodifiableList(issues); - } - - public void addIssue(Issue issue) { - issues.add(issue); - issue.addListener(this); - fireAdded(issue); - } - - public void removeIssue(Issue issue) { - issue.removeListener(this); - issues.remove(issue); - fireRemoved(issue); - } - - @Override - public void updated(Issue issue) { - fireUpdated(issue); - } - - @Override - public void disposed(Issue issue) { - issue.removeListener(this); - issues.remove(issue); - fireRemoved(issue); - } - - - public void addListener(IssueReporterListener listener) { - listeners.add(listener); - } - - public void removeListener(IssueReporterListener listener) { - listeners.remove(listener); - } - - protected void fireAdded(final Issue issue) { - Display.getDefault().asyncExec(new Runnable() { - @Override - public void run() { - for (IssueReporterListener l : listeners) - l.added(issue); - } - }); - } - - protected void fireRemoved(final Issue issue) { - Display.getDefault().asyncExec(new Runnable() { - @Override - public void run() { - for (IssueReporterListener l : listeners) - l.removed(issue); - } - }); - } - - protected void fireUpdated(final Issue issue) { - Display.getDefault().asyncExec(new Runnable() { - @Override - public void run() { - for (IssueReporterListener l : listeners) - l.updated(issue); - } - }); - } - - -} diff --git a/org.simantics.interop/src/org/simantics/interop/issues/IssueReporterListener.java b/org.simantics.interop/src/org/simantics/interop/issues/IssueReporterListener.java deleted file mode 100644 index 8b15040..0000000 --- a/org.simantics.interop/src/org/simantics/interop/issues/IssueReporterListener.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.simantics.interop.issues; - -public interface IssueReporterListener { - - public void added(Issue issue); - public void removed(Issue issue); - public void updated(Issue issue); - -} diff --git a/org.simantics.interop/src/org/simantics/interop/ui/IssuesViewPart.java b/org.simantics.interop/src/org/simantics/interop/ui/IssuesViewPart.java deleted file mode 100644 index a481623..0000000 --- a/org.simantics.interop/src/org/simantics/interop/ui/IssuesViewPart.java +++ /dev/null @@ -1,166 +0,0 @@ -package org.simantics.interop.ui; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.eclipse.jface.viewers.ILabelProviderListener; -import org.eclipse.jface.viewers.IStructuredContentProvider; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.jface.viewers.ITableLabelProvider; -import org.eclipse.jface.viewers.TableViewer; -import org.eclipse.jface.viewers.Viewer; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.SelectionAdapter; -import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.graphics.Image; -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.ui.part.ViewPart; -import org.simantics.interop.issues.Issue; -import org.simantics.interop.issues.IssueReporter; -import org.simantics.interop.issues.IssueReporterListener; - -public class IssuesViewPart extends ViewPart { - - private Composite parent; - private TableViewer viewer; - - - public IssuesViewPart() { - // TODO Auto-generated constructor stub - } - - @Override - public void createPartControl(Composite parent) { - this.parent = parent; - this.parent.setLayout(new GridLayout(1,false)); - viewer = new TableViewer(parent); - viewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); - viewer.setContentProvider(new IssueContentProvider()); - viewer.setLabelProvider(new IssueLabelProvider()); - - viewer.setInput(IssueReporter.getInstance().getIssues()); - - Composite buttonComposite = new Composite(parent, SWT.BORDER); - buttonComposite.setLayout(new FillLayout(SWT.HORIZONTAL)); - Button clearButton = new Button(buttonComposite, SWT.PUSH); - clearButton.setText("Clear all"); - clearButton.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - Collection issues = IssueReporter.getInstance().getIssues(); - for (Issue i : issues) - i.dispose(); - } - }); - - Button showButton = new Button(buttonComposite, SWT.PUSH); - showButton.setText("Show in editor"); - showButton.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - IStructuredSelection sel = (IStructuredSelection)viewer.getSelection(); - if (sel.isEmpty()) - return; - Issue i = (Issue)sel.getFirstElement(); - if (i.supportsEditor()) - i.showInEditor(); - } - }); - - - } - - @Override - public void setFocus() { - parent.setFocus(); - } - - - private class IssueContentProvider implements IStructuredContentProvider { - private IssueReporterListener listener; - private Viewer viewer; - - public IssueContentProvider() { - listener = new IssueReporterListener() { - - @Override - public void updated(Issue issue) { - viewer.refresh(); - } - - @Override - public void removed(Issue issue) { - viewer.setInput(IssueReporter.getInstance().getIssues()); - } - - @Override - public void added(Issue issue) { - viewer.setInput(IssueReporter.getInstance().getIssues()); - } - }; - IssueReporter.getInstance().addListener(listener); - } - - - @SuppressWarnings("unchecked") - @Override - public Object[] getElements(Object inputElement) { - Collection issues = (Collection)inputElement; - return issues.toArray(); - } - - @Override - public void dispose() { - IssueReporter.getInstance().removeListener(listener); - } - - @Override - public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { - this.viewer = viewer; - } - } - - private class IssueLabelProvider implements ITableLabelProvider { - - private List listeners = new ArrayList(); - - @Override - public Image getColumnImage(Object element, int columnIndex) { - return null; - } - - @Override - public String getColumnText(Object element, int columnIndex) { - Issue issue = (Issue)element; - if (columnIndex == 0) - return issue.getDescription(); - return null; - } - - @Override - public boolean isLabelProperty(Object element, String property) { - return true; - } - - @Override - public void addListener(ILabelProviderListener listener) { - listeners.add(listener); - } - - @Override - public void removeListener(ILabelProviderListener listener) { - listeners.remove(listener); - } - - @Override - public void dispose() { - - } - } - -} diff --git a/org.simantics.interop/src/org/simantics/interop/utils/LinkedBijectionMap.java b/org.simantics.interop/src/org/simantics/interop/utils/LinkedBijectionMap.java new file mode 100644 index 0000000..cfe6997 --- /dev/null +++ b/org.simantics.interop/src/org/simantics/interop/utils/LinkedBijectionMap.java @@ -0,0 +1,212 @@ +package org.simantics.interop.utils; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import java.util.Map.Entry; + +import org.simantics.utils.datastructures.BijectionMap; + +public class LinkedBijectionMap { + /** The keys of tableLeft are left-side-values and + * values are right-side-values */ + private final Map tableLeft = new LinkedHashMap(); + /** The keys of tableRight are right-side-values and + * values on it are left-side-values */ + private final Map tableRight = new LinkedHashMap(); + + /** The keys of tableLeft are left-side-values and + * values are right-side-values */ + private final Map imTableLeft = Collections.unmodifiableMap(tableLeft); + /** The keys of tableRight are right-side-values and + * values on it are left-side-values */ + private final Map imTableRight = Collections.unmodifiableMap(tableRight); + + private final Set imLeftSet = Collections.unmodifiableSet(tableLeft.keySet()); + private final Set imRightSet = Collections.unmodifiableSet(tableRight.keySet()); + + public LinkedBijectionMap() { + + } + + public LinkedBijectionMap(LinkedBijectionMap copyFrom) { + this.tableLeft.putAll(copyFrom.tableLeft); + this.tableRight.putAll(copyFrom.tableRight); + } + + public void addAll(BijectionMap map) + { + for (Entry e : map.getEntries()) + map(e.getKey(), e.getValue()); + } + + public boolean retainAllLeft(Collection values) + { + boolean result = false; + Collection remove = new ArrayList(size()); + for (L lValue : imLeftSet) { + if (!values.contains(lValue)) { + remove.add(lValue); + result = true; + } + } + if (!remove.isEmpty()) { + for (L lValue : remove) + removeWithLeft(lValue); + } + return result; + } + + public boolean retainAllRight(Collection values) + { + boolean result = false; + Collection remove = new ArrayList(size()); + for (R rValue : imRightSet) { + if (!values.contains(rValue)) { + remove.add(rValue); + result = true; + } + } + if (!remove.isEmpty()) { + for (R rValue : remove) + removeWithRight(rValue); + } + return result; + } + + public Set> getEntries() + { + return tableLeft.entrySet(); + } + + public boolean containsLeft(L leftValue) + { + return tableLeft.containsKey(leftValue); + } + + public boolean containsRight(R rightValue) + { + return tableRight.containsKey(rightValue); + } + + public boolean contains(L leftValue, R rightValue) + { + if (leftValue==null || rightValue==null) return false; + return rightValue.equals(tableLeft.get(leftValue)); + } + + public void map(L leftValue, R rightValue) + { + if (leftValue == null || rightValue == null) + throw new NullPointerException(); + // Remove possible old mapping + R oldRight = tableLeft.remove(leftValue); + if (oldRight != null) { + tableRight.remove(oldRight); + } else { + L oldLeft = tableRight.remove(rightValue); + if (oldLeft != null) { + tableLeft.remove(oldLeft); + } + } + + tableLeft.put(leftValue, rightValue); + tableRight.put(rightValue, leftValue); + } + + public boolean isEmpty() { + return tableLeft.isEmpty(); + } + + public int size() + { + return tableLeft.size(); + } + + public L getLeft(R rightValue) { + return tableRight.get(rightValue); + } + + public R getRight(L leftValue) { + return tableLeft.get(leftValue); + } + + public R removeWithLeft(L leftValue) { + R rightValue = tableLeft.remove(leftValue); + if (rightValue!=null) + tableRight.remove(rightValue); + return rightValue; + } + + public L removeWithRight(R rightValue) { + L leftValue = tableRight.remove(rightValue); + if (leftValue!=null) + tableLeft.remove(leftValue); + return leftValue; + } + + /** + * Get set of left values + * + * @return read-only set + */ + public Set getLeftSet() { + return imLeftSet; + } + + /** + * Get set of right values + * + * @return read-only set + */ + public Set getRightSet() { + return imRightSet; + } + + /** + * Get left-to-right map + * + * @return read only map + */ + public Map getLeftToRightMap() { + return imTableLeft; + } + + /** + * Get right-to-left map + * + * @return read only map + */ + public Map getRightToLeftMap() { + return imTableRight; + } + + public void clear() { + tableLeft.clear(); + tableRight.clear(); + } + + @Override + public String toString() { + int count = 0; + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (Entry e : tableLeft.entrySet()) + { + if (count++>0) sb.append(", "); + sb.append(e.getKey().toString()); + sb.append("="); + sb.append(e.getValue().toString()); + } + sb.append("]"); + return sb.toString(); + } + + @Override + public LinkedBijectionMap clone() { + return new LinkedBijectionMap(this); + } +} -- 2.47.1