Last active
December 5, 2016 15:19
-
-
Save jca02266/cd62e39eac4b82cf6b363fdb75d876dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2010 Google Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | |
* use this file except in compliance with the License. You may obtain a copy of | |
* the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
* License for the specific language governing permissions and limitations under | |
* the License. | |
*/ | |
package com.sample.webapp.client; | |
import com.google.gwt.cell.client.AbstractInputCell; | |
import com.google.gwt.cell.client.Cell; | |
import com.google.gwt.cell.client.ValueUpdater; | |
import com.google.gwt.core.client.GWT; | |
import com.google.gwt.dom.client.BrowserEvents; | |
import com.google.gwt.dom.client.Element; | |
import com.google.gwt.dom.client.NativeEvent; | |
import com.google.gwt.dom.client.SelectElement; | |
import com.google.gwt.safehtml.client.SafeHtmlTemplates; | |
import com.google.gwt.safehtml.shared.SafeHtml; | |
import com.google.gwt.safehtml.shared.SafeHtmlBuilder; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* A {@link Cell} used to render a drop-down list. | |
*/ | |
public class ListBoxCell extends AbstractInputCell<String, ListBoxCell.HasKey> { | |
public interface HasKey { | |
String getKey(); | |
String getText(); | |
} | |
interface Template extends SafeHtmlTemplates { | |
@Template("<option value=\"{0}\">{1}</option>") | |
SafeHtml deselected(String key, String value); | |
@Template("<option value=\"{0}\" selected=\"selected\">{1}</option>") | |
SafeHtml selected(String key, String value); | |
} | |
private static Template template; | |
public final Map<String, HasKey> options; | |
public void addOptions(HasKey value) { | |
options.put(value.getKey(), value); | |
} | |
/** | |
* Construct a new {@link ListBoxCell} with the specified options. | |
* | |
* @param options the options in the cell | |
*/ | |
public ListBoxCell(Map<String, HasKey> options) { | |
super(BrowserEvents.CHANGE); | |
if (template == null) { | |
template = GWT.create(Template.class); | |
} | |
this.options = options; | |
} | |
@Override | |
public void onBrowserEvent(Context context, Element parent, String value, | |
NativeEvent event, ValueUpdater<String> valueUpdater) { | |
super.onBrowserEvent(context, parent, value, event, valueUpdater); | |
String type = event.getType(); | |
if (BrowserEvents.CHANGE.equals(type)) { | |
Object key = context.getKey(); | |
SelectElement select = parent.getFirstChild().cast(); | |
HasKey newValue = options.get(select.getValue()); | |
setViewData(key, newValue); | |
finishEditing(parent, newValue.getText(), key, valueUpdater); | |
if (valueUpdater != null) { | |
valueUpdater.update(newValue.getKey()); | |
} | |
} | |
} | |
@Override | |
public void render(Context context, String value, SafeHtmlBuilder sb) { | |
// Get the view data. | |
Object key = context.getKey(); | |
HasKey viewData = getViewData(key); | |
if (viewData != null && viewData.equals(value)) { | |
clearViewData(key); | |
viewData = null; | |
} | |
String key2 = viewData == null ? value : viewData.getKey(); | |
sb.appendHtmlConstant("<select tabindex=\"-1\">"); | |
int index = 0; | |
for (Map.Entry<String, HasKey> entry : options.entrySet()) { | |
if (entry.getKey().equals(key2)) { | |
sb.append(template.selected(entry.getKey(), entry.getValue().getText())); | |
} else { | |
sb.append(template.deselected(entry.getKey(), entry.getValue().getText())); | |
} | |
} | |
sb.appendHtmlConstant("</select>"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment