How to save selected view highlighted in rcp application? - java

I have an Eclipse RCP application. In a perspective there are four views and I want to highlight respective views whenever I click on them. Is it possible to do it?
i have tried following code:
private void addFocusBackgroundOnSelectingView() {
viewer.getControl().addListener(SWT.MouseEnter, new Listener() {
#Override
public void handleEvent(Event event) {
viewer.getControl().setBackground(
PlatformUI.getWorkbench().getDisplay()
.getSystemColor(SWT.COLOR_GRAY));
}
});
viewer.getControl().addListener(SWT.MouseExit, new Listener() {
#Override
public void handleEvent(Event event) {
viewer.getControl().setBackground(
PlatformUI.getWorkbench().getDisplay()
.getSystemColor(SWT.COLOR_WHITE));
}
});
}
I want to save the selection even i mouse hover out if that view is already had selected.

The Eclipse PartService keeps track of which part (Editors, Views etc...) is currently active. You can add a listener to the service via the PlatfomUI class:
IPartListener partListener = ...;
IPartService partService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService();
partService.addPartListener(listener);
The IPartListener interface has partActivated and partDeactivated methods where you can do your highlighting.

Related

Java SWT: mouseUp() is not fired when opening a Dialog in mousDown()

My program opens a dialog if a certain string is clicked inside a StyledText. So in the mouseDown() I first want to check what has been clicked and then open a dialog. This works. After closing the dialog the mouseUp() is not called. This leads to selecting the text when moving the cursor, as if the user tries to select a text.
I can reproduce the behavior by performing the following tasks:
Click on String in StyledText
-> Dialog Opens
Close Dialog
Move Mouse without clicking
-> Text gets marked as selected
In my use case I don't need mouseUp() to be fired. But having it not fired means the OS assumes that the mouse button is still down and selects text. This may be the correct behavior if a dialog opens and steals the focus. But than there must be a possibility to tell the system, that the mouse button has been released.
myStlyedText.addMouseListener(new MouseListener() {
#Override
public void mouseUp(MouseEvent e) {
System.out.println("MouseUp is fired");
}
#Override
public void mouseDown(MouseEvent e) {
if (certainStringClicked()) {
openDialog();
}
}
#Override
public void mouseDoubleClick(MouseEvent e) {}
});
I can verify that mouseUp() is not called because "MousUp is fired" is not printed on console.
What is the best way to handle this? I already tried to set focus on another widget (setFocus() and forceFocus()), but that didn't help.
I tried to call mouseUp myself:
Event event = new Event();
event.type = SWT.MouseUp;
event.button = 1;
MouseEvent mouseUpEvent = new MouseEvent(event);
mouseUp(mouseUpEvent);
This leads to the message "MousUp is fired", but the selection problem still exists.
I could move the code into the mouseUp() method, but that's not actually what I want. The dialog should appear immediately. What else can I do?
Try adding myStlyedText.notifyListeners(SWT.MouseUp, null); to your code.
It should work.
myStlyedText.addMouseListener(new MouseListener() {
#Override
public void mouseUp(MouseEvent e) {
System.out.println("MouseUp is fired");
}
#Override
public void mouseDown(MouseEvent e) {
if (certainStringClicked()) {
myStlyedText.notifyListeners( SWT.MouseUp, null );
openDialog();
}
}
#Override
public void mouseDoubleClick(MouseEvent e) {}
});
This is not a good solution. But it may be a workaround for some.
It is possible to add SWT.MODELESS to the shell style in the constructor of the Dialog, which extends jface.dialog.Dialog.
setShellStyle(SWT.MODELESS);
MouseUp() get's fired now.
The problem here is that it is possible to open many dialogs by clicking the text although one dialog is already open.

How to configure intellij vaadin import settings?

I am trying to develop a vaadin application with maven in idea, but all listener methods calls its own object like above
Button btn = new Button("click!");
btn.addClickListener(new **Button**.ClickListener() {
#Override
public void buttonClick(**Button**.ClickEvent clickEvent) {
Notification.show("ok!");
}
});
i want to looks like that
Button btn = new Button("click!");
btn.addClickListener(new ClickListener() {
#Override
public void buttonClick(ClickEvent clickEvent) {
Notification.show("ok!");
}
});
how can i modify idea settings?
On Settings->Editor->Code Style->Java, select the "Insert imports for inner classes" check box (off by default). Then IntelliJ IDEA will automatically add static imports for Button.ClickListener and Button.ClickEvent.

