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