/**
   * Return the unsorted intersection of all the <code>IPropertyDescriptor</code>s for the objects.
   *
   * @return List
   */
  private List computeMergedPropertyDescriptors() {
    if (values.length == 0) {
      return new ArrayList(0);
    }

    IPropertySource firstSource = getPropertySource(values[0]);
    if (firstSource == null) {
      return new ArrayList(0);
    }

    if (values.length == 1) {
      return Arrays.asList(firstSource.getPropertyDescriptors());
    }

    // get all descriptors from each object
    Map[] propertyDescriptorMaps = new Map[values.length];
    for (int i = 0; i < values.length; i++) {
      Object object = values[i];
      IPropertySource source = getPropertySource(object);
      if (source == null) {
        // if one of the selected items is not a property source
        // then we show no properties
        return new ArrayList(0);
      }
      // get the property descriptors keyed by id
      propertyDescriptorMaps[i] = computePropertyDescriptorsFor(source);
    }

    // intersect
    Map intersection = propertyDescriptorMaps[0];
    for (int i = 1; i < propertyDescriptorMaps.length; i++) {
      // get the current ids
      Object[] ids = intersection.keySet().toArray();
      for (int j = 0; j < ids.length; j++) {
        Object object = propertyDescriptorMaps[i].get(ids[j]);
        if (object == null
            ||
            // see if the descriptors (which have the same id) are
            // compatible
            !((IPropertyDescriptor) intersection.get(ids[j]))
                .isCompatibleWith((IPropertyDescriptor) object)) {
          intersection.remove(ids[j]);
        }
      }
    }

    // sorting is handled in the PropertySheetViewer, return unsorted (in
    // the original order)
    ArrayList result = new ArrayList(intersection.size());
    IPropertyDescriptor[] firstDescs = firstSource.getPropertyDescriptors();
    for (int i = 0; i < firstDescs.length; i++) {
      IPropertyDescriptor desc = firstDescs[i];
      if (intersection.containsKey(desc.getId())) {
        result.add(desc);
      }
    }
    return result;
  }
Beispiel #2
0
 /**
  * Returns an map of property descritptors (keyed on id) for the given property source.
  *
  * @source a property source for which to obtain descriptors
  * @return a table of decriptors keyed on their id
  */
 private Map computePropertyDescriptorsFor(IPropertySource source) {
   IPropertyDescriptor[] descriptors = source.getPropertyDescriptors();
   Map result = new HashMap(descriptors.length * 2 + 1);
   for (int i = 0; i < descriptors.length; i++) {
     result.put(descriptors[i].getId(), descriptors[i]);
   }
   return result;
 }
  @Override
  protected void createColumns() {
    int bounds = 100;
    int column = 0;
    clearColumns();

    List list = propertySources;
    if (list.isEmpty() && exemplar != null) {
      list = new ArrayList();
      list.add(exemplar);
    }

    boolean usePropertySourceProviderIfItHasNicerRenderers = false;
    if (usePropertySourceProviderIfItHasNicerRenderers) {
      SortedMap<String, TableViewerColumn> headers = new TreeMap<String, TableViewerColumn>();
      for (Object object : list) {
        final IPropertySource propertySource = PropertySources.asPropertySource(object);
        IPropertyDescriptor[] descriptors = propertySource.getPropertyDescriptors();
        if (descriptors != null) {
          for (final IPropertyDescriptor descriptor : descriptors) {
            final Object id = descriptor.getId();
            String header = PropertyDescriptors.getReadablePropertyName(descriptor);
            TableViewerColumn col = headers.get(header);
            if (col == null) {
              col = createTableViewerColumn(header, bounds, column++);
              headers.put(header, col);

              IPropertySourceProvider propertySourceProvider =
                  new IPropertySourceProvider() {
                    @Override
                    public IPropertySource getPropertySource(Object object) {
                      return PropertySources.asPropertySource(object);
                    }
                  };
              col.setLabelProvider(new PropertyColumnLabelProvider(propertySourceProvider, id));
            }
          }
        }
      }
    } else {
      SortedMap<String, Function1> headers = new TreeMap<String, Function1>();
      for (Object object : list) {
        final IPropertySource propertySource = PropertySources.asPropertySource(object);
        IPropertyDescriptor[] descriptors = propertySource.getPropertyDescriptors();
        if (descriptors != null) {
          for (final IPropertyDescriptor descriptor : descriptors) {
            final Object id = descriptor.getId();
            String name = PropertyDescriptors.getReadablePropertyName(descriptor);
            Function1 function =
                new Function1WithReturnType() {
                  @Override
                  public Object apply(Object object) {
                    IPropertySource property = PropertySources.asPropertySource(object);
                    if (property != null) {
                      return property.getPropertyValue(id);
                    }
                    return null;
                  }

                  @Override
                  public Class<?> getReturnType() {
                    return PropertyDescriptors.getPropertyType(descriptor);
                  }
                };
            headers.put(name, function);
          }
        }
      }
      int idx = 0;
      boolean pickedSortColumn = false;
      Set<Entry<String, Function1>> entrySet = headers.entrySet();
      for (Entry<String, Function1> entry : entrySet) {
        String header = entry.getKey();
        if (!pickedSortColumn && isDefaultSortColumn(header)) {
          setDefaultSortColumnIndex(idx);
          pickedSortColumn = true;
        }
        Function1 function = entry.getValue();
        addFunction(function);
        TableViewerColumn col = createTableViewerColumn(header, bounds, column++);
        col.setLabelProvider(createColumnLabelProvider(header, function));
        idx++;
      }
    }
  }