List of usage examples for com.vaadin.ui TextField setValue
@Override public void setValue(String value)
From source file:fi.semantum.strategia.Utils.java
License:Open Source License
public static void editTextAndId(final Main main, String title, final Base container) { final Database database = main.getDatabase(); final Window subwindow = new Window(title, new VerticalLayout()); subwindow.setModal(true);/*from ww w . jav a2 s . c om*/ subwindow.setWidth("400px"); subwindow.setHeight("500px"); subwindow.setResizable(false); VerticalLayout winLayout = (VerticalLayout) subwindow.getContent(); winLayout.setMargin(true); winLayout.setSpacing(true); final TextField tf = new TextField(); tf.setCaption("Lyhytnimi:"); tf.addStyleName(ValoTheme.TEXTFIELD_SMALL); tf.setValue(container.getId(database)); tf.setWidth("100%"); winLayout.addComponent(tf); final TextArea ta = new TextArea(); ta.setCaption("Teksti:"); ta.setValue(container.getText(database)); ta.setWidth("100%"); ta.setHeight("290px"); winLayout.addComponent(ta); Button save = new Button("Tallenna", new Button.ClickListener() { private static final long serialVersionUID = 6641880870005364983L; public void buttonClick(ClickEvent event) { String idValue = tf.getValue(); String value = ta.getValue(); main.removeWindow(subwindow); container.modifyId(main, idValue); container.modifyText(main, value); Collection<String> tags = Tag.extractTags(value); database.assertTags(tags); ArrayList<Tag> tagObjects = new ArrayList<Tag>(); for (String s : tags) tagObjects.add(database.getOrCreateTag(s)); container.assertRelatedTags(database, tagObjects); Updates.update(main, true); Property emails = Property.find(database, Property.EMAIL); String addr = emails.getPropertyValue(container); if (addr != null && !addr.isEmpty()) { String[] addrs = addr.split(","); if (addrs.length > 0) { try { Email.send(addrs, "Muutos strategiakartassa: " + container.getId(database), "Kyttj " + main.account.getId(database) + " on muuttanut strategiakarttaa.<br/><br/>Lyhytnimi: " + container.getId(database) + "<br/><br/>Teksti: " + container.getText(database)); } catch (MessagingException e) { e.printStackTrace(); } } } } }); Button discard = new Button("Peru muutokset", new Button.ClickListener() { private static final long serialVersionUID = -784522457615993823L; public void buttonClick(ClickEvent event) { Updates.update(main, true); main.removeWindow(subwindow); } }); HorizontalLayout hl2 = new HorizontalLayout(); hl2.setSpacing(true); hl2.addComponent(save); hl2.addComponent(discard); winLayout.addComponent(hl2); winLayout.setComponentAlignment(hl2, Alignment.MIDDLE_CENTER); main.addWindow(subwindow); ta.setCursorPosition(ta.getValue().length()); }
From source file:fi.semantum.strategia.widget.Datatype.java
License:Open Source License
public AbstractField<?> getEditor(final Main main, final Base base, final Indicator indicator, final boolean forecast, final CommentCallback callback) { Object value = forecast ? indicator.getForecast() : indicator.getValue(); final String formatted = indicator.getDatatype(main.getDatabase()).format(value); final TextField tf = new TextField(); tf.setValue(formatted); if (main.canWrite(base)) { tf.addValidator(new Validator() { private static final long serialVersionUID = 9043601075831736114L; @Override/*from ww w . j a v a 2 s . c o m*/ public void validate(Object value) throws InvalidValueException { try { BigDecimal.valueOf(Double.parseDouble((String) value)); } catch (NumberFormatException e) { throw new InvalidValueException("Arvon tulee olla numero"); } } }); tf.addValueChangeListener(new ValueChangeListener() { private static final long serialVersionUID = 3547126051252580446L; @Override public void valueChange(ValueChangeEvent event) { try { final BigDecimal number = BigDecimal.valueOf(Double.parseDouble(tf.getValue())); indicator.updateWithComment(main, base, number, forecast, new AbstractCommentCallback() { public void canceled() { tf.setValue(formatted); if (callback != null) callback.canceled(); } public void runWithComment(String shortComment, String comment) { if (callback != null) callback.runWithComment(shortComment, comment); } }); } catch (NumberFormatException e) { tf.setComponentError(new UserError("Arvon tulee olla numero")); } } }); } else { tf.setReadOnly(true); } return tf; }
From source file:fi.semantum.strategia.widget.Indicator.java
License:Open Source License
private static TextField makeUnit(Database database, HorizontalLayout hl, Indicator indicator) { if (!(indicator.getDatatype(database) instanceof NumberDatatype)) return null; final TextField unit = new TextField("Yksikk"); unit.setValue(indicator.getUnit()); unit.setWidth("100%"); unit.addStyleName(ValoTheme.TEXTFIELD_SMALL); hl.addComponent(unit);//from w w w .j a va2s . c om hl.setComponentAlignment(unit, Alignment.MIDDLE_CENTER); hl.setExpandRatio(unit, 1.0f); return unit; }
From source file:fi.semantum.strategia.widget.Indicator.java
License:Open Source License
public static void editIndicator(final Main main, final Base base, final Indicator indicator) { final Database database = main.getDatabase(); VerticalLayout content = new VerticalLayout(); content.setSizeFull();// w ww . ja v a 2s. c om content.setHeightUndefined(); content.setSpacing(true); HorizontalLayout hl = new HorizontalLayout(); hl.setSpacing(true); hl.setWidth("100%"); content.addComponent(hl); content.setExpandRatio(hl, 0.0f); final TextField tf = new TextField("Tunniste"); tf.setValue(indicator.getId(database)); tf.setWidth("100%"); tf.addStyleName(ValoTheme.TEXTFIELD_SMALL); hl.addComponent(tf); hl.setComponentAlignment(tf, Alignment.MIDDLE_CENTER); hl.setExpandRatio(tf, 2.0f); final TextField tf1 = new TextField("Teksti"); tf1.setValue(indicator.getText(database)); tf1.setWidth("100%"); tf1.addStyleName(ValoTheme.TEXTFIELD_SMALL); hl.addComponent(tf1); hl.setComponentAlignment(tf1, Alignment.MIDDLE_CENTER); hl.setExpandRatio(tf1, 2.0f); final TextField unit = makeUnit(database, hl, indicator); final TextField tf2 = new TextField(); tf2.setCaption("Voimassaolo"); tf2.setValue(Utils.getValidity(database, indicator)); tf2.addStyleName(ValoTheme.TEXTFIELD_TINY); tf2.setWidth("100%"); hl.addComponent(tf2); hl.setComponentAlignment(tf2, Alignment.MIDDLE_CENTER); hl.setExpandRatio(tf2, 1.0f); final TextArea ta = new TextArea("Mritys"); ta.setValue(indicator.getText(database)); ta.setWidth("100%"); ta.setHeight("100px"); content.addComponent(ta); content.setComponentAlignment(ta, Alignment.MIDDLE_CENTER); content.setExpandRatio(ta, 0.0f); final Label ta2 = makeHistory(database, indicator, main.getUIState().forecastMeters); content.addComponent(ta2); content.setComponentAlignment(ta2, Alignment.MIDDLE_CENTER); content.setExpandRatio(ta2, 1.0f); HorizontalLayout buttons = new HorizontalLayout(); buttons.setSpacing(true); buttons.setMargin(false); Button ok = new Button("Tallenna", new Button.ClickListener() { private static final long serialVersionUID = 1992235622970234624L; public void buttonClick(ClickEvent event) { indicator.modifyId(main, tf.getValue()); Utils.modifyValidity(main, indicator, tf2.getValue()); indicator.modifyText(main, tf1.getValue()); indicator.modifyDescription(main, ta.getValue()); if (unit != null) { indicator.modifyUnit(main, unit.getValue()); } Updates.update(main, true); manageIndicators(main, main.getUIState().currentItem); } }); buttons.addComponent(ok); Button close = new Button("Sulje"); buttons.addComponent(close); final Window dialog = Dialogs.makeDialog(main, "650px", "800px", "Muokkaa indikaattoria", null, content, buttons); close.addClickListener(new Button.ClickListener() { private static final long serialVersionUID = 1992235622970234624L; public void buttonClick(ClickEvent event) { main.removeWindow(dialog); manageIndicators(main, main.getUIState().currentItem); } }); }
From source file:fi.semantum.strategia.widget.Indicator.java
License:Open Source License
public static String updateIndicatorValue(final Main main, HorizontalLayout hl, final Base base, final Indicator indicator, boolean canWrite) { final Database database = main.getDatabase(); Datatype dt = indicator.getDatatype(database); if (dt != null) { final AbstractField<?> field = dt.getEditor(main, base, indicator, false, null); field.setWidth("150px"); field.setReadOnly(!canWrite);/*from w ww . j a v a 2s. c om*/ hl.addComponent(field); hl.setComponentAlignment(field, Alignment.MIDDLE_LEFT); Object value = field.getValue(); return value != null ? value.toString() : ""; } else { Object value = indicator.getValue(); final String formatted = value != null ? value.toString() : ""; final TextField tf = new TextField(); tf.setValue(formatted); tf.addValueChangeListener(new ValueChangeListener() { private static final long serialVersionUID = 3547126051252580446L; @Override public void valueChange(ValueChangeEvent event) { try { double value = Double.parseDouble(tf.getValue()); indicator.updateWithComment(main, base, value, main.getUIState().forecastMeters, new AbstractCommentCallback() { public void canceled() { tf.setValue(formatted); } }); } catch (NumberFormatException e) { tf.setComponentError(new UserError("Arvon tulee olla numero")); } } }); tf.setWidth("150px"); tf.setReadOnly(!canWrite); hl.addComponent(tf); hl.setComponentAlignment(tf, Alignment.MIDDLE_LEFT); return tf.getValue(); } }
From source file:fi.semantum.strategia.widget.Meter.java
License:Open Source License
public static void editMeter(final Main main, final Base base, final Meter meter) { Database database = main.getDatabase(); final VerticalLayout content = new VerticalLayout(); content.setSizeFull();//from w ww.j av a 2 s.c o m content.setHeightUndefined(); content.setSpacing(true); HorizontalLayout hl = new HorizontalLayout(); hl.setWidth("100%"); hl.setSpacing(true); hl.setMargin(false); final TextField tf = new TextField(); tf.setCaption("Lyhytnimi"); tf.setValue(meter.getId(database)); tf.addStyleName(ValoTheme.TEXTFIELD_TINY); tf.setWidth("100%"); hl.addComponent(tf); hl.setComponentAlignment(tf, Alignment.TOP_CENTER); hl.setExpandRatio(tf, 1.0f); final TextField tf1 = new TextField(); tf1.setCaption("Teksti"); tf1.setValue(meter.getText(database)); tf1.addStyleName(ValoTheme.TEXTFIELD_TINY); tf1.setWidth("100%"); hl.addComponent(tf1); hl.setComponentAlignment(tf1, Alignment.TOP_CENTER); hl.setExpandRatio(tf1, 2.0f); content.addComponent(hl); content.setComponentAlignment(hl, Alignment.TOP_CENTER); content.setExpandRatio(hl, 0.0f); final TextField tf2 = new TextField(); tf2.setCaption("Voimassaolo"); tf2.setValue(Utils.getValidity(database, meter)); tf2.addStyleName(ValoTheme.TEXTFIELD_TINY); tf2.setWidth("100%"); content.addComponent(tf2); content.setComponentAlignment(tf2, Alignment.TOP_CENTER); content.setExpandRatio(tf2, 0.0f); final TextArea ta = new TextArea(); ta.setCaption("Mritys"); ta.setValue(meter.getText(database)); ta.addStyleName(ValoTheme.TEXTAREA_TINY); ta.setHeight("100%"); ta.setWidth("100%"); content.addComponent(ta); content.setComponentAlignment(ta, Alignment.TOP_CENTER); content.setExpandRatio(ta, 1.0f); final TrafficValuation valuation = meter.trafficValuation; final Runnable onOK = valuation != null ? valuation.getEditor(content, main, meter) : null; Indicator indicator = meter.getPossibleIndicator(database); if (indicator != null) { final Label ta2 = Indicator.makeHistory(database, indicator, main.getUIState().forecastMeters); content.addComponent(ta2); content.setComponentAlignment(ta2, Alignment.MIDDLE_CENTER); content.setExpandRatio(ta2, 1.0f); } HorizontalLayout buttons = new HorizontalLayout(); buttons.setSpacing(true); buttons.setMargin(false); Button ok = new Button("Tallenna", new Button.ClickListener() { private static final long serialVersionUID = 1992235622970234624L; public void buttonClick(ClickEvent event) { if (onOK != null) onOK.run(); meter.modifyId(main, tf.getValue()); meter.modifyText(main, tf1.getValue()); Utils.modifyValidity(main, meter, tf2.getValue()); meter.modifyDescription(main, ta.getValue()); Updates.update(main, true); manageMeters(main, main.getUIState().currentItem); } }); buttons.addComponent(ok); Button close = new Button("Sulje"); buttons.addComponent(close); final Window dialog = Dialogs.makeDialog(main, "500px", "800px", "Mrit mittaria", null, content, buttons); close.addClickListener(new Button.ClickListener() { private static final long serialVersionUID = -8065367213523520602L; public void buttonClick(ClickEvent event) { main.removeWindow(dialog); manageMeters(main, main.getUIState().currentItem); } }); }
From source file:fi.semantum.strategia.widget.NumberTrafficValuation.java
License:Open Source License
@Override public Runnable getEditor(VerticalLayout layout, final Main main, final Meter meter) { Indicator indicator = meter.getPossibleIndicator(main.getDatabase()); String unit = indicator.getUnit(); final ComboBox combo = new ComboBox("Valitse mittarin mritystapa"); combo.addItem(State.INCREASE3); combo.addItem(State.DECREASE3); combo.addItem(State.INCREASE2); combo.addItem(State.DECREASE2); combo.setInvalidAllowed(false);/*from w w w .j a v a2s . co m*/ combo.setNullSelectionAllowed(false); combo.setWidth("100%"); combo.addStyleName(ValoTheme.COMBOBOX_TINY); layout.addComponent(combo); layout.setComponentAlignment(combo, Alignment.TOP_CENTER); layout.setExpandRatio(combo, 0.0f); final VerticalLayout vl1 = new VerticalLayout(); vl1.setHeight("50px"); vl1.setWidth("100%"); vl1.setStyleName("redBlock"); layout.addComponent(vl1); layout.setComponentAlignment(vl1, Alignment.TOP_CENTER); layout.setExpandRatio(vl1, 0.0f); final Label l1 = new Label(" > " + df.format(greenLimit.doubleValue()) + " " + unit); l1.setSizeUndefined(); l1.addStyleName(ValoTheme.LABEL_LARGE); vl1.addComponent(l1); vl1.setComponentAlignment(l1, Alignment.MIDDLE_CENTER); HorizontalLayout hl1 = new HorizontalLayout(); hl1.setSpacing(true); layout.addComponent(hl1); layout.setComponentAlignment(hl1, Alignment.TOP_LEFT); final TextField tf1 = new TextField(); tf1.setValue(df.format(greenLimit.doubleValue())); tf1.setWidth("150px"); tf1.setCaption("Suurempi raja-arvo"); tf1.setStyleName(ValoTheme.TEXTFIELD_TINY); tf1.setValidationVisible(true); hl1.addComponent(tf1); Label unit1 = new Label(); unit1.setSizeUndefined(); unit1.setCaption(""); unit1.setValue(unit); hl1.addComponent(unit1); hl1.setComponentAlignment(unit1, Alignment.MIDDLE_LEFT); final VerticalLayout vl2 = new VerticalLayout(); vl2.setHeight("50px"); vl2.setWidth("100%"); vl2.addStyleName("yellowBlock"); layout.addComponent(vl2); layout.setComponentAlignment(vl2, Alignment.TOP_CENTER); layout.setExpandRatio(vl2, 0.0f); final Label l2 = new Label(); l2.setSizeUndefined(); l2.addStyleName(ValoTheme.LABEL_LARGE); vl2.addComponent(l2); vl2.setComponentAlignment(l2, Alignment.MIDDLE_CENTER); HorizontalLayout hl2 = new HorizontalLayout(); hl2.setSpacing(true); layout.addComponent(hl2); layout.setComponentAlignment(hl2, Alignment.TOP_LEFT); final TextField tf2 = new TextField(); tf2.setWidth("150px"); tf2.setCaption("Pienempi raja-arvo"); tf2.setStyleName(ValoTheme.TEXTFIELD_TINY); tf2.setValidationVisible(true); hl2.addComponent(tf2); hl2.setComponentAlignment(tf2, Alignment.TOP_CENTER); Label unit2 = new Label(); unit2.setSizeUndefined(); unit2.setCaption(""); unit2.setValue("" + unit); hl2.addComponent(unit2); hl2.setComponentAlignment(unit2, Alignment.MIDDLE_LEFT); final VerticalLayout vl3 = new VerticalLayout(); vl3.setHeight("50px"); vl3.setWidth("100%"); vl3.addStyleName("greenBlock"); layout.addComponent(vl3); layout.setComponentAlignment(vl3, Alignment.TOP_CENTER); layout.setExpandRatio(vl3, 0.0f); final Label l3 = new Label(); l3.setSizeUndefined(); l3.addStyleName(ValoTheme.LABEL_LARGE); vl3.addComponent(l3); vl3.setComponentAlignment(l3, Alignment.MIDDLE_CENTER); applyValues(main, meter, combo, vl1, l1, vl2, l2, vl3, l3, tf1, tf2); combo.addValueChangeListener(new ValueChangeListener() { private static final long serialVersionUID = 8396168732300003038L; @Override public void valueChange(ValueChangeEvent event) { if (inApply) return; State currentState = getState(); if (currentState.equals(combo.getValue())) return; if (State.DECREASE3.equals(combo.getValue())) { makeThree = true; if (State.INCREASE3.equals(currentState)) { swap(); } else if (State.INCREASE2.equals(currentState)) { redLimit = greenLimit; greenLimit = null; } } else if (State.INCREASE3.equals(combo.getValue())) { makeThree = true; if (State.DECREASE3.equals(currentState)) { swap(); } else if (State.DECREASE2.equals(currentState)) { greenLimit = redLimit; redLimit = null; } } else if (State.INCREASE2.equals(combo.getValue())) { if (State.DECREASE3.equals(currentState)) { greenLimit = redLimit; } else if (State.DECREASE2.equals(currentState)) { greenLimit = redLimit; } redLimit = null; makeThree = false; } else if (State.DECREASE2.equals(combo.getValue())) { if (State.INCREASE3.equals(currentState)) { redLimit = greenLimit; } else if (State.INCREASE2.equals(currentState)) { redLimit = greenLimit; } greenLimit = null; makeThree = false; } updateMakeThree(); applyValues(main, meter, combo, vl1, l1, vl2, l2, vl3, l3, tf1, tf2); } }); tf1.addValueChangeListener(new ValueChangeListener() { private static final long serialVersionUID = -5484608577999300097L; @Override public void valueChange(ValueChangeEvent event) { if (inApply) return; if (State.DECREASE3.equals(combo.getValue())) { if (makeThree) greenLimit = redLimit; redLimit = new BigDecimal(tf1.getValue()); } else if (State.INCREASE3.equals(combo.getValue())) { if (makeThree) redLimit = greenLimit; greenLimit = new BigDecimal(tf1.getValue()); } else if (State.INCREASE2.equals(combo.getValue())) { greenLimit = new BigDecimal(tf1.getValue()); } else if (State.DECREASE2.equals(combo.getValue())) { redLimit = new BigDecimal(tf1.getValue()); } updateMakeThree(); applyValues(main, meter, combo, vl1, l1, vl2, l2, vl3, l3, tf1, tf2); } }); tf2.addValueChangeListener(new ValueChangeListener() { private static final long serialVersionUID = 5825320869230527588L; @Override public void valueChange(ValueChangeEvent event) { if (inApply) return; // This should not happen! if (State.INCREASE2.equals(combo.getValue()) || State.DECREASE2.equals(combo.getValue())) return; if (State.DECREASE3.equals(combo.getValue())) { greenLimit = new BigDecimal(tf2.getValue()); } else if (State.INCREASE3.equals(combo.getValue())) { redLimit = new BigDecimal(tf2.getValue()); } updateMakeThree(); applyValues(main, meter, combo, vl1, l1, vl2, l2, vl3, l3, tf1, tf2); } }); return null; }
From source file:fi.semantum.strategia.widget.NumberTrafficValuation.java
License:Open Source License
private void applyValues(Main main, Meter meter, ComboBox combo, VerticalLayout vl1, Label l1, VerticalLayout vl2, Label l2, VerticalLayout vl3, Label l3, TextField tf1, TextField tf2) { inApply = true;/*from w ww. j av a 2 s. c om*/ Indicator indicator = meter.getPossibleIndicator(main.getDatabase()); String unit = indicator.getUnit(); combo.select(getState()); final double highDouble = getHighLimit().doubleValue(); final double lowDouble = getLowLimit().doubleValue(); String highLimit = df.format(highDouble); String lowLimit = df.format(lowDouble); tf1.setValue(highLimit); l1.setValue(highLimit + " < arvo " + unit); if (isIncrease()) { vl1.setStyleName("greenBlock"); } else { vl1.setStyleName("redBlock"); } tf1.removeAllValidators(); tf1.addValidator(new Validator() { private static final long serialVersionUID = 5929569929930454714L; @Override public void validate(Object value) throws InvalidValueException { try { double d = Double.parseDouble((String) value); if (d < lowDouble) throw new InvalidValueException("Value is too small"); } catch (NumberFormatException e) { throw new InvalidValueException(e.getMessage()); } } }); if (isTwo()) { l3.setVisible(false); vl3.setVisible(false); tf2.setVisible(false); tf1.setCaption("Raja-arvo"); if (isIncrease()) { vl2.setStyleName("redBlock"); } else { vl2.setStyleName("greenBlock"); } l2.setValue("arvo < " + lowLimit + " " + unit); } else { vl3.setVisible(true); l3.setVisible(true); tf2.setVisible(true); tf1.setCaption("Suurempi raja-arvo"); tf2.setCaption("Pienempi raja-arvo"); vl2.setStyleName("yellowBlock"); l2.setValue(lowLimit + " < arvo < " + highLimit + " " + unit); l3.setValue("arvo < " + lowLimit + " " + unit); tf2.setValue(lowLimit); if (isIncrease()) { vl3.setStyleName("redBlock"); } else { vl3.setStyleName("greenBlock"); } tf2.removeAllValidators(); tf2.addValidator(new Validator() { private static final long serialVersionUID = 5929569929930454714L; @Override public void validate(Object value) throws InvalidValueException { try { double d = Double.parseDouble((String) value); if (d > highDouble) throw new InvalidValueException("Value is too large"); } catch (NumberFormatException e) { throw new InvalidValueException(e.getMessage()); } } }); } inApply = false; }
From source file:fi.semantum.strategia.widget.Property.java
License:Open Source License
public static void updateProperties(final Main main, final Base base, boolean canWrite) { final Database database = main.getDatabase(); String headerText = main.getUIState().currentItem.getCaption(database); main.propertyCells.add(Utils.excelRow(headerText)); Label header = new Label(headerText); header.setWidth("800px"); header.addStyleName("propertyHeader"); header.addStyleName(ValoTheme.LABEL_HUGE); header.addStyleName(ValoTheme.LABEL_BOLD); main.properties.addComponent(header); main.properties.setComponentAlignment(header, Alignment.MIDDLE_CENTER); ArrayList<Pair> sorted = new ArrayList<Pair>(main.getUIState().currentItem.properties); Collections.sort(sorted, new Comparator<Pair>() { @Override//from w ww .j a va 2 s . com public int compare(Pair arg0, Pair arg1) { final Property p0 = database.find(arg0.first); final Property p1 = database.find(arg1.first); return p0.getId(database).compareTo(p1.getId(database)); } }); Property typeProperty = Property.find(database, Property.TYPE); for (Pair pair : sorted) { // Skip type if (typeProperty.uuid.equals(pair.first)) continue; final Property p = database.find(pair.first); String value = pair.second; final HorizontalLayout hl = new HorizontalLayout(); hl.setSpacing(true); String label = p.getText(database); main.propertyCells.add(Utils.excelRow(label, value)); Label l = new Label(label); l.setWidth("450px"); l.addStyleName("propertyName"); hl.addComponent(l); List<String> enumeration = p.getEnumeration(database); if (enumeration.isEmpty()) { final TextField tf = new TextField(); tf.setValue(value); tf.setWidth("350px"); hl.addComponent(tf); hl.setComponentAlignment(tf, Alignment.MIDDLE_LEFT); tf.setReadOnly(p.readOnly); tf.addValueChangeListener(new ValueChangeListener() { private static final long serialVersionUID = 7729833503749464603L; @Override public void valueChange(ValueChangeEvent event) { Utils.loseFocus(hl); if (p.set(main, main.getUIState().currentItem, tf.getValue())) Updates.update(main, true); } }); tf.setReadOnly(!canWrite); } else { final ComboBox combo = new ComboBox(); combo.setWidth("350px"); combo.setInvalidAllowed(false); combo.setNullSelectionAllowed(false); for (String e : enumeration) { combo.addItem(e); } combo.select(p.getEnumerationValue(database, value)); combo.setPageLength(0); combo.addValueChangeListener(new ValueChangeListener() { private static final long serialVersionUID = 3511164709346832901L; @Override public void valueChange(ValueChangeEvent event) { Utils.loseFocus(hl); if (p.set(main, main.getUIState().currentItem, combo.getValue().toString())) Updates.update(main, true); } }); combo.setReadOnly(!canWrite); hl.addComponent(combo); hl.setComponentAlignment(combo, Alignment.MIDDLE_LEFT); } hl.setComponentAlignment(l, Alignment.MIDDLE_LEFT); main.properties.addComponent(hl); main.properties.setComponentAlignment(hl, Alignment.MIDDLE_CENTER); } }
From source file:fi.vtt.RVaadin.RContainer.java
License:Apache License
/** * For general description, see//from w w w . jav a2s .c om * {@link RContainer#getParameterLayout(String, String)}. * * @param caption * Parameter name * @param parName * The numeric vector name (of 1..N elements) in R * @param enabled * Is the field enabled by default? * @return orizontalLayout element with implicit ValueChangeListeners to * update R */ public HorizontalLayout getParameterLayout(String caption, final String parName, final boolean[] enabled) { /* The horizontal layout to place the parameters [p1, p2, p3,..., pN] */ final HorizontalLayout hl = new HorizontalLayout(); hl.setCaption(caption); hl.setSpacing(true); /* TextFields serve as the input field for the parameters */ final TextField[] pFields; /* Get the default values for the parameter vector */ double[] p = getDoubles(parName); /* * Show the parameter names in the TextField caption, in case that they * were defined in R */ boolean namedPars = false; String[] names = null; if (isTRUE("!is.null(names(" + parName + "))")) { namedPars = true; names = getStrings("names(" + parName + ")"); } if (p != null) { /* * Construct TextField(s) for the parameter vector and set the * initial values */ pFields = new TextField[p.length]; for (int i = 0; i < pFields.length; i++) { pFields[i] = new TextField(); pFields[i].setValue(Double.toString(p[i])); if (enabled != null) { pFields[i].setEnabled(enabled[i]); } /* * Nice default width for a numeric parameter field. This could * actually be a parameter for this call. */ pFields[i].setWidth("10ex"); pFields[i].setImmediate(true); /* * In case that the parameters are named, show the name before * the text field */ if (namedPars) { pFields[i].setCaption(names[i]); } hl.addComponent(pFields[i]); } /* * Set up a listener which can be attached to all the parameter * fields */ ValueChangeListener parChanged = new ValueChangeListener() { private static final long serialVersionUID = 1L; @Override public void valueChange(ValueChangeEvent event) { /* Get the Property which fired the listener */ TextField tf = ((TextField) event.getProperty()); /* Find the corresponding parameter index */ int idx; for (idx = 0; idx < pFields.length; idx++) { if (tf == pFields[idx]) { break; } } /* * Try to update the new value. If it is illegal (cannot be * cast to Double), then stick with the old value and * display it to the user to indicate erroneous input. */ double newValue; try { newValue = Double.valueOf(tf.getValue().toString()); /* * Submit the new value to R, remembering that the * indexing is off by one between Java and R */ eval(parName + "[" + (idx + 1) + "] <- " + newValue); } catch (Exception e) { /* * The parameter was illegal. Do nothing for R, and * display the old value to indicate that the new value * was not eligible. */ Double oldPar = getDouble(parName + "[" + (idx + 1) + "]"); tf.setValue(Double.toString(oldPar)); } } }; /* Attach this listener to all TextField items */ for (int i = 0; i < pFields.length; i++) { pFields[i].addValueChangeListener(parChanged); } } else { /* The original parameters were not readable as double[] */ hl.addComponent(new Label("Error reading the parameters")); hl.setEnabled(false); } return hl; }