下面列出了怎么用org.eclipse.ui.ISources的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void setEnabled(Object evaluationContext) {
super.setEnabled(evaluationContext);
fEnabled = true;
Object s = HandlerUtil.getVariable(evaluationContext, ISources.ACTIVE_MENU_SELECTION_NAME);
if (s instanceof IStructuredSelection) {
IStructuredSelection selection = (IStructuredSelection) s;
// If we have traces selected, make sure they are all from the same
// project, disable handler otherwise
Object[] selectedElements = selection.toArray();
TmfProjectElement firstProject = null;
for (Object selectedElement : selectedElements) {
if (selectedElement instanceof TmfTraceElement) {
TmfTraceElement tmfTraceElement = (TmfTraceElement) selectedElement;
TmfProjectElement project = tmfTraceElement.getProject();
if (firstProject != null && !project.equals(firstProject)) {
fEnabled = false;
}
firstProject = project;
}
}
}
}
@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;
}
/**
* Change the UI state based on the state of the simulation (none, stopped,
* running or notready)
*/
@Override
public void updateStateTo(final String state) {
fireSourceChanged(ISources.WORKBENCH, SIMULATION_RUNNING_STATE, state);
final IExperimentPlan exp = GAMA.getExperiment();
final String type = exp == null ? "NONE" : exp.isBatch() ? "BATCH" : exp.isMemorize() ? "MEMORIZE" : "REGULAR";
fireSourceChanged(ISources.WORKBENCH, SIMULATION_TYPE, type);
String canStepBack = "CANNOT_STEP_BACK";
if (exp != null) {
if (exp.getAgent() != null) {
canStepBack = exp.getAgent().canStepBack() ? "CAN_STEP_BACK" : "CANNOT_STEP_BACK";
}
}
fireSourceChanged(ISources.WORKBENCH, SIMULATION_STEPBACK, canStepBack);
}
@Override
public void run(IAction action) {
System.err.println("In run(IACtion)");
if (serviceLocator == null) {
serviceLocator = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}
// Create an ExecutionEvent using Eclipse machinery.
ICommandService srv = (ICommandService) serviceLocator.getService(ICommandService.class);
IHandlerService hsrv = (IHandlerService) serviceLocator.getService(IHandlerService.class);
ExecutionEvent event = hsrv.createExecutionEvent(srv.getCommand(ActionCommands.COMPARE_WITH_ROUTE_ACTION), null);
// Fill it my current active selection.
if (event.getApplicationContext() instanceof IEvaluationContext) {
((IEvaluationContext) event.getApplicationContext()).addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, mySelection);
}
try {
handler.execute(event);
} catch (ExecutionException e) {
Activator.handleError(e.getMessage(), e, true);
}
}
@Override
public void run(IAction action) {
if (serviceLocator == null) {
serviceLocator = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}
// Create an ExecutionEvent using Eclipse machinery.
ICommandService srv = (ICommandService) serviceLocator.getService(ICommandService.class);
IHandlerService hsrv = (IHandlerService) serviceLocator.getService(IHandlerService.class);
ExecutionEvent event = hsrv.createExecutionEvent(srv.getCommand(ActionCommands.PERSIST_TO_ROUTE_MODEL_ACTION), null);
// Fill it my current active selection.
if (event.getApplicationContext() instanceof IEvaluationContext) {
((IEvaluationContext) event.getApplicationContext()).addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, mySelection);
}
try {
handler.execute(event);
} catch (ExecutionException e) {
Activator.handleError(e.getMessage(), e, true);
}
}
@Override
public void run(IAction action) {
System.err.println("In run(IACtion)");
if (serviceLocator == null) {
serviceLocator = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}
// Create an ExecutionEvent using Eclipse machinery.
ICommandService srv = (ICommandService) serviceLocator.getService(ICommandService.class);
IHandlerService hsrv = (IHandlerService) serviceLocator.getService(IHandlerService.class);
ExecutionEvent event = hsrv.createExecutionEvent(srv.getCommand(ActionCommands.COMPARE_WITH_ROUTE_ACTION), null);
// Fill it my current active selection.
if (event.getApplicationContext() instanceof IEvaluationContext) {
((IEvaluationContext) event.getApplicationContext()).addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, mySelection);
}
try {
handler.execute(event);
} catch (ExecutionException e) {
Activator.handleError(e.getMessage(), e, true);
}
}
@Override
public void run(IAction action) {
if (serviceLocator == null) {
serviceLocator = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}
// Create an ExecutionEvent using Eclipse machinery.
ICommandService srv = (ICommandService) serviceLocator.getService(ICommandService.class);
IHandlerService hsrv = (IHandlerService) serviceLocator.getService(IHandlerService.class);
ExecutionEvent event = hsrv.createExecutionEvent(srv.getCommand(ActionCommands.PERSIST_TO_ROUTE_MODEL_ACTION), null);
// Fill it my current active selection.
if (event.getApplicationContext() instanceof IEvaluationContext) {
((IEvaluationContext) event.getApplicationContext()).addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, mySelection);
}
try {
handler.execute(event);
} catch (ExecutionException e) {
Activator.handleError(e.getMessage(), e, true);
}
}
protected Object getFirstSelectVariable( )
{
IEvaluationContext context = (IEvaluationContext) event.getApplicationContext( );
Object selectVariable = UIUtil.getVariableFromContext( context, ISources.ACTIVE_CURRENT_SELECTION_NAME );
Object selectList = selectVariable;
if ( selectVariable instanceof StructuredSelection )
{
selectList = ( (StructuredSelection) selectVariable ).toList( );
}
if ( selectList instanceof List && ( (List) selectList ).size( ) > 0 )
{
selectVariable = getFirstElement( (List) selectList );
}
return selectVariable;
}
/**
* Returns a <code>List</code> containing the currently selected objects.
*
* @return A List containing the currently selected objects.
*/
protected IStructuredSelection getSelection( )
{
IEvaluationContext context = (IEvaluationContext) event.getApplicationContext( );
Object selectVariable = UIUtil.getVariableFromContext( context, ISources.ACTIVE_CURRENT_SELECTION_NAME );
if ( selectVariable != null )
{
if ( selectVariable instanceof IStructuredSelection )
{
return (IStructuredSelection) selectVariable;
}
else
{
return new StructuredSelection( selectVariable );
}
}
return null;
}
@Override
public void defaultKeyChanged ( final KeyInformation key, final Date validUntil )
{
logger.debug ( "Default key changed: {} -> {}", key, validUntil );
fireSourceChanged ( ISources.WORKBENCH, getCurrentState () );
}
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;
}
protected void renameFooToFooBar(final XtextEditor contextEditor) throws Exception {
contextEditor.getEditorSite().getPage().activate(contextEditor);
waitForDisplay();
IXtextDocument document = contextEditor.getDocument();
final int offset = document.get().indexOf("foo");
contextEditor.selectAndReveal(offset, 3);
EvaluationContext evaluationContext = new EvaluationContext(null, new Object());
evaluationContext.addVariable(ISources.ACTIVE_EDITOR_NAME, contextEditor);
ExecutionEvent executionEvent = new ExecutionEvent(null, newHashMap(), null, evaluationContext);
renameElementHandler.execute(executionEvent);
// syncUtil.totalSync(refactoringPreferences.isSaveAllBeforeRefactoring());
// IRenameElementContext context = document.readOnly(new IUnitOfWork<IRenameElementContext, XtextResource>() {
// public IRenameElementContext exec(XtextResource state) throws Exception {
// EObject target = eObjectAtOffsetHelper.resolveElementAt(state, offset);
// return renameContextFactory.createRenameElementContext(target, contextEditor, new TextSelection(offset,
// 3), state);
// }
// });
// controller.initialize(context);
// waitForDisplay();
// controller.startRefactoring(RefactoringType.LINKED_EDITING);
// waitForDisplay();
pressKeys(contextEditor, "fooBar\n");
waitForDisplay();
waitForReconciler(fooEditor);
waitForReconciler(barEditor);
waitForDisplay();
}
@Override
public void setEnabled(Object evaluationContext) {
boolean contentAssistAvailable = false;
if (evaluationContext instanceof IEvaluationContext) {
Object var = ((IEvaluationContext) evaluationContext).getVariable(ISources.ACTIVE_EDITOR_NAME);
if (var instanceof XtextEditor) {
contentAssistAvailable = ((XtextEditor) var).isContentAssistAvailable();
}
}
super.setBaseEnabled(isEnabled() & contentAssistAvailable);
}
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;
}
private IEvaluationContext createEvaluationContext( ISelection selection ) {
IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
IEvaluationContext result = new EvaluationContext( null, new Object() );
result.addVariable( ISources.ACTIVE_WORKBENCH_WINDOW_NAME, activeWorkbenchWindow );
result.addVariable( ISources.ACTIVE_CURRENT_SELECTION_NAME, selection );
return result;
}
private IEvaluationContext createEvaluationContext( Object editorInput ) {
IWorkbenchWindow activeWorkbenchWindow = workbenchPage.getWorkbenchWindow();
IEvaluationContext result = new EvaluationContext( null, new Object() );
result.addVariable( ISources.ACTIVE_WORKBENCH_WINDOW_NAME, activeWorkbenchWindow );
if( editorInput != null ) {
result.addVariable( ISources.ACTIVE_EDITOR_INPUT_NAME, editorInput );
}
return result;
}
private IEvaluationContext createEvaluationContext( Object editorInput ) {
IWorkbenchWindow activeWorkbenchWindow = workbenchPage.getWorkbenchWindow();
IEvaluationContext result = new EvaluationContext( null, new Object() );
result.addVariable( ISources.ACTIVE_WORKBENCH_WINDOW_NAME, activeWorkbenchWindow );
if( editorInput != null ) {
result.addVariable( ISources.ACTIVE_EDITOR_INPUT_NAME, editorInput );
}
return result;
}
private IEvaluationContext createEvaluationContext( IWorkbenchPart activePart ) {
IWorkbenchWindow activeWorkbenchWindow = workbenchPage.getWorkbenchWindow();
IEvaluationContext result = new EvaluationContext( null, new Object() );
result.addVariable( ISources.ACTIVE_WORKBENCH_WINDOW_NAME, activeWorkbenchWindow );
if( activePart != null ) {
result.addVariable( ISources.ACTIVE_PART_NAME, activePart );
}
return result;
}
@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);
}
}
}
}
}
}
}
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;
}
private List<TextHoverDescriptor> getEnabledTextHoverDescriptors(ITextViewer textViewer, int offset)
{
List<TextHoverDescriptor> result = new ArrayList<TextHoverDescriptor>();
if (fTextEditor == null)
{
return result;
}
try
{
QualifiedContentType contentType = CommonEditorPlugin.getDefault().getDocumentScopeManager()
.getContentType(textViewer.getDocument(), offset);
IEvaluationContext context = new EvaluationContext(null, textViewer);
IWorkbenchPartSite site = fTextEditor.getSite();
if (site != null)
{
context.addVariable(ISources.ACTIVE_EDITOR_ID_NAME, site.getId());
}
for (TextHoverDescriptor descriptor : TextHoverDescriptor.getContributedHovers())
{
if (descriptor.isEnabledFor(contentType, context))
{
result.add(descriptor);
}
}
}
catch (BadLocationException e)
{
}
return result;
}
@Override
public void setEnabled(Object evaluationContext)
{
Object activeSite = ((IEvaluationContext) evaluationContext).getVariable(ISources.ACTIVE_SITE_NAME);
Object activeEditor = ((IEvaluationContext) evaluationContext).getVariable(ISources.ACTIVE_EDITOR_NAME);
if (activeSite instanceof IWorkbenchSite && activeEditor instanceof AbstractThemeableEditor)
{
ICommandService commandService = (ICommandService) ((IWorkbenchSite) activeSite)
.getService(ICommandService.class);
Command command = commandService.getCommand(COMMAND_ID);
State state = command.getState(RegistryToggleState.STATE_ID);
state.setValue(((AbstractThemeableEditor) activeEditor).getWordWrapEnabled());
}
}
private final void connectionChanged() {
SWTUtils.runSafeSWTAsync(
null,
new Runnable() {
@Override
public void run() {
fireSourceChanged(ISources.WORKBENCH, SAROS, saros);
}
});
}
private final void sessionChanged(final ISarosSession session) {
SWTUtils.runSafeSWTAsync(
null,
new Runnable() {
@Override
public void run() {
fireSourceChanged(
ISources.WORKBENCH,
SAROS_SESSION,
session == null ? IEvaluationContext.UNDEFINED_VARIABLE : session);
}
});
}
public void setState(boolean state){
String value = FALSE;
if (state == true) {
value = TRUE;
} else {
value = FALSE;
}
fireSourceChanged(ISources.WORKBENCH, PATIENTACTIVE, value);
}
public void setLoginSession ( final LoginSession session )
{
this.session = session;
fireSourceChanged ( ISources.WORKBENCH, getCurrentState () );
}
public ExecutionEventBuilder withActiveShell(Shell shell) {
when(context.getVariable(ISources.ACTIVE_SHELL_NAME)).thenReturn(shell);
return this;
}
public ExecutionEventBuilder withCurrentSelection(IStructuredSelection selection) {
when(context.getVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME)).thenReturn(selection);
return this;
}
public void setEnabled() {
fireSourceChanged(ISources.WORKBENCH, ID, ENABLED);
}
public void fireBookmarkSet() {
markerCount++;
fireSourceChanged(ISources.WORKBENCH, EXIST_STATE, TRUE);
}