Java tutorial
/* * 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 org.mklab.taskit.client.ui.cell; import java.util.List; import com.google.gwt.cell.client.AbstractInputCell; import com.google.gwt.cell.client.ValueUpdater; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.SelectElement; import com.google.gwt.safehtml.shared.SafeHtmlBuilder; import com.google.gwt.safehtml.shared.SafeHtmlUtils; /** * ?????? * <p> * ????????????????????????????? * * @author ishikura * @param <E> ? */ public class SelectCell<E> extends AbstractInputCell<E, E> { private List<E> options; private boolean editable = true; private Renderer<E> renderer; private Comparator<E> comparator; /** * {@link SelectCell}??? * * @param options ???? */ public SelectCell(List<E> options) { this(options, new Renderer<E>() { @Override public String render(@SuppressWarnings("unused") int index, E value) { return String.valueOf(value); } }); } /** * {@link SelectCell}??? * * @param options ???? * @param renderer ??????? */ public SelectCell(List<E> options, Renderer<E> renderer) { this(options, renderer, new Comparator<E>() { @Override public boolean equals(E e1, E e2) { if (e1 == null) { if (e2 == null) { return true; } return false; } return e1.equals(e2); } }); } /** * {@link SelectCell}??? * * @param options ???? * @param renderer ??????? * @param comparator ??? */ public SelectCell(List<E> options, Renderer<E> renderer, Comparator<E> comparator) { super("change"); //$NON-NLS-1$ if (options == null) throw new NullPointerException(); if (renderer == null) throw new NullPointerException(); if (comparator == null) throw new NullPointerException(); this.options = options; this.renderer = renderer; this.comparator = comparator; } /** * editable??? * * @param editable editable */ public void setEditable(boolean editable) { this.editable = editable; } /** * {@inheritDoc} */ @Override public void onBrowserEvent(com.google.gwt.cell.client.Cell.Context context, Element parent, E value, NativeEvent event, ValueUpdater<E> valueUpdater) { if (this.editable == false) return; if ("change".equals(event.getType()) == false) //$NON-NLS-1$ return; final SelectElement select = getSelectElement(parent); final int selectedIndex = select.getSelectedIndex(); if (selectedIndex < 0) return; final E selectedValue = this.options.get(selectedIndex); finishEditing(parent, value, context.getKey(), valueUpdater); if (valueUpdater != null) { valueUpdater.update(selectedValue); } } private static SelectElement getSelectElement(Element parent) { Node child = parent; do { if (child.getNodeName().toLowerCase().equals("select")) //$NON-NLS-1$ return child.cast(); } while ((child = child.getFirstChild()) != null); return null; } /** * {@inheritDoc} */ @SuppressWarnings("nls") @Override public void render(@SuppressWarnings("unused") com.google.gwt.cell.client.Cell.Context context, E value, SafeHtmlBuilder sb) { if (this.editable) { sb.appendHtmlConstant("<select>"); } else { int index = 0; for (E option : this.options) { final boolean selected = this.comparator.equals(option, value); if (selected) { final String escapedOption = SafeHtmlUtils.htmlEscape(this.renderer.render(index++, option)); sb.appendEscaped(escapedOption); } } return; } int index = 0; for (E option : this.options) { final boolean selected = this.comparator.equals(option, value); final String escapedOption = SafeHtmlUtils.htmlEscape(this.renderer.render(index++, option)); sb.appendHtmlConstant("<option value='" + escapedOption + "'" + (selected ? " selected" : "") + ">" + escapedOption + "</option>"); } sb.appendHtmlConstant("</select>"); } /** * ????????? * * @author ishikura * @param <E> ? */ public static interface Renderer<E> { /** * ???? * * @param index ? * @param value * @return */ String render(int index, E value); } /** * ???? * * @author ishikura * @param <E> ? */ public static interface Comparator<E> { /** * ??????????? * * @param e1 * @param e2 * @return ????????? */ boolean equals(E e1, E e2); } }