JSlider with 2 data sources event handling - java

I have a JSlider component on my frame which is being constantly updated by an external component (a media player which sets a new value from time to time). I want the slider to handle the stateChanged event only when I manipulate the slider and not my external component.
Is there any way to achieve this?

I'd implement my own BoundedRangeModel, this way you can add additional flags that indicates whether it should accept updates or not
UPDATE with EXAMPLE
The basic idea would be to implement your own model, that way you can control when the value is actually changed
public class MyBoundedRangeModel extends DefaultBoundedRangeModel {
private boolean updatesAllowed;
public void setUpdatesAllowed(boolean updatesAllowed) {
this.updatesAllowed = updatesAllowed;
}
public boolean isUpdatesAllowed() {
return updatesAllowed;
}
#Override
public void setMinimum(int n) {
setUpdatesAllowed(true);
super.setMinimum(n);
setUpdatesAllowed(false);
}
#Override
public void setMaximum(int n) {
setUpdatesAllowed(true);
super.setMaximum(n);
setUpdatesAllowed(false);
}
#Override
public void setExtent(int n) {
setUpdatesAllowed(true);
super.setExtent(n);
setUpdatesAllowed(false);
}
#Override
public void setValue(int n) {
super.setValue(n);
}
#Override
public void setValueIsAdjusting(boolean b) {
setUpdatesAllowed(true);
super.setValueIsAdjusting(b);
setUpdatesAllowed(false);
}
#Override
public void setRangeProperties(int newValue, int newExtent, int newMin, int newMax, boolean adjusting) {
if (isUpdatesAllowed()) {
super.setRangeProperties(newValue, newExtent, newMin, newMax, adjusting);
}
}
}
This would allow you to control the change of the "value" property. The problem you have here is that ALL the set methods go through the setRangeProperties method, so you need to decide what should be allowed to effect it. In my example, the only method that does not control it is the setValue method.
In your code you would need to call it something like...
MyBoundedRangeModel boundedRangeModel = new MyBoundedRangeModel();
slider.setModel(boundedRangeModel);
...
boundedRangeModel.setUpdatesAllowed(true);
slider.setValue(value);
boundedRangeModel.setUpdatesAllowed(false);
Your only other choice is to extend the JSlider itself and override the setValue method directly in a similar way

Related

How to enable a JButton on a condition?

