Java Code Examples for org.eclipse.ui.IEditorPart#getEditorSite()

The following examples show how to use org.eclipse.ui.IEditorPart#getEditorSite() . 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: ServiceUtils.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an OSGi service from {@link ExecutionEvent}. It looks up a service in the following
 * locations (if exist) in the given order:
 *
 * {@code HandlerUtil.getActiveSite(event)}
 * {@code HandlerUtil.getActiveEditor(event).getEditorSite()}
 * {@code HandlerUtil.getActiveEditor(event).getSite()}
 * {@code HandlerUtil.getActiveWorkbenchWindow(event)}
 * {@code PlatformUI.getWorkbench()}
 */
public static <T> T getService(ExecutionEvent event, Class<T> api) {
  IWorkbenchSite activeSite = HandlerUtil.getActiveSite(event);
  if (activeSite != null) {
    return activeSite.getService(api);
  }

  IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
  if (activeEditor != null) {
    IEditorSite editorSite = activeEditor.getEditorSite();
    if (editorSite != null) {
      return editorSite.getService(api);
    }
    IWorkbenchPartSite site = activeEditor.getSite();
    if (site != null) {
      return site.getService(api);
    }
  }

  IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
  if (workbenchWindow != null) {
    return workbenchWindow.getService(api);
  }

  return PlatformUI.getWorkbench().getService(api);
}
 
Example 2
Source File: QuickXFind.java    From xds-ide with Eclipse Public License 1.0 6 votes vote down vote up
public static void findPrevious(IEditorPart editorPart) {
    IFindReplaceTarget target = (IFindReplaceTarget) editorPart.getAdapter(IFindReplaceTarget.class);
    if (target == null) 
        return;

    StatusLine statusLine = new StatusLine(editorPart.getEditorSite());
    statusLine.cleanStatusLine();

    SearchRegion region = getSearchRegion(target, editorPart);
    if (region == null) 
        return;
    
    int offset = Math.max(region.offset - 1, 0);
    
    offset = findAndSelect(target, offset, region.text, false);
    
    if (offset < 0) {
        String message = String.format(Messages.QuickFind_Status_FirstOccurence, region.text);
        statusLine.showMessage(message,  ImageUtils.getImage(ImageUtils.FIND_STATUS));
    }
}
 
Example 3
Source File: QuickXFind.java    From xds-ide with Eclipse Public License 1.0 6 votes vote down vote up
public static void findNext(IEditorPart editorPart) {
    IFindReplaceTarget target = (IFindReplaceTarget) editorPart.getAdapter(IFindReplaceTarget.class);
    if (target == null) 
        return;
    
    StatusLine statusLine = new StatusLine(editorPart.getEditorSite());
    statusLine.cleanStatusLine();

    SearchRegion region = getSearchRegion(target, editorPart);
    if (region == null) 
        return;
    
    int offset = region.offset + region.length;

    offset = findAndSelect(target, offset, region.text, true);

    if (offset < 0) {
        String message = String.format(Messages.QuickFind_Status_LastOccurence, region.text);
        statusLine.showMessage(message, ImageUtils.getImage(ImageUtils.FIND_STATUS));
    }
}
 
Example 4
Source File: OpenEditorTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void assertActiveEditor(String expectedEditorID, String expectedEditorTitle, final String expectedSelection) {
	IEditorPart editorPart = workbench.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
	assertEquals(expectedEditorTitle, editorPart.getTitle());
	IEditorSite editorSite = editorPart.getEditorSite();
	assertEquals(expectedEditorID, editorSite.getId());
	final ISelectionProvider selectionProvider = editorSite.getSelectionProvider();
	assertTrue(selectionProvider.getSelection() instanceof ITextSelection);
	
	// The selection may be updated asynchronously, so we may have to wait until the selection changes
	workbenchTestHelper.awaitUIUpdate(new Functions.Function0<Boolean>() {
		@Override
		public Boolean apply() {
			return expectedSelection.equals(((ITextSelection) selectionProvider.getSelection()).getText());
		}
	}, SELECTION_TIMEOUT);
	assertEquals(expectedSelection, ((ITextSelection) selectionProvider.getSelection()).getText());
}
 
Example 5
Source File: MultiPageReportEditor.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
private void bingdingKey( int newPageIndex )
{
	final IKeyBindingService service = getSite( ).getKeyBindingService( );
	final IEditorPart editor = (IEditorPart) pages.get( newPageIndex );
	if ( editor != null && editor.getEditorSite( ) != null )
	{
		editor.setFocus( );
		// There is no selected page, so deactivate the active service.
		if ( service instanceof INestableKeyBindingService )
		{
			final INestableKeyBindingService nestableService = (INestableKeyBindingService) service;
			if ( editor != null )
			{
				nestableService.activateKeyBindingService( editor.getEditorSite( ) );
			}
			else
			{
				nestableService.activateKeyBindingService( null );
			}
		}
		else
		{

		}
	}
}
 
