blob: 876246050ed87ed59206467015c59d293cbdb294 [file] [log] [blame]
/**
* Copyright (c) 2011, 2014 - Lunifera GmbH (Gross Enzersdorf)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.vaaclipse.addons.common.ui;
import org.eclipse.osbp.vaaclipse.addons.common.api.ResourceUtil;
import org.eclipse.osbp.vaaclipse.publicapi.theme.Theme;
import com.vaadin.server.Resource;
import com.vaadin.shared.ui.combobox.FilteringMode;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
/**
* The Class ThemeIconsComboBox.
*/
@SuppressWarnings("serial")
public class ThemeIconsComboBox extends CustomField<String> {
/** The theme. */
private Theme theme;
/** The cb. */
private ComboBox cb;
/**
* Instantiates a new theme icons combo box.
*
* @param caption
* the caption
* @param theme
* the theme
*/
public ThemeIconsComboBox(String caption, Theme theme) {
this.theme = theme;
setCaption(caption);
}
/* (non-Javadoc)
* @see com.vaadin.ui.CustomField#initContent()
*/
@SuppressWarnings("unchecked")
@Override
protected Component initContent() {
cb = new ComboBox();
cb.setSizeFull();
cb.setImmediate(true);
cb.setBuffered(false);
cb.setInvalidAllowed(true);
cb.setFilteringMode(FilteringMode.CONTAINS);
cb.addContainerProperty("icon", Resource.class, null);
cb.addContainerProperty("name", String.class, "");
cb.addContainerProperty("uri", String.class, "");
cb.setItemIconPropertyId("icon");
cb.setItemCaptionPropertyId("name");
for (String uri : theme.getImageURIs()) {
Object id = cb.addItem();
cb.getItem(id).getItemProperty("icon")
.setValue(ResourceUtil.getResource(uri));
cb.getItem(id).getItemProperty("name").setValue(extractName(uri));
cb.getItem(id).getItemProperty("uri").setValue(uri);
}
if (getInternalValue() != null) {
// set the selection to the combo box
setInternalValue(getInternalValue());
}
cb.addValueChangeListener(e -> setInternalSelection(cb.getValue()));
return cb;
}
/**
* Sets the internal selection.
*
* @param value
* the new internal selection
*/
private void setInternalSelection(Object value) {
if (value == null) {
return;
}
String internalSelected = (String) cb.getItem(value)
.getItemProperty("uri").getValue();
setConvertedValue(internalSelected);
}
/**
* Extract name.
*
* @param uri
* the uri
* @return the string
*/
private String extractName(String uri) {
String[] path = uri.split("/");
return path[path.length - 1];
}
/* (non-Javadoc)
* @see com.vaadin.ui.AbstractField#getType()
*/
@Override
public Class<String> getType() {
return String.class;
}
/* (non-Javadoc)
* @see com.vaadin.ui.AbstractField#setInternalValue(java.lang.Object)
*/
@Override
protected void setInternalValue(String newValue) {
super.setInternalValue(newValue);
if (cb != null) {
for (Object id : cb.getItemIds()) {
if (newValue.equals(cb.getItem(id).getItemProperty("uri").getValue())) {
cb.setValue(id);
}
}
}
}
}