I'm trying to create a JButton that enables when certain conditions are met. The program sets setEnabled(false) as initial value, but after an update, it should be setEnabled(true).
I tried several things. Here some code:
public class SwimAction extends AbstractAction {
private final PoolModel poolModel;
private final Swimmer swimmer;
public SwimAction(PoolModel poolModel, Swimmer swimmer) {
super("GO!");
this.poolModel = poolModel;
this.swimmer = swimmer;
// default
setEnabled(false);
}
I tried the following:
// Replaced the setEnabled(false) by setEnabled(checkGo())
public boolean checkGo(){
return(pool.isNotOccupied());
}
// Overwrite setEnabled
#Overwrite
public void setEnabled(boolean bool){
boolean oldBool = this.enabled;
if (oldBool != bool && pool.isNotOccupied()) {
this.enabled = bool;
this.firePropertyChange("enabled", oldBool, bool);
}
}
However, none of them worked. Anyone knows how to enable the button when a certain condition (pool.isNotOccupied()) is met?
Seems like you need to listener for a change in the state of the pool object's occupied property, and the best way to do this is to give it a listener of some sort. This could be as simple as a ChangeListener or perhaps better, a PropertyChangeListener. The details of the best solution would likely depend much on the structure of your program, of the rest of the code that we can't see, but, if PoolModel is what you're listening to, what if you gave it...
public class PoolModel {
public static final String OCCUPIED = "occupied";
private PropertyChangeSupport propChangeSupport = new PropertyChangeSupport(this);
private boolean occupied;
public void addPropertyChangeListener(PropertyChangeListener l) {
propChangeSupport.addPropertyChangeListener(l);
}
// also have a remove listener
public boolean isOccupied() {
return occupied;
}
public void setOccupied(boolean occupied) {
boolean oldValue = this.occupied;
boolean newValue = occupied;
this.occupied = occupied;
propChangeSupport.firePropertyChange(OCCUPIED, oldValue, newValue);
}
// ......
And then in the code that uses it:
poolModel.addPropertyChangeListener(pcEvent -> {
if (pcEvent.getPropertyName().equals(OCCUPIED)) {
setEnabled((boolean) pcEvent.getNewValue());
}
});

Key bindings don't work, Java SE, Swing

I'm trying to add a shortcut to my JButton. I've read How to Use Key Bindings tutorial and I also have read this page How to use Key Bindings instead of Key Listeners and a loooooooooooooot of other questions about key bindings, but haven't found any answer for me
What I've tried:
public class Example extends JFrame {
public static void main(String args[]) {
Example example = new Example();
}
Example(){
Action action = new Action() {
#Override
public Object getValue(String key) {
return null;
}
#Override
public void putValue(String key, Object value) {
}
#Override
public void setEnabled(boolean b) {
}
#Override
public boolean isEnabled() {
return false;
}
#Override
public void addPropertyChangeListener(PropertyChangeListener listener) {
}
#Override
public void removePropertyChangeListener(PropertyChangeListener listener) {
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Hello World!");
}
};
JButton button = new JButton("Hello World!");
button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("RIGHT"), "doSomething");
button.getActionMap().put("doSomething", action);
button.addActionListener(action);
add(button);
setVisible(true);
pack();
}
}
I've also tried to make it like that: getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "doSmth");
But nothing seems to be working, what am I doing wrong?
Your Action has a method called isEnabled that you've implemented. The Javadoc on it states:
/**
* Returns the enabled state of the <code>Action</code>. When enabled,
* any component associated with this object is active and
* able to fire this object's <code>actionPerformed</code> method.
*
* #return true if this <code>Action</code> is enabled
*/
Since you return a hardcoded false, the Action is never enabled and the actionPerformed method is never called. Your problem is not the binding, it's the action itself!
A simple fix is to change isEnabled to return true, or, even simpler yet, use AbstractAction in place of Action, and override only actionPerformed (AbstractAction is kind of the "I don't care about all this stuff, just give me the simplest thing possible with one method to implement!")

How to add a hook to save event for existing DataObject in NetBeans?

I want to make some processing every time when a particular DataObject is saved. If I understand NetBeans IDE API correctly, there is an Savable interface that can be used to implement saving options for custom editors. The problem here is that I do not want to implement my own editor, nor DataObject. I have a MIME type that is edited by a default Gsf editor (the common scripting language api) and has a GsfDataObject (I expect with the DOSavable). I want to keep all that way, just to add a hook, maybe a callback method or something, that would be called every time a save is done upon a given GsfDataObject (and I want a default save action be called, I dont want to override it).
So far I came to this simple solution but it seems ugly (it is more or less inspired by http://wiki.netbeans.org/DevFaqListenForSaveEvents ):
// I have a FileObject fobj
final DataObject dobj = DataObject.find(fobj);
dobj.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(DataObject.PROP_MODIFIED)) {
if (!((Boolean) evt.getOldValue()) & ((Boolean) evt.getNewValue())) {
System.out.println(">>>> here it gets modified");
} else {
System.out.println(">>>> here the data object gets saved");
}
}
}
});
However, this is not called only when the save is done, but also when the file gets modified, but then the modifications are reverted by Ctrl + Z. It only checks whether the data object changes its state from modified to unmodified. Is there a way to hook to a save event only?
P.S.: I tried to call new SJDOSavable(dobj).add(); in the moment when the DataObject gets modified and then to remove it in the other branch. However, the handleSave method does not get called. SJDOSavable class is a simple Savable implemented according to DOSavable from the DataSystems API:
private static final class SJDOSavable extends AbstractSavable implements Icon {
final DataObject obj;
public SJDOSavable(DataObject obj) {
this.obj = obj;
}
#Override
public String findDisplayName() {
return obj.getNodeDelegate().getDisplayName();
}
#Override
protected void handleSave() throws IOException {
System.out.println(">>>>> but this does not get called");
}
#Override
public boolean equals(Object other) {
if (other instanceof SJDOSavable) {
SJDOSavable dos = (SJDOSavable) other;
return obj.equals(dos.obj);
}
return false;
}
#Override
public int hashCode() {
return obj.hashCode();
}
final void remove() {
unregister();
}
final void add() {
register();
}
#Override
public void paintIcon(Component c, Graphics g, int x, int y) {
icon().paintIcon(c, g, x, y);
}
#Override
public int getIconWidth() {
return icon().getIconWidth();
}
#Override
public int getIconHeight() {
return icon().getIconHeight();
}
private Icon icon() {
return ImageUtilities.image2Icon(obj.getNodeDelegate().getIcon(BeanInfo.ICON_COLOR_16x16));
}
}
Did you try this ?
http://wiki.netbeans.org/DevFaqListenForSaveEvents
Also if you want to listen to global Save events, it seems you can do that now.
https://netbeans.org/bugzilla/show_bug.cgi?id=140719

