Skip to content

Instantly share code, notes, and snippets.

@eiswind
Last active December 17, 2015 12:39
Show Gist options
  • Save eiswind/5611873 to your computer and use it in GitHub Desktop.
Save eiswind/5611873 to your computer and use it in GitHub Desktop.
rap java code for select2 combo replacement
/*******************************************************************************
******************************************************************************/
package de.eiswind.mango.widgets.select2;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.rap.json.JsonArray;
import org.eclipse.rap.json.JsonObject;
import org.eclipse.rap.json.JsonValue;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.client.service.JavaScriptLoader;
import org.eclipse.rap.rwt.lifecycle.WidgetUtil;
import org.eclipse.rap.rwt.remote.AbstractOperationHandler;
import org.eclipse.rap.rwt.remote.Connection;
import org.eclipse.rap.rwt.remote.OperationHandler;
import org.eclipse.rap.rwt.remote.RemoteObject;
import org.eclipse.rap.rwt.service.ResourceManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Listener;
public class Select2 extends Composite {
private static final long serialVersionUID = 1L;
private static final String RESOURCES_PATH = "resources/";
private static final String REGISTER_PATH = "select2/";
private static final String[] RESOURCE_FILES = { "jquery/jquery-1.9.1.min.js", "select2/select2.min.js",
"handler.js", "select2/select2.css", "select2/select2-spinner.gif", "select2/select2.png", "select2/select2x2.png" };
private static final String REMOTE_TYPE = "eiswind.Select2";
private final RemoteObject remoteObject;
private String[] selection;
private boolean multi = false;
private boolean allowClear;
private String placeHolder;
private List<Listener> selectionListeners = new ArrayList<>();
private final OperationHandler operationHandler = new AbstractOperationHandler() {
@Override
public void handleNotify(String event, JsonObject properties) {
JsonValue evtValue = properties.get("value");
if (evtValue != null) {
if (evtValue.isString()) {
Select2.this.selection = new String[]{ evtValue.asString()};
}
else if( evtValue.isArray()){
JsonArray array = evtValue.asArray();
String [] sel = new String[array.size()];
for(int i=0;i<array.size();i++){
JsonValue value = array.get(i);
sel[i] = value.asString();
}
Select2.this.selection = sel;
}
Event e = new Event();
e.type = SWT.Selection;
e.data=Select2.this.selection;
e.widget = Select2.this;
for(Listener l: selectionListeners){
l.handleEvent(e);
}
}
}
};
public Select2(Composite parent, int style) {
super(parent, style);
registerResources();
loadJavaScript();
Connection connection = RWT.getUISession().getConnection();
remoteObject = connection.createRemoteObject(REMOTE_TYPE);
remoteObject.setHandler(operationHandler);
System.out.println(WidgetUtil.getId(this));
remoteObject.set("parent", WidgetUtil.getId(this));
if ((style & SWT.MULTI) > 0) {
multi = true;
}
remoteObject.set("multi", multi);
remoteObject.listen("selection", true);
}
private void registerResources() {
ResourceManager resourceManager = RWT.getResourceManager();
boolean isRegistered = resourceManager.isRegistered(REGISTER_PATH + RESOURCE_FILES[0]);
if (!isRegistered) {
try {
for (String fileName : RESOURCE_FILES) {
register(resourceManager, fileName);
}
} catch (IOException ioe) {
throw new IllegalArgumentException("Failed to load resources", ioe);
}
}
}
private void loadJavaScript() {
JavaScriptLoader jsLoader = RWT.getClient().getService(JavaScriptLoader.class);
ResourceManager resourceManager = RWT.getResourceManager();
jsLoader.require(resourceManager.getLocation(REGISTER_PATH + "jquery/jquery-1.9.1.min.js"));
jsLoader.require(resourceManager.getLocation(REGISTER_PATH + "select2/select2.min.js"));
jsLoader.require(resourceManager.getLocation(REGISTER_PATH + "handler.js"));
}
private void register(ResourceManager resourceManager, String fileName) throws IOException {
ClassLoader classLoader = Select2.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(RESOURCES_PATH + fileName);
try {
resourceManager.register(REGISTER_PATH + fileName, inputStream);
} finally {
inputStream.close();
}
}
// //////////////////
// overwrite methods
@Override
public void setLayout(Layout layout) {
throw new UnsupportedOperationException("Cannot change internal layout of Select2");
}
@Override
public void dispose() {
super.dispose();
remoteObject.destroy();
selectionListeners.clear();
}
// ////
// API
public void setData(List<Select2Item> data) {
JsonArray results = new JsonArray();
for (Select2Item item : data) {
JsonObject it = new JsonObject();
it.add("id", item.getId());
it.add("text", item.getText());
it.add("shortText", item.getShortText());
it.add("locked", item.isLocked());
results.add(it);
}
JsonObject datObj = new JsonObject();
datObj.add("results", results);
remoteObject.set("data", datObj);
}
public String[] getSelection() {
if(selection ==null){
selection = new String[]{};
}
return selection;
}
public void setSelection(String selection) {
setSelection(new String[]{selection});
}
public void setSelection(String[] selection){
this.selection =selection;
JsonArray array = new JsonArray();
for(String s :selection){
array.add(s);
}
remoteObject.set("selection", array);
}
public boolean isMulti() {
return multi;
}
public boolean isAllowClear() {
return allowClear;
}
public void setAllowClear(boolean allowClear) {
if(allowClear && placeHolder==null ){
throw new IllegalStateException("Placeholder must be set for allowClear");
}
this.allowClear = allowClear;
remoteObject.set("allowClear", allowClear);
}
public String getPlaceHolder() {
return placeHolder;
}
public void setPlaceHolder(String placeHolder) {
this.placeHolder = placeHolder;
remoteObject.set("placeHolder", placeHolder);
}
@Override
public void addListener(int type,Listener l){
// TODO implement other events
if( type == SWT.Selection){
selectionListeners .add(l);
} else {
super.addListener(type, l);
}
}
@Override
public void removeListener(int type, Listener l){
if(type == SWT.Selection){
selectionListeners.remove(l);
} else {
super.removeListener(type, l);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment