]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d/src/org/simantics/g3d/property/AnnotatedPropertyTabContributorFactory.java
Compiler warning elimination
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / property / AnnotatedPropertyTabContributorFactory.java
index a3fca78bb4206dc18adafa617073919702042778..7e3f85333bb88022ce5038e43670bdf0ab792346 100644 (file)
@@ -18,6 +18,7 @@ import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
@@ -81,7 +82,6 @@ import org.simantics.g3d.scenegraph.structural.IStructuralNode;
 import org.simantics.g3d.tools.AdaptationUtils;
 import org.simantics.selectionview.IPropertyTab;
 import org.simantics.selectionview.IPropertyTab2;
-import org.simantics.utils.datastructures.Callback;
 import org.simantics.utils.datastructures.MapList;
 
 public class AnnotatedPropertyTabContributorFactory implements PropertyTabContributorFactory {
@@ -131,91 +131,92 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
        
        
        private static void collectItems(Class<?> clazz, Map<String, IPropertyItem> items) throws InstantiationException, IllegalAccessException {
-                Class<?> superclass = clazz.getSuperclass();
-                if(superclass != null)
-                        collectItems(superclass, items);
-                
-                for (Method m : clazz.getDeclaredMethods()) {
-                        m.setAccessible(true);
-               for (Annotation annotation : m.getAnnotations()) {
-                       if (annotation.annotationType().equals(GetPropertyValue.class)) {
-                               GetPropertyValue get = (GetPropertyValue)annotation;
-                               PropertyItem item = (PropertyItem)items.get(get.value());
-                               if (item == null) {
-                                       item = new PropertyItem(get.value());
-                                       items.put(item.id, item);
-                               }
+               Class<?> superclass = clazz.getSuperclass();
+               if(superclass != null)
+                       collectItems(superclass, items);
+               
+               for (Method m : clazz.getDeclaredMethods()) {
+                       m.setAccessible(true);
+                       for (Annotation annotation : m.getAnnotations()) {
+                               if (annotation.annotationType().equals(GetPropertyValue.class)) {
+                                       GetPropertyValue get = (GetPropertyValue)annotation;
+                                       PropertyItem item = (PropertyItem)items.get(get.value());
+                                       if (item == null) {
+                                               item = new PropertyItem(get.value());
+                                               items.put(item.id, item);
+                                       }
 
-                               item.getter = m;
-                               item.manipulatorClass = get.manipulator().newInstance().get(m,null);
+                                       item.getter = m;
+                                       item.manipulatorClass = get.manipulator().newInstance().get(m,null);
 
-                               item.tabId = get.tabId();
+                                       item.tabId = get.tabId();
 
-                               item.name = get.name();
-                               
-                               
-                       } else if (annotation.annotationType().equals(SetPropertyValue.class)) {
-                               SetPropertyValue set = (SetPropertyValue)annotation;
-                               PropertyItem item = (PropertyItem)items.get(set.value());
-                               if (item == null) {
-                                       item = new PropertyItem(set.value());
-                                       items.put(item.id, item);
-                               }
-                               
-                               item.setter = m;
-                       } else if (annotation.annotationType().equals(CompoundGetPropertyValue.class)) {
-                               CompoundGetPropertyValue get = (CompoundGetPropertyValue)annotation;
-                               CompoundPropertyItem item = (CompoundPropertyItem)items.get(get.value());
-                               if (item == null) {
-                                       item = new CompoundPropertyItem(get.value());
-                                       items.put(item.id, item);
-                               }
+                                       item.name = get.name();
+                                       
+                                       
+                               } else if (annotation.annotationType().equals(SetPropertyValue.class)) {
+                                       SetPropertyValue set = (SetPropertyValue)annotation;
+                                       PropertyItem item = (PropertyItem)items.get(set.value());
+                                       if (item == null) {
+                                               item = new PropertyItem(set.value());
+                                               items.put(item.id, item);
+                                       }
+                                       
+                                       item.setter = m;
+                               } else if (annotation.annotationType().equals(CompoundGetPropertyValue.class)) {
+                                       CompoundGetPropertyValue get = (CompoundGetPropertyValue)annotation;
+                                       CompoundPropertyItem item = (CompoundPropertyItem)items.get(get.value());
+                                       if (item == null) {
+                                               item = new CompoundPropertyItem(get.value());
+                                               items.put(item.id, item);
+                                       }
 
-                               item.getter = m;
-                               item.manipulatorFactory = get.manipulator().newInstance();
+                                       item.getter = m;
+                                       item.manipulatorFactory = get.manipulator().newInstance();
 
-                               item.tabId = get.tabId();
+                                       item.tabId = get.tabId();
 
-                               item.name = get.name();
-                       } else if (annotation.annotationType().equals(CompoundSetPropertyValue.class)) {
-                               CompoundSetPropertyValue set = (CompoundSetPropertyValue)annotation;
-                               CompoundPropertyItem item = (CompoundPropertyItem)items.get(set.value());
-                               if (item == null) {
-                                       item = new CompoundPropertyItem(set.value());
-                                       items.put(item.id, item);
-                               }
-                               
-                               item.setter = m;
-                       }
-               }
-                }
+                                       item.name = get.name();
+                               } else if (annotation.annotationType().equals(CompoundSetPropertyValue.class)) {
+                                       CompoundSetPropertyValue set = (CompoundSetPropertyValue)annotation;
+                                       CompoundPropertyItem item = (CompoundPropertyItem)items.get(set.value());
+                                       if (item == null) {
+                                               item = new CompoundPropertyItem(set.value());
+                                               items.put(item.id, item);
+                                       }
+                                       
+                                       item.setter = m;
+                               }
+                       }
+               }
                
                
        }
        
        private static void collectBlacklist(Class<?> clazz, List<String> blacklist) throws InstantiationException, IllegalAccessException {
-                Class<?> superclass = clazz.getSuperclass();
-                if(superclass != null)
-                        collectBlacklist(superclass, blacklist);
-                
-                PropertyTabBlacklist ann = clazz.getAnnotation(PropertyTabBlacklist.class);
-                if (ann == null)
-                        return;
-                String s = ann.value();
-                if (s == null)
-                        return;
-                if (s.length() == 0)
-                        return;
-                for (String item : s.split(";")) {
-                        blacklist.add(item);
-                }
-                
+               Class<?> superclass = clazz.getSuperclass();
+               if(superclass != null)
+                       collectBlacklist(superclass, blacklist);
+               
+               PropertyTabBlacklist ann = clazz.getAnnotation(PropertyTabBlacklist.class);
+               if (ann == null)
+                       return;
+               String s = ann.value();
+               if (s == null)
+                       return;
+               if (s.length() == 0)
+                       return;
+               for (String item : s.split(";")) {
+                       blacklist.add(item);
+               }
+               
                
        }
        
        private static Map<PropertyItem, PropertyManipulator> createManipulators(CompoundPropertyItem item, Object obj) {
                try {
                        
+                       @SuppressWarnings("unchecked")
                        Map<String,Object> map = (Map<String, Object>)item.getter.invoke(obj);
                        Map<PropertyItem, PropertyManipulator> result = new HashMap<AnnotatedPropertyTabContributorFactory.PropertyItem, PropertyManipulator>();
                        for (String key : map.keySet()) {
@@ -232,7 +233,7 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                        return result;
                } catch (Exception e) {
                        e.printStackTrace();
-                       return Collections.EMPTY_MAP;
+                       return Collections.emptyMap();
                } 
                
        }
@@ -250,6 +251,7 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
        
        private static interface IPropertyItem {
                public String getTabId();
+               public String getName();
        }
        
        private static class PropertyItem implements IPropertyItem{
@@ -276,6 +278,11 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                public String getTabId() {
                        return tabId;
                }
+               
+               @Override
+               public String getName() {
+                   return name;
+               }
        }
        
        private static class CompoundPropertyItem implements IPropertyItem{
@@ -302,6 +309,11 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                public String getTabId() {
                        return tabId;
                }
+               
+               @Override
+        public String getName() {
+            return name;
+        }
        }
        
        private static class AnnotatedPropertyTabContributor implements PropertyTabContributor {
@@ -338,8 +350,8 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                
                private TableViewer viewer;
                
-               private IG3DNode node;
-               private NodeMap<?,?> nodeMap;
+               private INode node;
+               private NodeMap<?,?,?> nodeMap;
                
                private List<TableViewerColumn> valueColumns = new ArrayList<TableViewerColumn>();
                
@@ -389,27 +401,6 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                                }
                        });
                        