Single selection and fire selecion of AbstractGraphicalEditPart

I Have an AbstractDecoratedTextEditor and it has a tab with components of AbstractGraphicalEditPart.
I want to
1) Select single components. If I select one component, other componentes was unselected
2) When I select one component, I wan to to fire the selection listenner of eclipse. Because this listenner will change the properties view of Eclipse.
I tried this code for number 2, but not work.
((IFigure) componentFigure).addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent arg0) {
ComponentEditPart.this.setSelected(SELECTED);
fireSelectionChanged();
super.mousePressed(arg0);
}
}
There is a selection listener in GEF, but it's on the EditPartViewer. Add a ISelectionChangedListener to your Graphical viewer. Each editpart has a method #getViewer() (i.e. AbstractGraphicalEditPart#getViewer()).
graphicalEditPart.getViewer().addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
// TODO: implement it to handle selection change
}
}};

SWT/JFace: How to handle Keyevents for each ViewPart or Form?

I'm building an Eclipse application and I'm trying to make a shortcut for launching an action when pressing F5, and make that the default action when that Tab/ViewPart has the focus.
I have read that this is not possible, or very complex. Is there any simple/straight way of doing it?
I tried with:
Display.getCurrent().addFilter(...)
this.addKeyListener(new KeyAdapter() {...})
...
Making this in the constructor is my best:
this.getShell().addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.keyCode == SWT.F5) {
//doAnything()
}
}
});
And this doesn't work when loaded, but if I switch from this to another View/Tab starts to work. But it works too when others have focus (which I don't want).
Is there anyway of making this work at the start, and only when the focus is in the View?
You should define work in a handler & then should use key bindings as given in this example. You can find a good example here. Hope it solves your need.
You should look at RetargetableActions. I think that's Eclipse way of doing it:
You need to look at extensions org.eclipse.ui.bindings and org.eclipse.ui.contexts.
Define a command and its handler
Define a binding for the command
define context (cxtId)
associate context with command so that command is available only when context is active
Activate context when you open the view or form.
If you get the listener of the events of the component it will listen on the events. And if the event occur for this component it will be notified.
To add listener of the key event on the ViewPart we should create the control which could be able listen on the event.
public class SampleView extends ViewPart {
/**
* The ID of the view as specified by the extension.
*/
public static final String ID = "views.SampleView";
private Composite mycomposite;
public void createPartControl(Composite parent) {
mycomposite = new Composite(parent, SWT.FILL);
//then add listener
mycomposite.addKeyListener(keyListener);
}
private KeyListener keyListener = new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
showMessage("key pressed: "+ e.keyCode);
}
};
//the rest of focusing and handle event
private void showMessage(String message) {
MessageDialog.openInformation(
mycomposite.getShell(),
"Sample View",
message);
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
mycomposite.setFocus();
}
}
//the end

Java swt treeview popup menu

Hiho,
currently I have a working popup menu which appears when I click on a treeview item.
But I want to show different popups for different tree view entries. I don't get a idea how to do so...
Here is my code for creating the menu:
MenuManager menuMgr = new MenuManager("#PopupMenu");
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
#Override
public void menuAboutToShow(IMenuManager manager) {
Action action = new Action() {
public void run() {
// So something
}
};
action.setText("Set as working file");
manager.add(action);
}
});
Menu menu = menuMgr.createContextMenu(getTree());
getTree().setMenu(menu);
You should propably use a MouseListener on the tree:
final Tree tree = new Tree(parent, ...);
tree.addMouseListener(new MouseAdapter() {
#override
public void mouseDown(MouseEvent me) {
if(tree.getSelection() instanceof MySpecificTreeNode) {
// create menu...
}
}
});
Two ideas. For both you need to listen to selections on the TreeView, because that's the only way to determine which Menu (or special content) you want to show.
Then you could either set the correct menu to the the tree right after you know which one to use or contribute the needed items to the existing menu (that's how it's done in the eclipse framework).

Categories