Event between actors

I'm trying to use events to do something on actor but i don't understand how to do it properly.
I have a button on my screen and a text (just for example). they are both actors in a stage
my purpose is: if i click on the button, i would like to change text
I add listener on my button, i get the click but i don't know how to send event (or anything else) to my text to set it.
Main class with stage definition and his
public class AGame implements ApplicationListener {
private WorldRendererTouchPad renderer;
private Stage stage;
private static Vector3 cameraVelocity=new Vector3(0,0,0);
private ButtonJump button;
public static final int SCREEN_WIDTH=800;
public static final int SCREEN_HEIGHT=480;
public void create() {
stage = new Stage();
stage.setViewport(SCREEN_WIDTH, SCREEN_HEIGHT, true);
stage.getCamera().translate(-stage.getGutterWidth(), -stage.getGutterHeight(), 0);
renderer = new MyRenderer(SCREEN_WIDTH, SCREEN_HEIGHT);
stage.addActor(renderer);
renderer.create();
button=new ButtonJump();
stage.addActor(button);
button.create();
Gdx.input.setInputProcessor(stage);
}
....
resize and other methods
}
MyRenderer class (contains text actor):
public class MyRenderer {
private TextTest text;
public MyRenderer(float screenWidth, float screenHeight) {
setBounds(0, 0, screenWidth, screenHeight);
}
public void create() {
this.initActors();
}
private void initActors() {
text=new TextTest("Hello world!");
addActor(text);
}
// is it usefull?
public void setText(String newText) {
text.setText(newText);
}
}
and the ButtonJump class (extends MyButton just here for define Skin and ButtonStyle)
public class ButtonJump extends MyButton {
public boolean isJump=false;
private static InputListener buttonListener=new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("event" , "="+event.toString());
// do something to update text
return true;
}
};
public ButtonJump() {
super();
}
public void create() {
this.setPosition(getStage().getWidth()-60, 30);
this.addCaptureListener(buttonListener);
}
public void capture() {
if (this.isJump)
Gdx.app.log("jump button", "Jump is set");
else
Gdx.app.log("jump button", "No jump");
}
}
If you use the clicklistener you need to let the other actor hold an reference to it to call a method on click. It is not that good to let all Actor know of each other. Use an anonymous way.
There is a "common" system for it in games.
If you really want to use Events, do implement an Event-System. Therefore you have an interface Listen and an Interface Event_Handler. At the start of your game you init one Implementation of the Eventhandler. The interface should at least look like this:
public interface Interface_EventHandler extends Disposable
{
public void handleEvent(final Event... e);
public void registerListener(final Interface_Listen listener,
final Event_Type... type);
public void unregisterListener(final Interface_Listen... listener);
public void unregisterAllListener();
public void unregisterAllListener(final Event_Type... type);
public void processEvents();
public void processEvents(final int maxTimeInMS);
}
Okay so now how does it work. The handler has an hashmap with all eventtypes as Key and an list of listeners as Value. So if someone want to notice an event he registers with the registerListerner at the handler for the right Event_Type (Enum). It need to have the interface Listen to get events. Everyone can now push an Event into the handler with the handleEvent(...) method. Or even more than one.. (varargs) ..
Okay that still does not explain how it work. We now have a registered listener (actor for example) and we have events that get into the handler.
Every Rendercycle you call the processEvents() at the hanlder once. That mean that every event that get pushed in at a frame get handled at the next frame. (Asynchronus) While that he iterates over all events and push them to the listeners. Moreover the listener should have a queue too where they put all events and when they are at their .act() they handle the events. (more asynchronus).
Okay here is an Handler i use:
package com.portaaenigma.eventsystem;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import com.badlogic.gdx.utils.TimeUtils;
import com.portaaenigma.managers.Logger;
public class EventHandler implements Interface_EventHandler
{
private HashMap> listeners;
private LinkedList events;
public EventHandler()
{
listeners = new HashMap<Event_Type, ArrayList<Interface_Listen>>();
// add the arraylist for every Eventtype
for (Event_Type e : Event_Type.values())
{
listeners.put(e, new ArrayList<Interface_Listen>());
}
events = new LinkedList<Event>();
}
#Override
public void handleEvent(final Event... e)
{
for (Event event : e)
{
events.push(event);
}
}
#Override
public void unregisterListener(final Interface_Listen... listener)
{
for (Event_Type e : Event_Type.values())
{
for (Interface_Listen interface_Listen : listener)
{
listeners.get(e).remove(interface_Listen);
}
}
}
#Override
public void processEvents()
{
while (events.size() != 0)
{
// get the first element and delete it
Event e = events.pop();
for (Interface_Listen l : listeners.get(e.getType()))
{
l.handleEvent(e);
}
}
}
#Override
public void processEvents(final int maxTimeInMS)
{
int startSize = 0;
if (events.size() != 0)
{
startSize = events.size();
Logger.log("Processing Events: " + events.size());
}
long startTime = TimeUtils.millis();
while (events.size() != 0)
{
// get the first element and delete it
Event e = events.pop();
for (Interface_Listen l : listeners.get(e.getType()))
{
l.handleEvent(e);
}
// stop handling if time is up
if (startTime - TimeUtils.millis() > maxTimeInMS)
{
Logger.log("Handled " + (events.size() - startSize) + " Events");
break;
}
}
}
#Override
public void registerListener(final Interface_Listen listener,
Event_Type... type)
{
for (Event_Type event_Type : type)
{
listeners.get(event_Type).add(listener);
}
}
#Override
public void unregisterAllListener()
{
Logger.log("UnregisterAll");
for (Event_Type e : Event_Type.values())
{
listeners.get(e).clear();
}
}
#Override
public void unregisterAllListener(final Event_Type... type)
{
for (Event_Type event_Type : type)
{
listeners.get(event_Type).clear();
}
}
#Override
public void dispose()
{
unregisterAllListener();
events.clear();
listeners.clear();
}
}
The interface for all listeners is simple it's just this:
public interface Interface_Listen
{
public void handleEvent(final Event e);
}
Last but not least the event. How can you now send different data? Quiet simple. Have an hashmap out of Strings and Strings and for sure the EventType.
public class Event
{
private Event_Type type;
private HashMap<String, String> m_messages;
public Event(final Event_Type e, final Event_Message... m)
{
m_messages = new HashMap<String, String>();
for (Event_Message message : m)
{
m_messages.put(message.m_key, message.m_value);
}
type = e;
}
public Event_Type getType()
{
return type;
}
public void addMessages(final Event_Message... m)
{
for (Event_Message event_Message : m)
{
m_messages.put(event_Message.m_key, event_Message.m_value);
}
}
public String getMessage(final String name)
{
if (m_messages.get(name) == null)
{
Logger.error("Message not found: " + name);
}
// if null return an empty string
return m_messages.get(name) != null ? m_messages.get(name) : "";
}
public void clearMessages()
{
m_messages.clear();
}
}
Okay i hope this does explain how to implement an EventSystem at a Game. This meight not be the regular way at other Software but in games you queue up the events and handle them once in a Gameloop cycle. Also the listeners do the same.
So in your case. Implement such an handler and register the actors as listener. Sure they need to implement the listener interface and do something with the event. Let the one actor push an event into the handler which directs to the other actor and your are done. And they event dont need to know of each other and it does work for as much actors as you whish. You can even create 1 event for different classes different actors and so on. Usefull for example at mapchange. You push one event with the notice.. "changemap".. and every actor knows he need to stop moving and every subsystem knows that it does need to stop because of an mapchange and so on ...
It seems to be a bit overkill but it has alot of advantages and worth to use even at early stages. I made the misstake and started using it laterly and now i regret it.
Sorry for the bracing. It's not the regular java standart but more clear i think... Sorry for alot of varargs just like it at that point. Meight be confusing.
Literatur:
Game Coding Complete, Fourth Edition Chapter 11

