Need to have HashMap add(sum) multiple values within a key - jsf

I am attempting to have a single button process multiple inputs in the same input boxes. I have 16 input boxes, each with its own id# (YfProduct) which I am using as the key for my hashmap. For the input value, I have Weight. A user will enter in whatever double weight they want, in however many input boxes they wish, and click a button (an a4j:commandButton) which activates the method below.
private HashMap<Integer, Double> storeWeight = new HashMap<Integer, Double>();
public void storeWeight(Yieldfl yieldfl){
for (YieldItem row : yielditem) {
storeWeight.put(row.getYfProduct(), row.getWeight());
System.out.print(storeWeigt)}
}
Right now this code will set the appropriate values with the right key, and replace those values with new input entered and another button click. However what I am trying to do is have the bean save the previous values, and sum up the next values entered with the previous entry(s) that have the same key. So at the end of the user's input, the HashMap will contain 16 keys with the sum of the individual values added up for each key. I havn't been able to come up with a way to do this without some serious hardcoding. Help much appreciated.

So this actually turned out to be a simple fix, I'll put the solution on here just incase somebody runs into a similar problem. It's my first time working with a HashMap so I had a colleague help me out. I needed the value to call the key.
storeWeight.put(row.getYfProduct(), storeWeight.get(row.getYfProduct() + (row.getWeight()));
and to avoid null-pointers:
public void storeWeight(Yieldfl yieldfl){
for (YieldItem row : yielditem) {
Double oldValue = storeWeight.get(row.getYfProduct());
if (oldValue == null)
oldValue = 0.0;
storeWeight.put(row.getYfProduct(), oldValue + (row.getWeight()));
row.setWeight(0.0);
System.out.print(storeWeight);}
}

Related

How to override the textbox content in CSWPFAutoCompleteTextBox

I am using the autocomplete textbox from this MSDN link.
Question:
CSWPFAutoCompleteTextBox has property "AutoSuggestionList" which I bound this to observable string collection. Each string is made of id + description. When user selects an item from dropdown, how can I override the texbox content? I want to manipulate the text box content.
This is a textbox that extends a wpf combobox to make it searchable.
When the user types in a string in the textbox, it displays matching strings as a dropdown, user selects an item and the item is displayed in the textbox.
the question is how to override the textbox contents of this control.
Without actual format of your code it is hard to answer precisely, but for example, if your strings are
string[] suggestions = {"0: Yes", "1: No", "666: whatever"}
Then you could get the number by something like
sugestedString.Substring(0, sugestedString.IndexOf(':'));
EDIT: I misunderstood the question. So if I now understands correctly, you might do it with
for(int i = 0; i < suggestions.Length; i++) {
if(suggestions[i] == selectedString) {
return i;
}
}
If you seek only a number within your list of all possible suggestions.
If you seek a number within narrowed-down suggestions, it gets somewhat more difficult.
You firstly need to note down what user has typed so-far (e.g. "Aut"). Then you need what he actually selected (e.g. "Automotive"). With those things, you can then search all your possible suggestions, count how many of them satisfies the user-typed beginning and finaly which of them is the selected one. It might look like this:
int counter=0;
for(int i = 0; i < suggestions.Length; i++) {
if( suggestions[i].StartsWith(typedString)) {
counter++;
if(suggestions[i] == selectedString) {
counter;
}
}
}

get back to lower level j2me

I have a textfield (lower level) a call a function that call a higher level form that contains a datafield to pick up a date and display it in my textfield lowerlevel.
The problem is I cannot get back to my textfield (lower level) since the datefield appears
public void formdatepicker() {
final javax.microedition.lcdui.Command CONFIRM_COMMAND
= new javax.microedition.lcdui.Command("OK",
javax.microedition.lcdui.Command.OK, 1);
javax.microedition.lcdui.Command CANCEL_COMMAND
= new javax.microedition.lcdui.Command("Cancel",
javax.microedition.lcdui.Command.CANCEL, 2);
final DateField datefield = new DateField("Pick a date", DateField.DATE);
form.append(datefield);
form.addCommand(CONFIRM_COMMAND);
form.addCommand(CANCEL_COMMAND);
form.setCommandListener(new CommandListener() {
public void commandAction(javax.microedition.lcdui.Command c, Displayable d) {
if (c == CONFIRM_COMMAND) {
Date date = datefield.getDate();
display.setCurrent(null);// try to hide the current form to get
}
}
});
Display.getInstance().getJ2MEDisplay().setCurrent(form);
Your mistake is wrong assumption about what setCurrent(null) does. Per your question and code snippet, it looks like you expect it to somehow show the screen that has been displayed prior to form. It doesn't, see the exaplanation in the API javadocs (available online):
The application may pass null as the argument to setCurrent(). This does not have the effect of setting the current Displayable to null; instead, the current Displayable remains unchanged. However, the application management software may interpret this call as a request from the application that it is requesting to be placed into the background...
If you want to use setCurrent(Displayable) to show some screen instead of current one, you need to pass this screen as an argument to setCurrent.
For the sake of completeness, note that there is another version of setCurrent that accepts two parameters, first of which is Alert, which works a bit differently, but it is not applicable in your case because you use Form not Alert.

Is it possible to use custom JSF converter for UISelectOne, depending on value of Bean property or UIInputText value?

I need to get help with a problem.
I have an application, which displays data from database.
In database I am using one column for identifying an object with changes in time (version of object).
I mean that rows have different IDs, but same OBJECT_ID.
They are also differentiated by validity DATE columns (VALID_FROM and VALID_TO), so i can find the proper version of object in selected time.
When page is shown to user, he sets DATE for displaying data.
There is form on page, with Select Boxes (their items are object from database).
I need to show only items, that satisfy the validity condition.
Everything is OK before form handling.
I am using custom converter, that converts String value (ID of row in database table) to Object of my model, but I need two values for row to find. (ID and DATE from user form).
Converter method getAsObject gets only one VALUE parameter (ID from Select Box).
How to get second parametr to work?
Can i find it from Bean or from JSF input object?
If there is some way that includes using <f:attribute/>, is there way to set <f:attribute/> by Java code in Bean? (Form is generated by bean method).
I am sorry for my English, i try my best :o)
P.S.: Date parameter is important in sqlMap because of JOIN used. I search by ID, but other tables are joined by OBJECT_ID (version) => and i need to get one row only
Thanks for answers and have a nice day
Worsik
Edit:
Select Boxes are generated by this code:
uiInput = new HtmlSelectOneMenu();
UISelectItems items = new UISelectItems();
items.setValueExpression("value", createValueExpression(
"#{myBean.referencedList." + item.getName() + "}",
List.class));
uiInput.getChildren().add(items);
Converter method looks like this:
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
if (value.equals("")) {
return null;
}
Class entityType = component.getValueExpression("value").getType(
FacesContext.getCurrentInstance().getELContext());
// TODO: need to get date value from form
Date day = new Date();
return myService(context).findEntityById(entityType, Long.valueOf(value), day);
}
entityType is Class of model for object from database
myService calls Dao object and sqlMap is called
select * from object_table as t1
JOIN other_table as t2 ON t1.object_fk = t2.object_id // (or version_id)
where t1.id = #id:INTEGER#
and t2.valid_from <= #day:DATE#
and t2.valid_to >= #day:DATE#
I figured out how to do this by <f:attribute/> by code in converter:
Date day = (Date) component.getAttributes().get("dateForSearch");
and in bean method after generating SelectBox with code:
uiInput.getAttributes().put("dateForSearch", getSelectedDate());
and in pages without dynamic generating of forms i used:
<SelectOneMenu>
...
<f:attribute name="dateForSearch" value="#{myBean.selectedDate}" />
</SelectOneMenu>
I hope that this will not be only self answered question and will help somebody else

