/**
  * Determine if a multiple object selection has been passed to the label provider. If the objects
  * is a IStructuredSelection, see if all the objects in the selection are the same and if so, we
  * want to provide labels for the common selected element.
  *
  * @param objects a single object or a IStructuredSelection.
  * @param multiple first element in the array is true if there is multiple unequal selected
  *     elements in a IStructuredSelection.
  * @return the object to get labels for.
  */
 private Object getObject(Object objects, boolean[] multiple) {
   Assert.isNotNull(objects);
   Object object = null;
   if (objects instanceof IStructuredSelection) {
     IStructuredSelection selection = (IStructuredSelection) objects;
     object = selection.getFirstElement();
     if (selection.size() == 1) {
       // one element selected
       multiple[0] = false;
       return object;
     }
     // multiple elements selected
     multiple[0] = true;
     Class firstClass = typeMapper.mapType(object);
     // determine if all the objects in the selection are the same type
     if (selection.size() > 1) {
       for (Iterator i = selection.iterator(); i.hasNext(); ) {
         Object next = i.next();
         Class nextClass = typeMapper.mapType(next);
         if (!nextClass.equals(firstClass)) {
           multiple[0] = false;
           object = null;
           break;
         }
       }
     }
   } else {
     multiple[0] = false;
     object = objects;
   }
   return object;
 }
 private void enableButtons() {
   IStructuredSelection ssel = (IStructuredSelection) viewer.getSelection();
   btnEdit.setEnabled(ssel.size() == 1);
   boolean moreThanSelectedItem =
       ssel.size() > 0 && ssel.size() < viewer.getTable().getItemCount();
   btnRemove.setEnabled(moreThanSelectedItem);
 }
  private void updateStatusLine(IStructuredSelection selection) {
    String message;

    if (selection == null || selection.size() == 0) {
      message = "";
    } else if (selection.size() == 1) {
      Object sel = selection.getFirstElement();

      if (sel instanceof IMarker) {
        IMarker marker = (IMarker) sel;

        message = marker.getAttribute(IMarker.MESSAGE, "");
      } else {
        message = "";
      }
    } else {
      List<IMarker> selMarkers = new ArrayList<IMarker>();

      for (Object obj : selection.toList()) {
        if (obj instanceof IMarker) {
          selMarkers.add((IMarker) obj);
        }
      }

      message = getStatusSummary(selMarkers.toArray(new IMarker[selMarkers.size()]));
    }

    getViewSite().getActionBars().getStatusLineManager().setMessage(message);
  }