GWT Editors - how to add N sub-editors of the same type based on a Collection

I have an object, Supply, that can either be an ElecSupply or GasSupply (see related question).
Regardless of which subclass is being edited, they all have a list of BillingPeriods.
I now need to instantiate N number of BillingPeriodEditors based on the contents of that list, and am pretty baffled as to how I should do it.
I am using GWTP. Here is the code of the SupplyEditor I have just got working:
public class SupplyEditor extends Composite implements ValueAwareEditor<Supply>
{
private static SupplyEditorUiBinder uiBinder = GWT.create(SupplyEditorUiBinder.class);
interface SupplyEditorUiBinder extends UiBinder<Widget, SupplyEditor>
{
}
#Ignore
final ElecSupplyEditor elecSupplyEditor = new ElecSupplyEditor();
#Path("")
final AbstractSubTypeEditor<Supply, ElecSupply, ElecSupplyEditor> elecSupplyEditorWrapper = new AbstractSubTypeEditor<Supply, ElecSupply, ElecSupplyEditor>(
elecSupplyEditor)
{
#Override
public void setValue(final Supply value)
{
setValue(value, value instanceof ElecSupply);
if(!(value instanceof ElecSupply))
{
showGasFields();
}
else
{
showElecFields();
}
}
};
#Ignore
final GasSupplyEditor gasSupplyEditor = new GasSupplyEditor();
#Path("")
final AbstractSubTypeEditor<Supply, GasSupply, GasSupplyEditor> gasSupplyEditorWrapper = new AbstractSubTypeEditor<Supply, GasSupply, GasSupplyEditor>(
gasSupplyEditor)
{
#Override
public void setValue(final Supply value)
{
setValue(value, value instanceof GasSupply);
if(!(value instanceof GasSupply))
{
showElecFields();
}
else
{
showGasFields();
}
}
};
#UiField
Panel elecPanel, gasPanel, unitSection;
public SupplyEditor()
{
initWidget(uiBinder.createAndBindUi(this));
gasPanel.add(gasSupplyEditor);
elecPanel.add(elecSupplyEditor);
}
// functions to show and hide depending on which type...
#Override
public void setValue(Supply value)
{
if(value instanceof ElecSupply)
{
showElecFields();
}
else if(value instanceof GasSupply)
{
showGasFields();
}
else
{
showNeither();
}
}
}
Now, as the list of BillingPeriods is a part of any Supply, I presume the logic for this should be in the SupplyEditor.
I got some really good help on the thread How to access PresenterWidget fields when added dynamically, but that was before I had implemented the Editor Framework at all, so I think the logic is in the wrong places.
Any help greatly appreciated. I can post more code (Presenter and View) but I didn't want to make it too hard to read and all they do is get the Supply from the datastore and call edit() on the View.
I have had a look at some examples of ListEditor but I don't really get it!
You need a ListEditor
It depends of how you want to present them in your actual view, but the same idea apply:
public class BillingPeriodListEditor implements isEditor<ListEditor<BillingPeriod,BillingPeriodEditor>>, HasRequestContext{
private class BillingPeriodEditorSource extends EditorSource<BillingPeriodEditor>{
#Override
public EmailsItemEditor create(final int index) {
// called each time u add or retrive new object on the list
// of the #ManyToOne or #ManyToMany
}
#Override
public void dispose(EmailsItemEditor subEditor) {
// called each time you remove the object from the list
}
#Override
public void setIndex(EmailsItemEditor editor, int index) {
// i would suggest track the index of the subeditor.
}
}
private ListEditor<BillingPeriod, BillingPeriodEditor> listEditor = ListEditor.of(new BillingPeriodEditorSource ());
// on add new one ...
// apply or request factory
// you must implement the HasRequestContext to
// call the create.(Proxy.class)
public void createNewBillingPeriod(){
// create a new one then add to the list
listEditor.getList().add(...)
}
}
public class BillingPeriodEditor implements Editor<BillingPeriod>{
// edit you BillingPeriod object
}
Then in you actual editor edit as is in the path Example getBillingPeriods();
BillingPeriodListEditor billingPeriods = new BillingPeriodListEditor ();
// latter on the clickhandler
billingPeriods.createNewBillingPeriod()
You are done now.

Categories