]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/property/P3DSelectionProcessor.java
Publish Plant3D feature
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / property / P3DSelectionProcessor.java
1 package org.simantics.plant3d.property;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6
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
31 import vtk.vtkProp;
32
33 public class P3DSelectionProcessor implements SelectionProcessor<Object, ReadGraph>  {
34         
35         private static final boolean DEBUG = false;
36         @Override
37         public Collection<?> process(Object selection, ReadGraph backend) {
38 //           System.out.println(getClass().getSimpleName() + " incoming selection: " + ObjectUtils.toString(selection));
39
40
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())
50                                          readOnly = true;
51                                   resourceCollection.add(sr.getResource());
52                          }
53                  }
54                  
55                 
56                  if (nodeCollection.size() == 1) {
57                          IG3DNode node = nodeCollection.iterator().next();
58                          List<PropertyTabContributor> contributors = PropertyTabUtil.getContributors(node);
59                          int i = 100;
60                          for (PropertyTabContributor c : contributors) {
61                                  result.add(new ComparableTabContributor(c, i--, node, c.getId()));
62                          }
63                  }
64              
65                  if (DEBUG) {
66                          if (propCollection.size() == 1) {
67                                  vtkProp prop = propCollection.iterator().next();
68                                  if (prop == null)
69                                          throw new NullPointerException();
70                                  result.add(new ComparableTabContributor(new VTKPropertyTabContributor(), -2, prop, "VTK"));
71                          }
72                          
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){
77                                  try {
78                                                 Resource r = resourceCollection.iterator().next();
79                                         result.add(new ComparableTabContributor(new P3DBasicPropertyTab(!readOnly), 0, r, "Graph"));
80                                                         
81                                  } catch (Exception e) {
82                                          e.printStackTrace();
83                                  }
84                                  }
85                          }  
86                  }
87                  
88                  if(result.size() == 0) {
89                          result.add(new ComparableTabContributor(new NoneSelectionTabContributor(),0, resourceCollection, "Empty"));
90                  }
91              
92                 return result;
93         }
94         
95         public class P3DBasicPropertyTab extends BasicPropertyTab {
96                 
97                 boolean enabled;
98                 public P3DBasicPropertyTab(boolean enabled) {
99                         this.enabled = enabled;
100                 }
101                  public void updatePartName(ISelection forSelection, Callback<String> updateCallback) {
102                         Read<String> read = getPartNameReadRequest(forSelection);
103                         if (read == null) {
104                             updateCallback.run("Override to control part name (PropertyTabContributorImpl.updatePartName)");
105                         } else {
106                             Simantics.getSession().asyncRequest(read, new PartNameListener(updateCallback));
107                         }
108                     }
109                  public Read<String> getPartNameReadRequest(ISelection forSelection) {
110                          final Resource r  =  AdaptationUtils.adaptToSingle(forSelection, Resource.class);
111                          if (r == null)
112                                  return null;
113                          return new Read<String>() {
114                                  @Override
115                                 public String perform(ReadGraph graph) throws DatabaseException {
116                                         return NameUtils.getSafeName(graph, r);         
117                                 }
118                         };
119                  }
120                  
121                  @Override
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);
127                 }
128         }
129         
130         
131         
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);
135                 }
136                 
137                 public Read<String> getPartNameReadRequest(ISelection forSelection) {
138                          final Collection<Resource> coll =  AdaptationUtils.adaptToCollection(forSelection, Resource.class);
139                          if (coll.size() == 0)
140                                  return null;
141                          return new Read<String>() {
142                                  @Override
143                                 public String perform(ReadGraph graph) throws DatabaseException {
144                                         String title = "";
145                                         for (Resource r : coll) {
146                                                 title += NameUtils.getSafeName(graph, r) +",";
147                                         }
148                                         System.out.println(title);
149                                         return title.substring(0,title.length()-1);             
150                                 }
151                         };
152                  }
153         }
154         
155         public class NoneSelectionTabContributor extends PropertyTabContributorImpl {
156                 @Override
157                 public void createControls(Composite body, IWorkbenchSite site,
158                                 ISessionContext context, WidgetSupport support) {
159                         //Composite composite = new Composite(body, SWT.NONE);
160                         
161                 }
162                 
163                 public void updatePartName(ISelection forSelection, Callback<String> updateCallback) {
164                         updateCallback.run("No Selection");
165                 }
166         }
167         
168
169 }