Java Code Examples for org.eclipse.core.expressions.IEvaluationContext#getVariable()

The following examples show how to use org.eclipse.core.expressions.IEvaluationContext#getVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractViewEditorHandler.java    From depan with Apache License 2.0 6 votes vote down vote up
@Override
public void setEnabled(Object evalContext) {
  if (null == evalContext) {
    isEnabled = false;
    return;
  }

  IEvaluationContext context = (IEvaluationContext) evalContext;
  Object editor = context.getVariable(ISources.ACTIVE_EDITOR_NAME);
  if (editor instanceof ViewEditor) {
    isEnabled = true;
    return;
  }

  isEnabled = false;
  return;
}
 
Example 2
Source File: ThemeContribution.java    From tm4e with Eclipse Public License 1.0 5 votes vote down vote up
private static IEditorPart getActivePart(IEvaluationContext context) {
	if (context == null)
		return null;

	Object activePart = context.getVariable(ISources.ACTIVE_PART_NAME);
	if ((activePart instanceof IEditorPart))
		return (IEditorPart) activePart;

	return null;
}
 
Example 3
Source File: DeleteEditorFileHandler.java    From eclipse-extras with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isEnabled( IEvaluationContext evaluationContext ) {
  Object variable = evaluationContext.getVariable( ISources.ACTIVE_EDITOR_INPUT_NAME );
  boolean result = false;
  if( variable instanceof IEditorInput ) {
    IEditorInput editorInput = ( IEditorInput )variable;
    result = ResourceUtil.getFile( editorInput ) != null || getFile( editorInput ) != null;
  }
  return result;
}
 
Example 4
Source File: BaseHandler.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setEnabled(Object evaluationContext)
{
	// clear cached selection
	this.clearFileStores();

	if (evaluationContext instanceof IEvaluationContext)
	{
		IEvaluationContext context = (IEvaluationContext) evaluationContext;
		Object value = context.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);

		if (value instanceof ISelection)
		{
			ISelection selection = (ISelection) value;

			if (selection instanceof IStructuredSelection && selection.isEmpty() == false)
			{
				IStructuredSelection structuredSelection = (IStructuredSelection) selection;

				for (Object object : structuredSelection.toArray())
				{
					if (object instanceof IProject || object instanceof IFolder || object instanceof IFile)
					{
						IResource resource = (IResource) object;
						IFileStore fileStore = EFSUtils.getFileStore(resource);

						if (this.isValid(fileStore))
						{
							this.addFileStore(fileStore);
						}
					}
				}
			}
		}
	}
}
 
Example 5
Source File: UIUtils.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public static IResource getSelectedResource(IEvaluationContext evaluationContext)
{
	if (evaluationContext == null)
	{
		return null;
	}

	Object variable = evaluationContext.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME);
	if (variable instanceof IStructuredSelection)
	{
		Object selectedObject = ((IStructuredSelection) variable).getFirstElement();
		if (selectedObject instanceof IAdaptable)
		{
			IResource resource = (IResource) ((IAdaptable) selectedObject).getAdapter(IResource.class);
			if (resource != null)
			{
				return resource;
			}
		}
	}
	else
	{
		// checks the active editor
		variable = evaluationContext.getVariable(ISources.ACTIVE_EDITOR_NAME);
		if (variable instanceof IEditorPart)
		{
			IEditorInput editorInput = ((IEditorPart) variable).getEditorInput();
			if (editorInput instanceof IFileEditorInput)
			{
				return ((IFileEditorInput) editorInput).getFile();
			}
		}
	}
	return null;
}
 
Example 6
Source File: UIUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the object from the context through the key.If the value is a Object
 * return null.
 * 
 * @param context
 * @param key
 * @return
 */
public static Object getVariableFromContext( IEvaluationContext context,
		String key )
{
	Object retValue = context.getVariable( key );
	if ( retValue == null )
	{
		return null;
	}
	if ( retValue.getClass( ).getName( ).equals( "java.lang.Object" ) )//$NON-NLS-1$
	{
		retValue = null;
	}
	return retValue;
}
 
