how to call to context menu in swtboot - swtbot

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.

Related

How to capture mouse click events from console in eclipse plugin

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!");
}
}

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");
}

Update ListView Textview vom Asyntask

i need to update a textView from my asynctask. I have an custom adapter for the listview and there i want to have a countdown for each entry. I will start the asynctask for each entry from my Adapter. How can i update the textview each second from the asynctask?
Thanks for help :)
If you post your code, I can give you a better answer. However, a common way to update views periodically is by using Handlers.
private final Handler mHandler = new Handler(); //intialize in main thread
public void test() {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
mTextView.setText("hello");
}
}, 1000);
}
You can do something like this (this will add an entry to a list view every one second). I have used the normal ArrayAdapter to add a string. You can use your custom adapter to do something similar. The publishProgress() method basically triggers the onProgressUpdate() method which hooks to the UI thread and displays the elements getting added.:
class AddStringTask extends AsyncTask {
#Override
protected Void doInBackground(Void... params) {
for(String item : items) {
publishProgress(item);
SystemClock.sleep(1000);
}
return null;
}
#Override
protected void onProgressUpdate(String... item) {
adapter.add(item[0]);
}
#Override
protected void onPostExecute(Void unused) {
Toast.makeText(getActivity(), "Done adding string item", Toast.LENGTH_SHORT).show();
}
}