From 0bb21a4011be0c5517c4d5b27fcda4a834f0a4c6 Mon Sep 17 00:00:00 2001 From: lempinen Date: Thu, 22 Jul 2010 08:13:57 +0000 Subject: [PATCH] git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16767 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../ui/properties/widgets/ReferenceTable.java | 311 ++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceTable.java diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceTable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceTable.java new file mode 100644 index 00000000..7bc4af88 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceTable.java @@ -0,0 +1,311 @@ +package org.simantics.sysdyn.ui.properties.widgets; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.jface.viewers.ArrayContentProvider; +import org.eclipse.jface.viewers.CellEditor; +import org.eclipse.jface.viewers.ColumnViewer; +import org.eclipse.jface.viewers.ComboBoxCellEditor; +import org.eclipse.jface.viewers.EditingSupport; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ITableLabelProvider; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.jface.viewers.TableViewerColumn; +import org.eclipse.jface.viewers.TextCellEditor; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.viewers.ViewerComparator; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Table; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.databoard.binding.java.StringBindingDefault; +import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.BindingException; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.DoesNotContainValueException; +import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; +import org.simantics.db.exception.NoSingleResultException; +import org.simantics.db.exception.ServiceException; +import org.simantics.db.management.ISessionContext; +import org.simantics.db.request.Read; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.ISelectionUtils; + +public class ReferenceTable implements Widget{ + + private Table table; + private TableViewer tableViewer; + private List references; + public static final String FIRSTCOLUMN = "FIRST COLUMN"; + public static final String SECONDCOLUMN = "SECOND COLUMN"; + public static final String[] PROPS = { FIRSTCOLUMN, SECONDCOLUMN }; + + public ReferenceTable(Composite parent, WidgetSupport support, int style) { + support.register(this); + + Composite base = new Composite(parent, style); + GridLayoutFactory.fillDefaults().applyTo(base); + GridDataFactory.fillDefaults().grab(true, true).applyTo(base); + + table = new Table(base, SWT.BORDER|SWT.SINGLE|SWT.FULL_SELECTION); + GridDataFactory.fillDefaults().grab(true, true).applyTo(table); + table.setHeaderVisible (true); + table.setLinesVisible(true); + table.getVerticalBar().setVisible(true); + + // Create the viewer and connect it to the view + tableViewer = new TableViewer (table); + + String[] titles = { FIRSTCOLUMN, SECONDCOLUMN}; + int[] bounds = { 100, 100, 100, 100 }; + for (int i = 0; i < titles.length; i++) { + TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.NONE); + column.getColumn().setText(titles[i]); + column.getColumn().setWidth(bounds[i]); + column.getColumn().setResizable(true); + column.getColumn().setMoveable(false); + // enable editing support + column.setEditingSupport(new ReferenceRowEditingSupport(tableViewer, i)); + } + + tableViewer.setContentProvider (new ArrayContentProvider()); + tableViewer.setLabelProvider (new ReferenceRowLabelProvider()); + tableViewer.setComparator(new ReferenceRowComparator()); + + references = new ArrayList(); + tableViewer.setInput(references); + } + + @Override + public void setInput(ISessionContext context, Object input) { + final Resource module = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); + ArrayList referenceRows = null; + try { + referenceRows = SimanticsUI.getSession().syncRequest(new Read>() { + + @Override + public ArrayList perform(ReadGraph graph) throws DatabaseException { + Builtins b = graph.getBuiltins(); + SysdynResource sr = SysdynResource.getInstance(graph); + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + ArrayList result = new ArrayList(); + Resource instanceOf = graph.getSingleObject(module, b.InstanceOf); + Resource configuration = graph.getSingleObject(instanceOf, sr2.IsDefinedBy); + for(Resource input : graph.syncRequest(new ObjectsWithType(configuration, b.ConsistsOf, sr.Input))) { + ReferenceRow rr = new ReferenceRow(graph, module, input); + result.add(rr); + } + return result; + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + tableViewer.setInput(referenceRows); + } + + public class ReferenceRow { + + Resource module; + Resource reference; + String name; + String refersTo; + + public ReferenceRow(ReadGraph graph, final Resource module, final Resource reference) throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException, ManyObjectsForFunctionalRelationException { + this.module = module; + this.reference = reference; + SysdynResource sr = SysdynResource.getInstance(graph); + Builtins b = graph.getBuiltins(); + setName((String)graph.getRelatedValue(reference, b.HasName, StringBindingDefault.INSTANCE)); + for(Resource dependency : graph.getObjects(module, sr.IsHeadOf)) { + Resource refersTo; + refersTo = graph.getPossibleObject(dependency, sr.RefersTo); + if(refersTo != null && refersTo.equals(getReference())) { + Resource output = graph.getSingleObject(dependency, sr.HasTail); + setRefersTo((String)graph.getRelatedValue(output, b.HasName, StringBindingDefault.INSTANCE)); + break; + } + } + } + + public Resource getModule() { + return this.module; + } + + public void setModule(Resource module) { + this.module = module; + } + + public Resource getReference() { + return reference; + } + public void setReference(Resource reference) { + this.reference = reference; + SimanticsUI.getSession().asyncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + Builtins b = graph.getBuiltins(); + setName((String)graph.getRelatedValue(getReference(), b.HasName, StringBindingDefault.INSTANCE)); + } + }); + } + public String getName() { + return name; + } + private void setName(String name) { + this.name = name; + } + public String getRefersTo() { + return refersTo; + } + public void setRefersTo(String refersTo) { + this.refersTo = refersTo; + } + } + + private class ReferenceRowLabelProvider extends LabelProvider implements ITableLabelProvider { + + @Override + public Image getColumnImage(Object element, int columnIndex) { + return null; + } + + @Override + public String getColumnText(Object element, int columnIndex) { + ReferenceRow referenceRow = (ReferenceRow) element; + switch (columnIndex) { + case 0: + return referenceRow.getName(); + case 1: + return referenceRow.getRefersTo(); + default: + throw new RuntimeException("Should not happen"); + } + } + + } + + private class ReferenceRowComparator extends ViewerComparator { + @Override + public int compare(Viewer viewer, Object e1, Object e2) { + return 0; + } + } + + public class ReferenceRowEditingSupport extends EditingSupport { + private CellEditor editor; + private int column; + private HashMap optionResources; + private String[] options; + + public ReferenceRowEditingSupport(ColumnViewer viewer, int column) { + super(viewer); + this.column = column; + } + + @Override + protected boolean canEdit(Object element) { + switch (this.column) { + case 0: return false; + default: return true; + } + } + + @Override + protected CellEditor getCellEditor(Object element) { + // Create the correct editor based on the column index + switch (column) { + case 0: + editor = new TextCellEditor(table); + case 1: + ReferenceRow row = (ReferenceRow)element; + final Resource module = row.getModule(); + final Resource reference = row.getReference(); + + optionResources = new HashMap(); + try { + optionResources = SimanticsUI.getSession().syncRequest(new Read>() { + + @Override + public HashMap perform(ReadGraph graph) throws DatabaseException { + Builtins b = graph.getBuiltins(); + SysdynResource sr = SysdynResource.getInstance(graph); + HashMap result = new HashMap(); + for(Resource dependency : graph.syncRequest(new ObjectsWithType(module, sr.IsHeadOf, sr.Dependency))) { + if(graph.getPossibleObject(dependency, sr.RefersTo) == null || + graph.getPossibleObject(dependency, sr.RefersTo).equals(reference)) { + Resource output = graph.getSingleObject(dependency, sr.HasTail); + result.put((String)graph.getRelatedValue(output, b.HasName), output); + } + } + return result; + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + options = optionResources.keySet().toArray(new String[optionResources.keySet().size() + 1]); + options[optionResources.keySet().size()] = ""; + editor = new ComboBoxCellEditor(table, options, SWT.READ_ONLY); + break; + default: + editor = null; + } + + return editor; + } + + @Override + protected Object getValue(Object element) { + ReferenceRow referenceRow = (ReferenceRow) element; + + switch (this.column) { + case 0: + return referenceRow.getName(); + case 1: + for(int i = 0; i < options.length ; i++) { + if(referenceRow.getRefersTo().equals(options[i])) return i; + } + return options[options.length - 1]; + default: + break; + } + return null; + } + + @Override + protected void setValue(Object element, Object value) { + ReferenceRow referenceRow = (ReferenceRow) element; + String valueString = String.valueOf(value); + switch (this.column) { + case 0: + referenceRow.setName(valueString); + break; + case 1: + referenceRow.setReference(optionResources.get(valueString)); + break; + default: + break; + } + + getViewer().update(element, null); + } + + } + +} -- 2.47.1