The preferences of our application are shared (i.e. all users in an office have the same one). For that to work we have implemented a custom IPreferenceStore that saves and (re-)loads the data to / from a database.
Now of course we can't have the IPreferenceStore reloading while the preference window is open - at the very least we should bring some kind of message in that case or something. So I need to have a way to listen to the PreferenceDialog opening.
Since we're implementing an Eclipse application, it might suffice to listen for Shells opening and checking their title, if it's not possible to react to the preference dialog directly.
What I tried:
// this only reacts to the main window changing
PlatformUI.getWorkbench().addWindowListener(new IWindowListener() { ... });
// shells (and composites in general) are missing listeners for
// adding child controls
Shell currentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
currentShell.addListener(SWT.???, e-> System.out.println(e));
// what listeners do display even have?
Display.getDefault().addListener(SWT.???, e-> System.out.println(e));
Any thoughts?
Display.addFilter for SWT.Activate does give an event as a shell is activated (and a lot of other things as well). For some reason addListener does not work in my test.
Display.getDefault().addFilter(SWT.Activate, listener);
The widget field of the Event contains the control being activated.
Related
I'm dealing with a WebApp (Vaadin19) and stuck now in the question, how to share an object-state change from one component to another. There is one object instance in two or more components. After changing an attribute of the object in one component and going back to another component, I want to see the changed attribute.
Let me explain, what I mean:
There is a grid with some lines of data. The grid shows only a subset of the data to respect the clarity.
A click on the grid opens a detailed view in "read mode". The data is structured (contains sub-objects itself).
A click on the "read mode"-view opens then a dialog with tabs. The activated tab depends on the sub-object, that was clicked before.
After changing an element in the sub-object and closing the dialog, I want that the UI will reload/revalidate it's content. I think it's clear, that I use there the same object-instance.
Is there an event I have to submit to the UI?
Or:
What is the best approach for this?
The actual refresh is easy: theGrid.getDataProvider().refreshAll(), or refreshItem instead if you have access to the item that has been changed and it has a good implementation of equals and hashCode.
How to hook things up so that the dialog notifies the grid is then really depending on your architecture.
If they are already close to each other in the code, then you could e.g. store a reference to the Grid in an instance field and just reference that in the dialog handler.
If you want to decouple, then you need some kind of event bus. You can use the regular Spring event mechanisms as long as you ensure that the event stays within the UI scope. Alternatively, you can use ComponentUtil::addListener and ComponentUtil::fireEvent to use e.g. UI.getCurrent as a simple event bus.
I'm trying to write a view plugin that, when opened, will cycle through all the currently open editors and add a mouse listener to each. I know I can get all the editors by using something like:
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()
However, I'm at a loss as to how add the listener. From the editor reference I can get the editor part, but I don't know how to get the underlying control to which I would add my mouse listener. What am I missing? Thanks!
Editors may have lots of controls, so there is no one control you could add your listener to.
You can add a listener that is called from everything in the application using Display.addFilter, something like:
Display display = Display.getDefault();
display.addFilter(SWT.MouseDown, listener);
I am making some application for myself, just to practice on my designs and GUI.
My application is divided into two sides, first side is the model / logic however you call this, second side is the visual side, where you handle the gui, buttons and view.
So now my application has a feature that pops up and asks the user if he wants to use some feature, and then if he clicks yes, it will open a new JFrame window with many configurations.
these configurations will "probably" be in the Config class.
My question is, what is the best way to transfer data from the GUI to the model? Since you have to create a button listener in order to detect button clicks or text, what is the best proper way to update the configuration after the button was clicked?
For example, you have an application, and in order to start it you need to click on the button, I have two ideas in my head:
Transfer the Config object to the area where you handle the button & listen to it
Make Config static, and set up a Set() method, so you can set configurations without any objects, like Application.setConfiguration(config, type)
But I heard that statics are NOT always good, so I wondered, using static in this case is OK and good or are there better ways to do this? Or passing the config object to the GUI area is OK aswell?
This is how my structure looks like:
(source: gyazo.com)
I want to understand how java.awt.Dialog achieves modality (blocking other windows)?
Yeah, I tried reading the code of java.awt.Dialog, but I am kind of getting lost in it. So I want to know briefly what they do. Do they disable all events of all other windows?
Thanks
Prem
Its system dependent. On windows the main event loop is modified. You are most of the time better off opening a dialog non-modal and lock the underlying window manually (e.g. set a glasspane to the rootpane and disable it, or block all events setting a custom focus manager).
I want the active window (i.e JFrame ot JDialog) to receive specific keyEvent so that whereever the focusing this keyevent is handling by the window such as ALT+F4 if you press
it wherever the focusing the active window will close,I try to override postprocesskeyEvent but it doesn't work fine
You can add a global event listener to you application using the addAWTEventListener() method in java.awt.Toolkit.
http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#addAWTEventListener%28java.awt.event.AWTEventListener,%20long%29
You will need to choose which type of events you want to receive with the event mask when you add the listener.
For example:
// Then on startup register.
AWTEventListener myGlobalKeyListener = new MyGlobalKeyListener();
Toolkey.getDefaultToolkit().addAWTEventListener(myGlobalKeyListener, AWTEvent.KEY_EVENT_MASK);
If you are trying to stop windows from shutting down your application when the user presses ALT-F4 then an event handler will not help you. I belive the operating system handles this by sending SIGTERM to the application. Java does not receive the KeyEvent for this.
The standard approach for intercepting KeyStrokes when using Swing is to use Key Bindings. Although as mentioned earlier this still won't work for Alt+F4.
If you are trying to prevent Alt+F4 from closing the window, then you need to use the setDefaltCloseOperation(...) method to do nothing. Closing an Application gives more information on this approach.