1 package org.simantics.plant3d.property;
3 import java.util.ArrayList;
4 import java.util.Collection;
7 import org.eclipse.jface.viewers.ISelection;
8 import org.eclipse.swt.widgets.Composite;
9 import org.eclipse.ui.IWorkbenchSite;
10 import org.simantics.Simantics;
11 import org.simantics.browsing.ui.swt.PartNameListener;
12 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.common.utils.NameUtils;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.management.ISessionContext;
18 import org.simantics.db.request.Read;
19 import org.simantics.g3d.property.PropertyTabContributor;
20 import org.simantics.g3d.property.PropertyTabUtil;
21 import org.simantics.g3d.scenegraph.IG3DNode;
22 import org.simantics.g3d.tools.AdaptationUtils;
23 import org.simantics.g3d.vtk.property.VTKPropertyTabContributor;
24 import org.simantics.objmap.structural.StructuralResource;
25 import org.simantics.selectionview.BasicPropertyTab;
26 import org.simantics.selectionview.ComparableTabContributor;
27 import org.simantics.selectionview.PropertyTabContributorImpl;
28 import org.simantics.selectionview.SelectionProcessor;
29 import org.simantics.utils.datastructures.Callback;
33 public class P3DSelectionProcessor implements SelectionProcessor<Object, ReadGraph> {
35 private static final boolean DEBUG = false;
37 public Collection<?> process(Object selection, ReadGraph backend) {
38 // System.out.println(getClass().getSimpleName() + " incoming selection: " + ObjectUtils.toString(selection));
41 Collection<ComparableTabContributor> result = new ArrayList<ComparableTabContributor>();
42 Collection<Resource> resourceCollection = AdaptationUtils.adaptToCollection(selection, Resource.class);
43 Collection<StructuralResource> structuralResourceCollection = AdaptationUtils.adaptToCollection(selection, StructuralResource.class);
44 Collection<vtkProp> propCollection = AdaptationUtils.adaptToCollection(selection, vtkProp.class);
45 Collection<IG3DNode> nodeCollection = AdaptationUtils.adaptToCollection(selection, IG3DNode.class);
46 boolean readOnly = false;
47 if (resourceCollection.size() == 0 && structuralResourceCollection.size() > 0) {
48 for (StructuralResource sr : structuralResourceCollection) {
49 if (sr.isStructural() && !sr.isStructuralRoot())
51 resourceCollection.add(sr.getResource());
56 if (nodeCollection.size() == 1) {
57 IG3DNode node = nodeCollection.iterator().next();
58 List<PropertyTabContributor> contributors = PropertyTabUtil.getContributors(node);
60 for (PropertyTabContributor c : contributors) {
61 result.add(new ComparableTabContributor(c, i--, node, c.getId()));
66 if (propCollection.size() == 1) {
67 vtkProp prop = propCollection.iterator().next();
69 throw new NullPointerException();
70 result.add(new ComparableTabContributor(new VTKPropertyTabContributor(), -2, prop, "VTK"));
73 if (resourceCollection.size() > 0) {
74 if (resourceCollection.size() > 1)
75 result.add(new ComparableTabContributor(new MultiSelectionTabContibutor(),0, resourceCollection, "Graph"));
76 else if (resourceCollection.size() == 1){
78 Resource r = resourceCollection.iterator().next();
79 result.add(new ComparableTabContributor(new P3DBasicPropertyTab(!readOnly), 0, r, "Graph"));
81 } catch (Exception e) {
88 if(result.size() == 0) {
89 result.add(new ComparableTabContributor(new NoneSelectionTabContributor(),0, resourceCollection, "Empty"));
95 public class P3DBasicPropertyTab extends BasicPropertyTab {
98 public P3DBasicPropertyTab(boolean enabled) {
99 this.enabled = enabled;
101 public void updatePartName(ISelection forSelection, Callback<String> updateCallback) {
102 Read<String> read = getPartNameReadRequest(forSelection);
104 updateCallback.run("Override to control part name (PropertyTabContributorImpl.updatePartName)");
106 Simantics.getSession().asyncRequest(read, new PartNameListener(updateCallback));
109 public Read<String> getPartNameReadRequest(ISelection forSelection) {
110 final Resource r = AdaptationUtils.adaptToSingle(forSelection, Resource.class);
113 return new Read<String>() {
115 public String perform(ReadGraph graph) throws DatabaseException {
116 return NameUtils.getSafeName(graph, r);
122 public void createControls(Composite body, IWorkbenchSite site,
123 ISessionContext context, WidgetSupport support) {
124 // TODO Auto-generated method stub
125 super.createControls(body, site, context, support);
126 ((Composite)parameterExplorer.getExplorerControl()).setEnabled(enabled);
132 public class MultiSelectionTabContibutor extends PropertyTabContributorImpl {
133 public void createControls(org.eclipse.swt.widgets.Composite body, org.eclipse.ui.IWorkbenchSite site, org.simantics.db.management.ISessionContext context, org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport support) {
134 //Composite composite = new Composite(body, SWT.NONE);
137 public Read<String> getPartNameReadRequest(ISelection forSelection) {
138 final Collection<Resource> coll = AdaptationUtils.adaptToCollection(forSelection, Resource.class);
139 if (coll.size() == 0)
141 return new Read<String>() {
143 public String perform(ReadGraph graph) throws DatabaseException {
145 for (Resource r : coll) {
146 title += NameUtils.getSafeName(graph, r) +",";
148 System.out.println(title);
149 return title.substring(0,title.length()-1);
155 public class NoneSelectionTabContributor extends PropertyTabContributorImpl {
157 public void createControls(Composite body, IWorkbenchSite site,
158 ISessionContext context, WidgetSupport support) {
159 //Composite composite = new Composite(body, SWT.NONE);
163 public void updatePartName(ISelection forSelection, Callback<String> updateCallback) {
164 updateCallback.run("No Selection");