Exemple #4
0
  private void updateFilterControl(IStructuredSelection selection) {
    if (selection.size() >= 1) {
      ArrayList<String> tags = new ArrayList<String>();
      for (Object o : selection.toArray()) {
        if (o instanceof Tag) {
          String tagName = ((Tag) o).getPath();
          if (tagName.matches(REGEX_SPEC_CHARS)) {
            tags.add("\"" + tagName + "\"");
          } else {
            tags.add(tagName);
          }
        }
      }

      proposalAdapter.setEnabled(false);
      searchControl.getFilterControl().setText(StringUtils.join(" " + OPERATOR_OR + " ", tags));
      proposalAdapter.setEnabled(true);
      if (selection.size() == 1) {
        listViewer.setInput(
            selection.getFirstElement()); // Don't run searching if only one tag is selected
      } else {
        updateList();
      }
    }
  }
 private boolean canEnable(IStructuredSelection selection) throws JavaModelException {
   if ((selection.size() == 1) && (selection.getFirstElement() instanceof IType)) {
     final IType type = (IType) selection.getFirstElement();
     return type.getCompilationUnit() != null && !type.isInterface();
   }
   if ((selection.size() == 1) && (selection.getFirstElement() instanceof ICompilationUnit))
     return true;
   return false;
 }
  // TODO Return a more interesting suggestion based on the selection.
  String getFilterSuggestion() {
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    if (selection != null) {
      if (selection.size() == 1) {
        return getFilterSuggestionBasedOnSingleSelection(selection);
      }
      if (selection.size() > 1) {
        return getFilterSuggestionBasedOnMultipleSelection(selection);
      }
    }

    return FilterUtils.getDefaultFilterSuggestion();
  }
 private ServiceRef[] getServiceRefs() {
   IStructuredSelection initSel = getInitialSelection();
   ServiceRef[] serviceRefs = new ServiceRef[initSel.size()];
   Iterator iter = initSel.iterator();
   for (int i = 0; i < initSel.size(); i++) {
     Object obj = iter.next();
     if (obj instanceof ServiceRef) {
       serviceRefs[i] = (ServiceRef) obj;
     } else {
       errorStatusMsg_ = ConsumptionUIMessages.MSG_ERROR_INVALID_MULTIPLE_SERVICE_SELECT;
       return null;
     }
   }
   return serviceRefs;
 }
 /** Called when master selection changes. */
 public void selectionChanged(IFormPart part, ISelection selection) {
   IStructuredSelection ssel = (IStructuredSelection) selection;
   m_input = null; // clear old input
   if (ssel.size() == 1 && ssel.getFirstElement() instanceof ProvidedCapabilityBuilder)
     m_input = (ProvidedCapabilityBuilder) (ssel.getFirstElement());
   refresh();
 }
 /*
  * (non-Javadoc)
  *
  * @see org.talend.repository.ui.actions.ITreeContextualAction#init(org.eclipse.jface.viewers.TreeViewer,
  * org.eclipse.jface.viewers.IStructuredSelection)
  */
 public void init(TreeViewer viewer, IStructuredSelection selection) {
   boolean canWork = !selection.isEmpty() && selection.size() == 1;
   if (canWork) {
     Object o = selection.getFirstElement();
     RepositoryNode node = (RepositoryNode) o;
     if (CamelEditorUtil.hasEditorOpened(node)) {
       canWork = false;
     } else {
       switch (node.getType()) {
         case REPOSITORY_ELEMENT:
           if (node.getObjectType() != CamelRepositoryNodeType.repositoryRoutesType) {
             canWork = false;
           }
           break;
         default:
           canWork = false;
       }
     }
     if (canWork
         && node.getObject() != null
         && ProxyRepositoryFactory.getInstance().getStatus(node.getObject())
             == ERepositoryStatus.LOCK_BY_USER) {
       canWork = false;
     }
   }
   setEnabled(canWork);
 }
 public void init(TreeViewer viewer, IStructuredSelection selection) {
   boolean canWork = selection.size() == 1;
   if (canWork) {
     Object o = ((IStructuredSelection) selection).getFirstElement();
     if (o instanceof RepositoryNode) {
       RepositoryNode node = (RepositoryNode) o;
       switch (node.getType()) {
         case REPOSITORY_ELEMENT:
           if (node.getObjectType() == JSONRepositoryNodeType.JSON) {
             canWork = true;
           } else {
             canWork = false;
           }
           break;
         default:
           canWork = false;
           break;
       }
       if (canWork) {
         canWork = (node.getObject().getRepositoryStatus() != ERepositoryStatus.DELETED);
       }
       if (canWork) {
         canWork = isLastVersion(node);
       }
     }
   }
   setEnabled(canWork);
 }
  @Override
  public void menuShown(MenuEvent e) {
    oldListener.menuShown(e);

    // now lets see if we have a rascal menu
    IStructuredSelection selection = (IStructuredSelection) selectionProvider.getSelection();
    if (selection.size() != 1) {
      return;
    }

    final TestElement testElement = (TestElement) selection.getFirstElement();
    if (TestNameTranslator.isRascalTestElement(testElement)) {
      MenuItem gotoFile = ((Menu) e.getSource()).getItem(0);
      Listener oldListener = gotoFile.getListeners(SWT.Selection)[0];
      gotoFile.removeListener(SWT.Selection, oldListener);
      gotoFile.addListener(
          SWT.Selection,
          new TypedListener(
              new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                  TestNameTranslator.tryOpenRascalTest(testElement);
                }
              }));
    } else if (TestNameTranslator.isRascalSuiteElement(testElement)) {
      MenuItem gotoFile = ((Menu) e.getSource()).getItem(0);
      gotoFile.setEnabled(false);
    }
  }