Example 7
Source File: StartLaunchHandler.java    From tlaplus with MIT License 4 votes vote down vote up
private ModelEditor getModelEditor(final IEvaluationContext context) {
	// is current editor a model editor?
	Object variable = context.getVariable(ISources.ACTIVE_EDITOR_ID_NAME);
	final String id = (variable != IEvaluationContext.UNDEFINED_VARIABLE) ? (String)variable : null;
	
	if ((id != null) && (id.startsWith(ModelEditor.ID))) {
		variable = context.getVariable(ISources.ACTIVE_EDITOR_NAME);
		
		if (variable instanceof IEditorPart) {
			lastModelEditor = (ModelEditor)variable;
		}
	}
	// If lastModelEditor is still null, it means we haven't run the model
	// checker yet AND the model editor view is *not* active. Lets search
	// through all editors to find a model checker assuming only a single one
	// is open right now. If more than one model editor is open, randomly
	// select one. In case it's not the one intended to be run by the user,
	// she has to activate the correct model editor manually.
	//
	// It is tempting to store the name of the lastModelEditor
	// in e.g. an IDialogSetting to persistently store even across Toolbox
	// restarts. However, the only way to identify a model editor here is by
	// its name and almost all model editors carry the name "Model_1" (the
	// default name). So we might end up using Model_1 which was the last
	// model that ran for spec A, but right now spec B and two of its model
	// editors are open ("Model_1" and "Model_2"). It would launch Model_1,
	// even though Model_2 might be what the user wants.
	if (lastModelEditor == null) {
		final IWorkbenchWindow workbenchWindow = (IWorkbenchWindow) context
				.getVariable(ISources.ACTIVE_WORKBENCH_WINDOW_NAME);

		for (final IWorkbenchPage page : workbenchWindow.getPages()) {
			for (final IEditorReference editorRefs : page.getEditorReferences()) {
				if (editorRefs.getId().equals(ModelEditor.ID)) {
					lastModelEditor = (ModelEditor) editorRefs.getEditor(true);
					break;
				}
			}
		}
	}
	// Validate that the lastModelEditor still belongs to the current
	// open spec. E.g. lastModelEditor might still be around from when
	// the user ran a it on spec X, but has switched to spec Y in the
	// meantime. Closing the spec nulls the ModelEditor
	if ((lastModelEditor != null) && lastModelEditor.isDisposed()) {
		lastModelEditor = null;
	}
	
	// If the previous two attempts to find a model editor have failed, lets
	// return whatever we have... which might be null.
	return lastModelEditor;
}
 
Example 8
Source File: CloseViewHandler.java    From eclipse-extras with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean isEnabled( IEvaluationContext evaluationContext ) {
  Object activePart = evaluationContext.getVariable( ISources.ACTIVE_PART_NAME );
  return activePart instanceof IViewPart;
}
 
Example 9
Source File: KeybindingsManager.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
private boolean processKeyStroke(Event event, KeyStroke keyStroke)
{
	IBindingService bindingService = (IBindingService) workbench.getService(IBindingService.class);
	KeySequence sequenceBeforeKeyStroke = state.getCurrentSequence();
	KeySequence sequenceAfterKeyStroke = KeySequence.getInstance(sequenceBeforeKeyStroke, keyStroke);

	if (uniqueKeySequences.contains(sequenceAfterKeyStroke))
	{
		IEvaluationService evaluationService = (IEvaluationService) workbench.getService(IEvaluationService.class);
		IEvaluationContext evaluationContext = evaluationService.getCurrentState();
		IWorkbenchPart workbenchPart = (IWorkbenchPart) evaluationContext.getVariable(ISources.ACTIVE_PART_NAME);
		ICommandElementsProvider commandElementsProvider = (ICommandElementsProvider) workbenchPart
				.getAdapter(ICommandElementsProvider.class);
		if (commandElementsProvider != null)
		{
			// Is there a Eclipse binding that matches the key sequence?
			Binding binding = null;
			if (bindingService.isPerfectMatch(sequenceAfterKeyStroke))
			{
				// Record it
				binding = bindingService.getPerfectMatch(sequenceAfterKeyStroke);
			}

			List<CommandElement> commandElements = commandElementsProvider
					.getCommandElements(sequenceAfterKeyStroke);
			if (commandElements.size() == 0)
			{
				if (binding == null)
				{
					// Remember the prefix
					incrementState(sequenceAfterKeyStroke);
				}
				else
				{
					// Reset our state
					resetState();
				}

				// Do not consume the event. Let Eclipse handle it.
				return false;
			}
			else
			{
				if (binding == null && commandElements.size() == 1)
				{
					// We have a unique scripting command to execute
					executeCommandElement(commandElementsProvider, commandElements.get(0));

					// Reset our state
					resetState();

					// The event should be consumed
					return true;
				}
				else
				{
					// We need to show commands menu to the user
					IContextService contextService = (IContextService) workbench.getService(IContextService.class);
					popup(workbenchPart.getSite().getShell(), bindingService, contextService,
							commandElementsProvider, commandElements, event, binding,
							getInitialLocation(commandElementsProvider));

					// Reset our state
					resetState();

					// The event should be consumed
					return true;
				}
			}
		}
	}
	else if (uniqueKeySequencesPrefixes.contains(sequenceAfterKeyStroke))
	{
		// Prefix match

		// Is there a Eclipse command with a perfect match
		if (bindingService.isPerfectMatch(sequenceAfterKeyStroke))
		{
			// Reset our state
			resetState();
		}
		else
		{
			// Remember the prefix
			incrementState(sequenceAfterKeyStroke);
		}
	}
	else
	{
		// Reset our state
		resetState();
	}

	// We did not handle the event. Do not consume the event. Let Eclipse handle it.
	return false;
}