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;
30 import org.simantics.utils.threads.SWTThread;
34 public class P3DSelectionProcessor implements SelectionProcessor<Object, ReadGraph> {
36 private static final boolean DEBUG = false;
38 public Collection<?> process(Object selection, ReadGraph backend) {
39 // System.out.println(getClass().getSimpleName() + " incoming selection: " + ObjectUtils.toString(selection));
42 Collection<ComparableTabContributor> result = new ArrayList<ComparableTabContributor>();
43 Collection<Resource> resourceCollection = AdaptationUtils.adaptToCollection(selection, Resource.class);
44 Collection<StructuralResource> structuralResourceCollection = AdaptationUtils.adaptToCollection(selection, StructuralResource.class);
45 Collection<vtkProp> propCollection = AdaptationUtils.adaptToCollection(selection, vtkProp.class);
46 Collection<IG3DNode> nodeCollection = AdaptationUtils.adaptToCollection(selection, IG3DNode.class);
47 boolean readOnly = false;
48 if (resourceCollection.size() == 0 && structuralResourceCollection.size() > 0) {
49 for (StructuralResource sr : structuralResourceCollection) {
50 if (sr.isStructural() && !sr.isStructuralRoot())
52 resourceCollection.add(sr.getResource());
57 if (nodeCollection.size() == 1) {
58 IG3DNode node = nodeCollection.iterator().next();
59 List<PropertyTabContributor> contributors = PropertyTabUtil.getContributors(node);
61 for (PropertyTabContributor c : contributors) {
62 result.add(new ComparableTabContributor(c, i--, node, c.getId()));
67 if (propCollection.size() == 1) {
68 vtkProp prop = propCollection.iterator().next();
70 throw new NullPointerException();
71 result.add(new ComparableTabContributor(new VTKPropertyTabContributor(SWTThread.getThreadAccess()), -2, prop, "VTK"));
74 if (resourceCollection.size() > 0) {
75 if (resourceCollection.size() > 1)
76 result.add(new ComparableTabContributor(new MultiSelectionTabContibutor(),0, resourceCollection, "Graph"));
77 else if (resourceCollection.size() == 1){
79 Resource r = resourceCollection.iterator().next();
80 result.add(new ComparableTabContributor(new P3DBasicPropertyTab(!readOnly), 0, r, "Graph"));
82 } catch (Exception e) {
89 if(result.size() == 0) {
90 result.add(new ComparableTabContributor(new NoneSelectionTabContributor(),0, resourceCollection, "Empty"));
96 public class P3DBasicPropertyTab extends BasicPropertyTab {
99 public P3DBasicPropertyTab(boolean enabled) {
100 this.enabled = enabled;
102 public void updatePartName(ISelection forSelection, Callback<String> updateCallback) {
103 Read<String> read = getPartNameReadRequest(forSelection);
105 updateCallback.run("Override to control part name (PropertyTabContributorImpl.updatePartName)");
107 Simantics.getSession().asyncRequest(read, new PartNameListener(updateCallback));
110 public Read<String> getPartNameReadRequest(ISelection forSelection) {
111 final Resource r = AdaptationUtils.adaptToSingle(forSelection, Resource.class);
114 return new Read<String>() {
116 public String perform(ReadGraph graph) throws DatabaseException {
117 return NameUtils.getSafeName(graph, r);
123 public void createControls(Composite body, IWorkbenchSite site,
124 ISessionContext context, WidgetSupport support) {
125 // TODO Auto-generated method stub
126 super.createControls(body, site, context, support);
127 ((Composite)parameterExplorer.getExplorerControl()).setEnabled(enabled);
133 public class MultiSelectionTabContibutor extends PropertyTabContributorImpl {
134 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) {
135 //Composite composite = new Composite(body, SWT.NONE);
138 public Read<String> getPartNameReadRequest(ISelection forSelection) {
139 final Collection<Resource> coll = AdaptationUtils.adaptToCollection(forSelection, Resource.class);
140 if (coll.size() == 0)
142 return new Read<String>() {
144 public String perform(ReadGraph graph) throws DatabaseException {
146 for (Resource r : coll) {
147 title += NameUtils.getSafeName(graph, r) +",";
149 System.out.println(title);
150 return title.substring(0,title.length()-1);
156 public class NoneSelectionTabContributor extends PropertyTabContributorImpl {
158 public void createControls(Composite body, IWorkbenchSite site,
159 ISessionContext context, WidgetSupport support) {
160 //Composite composite = new Composite(body, SWT.NONE);
164 public void updatePartName(ISelection forSelection, Callback<String> updateCallback) {
165 updateCallback.run("No Selection");