Exemple #12
0
    public void keyPressed(KeyEvent e) {
      IStructuredSelection s = (IStructuredSelection) getSelection();

      switch (e.keyCode) {
        case SWT.CR:
          if (isHierarchical()) {
            if (s.size() == 1) {
              TagTreeItem selected = (TagTreeItem) s.getFirstElement();
              if (selected.hasChildren()) {
                viewer.setInput(selected);
                refresh();
              }
            }
          }
          break;
        case SWT.BS:
          if (isHierarchical()) {
            TagTreeItem current = (TagTreeItem) viewer.getInput();
            if (current.getParent() != null) {
              viewer.setInput(current.getParent());
              refresh();
            }
          }
          break;
      }
    }
 /** {@inheritDoc} */
 public boolean selectionChanged(IStructuredSelection selection) {
   if (selection.size() == 1 && selection.getFirstElement() instanceof IScriptProject) {
     fSelectedProject = (IScriptProject) selection.getFirstElement();
     return true;
   }
   return false;
 }
 /*
  * (non-Javadoc)
  *
  * @see org.talend.commons.ui.swt.actions.ITreeContextualAction#init(org.eclipse.jface.viewers.TreeViewer,
  * org.eclipse.jface.viewers.IStructuredSelection)
  */
 @Override
 public void init(TreeViewer viewer, IStructuredSelection selection) {
   boolean canWork = !selection.isEmpty() && selection.size() == 1;
   IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
   if (factory.isUserReadOnlyOnCurrentProject()) {
     canWork = false;
   }
   if (canWork) {
     Object o = selection.getFirstElement();
     RepositoryNode node = (RepositoryNode) o;
     switch (node.getType()) {
       case SIMPLE_FOLDER:
       case SYSTEM_FOLDER:
         ERepositoryObjectType nodeType =
             (ERepositoryObjectType) node.getProperties(EProperties.CONTENT_TYPE);
         if (nodeType != ERepositoryObjectType.PIG_UDF) {
           canWork = false;
         }
         if (node.getObject() != null && node.getObject().isDeleted()) {
           canWork = false;
         }
         break;
       default:
         canWork = false;
     }
     if (canWork && !ProjectManager.getInstance().isInCurrentMainProject(node)) {
       canWork = false;
     }
   }
   setEnabled(canWork);
 }
  private void addActionsForSelection(IMenuManager menuManager) {
    IStructuredSelection selection = (IStructuredSelection) getViewer().getSelection();

    if (selection.size() == 1) {
      Object element = selection.getFirstElement();

      if (!(element instanceof IMarker)) {
        return;
      }

      final IMarker marker = (IMarker) element;

      IMarkerResolution[] resolutions = IDE.getMarkerHelpRegistry().getResolutions(marker);

      for (final IMarkerResolution resolution : resolutions) {
        Action action =
            new Action(escapeSpecialChars(resolution.getLabel())) {
              @Override
              public void run() {
                resolution.run(marker);
              }
            };

        if (resolution instanceof IMarkerResolution2) {
          IMarkerResolution2 resolution2 = (IMarkerResolution2) resolution;
          Image image = resolution2.getImage();
          if (image != null) {
            action.setImageDescriptor(ImageDescriptor.createFromImage(image));
          }
        }

        menuManager.add(action);
      }
    }
  }
    /** {@inheritDoc} */
    public boolean selectionChanged(IStructuredSelection selection) {
      if (selection.size() != 1) return false;

      try {
        Object element = selection.getFirstElement();
        if (element instanceof IScriptProject) {
          IScriptProject project = (IScriptProject) element;
          if (BuildpathModifier.isSourceFolder(project)) {
            fSelectedProject = project;
            fSelectedElement = (IModelElement) element;
            return true;
          }
        } else if (element instanceof IProjectFragment) {
          IProjectFragment ProjectFragment = ((IProjectFragment) element);
          IScriptProject project = ProjectFragment.getScriptProject();
          if (ProjectFragment.getKind() == IProjectFragment.K_SOURCE && project != null) {
            fSelectedProject = project;
            fSelectedElement = (IModelElement) element;
            return true;
          }
        }
      } catch (ModelException e) {
        return false;
      }
      return false;
    }
 private boolean isOneFeatureSelected(IStructuredSelection selection) {
   return selection.size() == 1
       && (selection.getFirstElement() instanceof FeatureEditPart
           || selection.getFirstElement() instanceof ConnectionEditPart
           || selection.getFirstElement() instanceof FmOutlineGroupStateStorage
           || selection.getFirstElement() instanceof IFeature);
 }
 @Override
 public void fillContextMenu(IMenuManager aMenu) {
   IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
   if (selection.size() != 1) {
     return;
   }
   Object object = selection.getFirstElement();
   IFolder folder = Adapters.adapt(object, IFolder.class);
   if (folder == null) {
     return;
   }
   if (folder.getFile(IProjectDescription.DESCRIPTION_FILE_NAME).exists()) {
     for (IProject project : folder.getWorkspace().getRoot().getProjects()) {
       if (project.getLocation().equals(folder.getLocation())) {
         // project already in workspace
         SelectProjectForFolderAction action =
             new SelectProjectForFolderAction(project, this.viewer);
         aMenu.appendToGroup(ICommonMenuConstants.GROUP_OPEN, action);
         return;
       }
     }
     OpenFolderAsProjectAction action = new OpenFolderAsProjectAction(folder, this.viewer);
     aMenu.prependToGroup(ICommonMenuConstants.GROUP_PORT, action);
   }
 }
  /**
   * Action is enabled when only items of the same type are selected.
   *
   * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
   *     org.eclipse.jface.viewers.ISelection)
   */
  public void selectionChanged(IWorkbenchPart part, ISelection input) {
    if (input instanceof IStructuredSelection) {
      IStructuredSelection selection = (IStructuredSelection) input;

      if (selection.size() < 2) {
        setEnabled(false);
        return;
      }

      String kapitel = null;
      for (Iterator iter = selection.iterator(); iter.hasNext(); ) {
        Object o = iter.next();
        if (o instanceof BausteinUmsetzung) {
          BausteinUmsetzung bst = (BausteinUmsetzung) o;
          if (kapitel == null) {
            kapitel = bst.getKapitel();
          } else {
            if (!bst.getKapitel().equals(kapitel)) {
              setEnabled(false);
              return;
            }
          }
        } else {
          setEnabled(false);
          return;
        }
      }
      if (checkRights()) {
        setEnabled(true);
      }
      return;
    }
    // no structured selection:
    setEnabled(false);
  }
  /**
   * Validates if the drop is valid by validating the local selection transfer to ensure that a
   * watch expression can be created for each contained IVariable.
   *
   * @param target target of the drop
   * @return whether the drop is valid
   */
  private boolean validateVariableDrop(Object target) {
    // Target must be null or an IExpression, you cannot add a new watch expression inside another
    if (target != null && getTargetExpression(target) == null) {
      return false;
    }

    IStructuredSelection selection =
        (IStructuredSelection) LocalSelectionTransfer.getTransfer().getSelection();
    int enabled = 0;
    int size = -1;
    if (selection != null) {
      size = selection.size();
      IExpressionManager manager = DebugPlugin.getDefault().getExpressionManager();
      Iterator iterator = selection.iterator();
      while (iterator.hasNext()) {
        Object element = iterator.next();
        if (element instanceof IVariable) {
          IVariable variable = (IVariable) element;
          if (variable instanceof IndexedVariablePartition) {
            break;
          } else if (manager.hasWatchExpressionDelegate(variable.getModelIdentifier())
              && isFactoryEnabled(variable)) {
            enabled++;
          } else {
            break;
          }
        }
      }
    }
    return enabled == size;
  }
  public boolean show(ShowInContext context) {
    ISelection selection = context.getSelection();
    if (selection instanceof IStructuredSelection) {
      IStructuredSelection structuredSelection = ((IStructuredSelection) selection);
      if (structuredSelection.size() >= 1) {
        List<Object> input = new ArrayList<Object>();
        for (Iterator<?> iter = structuredSelection.iterator(); iter.hasNext(); ) {
          Object item = iter.next();
          if (item instanceof IJavaElement
              || item instanceof IResource
              || item instanceof IJarEntryResource) {
            input.add(item);
          }
        }
        if (input.size() > 0) {
          setInput(input);
          return true;
        }
      }
    }

    Object input = context.getInput();
    if (input instanceof IEditorInput) {
      Object elementOfInput = getElementOfInput((IEditorInput) context.getInput());
      if (elementOfInput != null) {
        setSingleInput(elementOfInput);
        return true;
      }
    }

    return false;
  }
 /* (non-Javadoc)
  * @see org.eclipse.debug.ui.contexts.IDebugContextListener#debugContextChanged(org.eclipse.debug.ui.contexts.DebugContextEvent)
  */
 public void debugContextChanged(DebugContextEvent event) {
   if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) {
     IWorkbenchPart part = event.getDebugContextProvider().getPart();
     if (part != null) {
       IWorkbenchPage page = part.getSite().getPage();
       ISelection selection = event.getContext();
       if (selection instanceof IStructuredSelection) {
         IStructuredSelection ss = (IStructuredSelection) selection;
         if (ss.size() == 1) {
           Object element = ss.getFirstElement();
           if (element instanceof IAdaptable) {
             IJavaScriptStackFrame frame =
                 (IJavaScriptStackFrame)
                     ((IAdaptable) element).getAdapter(IJavaScriptStackFrame.class);
             if (frame != null) {
               setContext(page, frame);
               return;
             }
           }
         }
       }
       // no context in the given view
       removeContext(page);
     }
   }
 }
  /**
   * Returns true if given structured selection matches the conditions specified in the registry for
   * this action.
   */
  private boolean isEnabledFor(IStructuredSelection ssel) {
    int count = ssel.size();

    if (verifySelectionCount(count) == false) {
      return false;
    }

    // Compare selection to enablement expression.
    if (enablementExpression != null) {
      return enablementExpression.isEnabledFor(ssel);
    }

    // Compare selection to class requirements.
    if (classes.isEmpty()) {
      return true;
    }
    for (Iterator elements = ssel.iterator(); elements.hasNext(); ) {
      Object obj = elements.next();
      if (obj instanceof IAdaptable) {
        IAdaptable element = (IAdaptable) obj;
        if (verifyElement(element) == false) {
          return false;
        }
      } else {
        return false;
      }
    }

    return true;
  }
 private void addOpenWithMenu(IMenuManager menu) {
   ISelection selection = getContext().getSelection();
   if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) return;
   IStructuredSelection ss = (IStructuredSelection) selection;
   if (ss.size() != 1) return;
   Object o = ss.getFirstElement();
   if (!(o instanceof IAdaptable)) return;
   IAdaptable element = (IAdaptable) o;
   Object resource = element.getAdapter(IResource.class);
   if (element instanceof IStorage && !(resource instanceof IFile)) {
     // Create a menu.
     IMenuManager submenu = new MenuManager(ActionMessages.OpenWithMenu_label);
     submenu.add(new OpenStorageWithMenu(fSite.getPage(), element));
     // Add the submenu.
     menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
   } else if ((element instanceof IModelElement)
       || (element.getAdapter(IModelElement.class) != null)) {
     // Create a menu.
     IMenuManager submenu = new MenuManager(ActionMessages.OpenWithMenu_label);
     submenu.add(
         new OpenModelElementWithMenu(
             fSite.getPage(), (IModelElement) element.getAdapter(IModelElement.class)));
     // Add the submenu.
     menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
   } else if ((resource instanceof IFile)) {
     // Create a menu.
     IMenuManager submenu = new MenuManager(ActionMessages.OpenWithMenu_label);
     submenu.add(new OpenWithMenu(fSite.getPage(), (IFile) resource));
     // Add the submenu.
     menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
   }
 }
 public boolean appliesTo(ISelection selection, IConfigurationElement configuration) {
   if (selection instanceof IStructuredSelection) {
     IStructuredSelection ss = ((IStructuredSelection) selection);
     if (ss.size() <= 0 || ss.size() > 1) {
       return false;
     }
     Object first = ss.getFirstElement();
     if (first instanceof CompartmentEditPart) {
       CompartmentEditPart compEP = (CompartmentEditPart) first;
       // Check if he is really a children
       if (compEP.getParent() instanceof ListCompartmentEditPart) return true;
       else return false;
     }
   }
   return false;
 }
  private IResource getElement(IStructuredSelection selection) {
    if (selection.size() != 1) return null;

    Object element = selection.getFirstElement();
    if (!(element instanceof IAdaptable)) return null;
    return (IResource) ((IAdaptable) element).getAdapter(IResource.class);
  }
