When adding the key it displays key added but then when I type inventory it says backpack empty, also when I go to use the item it wont let me, as its not usable.
public class Get extends AbstractCommand {
public Get (String name, String description){
super (name, description);
}
#Override
public void execute (Game game, String string) {
Item temp = game.getCurrentRoom().getItem(string);
UserInterface ui = game.getUI();
if(temp == null) ui.println("Item added\n");
else if (!(temp instanceof Moveable)) ui.println("item can not be moved");
else if (!game.getBackpack().add(temp)) ui.println("item can not be moved");
else game.getCurrentRoom().removeItem(temp);
}
}
#Override
public void execute(Game game, String string){
Item itemInBag = game.getBackpack().getItem(string);
Item itemInRoom = game.getCurrentRoom().getItem(string);
if(itemInBag!= null) {
if(itemInBag instanceof Useable){
game.setGameState(((Useable)itemInBag).use(game));
}else game.getUI().println("you can not use item");
}else if(itemInRoom!= null){
if(itemInRoom instanceof Useable){
game.setGameState(((Useable)itemInRoom).use(game));
}else game.getUI().println("this item can not be used");
}else {
game.getUI().println("take the item and use when you need to");
}
}
}
public class Inventory extends AbstractCommand {
public Inventory (String name, String description){
super (name, description);
}
#Override
public void execute (Game game, String string) {
if(((BackpackImpl)game.getBackpack()).getCount()==0) {
game.getUI().println("Backpack is empty");
}else {
for(Item i: game.getBackpack().getItems()) {
game.getUI().println(i.getName());
}
}
}
}
List<Item> items;
int maxCapacity;
public BackpackImpl (int maxCapacity){
this.maxCapacity = maxCapacity;
items = new ArrayList<Item>(5);
}
public BackpackImpl () {
this.maxCapacity = 5;
}
#Override
public boolean add(Item item){
if(items.size()>maxCapacity)
{System.out.println("your backpack canot fit no more\n");
return false;
}
items.add(item);
return true;
}
#Override
public Item remove(String string){
for(Item i: items){
if(((i.getName()).toLowerCase()).equals(string.toLowerCase())) {
items.remove(i);
return i;
}
}
return null;
}
#Override
public Item getItem(String string){
for(Item i: items){
if(((i.getName()).toLowerCase()).equals(string.toLowerCase())) {
return i;
}
}
return null;
}
#Override
public int getCapacity(){
return maxCapacity;
}
#Override
public List<Item> getItems(){
return items;
}
public int getCount (){
return items.size();
}
public void clear(){
items.clear();
}
}
Due to the "if/else (if)" construction in your Get.execute method, a new item is never added via the BackpackImpl.add() method.
Revise your Get.execute method and only print "Item added" when your code has actually called the BackpackImpl.add() method. Also, to keep your code readable, always use {} when using "if/else (if)" constructions. E.g.:
if (temp == null) {
ui.println("Item added\n");
} else if (!(temp instanceof Moveable || game.getBackpack().add(temp))) {
ui.println("item can not be moved");
} else {
game.getCurrentRoom().removeItem(temp);
}
Related
My class is a sensor, it can be on or off, ive been told I need to remove the "on" field as the only fields I can use are the two lists.
How can I remove the "on" field and still make the class function, without changing the sensor interface (so no changing the return types or adding parameters to the on, off, isOn methods)?
public class AverageSensor implements Sensor {
private ArrayList<Sensor> sensorList = new ArrayList<Sensor>();
private List<Integer> readingsList = new ArrayList<Integer>();
private boolean on = false;
public void addSensor(Sensor additional) {
sensorList.add(additional);
for (Sensor i : sensorList) {
if ((!i.isOn())) {
return;
}
}
on = true;
}
public List<Integer> readings() {
return readingsList;
}
#Override
public boolean isOn() {
if (on) {
return true;
}
return false;
}
#Override
public void on() {
for (Sensor i : sensorList) {
i.on();
}
on = true;
}
#Override
public void off() {
for (Sensor i : sensorList) {
if ((!i.isOn())) {
on = false;
return;
}
}
for (Sensor i : sensorList) {
i.off();
on = false;
return;
}
}
#Override
public int measure() {
int total = 0;
int average = 0;
if ((!isOn()) || sensorList.isEmpty()) {
throw new IllegalStateException();
}
for (Sensor i : sensorList) {
total = total + i.measure();
}
average = total / sensorList.size();
readingsList.add(average);
return average;
}
}
I have a combobox which shows list of User objects. I have coded a custom cell factory for the combobox:
#FXML ComboBox<User> cmbUserIds;
cmbUserIds.setCellFactory(new Callback<ListView<User>,ListCell<User>>(){
#Override
public ListCell<User> call(ListView<User> l){
return new ListCell<User>(){
#Override
protected void updateItem(Useritem, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setText(item.getId()+" "+item.getName());
}
}
} ;
}
});
ListView is showing a string(id+name), but when I select an item from listview, Combobox is showing toString() method return value i.e address of object.
I can't override toString() method, because the User domain object should be same as the one at server.
How to display id in combobox? Please suggest
EDIT1
I tried below code. Now combo box shows id when I select a value from the listview.
cmbUserIds.setConverter(new StringConverter<User>() {
#Override
public String toString(User user) {
if (user== null){
return null;
} else {
return user.getId();
}
}
#Override
public User fromString(String id) {
return null;
}
});
The selected value in combo box is cleared when control focus is lost. How to fix this?
EDIT2:
#FXML AnchorPane root;
#FXML ComboBox<UserDTO> cmbUsers;
List<UserDTO> users;
public class GateInController implements Initializable {
#Override
public void initialize(URL location, ResourceBundle resources) {
users = UserService.getListOfUsers();
cmbUsers.setItems(FXCollections.observableList(users));
cmbUsers.getSelectionModel().selectFirst();
// list of values showed in combo box drop down
cmbUsers.setCellFactory(new Callback<ListView<UserDTO>,ListCell<UserDTO>>(){
#Override
public ListCell<UserDTO> call(ListView<UserDTO> l){
return new ListCell<UserDTO>(){
#Override
protected void updateItem(UserDTO item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setText(item.getUserId()+" "+item.getUserNm());
}
}
} ;
}
});
//selected value showed in combo box
cmbUsers.setConverter(new StringConverter<UserDTO>() {
#Override
public String toString(UserDTO user) {
if (user == null){
return null;
} else {
return user.getUserId();
}
}
#Override
public UserDTO fromString(String userId) {
return null;
}
});
}
}
Just create and set a CallBack like follows:
#FXML ComboBox<User> cmbUserIds;
Callback<ListView<User>, ListCell<User>> cellFactory = new Callback<ListView<User>, ListCell<User>>() {
#Override
public ListCell<User> call(ListView<User> l) {
return new ListCell<User>() {
#Override
protected void updateItem(User item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setText(item.getId() + " " + item.getName());
}
}
} ;
}
}
// Just set the button cell here:
cmbUserIds.setButtonCell(cellFactory.call(null));
cmbUserIds.setCellFactory(cellFactory);
You need to provide a functional fromString() Method within the Converter!
I had the same problem as you have and as I implemented the fromString() with working code, the ComboBox behaves as expected.
This class provides a few of my objects, for dev-test purposes:
public class DevCatProvider {
public static final CategoryObject c1;
public static final CategoryObject c2;
public static final CategoryObject c3;
static {
// Init objects
}
public static CategoryObject getCatForName(final String name) {
switch (name) {
case "Kategorie 1":
return c1;
case "Cat 2":
return c2;
case "Steuer":
return c3;
default:
return c1;
}
}
}
The converter object:
public class CategoryChooserConverter<T> extends StringConverter<CategoryObject> {
#Override
public CategoryObject fromString(final String catName) {
//This is the important code!
return Dev_CatProvider.getCatForName(catName);
}
#Override
public String toString(final CategoryObject categoryObject) {
if (categoryObject == null) {
return null;
}
return categoryObject.getName();
}
}
I build a custom property and add it to a observable list. But no listener is called if property content is changed. The following code snippets shows you the 'building':
public static final class TestObject {
private final ReadOnlyStringWrapper title = new ReadOnlyStringWrapper();
private final BooleanProperty selected = new SimpleBooleanProperty(false);
public TestObject(String title) {
this.title.set(title);
}
public String getTitle() {
return title.get();
}
public ReadOnlyStringProperty titleProperty() {
return title.getReadOnlyProperty();
}
public boolean getSelected() {
return selected.get();
}
public BooleanProperty selectedProperty() {
return selected;
}
public void setSelected(boolean selected) {
this.selected.set(selected);
}
#Override
public int hashCode() {
return Objects.hash(title.get());
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final TestObject other = (TestObject) obj;
return Objects.equals(this.title.get(), other.title.get());
}
#Override
public String toString() {
return "TestObject{" +
"title=" + title.get() +
", selected=" + selected.get() +
'}';
}
}
This is my POJO class with my internal property values like name and selected.
public static final class TestProperty extends SimpleObjectProperty<TestObject> {
public TestProperty(String name) {
super(new TestObject(name));
init();
}
public TestProperty(TestObject testObject) {
super(testObject);
init();
}
public String getTitle() {
return getValue().getTitle();
}
public void setSelected(boolean selected) {
getValue().setSelected(selected);
}
public boolean getSelected() {
return getValue().getSelected();
}
public BooleanProperty selectedProperty() {
return getValue().selectedProperty();
}
public ReadOnlyStringProperty titleProperty() {
return getValue().titleProperty();
}
#Override
public void set(TestObject testObject) {
super.set(testObject);
init();
}
#Override
public void setValue(TestObject testObject) {
super.setValue(testObject);
init();
}
private void init() {
if (get() == null)
return;
get().titleProperty().addListener((v, o, n) -> fireValueChangedEvent());
get().selectedProperty().addListener((v, o, n) -> {
fireValueChangedEvent();
});
}
}
This is my custom property based on the POJO. All property changes will fire a change event for my custom property.
#Test
public void testSimple() {
final AtomicInteger counter = new AtomicInteger(0);
final TestProperty testProperty = new TestProperty("Test");
testProperty.addListener(observable -> {
System.out.println("New state: " + testProperty.get().toString());
counter.incrementAndGet();
});
testProperty.setSelected(true);
testProperty.setSelected(false);
Assert.assertEquals(2, counter.intValue());
}
In this test you can see that the property change event works fine.
#Test
public void testList() {
final AtomicInteger counter = new AtomicInteger(0);
final ObservableList<TestProperty> observableList = new ObservableListWrapper<>(new ArrayList<>());
observableList.add(new TestProperty("Test 1"));
observableList.add(new TestProperty("Test 2"));
observableList.add(new TestProperty("Test 3"));
observableList.addListener(new ListChangeListener<TestProperty>() {
#Override
public void onChanged(Change<? extends TestProperty> change) {
System.out.println("**************");
}
});
observableList.addListener((Observable observable) -> {
System.out.println("New state: " + ((TestProperty) observable).get().toString());
counter.incrementAndGet();
});
observableList.get(1).setSelected(true);
observableList.get(2).setSelected(true);
observableList.get(1).setSelected(false);
observableList.get(2).setSelected(false);
Assert.assertEquals(4, counter.intValue());
}
But in this code you see that the observable list not called the invalidation listener nor the change listener if a property value has changed in list.
What is wrong?
Thanks.
To create an observable list that will send "list updated" notifications if properties of elements of the list change, you need to create the list with an extractor. The extractor is a Callback that maps each element of the list to an array of Observables. If any of the Observables changes, InvalidationListeners and ListChangeListeners registered with the list will be notified.
So in your testList() method, you can do
final ObservableList<TestProperty> observableList = FXCollections.observableList(
new ArrayList<>(),
(TestProperty tp) -> new Observable[]{tp.selectedProperty()});
If the title were able to change, and you also wanted the list to receive notifications when that happened, you could do that too:
final ObservableList<TestProperty> observableList = FXCollections.observableList(
new ArrayList<>(),
(TestProperty tp) -> new Observable[]{tp.selectedProperty(), tp.titleProperty()});
Note that because the extractor is a Callback (essentially a function), the implementation can be arbitrarily complex (observe one property conditionally based on the value of another, etc).
The following code shows a simple implementation for a observable list with observable values:
public class ObservableValueListWrapper<E extends ObservableValue<E>> extends ObservableListWrapper<E> {
public ObservableValueListWrapper(List<E> list) {
super(list, o -> new Observable[] {o});}}
Or you must create your list with a POJO:
final ObservableList<MyPOJO> list = new ObservableListWrapper<>(new ArrayList(), o -> new Observable[] { new MyPOJOProperty(o) });
Or you use it so:
final ObservableList<MyPOJO> list = new ObservableListWrapper<>(new ArrayList(), o -> { return new Observable[] {
o.value1Property(),
o.value2Property(),
...};});
That is it! Thanks.
The ObservableList isn't notifying the listeners whenever a property contained within the list is modified, it notifies when the list is notified.
This can be seen when you modify your test:
#Test
public void testList() {
final AtomicInteger counter = new AtomicInteger(0);
final ObservableList<TestProperty> observableList = new ObservableListWrapper<>(new ArrayList<>());
observableList.addListener(new ListChangeListener<TestProperty>() {
#Override
public void onChanged(Change<? extends TestProperty> change) {
System.out.println("**************");
counter.incrementAndGet();
}
});
observableList.add(new TestProperty("Test 1"));
observableList.add(new TestProperty("Test 2"));
observableList.add(new TestProperty("Test 3"));
observableList.get(1).setSelected(true);
observableList.get(2).setSelected(true);
observableList.get(1).setSelected(false);
observableList.get(2).setSelected(false);
Assert.assertEquals(3, counter.intValue());
}
EDIT: Added an example ObserverListener decorator which provides the auto registration/deregistration of the ObservableValue change listener as desired by the OP.
/**
* Decorates an {#link ObservableList} and auto-registers the provided
* listener to all new observers, and auto-unregisters listeners when the
* item is removed from the list.
*
* #param <T>
*/
public class ObservableValueList<T extends ObservableValue> implements ObservableList<T> {
private final ObservableList<T> list;
private final ChangeListener<T> valueListener;
public ObservableValueList(ObservableList<T> list, ChangeListener<T> valueListener) {
this.list = list;
//list to existing contents of list
this.list.stream().forEach((item) -> item.addListener(valueListener));
//register listener which will add/remove listner on change to list
this.list.addListener((Change<? extends T> change) -> {
change.getAddedSubList().stream().forEach(
(item) -> item.addListener(valueListener));
change.getRemoved().stream().forEach(
(item) -> item.removeListener(valueListener));
});
this.valueListener = valueListener;
}
/* What follows is all the required delegate methods */
#Override
public int size() {
return list.size();
}
#Override
public boolean isEmpty() {
return list.isEmpty();
}
#Override
public boolean contains(Object o) {
return list.contains(o);
}
#Override
public Iterator<T> iterator() {
return list.iterator();
}
#Override
public Object[] toArray() {
return list.toArray();
}
#Override
public <T> T[] toArray(T[] ts) {
return list.toArray(ts);
}
#Override
public boolean add(T e) {
return list.add(e);
}
#Override
public boolean remove(Object o) {
return list.remove(o);
}
#Override
public boolean containsAll(Collection<?> clctn) {
return list.containsAll(clctn);
}
#Override
public boolean addAll(Collection<? extends T> clctn) {
return list.addAll(clctn);
}
#Override
public boolean addAll(int i, Collection<? extends T> clctn) {
return list.addAll(i, clctn);
}
#Override
public boolean removeAll(Collection<?> clctn) {
return list.removeAll(clctn);
}
#Override
public boolean retainAll(Collection<?> clctn) {
return list.retainAll(clctn);
}
#Override
public void replaceAll(UnaryOperator<T> uo) {
list.replaceAll(uo);
}
#Override
public void sort(Comparator<? super T> cmprtr) {
list.sort(cmprtr);
}
#Override
public void clear() {
list.clear();
}
#Override
public T get(int i) {
return list.get(i);
}
#Override
public T set(int i, T e) {
return list.set(i, e);
}
#Override
public void add(int i, T e) {
list.add(i, e);
}
#Override
public T remove(int i) {
return list.remove(i);
}
#Override
public int indexOf(Object o) {
return list.indexOf(o);
}
#Override
public int lastIndexOf(Object o) {
return list.lastIndexOf(o);
}
#Override
public ListIterator<T> listIterator() {
return list.listIterator();
}
#Override
public ListIterator<T> listIterator(int i) {
return list.listIterator(i);
}
#Override
public List<T> subList(int i, int i1) {
return list.subList(i, i1);
}
#Override
public Spliterator<T> spliterator() {
return list.spliterator();
}
#Override
public void addListener(ListChangeListener<? super T> ll) {
list.addListener(ll);
}
#Override
public void removeListener(ListChangeListener<? super T> ll) {
list.removeListener(ll);
}
#Override
public boolean addAll(T... es) {
return list.addAll(es);
}
#Override
public boolean setAll(T... es) {
return list.setAll(es);
}
#Override
public boolean setAll(Collection<? extends T> clctn) {
return list.setAll(clctn);
}
#Override
public boolean removeAll(T... es) {
return list.removeAll(es);
}
#Override
public boolean retainAll(T... es) {
return list.retainAll(es);
}
#Override
public void remove(int i, int i1) {
list.remove(i, i1);
}
#Override
public FilteredList<T> filtered(Predicate<T> prdct) {
return list.filtered(prdct);
}
#Override
public SortedList<T> sorted(Comparator<T> cmprtr) {
return list.sorted(cmprtr);
}
#Override
public SortedList<T> sorted() {
return list.sorted();
}
#Override
public void addListener(InvalidationListener il) {
list.addListener(il);
}
#Override
public void removeListener(InvalidationListener il) {
list.removeListener(il);
}
}
I want a javax.swing.ListModel be shared among multiple javax.swing.ComboBoxModels.
With the following code , when I select an item from the JComboBox,in a running program through the view, the JComboBox always shows the first item as selected.
public class DelegatedComboBoxModel<T> extends AbstractListModel<T> implements
ComboBoxModel<T> {
protected ListModel<T> listModel;
protected Object selectedObject;
ListDataListener listDataChangeDelegater = new ListDataListener() {
#Override
public void intervalRemoved(ListDataEvent e) {
fireIntervalRemoved(DelegatedComboBoxModel.this, e.getIndex0(),
e.getIndex1());
}
#Override
public void intervalAdded(ListDataEvent e) {
fireIntervalAdded(DelegatedComboBoxModel.this, e.getIndex0(),
e.getIndex1());
}
#Override
public void contentsChanged(ListDataEvent e) {
fireContentsChanged(DelegatedComboBoxModel.this, e.getIndex0(),
e.getIndex1());
}
};
public DelegatedComboBoxModel(ListModel<T> listModel) {
// DefaultComboBoxModel<E>
this.listModel = listModel;
if (listModel.getSize() > 0) {
selectedObject = listModel.getElementAt(0);
}
listModel.addListDataListener(listDataChangeDelegater);
}
#Override
public T getElementAt(int index) {
if (index >= 0 && index < listModel.getSize())
return listModel.getElementAt(index);
else
return null;
}
#Override
public int getSize() {
return listModel.getSize();
}
#Override
public void setSelectedItem(Object anObject) {
if ((selectedObject != null && !selectedObject.equals(anObject))
|| selectedObject == null && anObject != null) {
selectedObject = anObject;
fireContentsChanged(this, -1, -1);
}
}
#Override
public Object getSelectedItem() {
return selectedObject;
}
}
I cannot figure out what went wrong with the above code.
How to fix the code ?
(Limitation : Should not use or subclass DefaultComboBoxModel or use any third-party library.)
I've created a ComboBoxModel class which extends AbstractListModel. I can add item to the combobox, but when I try to remove, I get an exception
Exception in thread "AWT-EventQueue-0"
java.lang.IllegalArgumentException: null source
at line
this.fireIntervalRemoved(selectedItem, itemIndex, itemIndex);
public class TComboBoxModel extends AbstractListModel implements ComboBoxModel {
private int itemIndex;
private Object selectedItem = null;
private ArrayList<Object> itemList;
public TComboBoxModel() {
itemList = new ArrayList<>();
}
public void addItem(String item) {
this.itemList.add(item);
this.fireIntervalAdded(item, itemIndex, itemIndex);
}
public void removeItem() {
if (itemIndex >= 0 && itemIndex < getSize()) {
this.itemList.remove(itemIndex);
this.fireIntervalRemoved(selectedItem, itemIndex, itemIndex);
}
}
#Override
public void setSelectedItem(Object anObject) {
if ((selectedItem != null && !selectedItem.equals(anObject)) || selectedItem == null && anObject != null) {
this.selectedItem = anObject;
this.fireContentsChanged(anObject, -1, -1);
}
}
#Override
public Object getSelectedItem() {
return selectedItem;
}
#Override
public int getSize() {
return itemList.size();
}
#Override
public Object getElementAt(int index) {
return itemList.get(index).toString();
}
public int getItemIndex() {
return itemIndex;
}
public void increaseItemIndex() {
itemIndex++;
}
public void decreaseItemIndex() {
itemIndex--;
}
}
Pass this to the fire* methods in the model. The event source is the model, not the item.
From the documentation:
source - the ListModel that changed, typically "this"
You should probably change it to say:
if (selectedItem != null) {
fireIntervalRemoved(this, itemIndex, itemIndex);
}
Since you can't remove an item unless you know which one to remove by having a selected item.
You are going to have to be setting the itemIndex variable appropriately too.
public void setSelectedItem(Object anObject) {
if ((selectedItem != null && !selectedItem.equals(anObject)) || selectedItem == null && anObject != null) {
this.selectedItem = anObject;
this.fireContentsChanged(anObject, -1, -1);
itemIndex = ... index in itemList where anObject is located (or -1 if not found) ...
}
}
Thanks to #kiheru for pointing out the problem with the 1st argument.