Пример #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;
 }
Пример #2
0
  @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;
  }