Here are the examples of the java api com.vaadin.ui.GridLayout taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
1 Examples
18
Source : MapField.java
with Apache License 2.0
from ijazfx
with Apache License 2.0
from ijazfx
/**
* A field to edit simple map structures like string to Integer/Double/Float
* maps. The field is still EXPERIMENTAL, so the should be considered less
* stable than other Viritin API.
*
* @author Matti Tahvonen
* @param <K> The type of key in the edited map
* @param <V> The type of value in the edited map
*/
public clreplaced MapField<K, V> extends CustomField<Map> {
private static final Method ADDED_METHOD;
private static final Method REMOVED_METHOD;
static {
ADDED_METHOD = ReflectTools.findMethod(ElementAddedListener.clreplaced, "elementAdded", ElementAddedEvent.clreplaced);
REMOVED_METHOD = ReflectTools.findMethod(ElementRemovedListener.clreplaced, "elementRemoved", ElementRemovedEvent.clreplaced);
}
private GridLayout mainLayout = new GridLayout(3, 1);
private Clreplaced<K> keyType;
private Clreplaced<V> valueType;
private Clreplaced<?> editorType;
protected K newInstance;
private final FieldGroupListener fieldGroupListener = new FieldGroupListener() {
private static final long serialVersionUID = 1741634663680831911L;
@Override
public void onFieldGroupChange(MBeanFieldGroup beanFieldGroup) {
if (beanFieldGroup.gereplacedemDataSource().getBean() == newInstance) {
if (!getFieldGroupFor(newInstance).valueEditor.isValid()) {
return;
}
getAndEnsureValue().put(newInstance, null);
fireEvent(new ElementAddedEvent(MapField.this, newInstance));
setPersisted(newInstance, true);
onElementAdded();
}
// TODO could optimize for only repainting on changed validity
fireValueChange(false);
}
};
private boolean allowNewItems = true;
private boolean allowRemovingItems = true;
private boolean allowEdireplacedems = true;
private boolean showHeader = true;
private final Map<K, EntryEditor> pojoToEditor = new HashMap<>();
private EntryEditor newEntryEditor;
public MapField() {
}
public MapField(Clreplaced<K> elementType, Clreplaced<?> formType) {
this.keyType = elementType;
this.editorType = formType;
}
private void ensureInited() {
if (mainLayout.getComponentCount() == 0 && showHeader) {
mainLayout.addComponent(new Label("Key ->"));
mainLayout.addComponent(new Label("Value"));
mainLayout.addComponent(new Label("Delete entry"));
}
}
@Override
public Clreplaced<? extends Map> getType() {
return Map.clreplaced;
}
public MapField<K, V> addElementAddedListener(ElementAddedListener<K> listener) {
addListener(ElementAddedEvent.clreplaced, listener, ADDED_METHOD);
return this;
}
public MapField<K, V> removeElementAddedListener(ElementAddedListener listener) {
removeListener(ElementAddedEvent.clreplaced, listener, ADDED_METHOD);
return this;
}
public MapField<K, V> addElementRemovedListener(ElementRemovedListener<K> listener) {
addListener(ElementRemovedEvent.clreplaced, listener, REMOVED_METHOD);
return this;
}
public MapField<K, V> removeElementRemovedListener(ElementRemovedListener listener) {
removeListener(ElementRemovedEvent.clreplaced, listener, REMOVED_METHOD);
return this;
}
public boolean isAllowNewItems() {
return allowNewItems;
}
public boolean isAllowRemovingItems() {
return allowRemovingItems;
}
public boolean isAllowEdireplacedems() {
return allowEdireplacedems;
}
public boolean isShowHeader() {
return showHeader;
}
public MapField<K, V> setAllowNewItems(boolean allowNewItems) {
this.allowNewItems = allowNewItems;
return this;
}
public MapField<K, V> setAllowEdireplacedems(boolean allowEdireplacedems) {
this.allowEdireplacedems = allowEdireplacedems;
return this;
}
public MapField<K, V> setAllowRemovingItems(boolean allowRemovingItems) {
this.allowRemovingItems = allowRemovingItems;
return this;
}
public MapField<K, V> setShowHeader(boolean showHeader) {
this.showHeader = showHeader;
return this;
}
public MapField<K, V> withCaption(String caption) {
setCaption(caption);
return this;
}
@Override
public void validate() throws Validator.InvalidValueException {
super.validate();
Map<K, V> v = getValue();
if (v != null) {
for (K o : v.keySet()) {
// TODO, should validate both key and value
// for (Field f : getFieldGroupFor((K) o).getFields()) {
// f.validate();
// }
}
}
}
private Map<K, V> getAndEnsureValue() {
Map<K, V> value = getValue();
if (value == null) {
if (getPropertyDataSource() == null) {
// this should never happen :-)
return new HashMap();
}
Clreplaced fieldType = getPropertyDataSource().getType();
if (fieldType.isInterface()) {
value = new HashMap<>();
} else {
try {
value = (Map) fieldType.newInstance();
} catch (IllegalAccessException | InstantiationException ex) {
throw new RuntimeException("Could not instantiate the used colleciton type", ex);
}
}
getPropertyDataSource().setValue(value);
}
return value;
}
public MapField<K, V> setAllowNewElements(boolean allowNewItems) {
this.allowNewItems = allowNewItems;
return this;
}
public Clreplaced<K> getKeyType() {
return keyType;
}
protected TextField createKeyEditorInstance() {
MTextField tf = new MTextField().withInputPrompt("key");
return tf;
}
protected TextField createValueEditorInstance() {
MTextField tf = new MTextField().withInputPrompt("value");
return tf;
}
protected Button createDeleteButton() {
return new Button(FontAwesome.TRASH);
}
private void replaceValue(K key, String value) {
if (key == null) {
// new value without proper key, ignore
return;
}
K tKey;
try {
tKey = key;
} catch (ClreplacedCastException e) {
try {
tKey = keyType.getConstructor(String.clreplaced).newInstance(key);
} catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException | InvocationTargetException ex) {
throw new RuntimeException("No suitable constructor found", ex);
}
}
V tVal;
try {
tVal = (V) value;
} catch (ClreplacedCastException e) {
try {
tVal = valueType.getConstructor(String.clreplaced).newInstance(value);
} catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException | InvocationTargetException ex) {
throw new RuntimeException("No suitable constructor found", ex);
}
}
getAndEnsureValue().put(tKey, tVal);
}
private void renameValue(K oldKey, String key) {
K tKey;
try {
tKey = (K) key;
} catch (ClreplacedCastException e) {
try {
tKey = keyType.getConstructor(String.clreplaced).newInstance(key);
} catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException | InvocationTargetException ex) {
throw new RuntimeException("No suitable constructor found", ex);
}
}
EntryEditor e;
V value;
if (oldKey != null) {
value = getAndEnsureValue().remove(oldKey);
e = pojoToEditor.remove(oldKey);
} else {
e = newEntryEditor;
String strValue = newEntryEditor.valueEditor.getValue();
try {
value = (V) strValue;
} catch (ClreplacedCastException ex) {
try {
value = valueType.getConstructor(String.clreplaced).newInstance(strValue);
} catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException | InvocationTargetException ex1) {
value = null;
}
}
e.delete.setEnabled(true);
// Old editor is used for the new value, create a new one
createNewEntryRow();
}
e.oldKey = tKey;
getAndEnsureValue().put(tKey, value);
pojoToEditor.put(tKey, e);
}
private EntryEditor getFieldGroupFor(K key) {
EntryEditor ee = pojoToEditor.get(key);
if (ee == null) {
final TextField k = createKeyEditorInstance();
final TextField v = createValueEditorInstance();
// TODO fieldgroup listener
final EntryEditor ee1 = ee = new EntryEditor(k, v, key);
// TODO listen for all changes for proper modified/validity changes
pojoToEditor.put(key, ee);
}
return ee;
}
public void addElement(K key, V value) {
getAndEnsureValue().put(key, value);
addInternalElement(key, value);
fireValueChange(false);
fireEvent(new ElementAddedEvent<>(this, key));
}
public void removeElement(K keyToBeRemoved) {
removeInternalElement(keyToBeRemoved);
getAndEnsureValue().remove(keyToBeRemoved);
fireValueChange(false);
fireEvent(new ElementRemovedEvent<>(this, keyToBeRemoved));
}
@Override
protected Component initContent() {
return getLayout();
}
@Override
protected void setInternalValue(Map newValue) {
super.setInternalValue(newValue);
clearCurrentEditors();
// make sure that the header is always initialized
ensureInited();
Map<K, V> value = newValue;
if (value != null && !value.isEmpty()) {
for (Map.Entry<K, V> entry : value.entrySet()) {
K key = entry.getKey();
V value1 = entry.getValue();
addInternalElement(key, value1);
}
}
if (isAllowNewItems()) {
createNewEntryRow();
}
onElementAdded();
}
private void createNewEntryRow() throws ReadOnlyException {
TextField keyEditor = createKeyEditorInstance();
TextField valueEditor = createValueEditorInstance();
newEntryEditor = new EntryEditor(keyEditor, valueEditor, null);
addRowForEntry(newEntryEditor, null, null);
}
protected void addInternalElement(K k, V v) {
ensureInited();
EntryEditor fieldGroupFor = getFieldGroupFor(k);
addRowForEntry(fieldGroupFor, k, v);
}
private void addRowForEntry(EntryEditor editor, K k, V v) throws ReadOnlyException {
editor.bindValues(k, v);
getLayout().addComponents(editor.keyEditor, editor.valueEditor, editor.delete);
}
protected void setPersisted(K v, boolean persisted) {
// TODO create new "draft row"
}
protected void removeInternalElement(K v) {
// TODO remove from layout
}
protected Layout getLayout() {
return mainLayout;
}
protected void onElementAdded() {
System.out.println("What now!?");
}
private void clearCurrentEditors() {
// remove the row, until there are components left in the layout
// the getRows() method is unreliable, as it returns 1, even if the layout is empty
while (mainLayout.getComponentCount() > 1) {
mainLayout.removeRow(0);
}
}
public static clreplaced ElementAddedEvent<K> extends Component.Event {
private final K key;
public ElementAddedEvent(MapField source, K element) {
super(source);
this.key = element;
}
public K getKey() {
return key;
}
}
public static clreplaced ElementRemovedEvent<K> extends Component.Event {
private final K key;
public ElementRemovedEvent(MapField source, K element) {
super(source);
this.key = element;
}
public K getKey() {
return key;
}
}
public interface ElementAddedListener<K> extends Serializable {
void elementAdded(ElementAddedEvent<K> e);
}
public interface ElementRemovedListener<K> extends Serializable {
void elementRemoved(ElementRemovedEvent<K> e);
}
public interface Instantiator<ET> extends Serializable {
ET create();
}
private clreplaced EntryEditor implements Serializable {
private static final long serialVersionUID = 5710635901082609223L;
TextField keyEditor;
TextField valueEditor;
Button delete;
K oldKey;
EntryEditor(TextField ke, TextField valueEditor, K k) {
this.keyEditor = ke;
this.valueEditor = valueEditor;
delete = createDeleteButton();
delete.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(Button.ClickEvent event) {
Object removed = getValue().remove(oldKey);
pojoToEditor.remove(oldKey);
Iterator<Component> iterator = mainLayout.iterator();
int idx = 0;
while (iterator.next() != keyEditor) {
idx++;
}
mainLayout.removeRow(idx / 3);
}
});
this.oldKey = k;
if (oldKey == null) {
delete.setEnabled(false);
}
}
private void bindValues(K k, V v) {
keyEditor.setValue(k == null ? null : k.toString());
valueEditor.setValue(v == null ? null : v.toString());
keyEditor.addValueChangeListener(new Property.ValueChangeListener() {
@Override
public void valueChange(Property.ValueChangeEvent event) {
renameValue(EntryEditor.this.oldKey, EntryEditor.this.keyEditor.getValue());
}
});
valueEditor.addValueChangeListener(new Property.ValueChangeListener() {
@Override
public void valueChange(Property.ValueChangeEvent event) {
replaceValue(EntryEditor.this.oldKey, EntryEditor.this.valueEditor.getValue());
}
});
}
}
}