How to cancel BeforeEdit event in gxt EditorGrid based on data stored in model for the selected row

I have an EditorGrid. I would like to cancel the edit (using the BeforeEdit event) if the user does not have edit rights to that specific column. This data (a "canEdit" value), is in the data store for the row, but has not been added a column to the grid.
I think the best way to go about this is by cancelling the edit in the BeforeEdit event. I am having trouble, however, getting the data from the selected row in the BeforeEdit event. If someone could point me in the right direction with a small code snippet for how to grab data values from the selected row in a BeforeEdit event, I would be most appreciative! Or, if there is a better way to proceed, would love to hear from you.
Thank you!
Jennifer
final EditorGrid<Plant> grid = new EditorGrid<Plant>(store, cm);
grid.addListener(Events.BeforeEdit, new Listener<GridEvent<Plant>>(){
#Override
public void handleEvent(GridEvent<Plant> be) {
//This retrieves the model being edited.
Plant model = be.getModel();
GWT.log("Model edited "+model.getName());
}
});
This snippet uses the sample program provided by GXT. You can see the demo of the original sample as well the full code here.
Here's what I used that worked:
grid.addListener(Events.BeforeEdit, new Listener<BaseEvent>() {
#Override
ModelData comment = ((EditorGrid)be.GetSource().getSelectionModel().getSelectedItem();
Boolean canEdit = Boolean.parseBoolean(comment.get("canEdit").toString());
be.setCancelled(!canEdit);
}

how to get the name of an object and change the text of a textbox with its' name?

Hi there (developers of wp7 apps).
The following problem already caused countless sleepless nights, so i hope anyone could provide me with a solution.
Elements grouped as a grid should call (on tap) a function that changes a textbox' text with one i declare.
I already have the function that reads the "name" of the object:
private void FunctionABC(object sender, System.Windows.Input.GestureEventArgs e)
{
//Objects name
string ObjectSender = (sender as Grid).Name.ToString();
//But how to continue, if I want kind of "this" result?:
this.ObjectSender.Text = "abc";
}
I appreciate any answer my problem. Thanks.
If your question is how to change the textbox.text property which is placed inside a grid if you tap the grid then you should iterate through the grids Children and find the textbox you are looking for and then change it's text property.
First of all you need to change the this line :
string ObjectSender = (sender as Grid).Name.ToString();
because this line gives you the name of the Grid and not the Grid itself.
Grid ObjectSender = (Grid) sender;
And then you can search through it's children.

Resources