How to capture mouse click events from console in eclipse plugin - eclipse

I'm developing an eclipse plugin. It writes some lines in a console. In order to select a line displayed in the console, I’m trying to capture mouse double click event from that console.
The console has been implemented by following this eclipse FAQ. MessageConsole or IconsoleView classes doesn‘t seem to provide a methode to add a listener with an SWT.MouseDoubleClick event.
Is there any way to capture a mouse event from a console and then read the selected line ?

The MessageConsole doesn't know anything about how the data is displayed, it is the TextConsoleViewer that deals with that.
To access the console viewer you need to use a custom message console - extending MessageConsole or TextConsole and overriding createPage to create your own console page extending TextConsolePage.
The console page needs to override the createViewer method to create your own text console viewer extending TextConsoleViewer.
In the viewer you can override the mouseDoubleClick method to receive the double clicks.
For an example see the Eclipse JDT JavaStackTraceConsole, JavaStackTraceConsolePage, and JavaStackTraceConsoleViewer classes.
public class JavaStackTraceConsole extends TextConsole {
...
#Override
public IPageBookViewPage createPage(IConsoleView view) {
return new JavaStackTraceConsolePage(this, view);
}
}
public class JavaStackTraceConsolePage extends TextConsolePage {
...
#Override
protected TextConsoleViewer createViewer(Composite parent) {
return new JavaStackTraceConsoleViewer(parent, (JavaStackTraceConsole) getConsole());
}
}
public class JavaStackTraceConsoleViewer extends TextConsoleViewer {
...
}

