1 package org.simantics.plant3d.property;
3 import java.util.ArrayList;
4 import java.util.Collection;
6 import java.util.function.Consumer;
8 import org.eclipse.jface.viewers.ISelection;
9 import org.eclipse.swt.widgets.Composite;
10 import org.eclipse.ui.IWorkbenchSite;
11 import org.simantics.Simantics;
12 import org.simantics.browsing.ui.swt.PartNameListener;
13 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.utils.NameUtils;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.management.ISessionContext;
19 import org.simantics.db.request.Read;
20 import org.simantics.g3d.property.PropertyTabContributor;
21 import org.simantics.g3d.property.PropertyTabUtil;
22 import org.simantics.g3d.scenegraph.base.INode;
23 import org.simantics.g3d.tools.AdaptationUtils;
24 import org.simantics.g3d.vtk.property.VTKPropertyTabContributor;
25 import org.simantics.objmap.structural.StructuralResource;
26 import org.simantics.selectionview.BasicPropertyTab;
27 import org.simantics.selectionview.ComparableTabContributor;
28 import org.simantics.selectionview.PropertyTabContributorImpl;
29 import org.simantics.selectionview.SelectionProcessor;
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<INode> nodeCollection = AdaptationUtils.adaptToCollection(selection, INode.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 INode 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 (resourceCollection.size() == 1){
91 Resource r = resourceCollection.iterator().next();
92 result.add(new ComparableTabContributor(new P3DBasicPropertyTab(!readOnly), 0, r, "Properties"));
93 } catch (Exception e) {
98 if(result.size() == 0) {
99 result.add(new ComparableTabContributor(new NoneSelectionTabContributor(),0, resourceCollection, "Empty"));
105 public class P3DBasicPropertyTab extends BasicPropertyTab {
108 public P3DBasicPropertyTab(boolean enabled) {
109 this.enabled = enabled;
115 public void updatePartName(ISelection forSelection, Consumer<String> updateCallback) {
116 Read<String> read = getPartNameReadRequest(forSelection);
118 updateCallback.accept("Override to control part name (PropertyTabContributorImpl.updatePartName)");
120 Simantics.getSession().asyncRequest(read, new PartNameListener(updateCallback));
125 public Read<String> getPartNameReadRequest(ISelection forSelection) {
126 final Resource r = AdaptationUtils.adaptToSingle(forSelection, Resource.class);
129 return new Read<String>() {
131 public String perform(ReadGraph graph) throws DatabaseException {
132 return NameUtils.getSafeName(graph, r);
138 public void createControls(Composite body, IWorkbenchSite site,
139 ISessionContext context, WidgetSupport support) {
140 // TODO Auto-generated method stub
141 super.createControls(body, site, context, support);
142 ((Composite)parameterExplorer.getExplorerControl()).setEnabled(enabled);
148 public class MultiSelectionTabContibutor extends PropertyTabContributorImpl {
150 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) {
151 //Composite composite = new Composite(body, SWT.NONE);
155 public Read<String> getPartNameReadRequest(ISelection forSelection) {
156 final Collection<Resource> coll = AdaptationUtils.adaptToCollection(forSelection, Resource.class);
157 if (coll.size() == 0)
159 return new Read<String>() {
161 public String perform(ReadGraph graph) throws DatabaseException {
163 for (Resource r : coll) {
164 title += NameUtils.getSafeName(graph, r) +",";
166 System.out.println(title);
167 return title.substring(0,title.length()-1);
173 public class NoneSelectionTabContributor extends PropertyTabContributorImpl {
175 public void createControls(Composite body, IWorkbenchSite site,
176 ISessionContext context, WidgetSupport support) {
177 //Composite composite = new Composite(body, SWT.NONE);
182 public void updatePartName(ISelection forSelection, Consumer<String> updateCallback) {
183 updateCallback.accept("No Selection");