Example #1
0
 @Nullable
 public static <T> T findView(IWorkbenchWindow workbenchWindow, Class<T> viewClass) {
   IViewReference[] references = workbenchWindow.getActivePage().getViewReferences();
   for (IViewReference ref : references) {
     IViewPart view = ref.getView(false);
     if (view != null && viewClass.isAssignableFrom(view.getClass())) {
       return viewClass.cast(view);
     }
   }
   return null;
 }
Example #2
0
 public static CellEditor createCellEditor(
     Composite parent, Object object, DBPPropertyDescriptor property) {
   // List
   if (property instanceof IPropertyValueListProvider) {
     final IPropertyValueListProvider listProvider = (IPropertyValueListProvider) property;
     final Object[] items = listProvider.getPossibleValues(object);
     if (!ArrayUtils.isEmpty(items)) {
       final String[] strings = new String[items.length];
       for (int i = 0, itemsLength = items.length; i < itemsLength; i++) {
         strings[i] =
             items[i] instanceof DBPNamedObject
                 ? ((DBPNamedObject) items[i]).getName()
                 : CommonUtils.toString(items[i]);
       }
       final CustomComboBoxCellEditor editor =
           new CustomComboBoxCellEditor(
               parent,
               strings,
               SWT.DROP_DOWN | (listProvider.allowCustomValue() ? SWT.NONE : SWT.READ_ONLY));
       return editor;
     }
   }
   Class<?> propertyType = property.getDataType();
   if (propertyType == null || CharSequence.class.isAssignableFrom(propertyType)) {
     return new CustomTextCellEditor(parent);
   } else if (BeanUtils.isNumericType(propertyType)) {
     return new CustomNumberCellEditor(parent, propertyType);
   } else if (BeanUtils.isBooleanType(propertyType)) {
     return new CustomCheckboxCellEditor(parent);
     // return new CheckboxCellEditor(parent);
   } else if (propertyType.isEnum()) {
     final Object[] enumConstants = propertyType.getEnumConstants();
     final String[] strings = new String[enumConstants.length];
     for (int i = 0, itemsLength = enumConstants.length; i < itemsLength; i++) {
       strings[i] = ((Enum) enumConstants[i]).name();
     }
     return new CustomComboBoxCellEditor(parent, strings, SWT.DROP_DOWN | SWT.READ_ONLY);
   } else {
     log.warn("Unsupported property type: " + propertyType.getName());
     return null;
   }
 }
Example #3
0
  private static Method findPropertyGetter(Class<?> clazz, String getName, String isName) {
    Method[] methods = clazz.getDeclaredMethods();

    for (Method method : methods) {
      if ((!Modifier.isPublic(method.getModifiers()))
          || (!Modifier.isPublic(method.getDeclaringClass().getModifiers()))
          || (method.getReturnType().equals(void.class))) {
        // skip
      } else if (method.getName().equals(getName)
          || (method.getName().equals(isName) && method.getReturnType().equals(boolean.class))) {
        // If it matches the get name, it's the right method
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length == 0
            || (parameterTypes.length == 1 && parameterTypes[0] == DBRProgressMonitor.class)) {
          return method;
        }
      }
    }
    return clazz == Object.class
        ? null
        : findPropertyGetter(clazz.getSuperclass(), getName, isName);
  }
  @Override
  public Object getAdapter(Object adaptableObject, Class adapterType) {
    if (adapterType == DBSDataSourceContainer.class) {
      if (adaptableObject instanceof DBNDataSource) {
        return ((DBNDataSource) adaptableObject).getDataSourceContainer();
      }
      DBSObject object = null;
      if (adaptableObject instanceof DBSWrapper) {
        object = ((DBSWrapper) adaptableObject).getObject();
      } else if (adaptableObject instanceof DBSObject) {
        object = (DBSObject) adaptableObject;
      }
      if (object == null) {
        return null;
      }
      if (object instanceof DBSDataSourceContainer) {
        return object;
      }
      DBPDataSource dataSource = object.getDataSource();
      return dataSource == null ? null : dataSource.getContainer();
    } else if (DBPObject.class.isAssignableFrom(adapterType)) {
      DBPObject object = null;
      if (adaptableObject instanceof DBSWrapper) {
        object = ((DBSWrapper) adaptableObject).getObject();
      } else if (adaptableObject instanceof DBPObject) {
        object = (DBPObject) adaptableObject;
      }
      if (object != null && adapterType.isAssignableFrom(object.getClass())) {
        return object;
      }
    } else if (IResource.class.isAssignableFrom(adapterType)) {
      if (adaptableObject instanceof DBNResource) {
        IResource resource = ((DBNResource) adaptableObject).getResource();
        if (resource != null && adapterType.isAssignableFrom(resource.getClass())) {
          return resource;
        } else {
          return null;
        }
      }
    } else if (adapterType == IPropertySource.class) {
      DBPObject dbObject = null;
      if (adaptableObject instanceof DBSWrapper) {
        dbObject = ((DBSWrapper) adaptableObject).getObject();
      } else if (adaptableObject instanceof DBPObject) {
        dbObject = (DBPObject) adaptableObject;
      }
      if (dbObject instanceof IPropertySource) {
        return dbObject;
      }
      if (dbObject instanceof DBPPropertySource) {
        return new PropertySourceDelegate((DBPPropertySource) dbObject);
      }
      if (dbObject instanceof IAdaptable) {
        Object adapter = ((IAdaptable) dbObject).getAdapter(IPropertySource.class);
        if (adapter != null) {
          return adapter;
        }
        adapter = ((IAdaptable) dbObject).getAdapter(DBPPropertySource.class);
        if (adapter != null) {
          return new PropertySourceDelegate((DBPPropertySource) adapter);
        }
      }
      if (dbObject != null) {
        PropertyCollector props = new PropertyCollector(adaptableObject, dbObject, true);
        props.collectProperties();
        if (props.isEmpty() && adaptableObject instanceof DBSObject) {
          // Add default properties
          DBSObject meta = (DBSObject) adaptableObject;
          props.addProperty(
              null, "name", CoreMessages.model_navigator_Name, meta.getName()); // $NON-NLS-1$
          props.addProperty(
              null,
              "desc",
              CoreMessages.model_navigator_Description,
              meta.getDescription()); // $NON-NLS-1$
        }
        return new PropertySourceDelegate(props);
      }
    } else if (adapterType == IWorkbenchAdapter.class) {
      // Workbench adapter
      if (adaptableObject instanceof DBNNode) {
        final DBNNode node = (DBNNode) adaptableObject;
        return new WorkbenchAdapter() {
          @Override
          public ImageDescriptor getImageDescriptor(Object object) {
            return DBeaverIcons.getImageDescriptor(node.getNodeIconDefault());
          }

          @Override
          public String getLabel(Object o) {
            return node.getNodeName();
          }

          @Override
          public Object getParent(Object o) {
            return node.getParentNode();
          }
        };
      } else {
        return null;
      }
    }
    return null;
  }