Thank you, it works fine. I just had to managed the mouse event in another way because overriding the mouseDoubleClick method didn’t work. Here is my code :
public class MyTextConsoleViewer extends TextConsoleViewer {
public MyTextConsoleViewer(Composite parent, MyMessageConsole console) {
super(parent, console);
StyledText styledText = getTextWidget();
MouseListener listener = new MouseListener() {
#Override
public void mouseUp(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseDown(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseDoubleClick(MouseEvent event) {
// TODO Auto-generated method stub
IDocument document = console.getDocument();
try {
int currentLine = document.getLineOfOffset(styledText.getOffsetAtLocation(new Point (event.x, event.y)));
IRegion lineInfo = document.getLineInformation(currentLine);
System.out.println(document.get(lineInfo.getOffset(), lineInfo.getLength()));
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
styledText.addMouseListener(listener );
// TODO Auto-generated constructor stub
}
public MyTextConsoleViewer(Composite parent, TextConsole console,
IScrollLockStateProvider scrollLockStateProvider) {
super(parent, console, scrollLockStateProvider);
// TODO Auto-generated constructor stub
}
#Override
public void mouseDoubleClick(MouseEvent e) {
System.out.println("This even doesn't work!");
}
}

Related

Close Eclipse ViewPart tab when createPartControl function fails when launched from "Quick Access"?

The default behaviour when creating a new Eclipse ViewPart is to show the new tab regardless of what happens in the createPartControl function. For example, if didn't create anything, no widgets, nothing, a blank tab will be shown. I don't like this behaviour. I want to close that tab if initialization in createPartControl fails.
Now, I have a mouse-button-context-menu handler that can do this, e.g.
public class MyPartMB3Handler extends AbstractHandler {
#Override
public Object execute(final ExecutionEvent event)
throws ExecutionException {
// Create a view and show it.
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage page = window.getActivePage();
try {
MyPart viewPart = (MyPart)page.showView(MyPart.ID);
if(!viewPart.isCreated()) {
page.hideView(viewPart);
}
}
catch(PartInitException e) {
e.printStackTrace();
}
return null;
}
}
The isCreated function is a little hack that lets me know if my ViewPart initialization fails, e.g.
public class MyPart extends ViewPart {
public static final String ID = "com.myplugin.MyPart";
private Composite _parent = null;
#Override
public void createPartControl(Composite parent) {
if(!MyPlugin.createPartControl(parent) { // Some common part creation code I use.
//PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(this);
return;
}
_parent = parent;
}
#Override
public void setFocus() {
}
public boolean isCreated() {
return _parent != null;
}
}
The problem arises when I launch this ViewPart from the Eclipse "Quick Access" field. I don't own the handler now. From an exception I forced, the handler might be org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.createPartControl or org.eclipse.ui.internal.e4.compatibility.CompatibilityView.createPartControl or org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.create.
I tried hiding the view inside the createPartControl function (see the commented line above), but Eclipse did not like that and spewed a pile of exceptions.
I thought maybe I could throw a PartInitException in createPartControl, but Eclipse tells me I'm not allowed to do that.
So, how do I get my menu handler behaviour when launching from "Quick Access"?
An underlying question might be, is there a better/proper way to achieve this behaviour?
You can close the view by running the hideView asynchronously after the createPartControl has finished - like this:
#Override
public void createPartControl(Composite parent) {
parent.getDisplay().asyncExec(new Runnable() {
#Override
public void run()
{
getSite().getPage().hideView(MyPart.this);
}
});

Catch closing event of a part (Eclipse e4 RCP)

I'm currently working on a eclipse e4 RCP application and I have a part that serves as a job manager where the user can see all active jobs and their progresses, like one in eclipse. The problem is now that the user can open the progress part by double clicking in the toolbar and he should also be able to close the progress part whenever he wants, but instead of disposing the part I want to just make it invisible.
I thought at first this shouldn't be a problem because I can set the part to be not visible, but the problem is how to catch the closing event and process it by my way. Is there any event, interfaces or listeners I can implement to catch the closing event and prevent the part from getting disposed?
You can implement a CustomSaveHandler and replace the Default Eclipse Save Handler with a Processor. In that SaveHandler you can control if the Part shoud get closed or not. So you could do not close it and make it invisible.
ExampleCode:
public class ReplaceSaveHandlerProcessor {
#Named("your.id.to.window")
#Inject
MWindow window;
#Inject
IEventBroker eventBroker;
#Execute
void installIntoContext() {
eventBroker.subscribe(UIEvents.Context.TOPIC_CONTEXT, new EventHandler() {
#Override
public void handleEvent(final Event event) {
if (UIEvents.isSET(event)) {
if (window.equals(event.getProperty("ChangedElement")) && (window.getContext() != null)) {
window.getContext().runAndTrack(new RunAndTrack() {
private final ISaveHandler saveHandler = new CustomSaveHandler();
#Override
public boolean changed(final IEclipseContext context) {
Object getSaveHandlerValue = context.get(ISaveHandler.class);
if (!saveHandler.equals(getSaveHandlerValue)) { // prevents endless loop
ContextInjectionFactory.inject(saveHandler, window.getContext());
context.set(ISaveHandler.class, saveHandler);
}
return true; // ture keeps tracking and the saveHandler as the only opportunity
}
});
}
}
}
});
}
}
You have to define a Extention for ExtentionPoint org.eclipse.e4.workbench.model
With Your ReplaceSaveHandlerProcessor. (You have to declare the window id as "element" in this extention. (Added Screenshot: )
The CustomSaveHandler have to implement the ISaveHandler interface. In its Methods ypu can say if the Part should realy be closed.
public class CustomSaveHandler implements ISaveHandler {
#Override
public boolean save(MPart dirtyPart, boolean confirm) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean saveParts(Collection<MPart> dirtyParts, boolean confirm) {
// TODO Auto-generated method stub
return false;
}
#Override
public Save promptToSave(MPart dirtyPart) {
// TODO Auto-generated method stub
return null;
}
#Override
public Save[] promptToSave(Collection<MPart> dirtyParts) {
// TODO Auto-generated method stub
return null;
}
}

Page is not being loaded in GWT

I am using activities and places to develop my application. When I click on the link on the left, my page is loaded, and I put the values in the fields. After I put the values, I send a RPC to the server, and get response back. This is also shown. Now the problem is that even if I click on the link on the left, I am getting the result page again. I am not getting the input page.
Impl code
#UiHandler("entInvoiceCompare")
void onClickSubmit(ClickEvent e) {
GWT.log("Going to enterprise compare place");
//listener.goTo(new EnterpriseInvoiceCompareViewPlace());
NewEBRM.getClientFactory().getPlaceController().goTo(new EnterpriseInvoiceCompareViewPlace());
}
Place
public class EnterpriseInvoiceCompareViewPlace extends Place{
public EnterpriseInvoiceCompareViewPlace() {
GWT.log("EnterpriseInvoiceCompareViewPlace: constructor");
}
public static class Tokenizer implements PlaceTokenizer<EnterpriseInvoiceCompareViewPlace> {
#Override
public String getToken(EnterpriseInvoiceCompareViewPlace place {
GWT.log("EnterpriseInvoiceCompareViewPlace: getToken: call");
return "EntInvoiceCompare";
}
#Override
public EnterpriseInvoiceCompareViewPlace getPlace(String token) {
GWT.log("EnterpriseInvoiceCompareViewPlace: getPlace: call");
return new EnterpriseInvoiceCompareViewPlace();
}
}
}
Activity
#Override
public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
// TODO Auto-generated method stub
GWT.log("EnterpriseInvoiceCompareActivity: start: starting activity");
EnterpriseInvoiceCompareView entInvoiceCompareView = clientFactory.getEnterpriseInvoiceCompareView();
entInvoiceCompareView.setPresenter(this);
containerWidget.setWidget(entInvoiceCompareView.asWidget());
GWT.log("EnterpriseInvoiceCompareActivity: start: ending activity");
}

how to call to context menu in swtboot

I want to call to context menu in my application.
The issue that I don't have any items in the tree.
I active my view and then I want to open context menu.
SWTBotView view = bot.viewByTitle("Project Explorer");
view.bot.tree().contextMenu("New").click();
then I got error message
Could you please advise me how I can open contextMeny without any item in the tree ?
As there is no direct way to do this. I assume you have a shortcut for opening your context menu.
bot.activeShell().pressShortcut(<your shortcut>);
bot.waitUntil(new ContextMenuAppears(tree,
"New"));
tree.contextMenu("New").click();
Where ContextMenuAppears is an ICondition which waits for the desired context menu to appear.
public class ContextMenuAppears implements ICondition {
private SWTBotTree swtBotTree;
private String mMenuText;
public TekstContextMenuAppears(SWTBotTree pSwtBotTree, String menuText) {
swtBotTree = pSwtBotTree;
mMenuText = menuText;
}
#Override
public boolean test() throws Exception {
try {
return swtBotTree.contextMenu(mMenuText).isVisible();
} catch (WidgetNotFoundException e) {
return false;
}
}
#Override
public void init(SWTBot bot) {
// TODO Auto-generated method stub
}
#Override
public String getFailureMessage() {
// TODO Auto-generated method stub
return null;
}
}
depending on what you're trying to achieve, you could try going via the file menu instead of the context menu. "new" should work that way.

How to add an GUI component like a Button in a Editor?

I'm a beginner in RCP just started building RCP application today.I want to a GUI component like a Button ,comboBox,Checkbox in a Editor .I've managed to add a editor in Extensions and create a class for it.I have written the code to create a label in creatPartControl but it does not work..I get a black window.Should I add the editor in perspective like this
layout.addStandaloneView(Editor.id, true, IPageLayout.TOP,0.7f,
layout.getEditorArea());
layout.addStandaloneView(View.ID, true, IPageLayout.BOTTOM,0.4f,
layout.getEditorArea());
Please help me resolve this issue.If possible please give an eg on how to add a editor and create a label and a button in it.
Thank you for your help in advance
code in my Editor.java content in createPartControl()
parent.setLayout(new GridLayout());
Button b=new Button(parent,SWT.TOGGLE);
b.setText("Hello ");
Label label1 = new Label(parent, SWT.NONE);
label1.setText("First Name");
package com.hello;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
public class Editor extends EditorPart {
public static final String ID = "TestApplication.editor3";
public Editor() {
// TODO Auto-generated constructor stub
}
#Override
public void doSave(IProgressMonitor monitor) {
// TODO Auto-generated method stub
}
#Override
public void doSaveAs() {
// TODO Auto-generated method stub
}
#Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
// TODO Auto-generated method stub
}
#Override
public boolean isDirty() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isSaveAsAllowed() {
// TODO Auto-generated method stub
return false;
}
#Override
public void createPartControl(Composite parent) {
Label label = new Label(parent, SWT.NONE);
label.setText("sssssss");
}
#Override
public void setFocus() {
// TODO Auto-generated method stub
}
}
You are not correctly initializing the editor and it causes problems when opening the editor. Fill up your init() method like below and see if this helps:
#Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
setSite(site);
setInput(input);
}
It's been a few years since I worked on an Eclipse editor. Here's a screen capture of the editor so you can see I did more than add a Button.
I extended the Viewer class to create the GUI of the editor.
I extended the EditorPart class to create the functionality of the editor.
Because of the kind of editor I was building, I had to create my own version of Canvas and my own version of IDocument.