-                       int valueCount = 0;
-                       for (IPropertyItem item : contibutedItems) {
-                               if (item instanceof PropertyItem) {
-                                       PropertyManipulator manipulator = createManipulator((PropertyItem)item, null);
-                                       if (manipulator == null)
-                                               continue;
-                                       if (valueCount < manipulator.getValueCount())
-                                               valueCount = manipulator.getValueCount();
-                               } else if (item instanceof CompoundPropertyItem) {
-                                       if (valueCount < 1)
-                                               valueCount = 1;
-                               }
-                       }
-                       for (int i = 0; i < valueCount; i++) {
-                               TableViewerColumn value = new TableViewerColumn(viewer, SWT.LEFT);
-                               //value.getColumn().setText("Value " + (i+1));
-                               value.getColumn().setText("");
-                               value.getColumn().setWidth(200);
-                               valueColumns.add(value);
-                               //value.setEditingSupport(new )
-                       }
                        viewer.getTable().setHeaderVisible(true);
                        viewer.getTable().setLinesVisible(true);
                        viewer.addSelectionChangedListener(new ISelectionChangedListener() {
@@ -547,7 +538,7 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                @Override
                public void setInput(ISessionContext context, ISelection selection,
                                boolean force) {
-                       Collection<IG3DNode> nodes = AdaptationUtils.adaptToCollection(selection, IG3DNode.class);
+                       Collection<INode> nodes = AdaptationUtils.adaptToCollection(selection, INode.class);
                        if (nodes.size() != 1) {
                                if (node != null) {
                                        node.removeListener(this);
@@ -555,7 +546,7 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                                }
                                return;
                        }
-                       IG3DNode n = nodes.iterator().next();
+                       INode n = nodes.iterator().next();
                        if (node != null) {
                                if (!node.equals(n)) {
                                        node.removeListener(this);
@@ -568,34 +559,25 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                
                
                
-               private void setInput(IG3DNode node) {
+               private void setInput(INode node) {
                        this.node = node;
                        this.node.addListener(this);
                        // resolve nodemap
-                       IG3DNode n = node;
+                       INode n = node;
                        while (true) {
                                if (n == null) {
                                        nodeMap = null;
                                        break;
                                }
-                               if (n instanceof NodeMapProvider<?,?>) {
-                                       nodeMap = ((NodeMapProvider<?,?>) n).getNodeMap();
+                               if (n instanceof NodeMapProvider<?,?,?>) {
+                                       nodeMap = ((NodeMapProvider<?,?,?>) n).getNodeMap();
                                        if (nodeMap != null)
                                                break;
                                }
                                n = (IG3DNode)n.getParent();
                        }
                        boolean readOnly =  (node instanceof IStructuralNode && ((IStructuralNode)node).isPartOfInstantiatedModel() && !((IStructuralNode)node).isInstantiatedModelRoot());
-                       // create label providers
-                       PropertyValueLabelProvider2 p = new PropertyValueLabelProvider2(this);
-                       int index = 0;
-                       for (TableViewerColumn c : valueColumns) {
-                               c.setLabelProvider(p);
-                               if (!readOnly) {
-                                       PropertyEditingSupport support = new PropertyEditingSupport(this, viewer, index++, nodeMap);
-                                       c.setEditingSupport(support);
-                               }
-                       }
+                       
                        resolvedItems.clear();
                        manipulators.clear();
                        for (IPropertyItem item : contibutedItems) {
@@ -613,6 +595,36 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                                }
                        }
                        
+                       int valueCount = 0;
+                       for (PropertyManipulator manipulator : manipulators.values()) {
+                           if (valueCount < manipulator.getValueCount())
+                   valueCount = manipulator.getValueCount();
+                       }
+            for (int i = 0; i < valueCount; i++) {
+                TableViewerColumn value = new TableViewerColumn(viewer, SWT.LEFT);
+                //value.getColumn().setText("Value " + (i+1));
+                value.getColumn().setText("");
+                value.getColumn().setWidth(200);
+                valueColumns.add(value);
+                //value.setEditingSupport(new )
+            }
+            
+            // create label providers
+            PropertyValueLabelProvider2 p = new PropertyValueLabelProvider2(this);
+            int index = 0;
+            for (TableViewerColumn c : valueColumns) {
+                c.setLabelProvider(p);
+                if (!readOnly) {
+                    PropertyEditingSupport support = new PropertyEditingSupport(this, viewer, index++, nodeMap);
+                    c.setEditingSupport(support);
+                }
+            }
+                       Collections.sort(resolvedItems, new Comparator<IPropertyItem>() {
+                           @Override
+                           public int compare(IPropertyItem o1, IPropertyItem o2) {
+                               return o1.getName().compareTo(o2.getName());
+                           }
+            });
                        viewer.getTable().setEnabled(!readOnly);
                        viewer.setInput(resolvedItems);
                }
@@ -733,11 +745,11 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
        private static class PropertyEditingSupport extends EditingSupport {
                AnnotatedPropertyTab tab;
                int index;
-               NodeMap<?,?> nodeMap;
+               NodeMap<?,?,?> nodeMap;
                TableViewer viewer;
                CellEditor editor;
 
-               public PropertyEditingSupport(AnnotatedPropertyTab tab, TableViewer viewer, int index, NodeMap<?,?> nodeMap) {
+               public PropertyEditingSupport(AnnotatedPropertyTab tab, TableViewer viewer, int index, NodeMap<?,?,?> nodeMap) {
                        super(viewer);
                        this.tab = tab;
                        this.index = index;
@@ -969,9 +981,9 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                        if (focusCellManager != null) {
                                try {
                                        Method m = focusCellManager.getClass().getSuperclass()
-                                                       .getDeclaredMethod("init", null);
+                                                       .getDeclaredMethod("init");
                                        m.setAccessible(true);
-                                       m.invoke(focusCellManager, null);
+                                       m.invoke(focusCellManager);
                                } catch (SecurityException e) {
                                        e.printStackTrace();
                                } catch (IllegalArgumentException e) {
@@ -1039,9 +1051,10 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                                                if (DEBUG)System.err.println("FOCUS CELL: " + focusCell);
                                                
                                                Method m = AbstractTableViewer.class.getDeclaredMethod(
-                                                               "getSelectionFromWidget", null);
+                                                               "getSelectionFromWidget");
                                                m.setAccessible(true);
-                                               List l = (List) m.invoke(getViewer(), null);
+                                               @SuppressWarnings("rawtypes")
+                                               List l = (List) m.invoke(getViewer());
                                                if (focusCellManager != null) {
                                                        m = focusCellManager
                                                                        .getClass()
@@ -1141,9 +1154,9 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                                        ViewerColumn column = (ViewerColumn) m.invoke(viewer,
                                                        new Object[] { new Integer(columnIndex) });
                                        m = ViewerColumn.class.getDeclaredMethod(
-                                                       "getEditingSupport", null);
+                                                       "getEditingSupport");
                                        m.setAccessible(true);
-                                       EditingSupport es = (EditingSupport) m.invoke(column, null);
+                                       EditingSupport es = (EditingSupport) m.invoke(column);
                                        if (column != null && es != null) {
                                                m = EditingSupport.class.getDeclaredMethod("canEdit",
                                                                new Class[] { Object.class });
@@ -1247,10 +1260,9 @@ public class AnnotatedPropertyTabContributorFactory implements PropertyTabContri
                                m.setAccessible(true);
                                ViewerColumn column = (ViewerColumn) m.invoke(viewer,
                                                new Object[] { new Integer(cell.getColumnIndex()) });
-                               m = ViewerColumn.class.getDeclaredMethod("getEditingSupport",
-                                               null);
+                               m = ViewerColumn.class.getDeclaredMethod("getEditingSupport");
                                m.setAccessible(true);
-                               EditingSupport es = (EditingSupport) m.invoke(column, null);
+                               EditingSupport es = (EditingSupport) m.invoke(column);
                                if (column != null && es != null) {
                                        m = EditingSupport.class.getDeclaredMethod("canEdit",
                                                        new Class[] { Object.class });