Example 6
Source File: XFindPanel.java    From xds-ide with Eclipse Public License 1.0 4 votes vote down vote up
public XFindPanel(final Composite parent, IEditorPart editorPart) {
      super(parent, SWT.NONE);
      statusLine = new StatusLine(editorPart.getEditorSite());
      setVisible(false);

      if (editorPart != null) {
          target = (IFindReplaceTarget) editorPart.getAdapter(IFindReplaceTarget.class);
          isRegExSupported = (target instanceof IFindReplaceTargetExtension3);
      }

      final IPreferenceStore store = XFindPlugin.getDefault().getPreferenceStore(); 
      if (!store.contains(XFIND_PANEL_PLACEMENT)) {
          store.setDefault(XFIND_PANEL_PLACEMENT, XFIND_PANEL_PLACEMENT_TOP);
      }

      if (store.getInt(XFIND_PANEL_PLACEMENT) == XFIND_PANEL_PLACEMENT_BOTTOM) {
          moveBelow(null);
      } else {
          moveAbove(null);
      }
      
      createContents();
      loadHistory(store);

      store.addPropertyChangeListener(new IPropertyChangeListener() {
          @Override
          public void propertyChange(final PropertyChangeEvent event) {
              Display.getDefault().syncExec(new Runnable() {
                  @Override
                  public void run() {
                      if (XFIND_PANEL_PLACEMENT.equals(event.getProperty())) {
                          if (store.getInt(XFIND_PANEL_PLACEMENT) == XFIND_PANEL_PLACEMENT_BOTTOM) {
                              moveBelow(null);
                          } else {
                              moveAbove(null);
                          }
                          parent.layout();
                      }
                  }
              });
          }
      });
      
      parent.addDisposeListener(new DisposeListener() {
	@Override
	public void widgetDisposed(DisposeEvent e) {
		saveHistory(store);
	}
});
  }
 
Example 7
Source File: OpenTalendJobRefAction.java    From tesb-studio-se with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
	//diable switch perspective suggestion dialog. just for defense.
	String notAskAutoSwitchToDIKey="notAskAutoSwitchToDI";
	boolean oldValueNotSwitchToDiKey=PlatformUI.getPreferenceStore().getBoolean(notAskAutoSwitchToDIKey);
	PlatformUI.getPreferenceStore().setValue(notAskAutoSwitchToDIKey, true);
	//need to be  restore at the end .


    // open in editor, type and count already checked in calculateEnabled()
    List<?> selectedObjects = getSelectedObjects();
    Object select = selectedObjects.get(0);
    NodePart nodePart = (NodePart) select;
    nodePart.performRequest(new org.eclipse.gef.Request(REQUEST_TYPE_OPEN_NODE_PART));

    IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    if(activePage==null) {
    	return;
    }
    IEditorPart activeEditor = activePage.getActiveEditor();
    if(activeEditor==null||activeEditor.getEditorSite()==null) {
    	return;
    }

    Node node = (Node) nodePart.getModel();
    String selectedJobName=(String)node.getElementParameter(NODE_PARAM_SELECTED_JOB_NAME).getValue();
    String openJobName=activeEditor.getEditorInput().getName();
    if(!selectedJobName.equals(openJobName)) {
    	return;
    }
    // open/switch editor success and then  try to switch perspective.
    try {
    	//if current perspective is ORG_TALEND_RCP_INTEGRATION_PERSPECTIVE, will do nothing in under layer.
        PlatformUI.getWorkbench().showPerspective(ORG_TALEND_RCP_INTEGRATION_PERSPECTIVE, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
    } catch (WorkbenchException e) {
        e.printStackTrace();
    }finally {
    	//Restore config of switch perspective suggestion dialog. just for defense.
    	PlatformUI.getPreferenceStore().setValue(notAskAutoSwitchToDIKey, oldValueNotSwitchToDiKey);
    }

}
 
Example 8
Source File: MarkGlobalHandler.java    From e4macs with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Get the next position off the global mark ring and move to that file and location
 *
 * @param editor
 * @param document
 * @param currentSelection
 * @param norotate - if true, pop else rotate and pop
 * @return NO_OFFSET
 * @throws BadLocationException
 */