Exemple #27
0
  private static File[] convertSelectionToFiles(ISelection selection) {
    if (!(selection instanceof IStructuredSelection)) return new File[0];

    IStructuredSelection structSel = (IStructuredSelection) selection;
    List<File> files = new ArrayList<File>(structSel.size());

    for (Iterator<?> iter = structSel.iterator(); iter.hasNext(); ) {
      Object element = iter.next();
      if (element instanceof IFile) files.add(((IFile) element).getLocation().toFile());
      else if (element instanceof IAdaptable) {
        IAdaptable adaptable = (IAdaptable) element;
        IFile ifile = (IFile) adaptable.getAdapter(IFile.class);
        if (ifile != null) {
          files.add(ifile.getLocation().toFile());
        } else {
          File file = (File) adaptable.getAdapter(File.class);
          if (file != null) {
            files.add(file);
          }
        }
      }
    }

    return files.toArray(new File[files.size()]);
  }
 /*
  * (non-Javadoc)
  *
  * @see org.talend.repository.ui.actions.ITreeContextualAction#init(org.eclipse.jface.viewers.TreeViewer,
  * org.eclipse.jface.viewers.IStructuredSelection)
  */
 @Override
 public void init(TreeViewer viewer, IStructuredSelection selection) {
   super.init(viewer, selection);
   boolean canWork = !selection.isEmpty() && selection.size() == 1;
   IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
   if (factory.isUserReadOnlyOnCurrentProject()) {
     canWork = false;
   }
   RepositoryNode node = (RepositoryNode) selection.getFirstElement();
   if (canWork) {
     if (node.getObjectType() != ERepositoryObjectType.ROUTINES
         || !ProjectManager.getInstance().isInCurrentMainProject(node)
         || !isLastVersion(node)) {
       canWork = false;
     } else {
       Item item = node.getObject().getProperty().getItem();
       if (item instanceof RoutineItem) {
         canWork = !((RoutineItem) item).isBuiltIn();
       }
     }
   }
   if (canWork) {
     canWork = (factory.getStatus(node.getObject()) != ERepositoryStatus.DELETED);
   }
   setEnabled(canWork);
 }
  @Override
  protected boolean canHandle(IStructuredSelection selection) {
    if (selection.size() != 1) return false;

    if (!(selection.getFirstElement() instanceof IJavaProject)) return false;

    return true;
  }
 /**
  * Returns whether "go into" is possible for child tree. This is only possible if the current
  * selection in the client has one item and it has children.
  *
  * @return <code>true</code> if "go into" is possible; <code>false</code> otherwise
  */
 public boolean canGoInto() {
   IStructuredSelection oSelection = (IStructuredSelection) fChildTree.getSelection();
   if (oSelection == null || oSelection.size() != 1) {
     return false;
   }
   Object anElement = oSelection.getFirstElement();
   return canExpand(anElement);
 }