protected int doTransform(ITextEditor editor, IDocument document, ITextSelection currentSelection, boolean norotate, boolean isTags)
throws BadLocationException {
	// get editor and offset
	IBufferLocation location = (isTags ? MarkUtils.popTagMark() : MarkUtils.popGlobalMark(norotate));
	if (location != null) {
		if (currentSelection != null &&
				location.getEditor() == editor && location.getOffset() == currentSelection.getOffset()) {
			// if we're already at the global mark location, move to next location
			// recurse with no selection to avoid infinite loop if only one global location
			return doTransform(editor,document,null,norotate, isTags);
		}
		ITextEditor jumpTo = location.getEditor();
		int offset = location.getOffset();
		IWorkbenchPage page = getWorkbenchPage();
		IEditorPart part = jumpTo;
		if (part != null) {
			// move to the correct page
			IEditorPart apart = part;
			IEditorSite esite = part.getEditorSite();
			if (esite instanceof MultiPageEditorSite) {
				apart = ((MultiPageEditorSite)esite).getMultiPageEditor();
				// handle multi page by activating the correct part within the parent
				if (apart instanceof MultiPageEditorPart) {
					((MultiPageEditorPart)apart).setActiveEditor(part);
				}
			}
			// check to make sure the editor is still valid
			if (page.findEditor(apart.getEditorInput()) != null)  {
				// then activate
				page.activate(apart);
				page.bringToTop(apart);
				if (part instanceof ITextEditor) {
					selectAndReveal((ITextEditor) part,offset,offset);
					EmacsPlusUtils.clearMessage(part);
				}
			} else {
				EmacsPlusUtils.showMessage(editor, String.format(BAD_MARK, apart.getTitle()), true);
			}
		}
	} else {
		beep();
	}
	return NO_OFFSET;
}
 
Example 9
Source File: RegisterJumpToHandler.java    From e4macs with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @see com.mulgasoft.emacsplus.minibuffer.IMinibufferExecutable#executeResult(org.eclipse.ui.texteditor.ITextEditor, java.lang.Object)
 */
public boolean doExecuteResult(ITextEditor editor, Object minibufferResult) {

	if (minibufferResult != null) {
		String key = (String)minibufferResult;
		IRegisterLocation location = TecoRegister.getInstance().getLocation(key);
		if (location != null) {
			IWorkbenchPage page = getWorkbenchPage();
			IEditorPart part = location.getEditor(); 
			int offset = location.getOffset();
			if (part != null) {
				// move to the correct page
				IEditorPart apart = part;
				IEditorSite esite = part.getEditorSite();
				if (esite instanceof MultiPageEditorSite) {
					apart = ((MultiPageEditorSite)esite).getMultiPageEditor();
					// handle multi page by activating the correct part within the parent
					if (apart instanceof MultiPageEditorPart) {
						((MultiPageEditorPart)apart).setActiveEditor(part);
					}
				}
				// now activate
				page.activate(apart);
				page.bringToTop(apart);
			} else {
				// restore the resource from the file system
				if (location.getPath() != null) {
					try {
						// loads and activates
						part = IDE.openEditor(page, location.getPath(), true);
						if (part instanceof IEditorPart) {
							if (part instanceof MultiPageEditorPart) {
								IEditorPart[] parts = ((MultiPageEditorPart)part).findEditors(part.getEditorInput());
								//  TODO this will only work on the first load of a multi page
								// There is no supported way to determine the correct sub part in this case
								// Investigate org.eclipse.ui.PageSwitcher (used in org.eclipse.ui.part.MultiPageEditorPart)
								// as a means for locating the correct sub page at this level
								for (int i = 0; i < parts.length; i++) {
									if (parts[i] instanceof ITextEditor) {
										((MultiPageEditorPart)part).setActiveEditor(parts[i]);
										part = parts[i];
										break;
									}
								}
							}
							location.setEditor((ITextEditor)part);
						}
					} catch (PartInitException e) {
						showResultMessage(editor, String.format(BAD_LOCATION,key + ' ' + e.getLocalizedMessage()), true);				
					}
				} else {
					showResultMessage(editor, String.format(NO_LOCATION,key), true);				
				}
			}
			if (part instanceof ITextEditor) {
				((ITextEditor) part).selectAndReveal(offset, 0);
				showResultMessage(editor, String.format(LOCATED, key), false);
			} else {
			
			}
		} else {
			showResultMessage(editor, NO_REGISTER, true);
		}
	}
	return true;
}
 
Example 10
Source File: WorkbenchUtils.java    From xds-ide with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Fetches the editor site of the active editor. If the active editor is a
 * multi page editor and the param getSubEditor is true, then the editor
 * site of the active page is returned instead of the multi page editors
 * site.
 * 
 * @param getSubEditor
 *            indicates that a sub editor should be returned
 * @return the editor site or <code>null</code>
 */
public static IEditorSite getEditorSite(boolean getSubEditor) {
    IEditorPart editor = getActiveEditor(getSubEditor);
    if (editor != null) {
        return editor.getEditorSite();
    }
    return null;
}