Votre question

Résolu [GWT] Tabulation dans une CellTable composée de TextInputCell

Tags :
  • Programmation
Dernière réponse : dans Programmation
22 Novembre 2012 17:30:23

Bonjour,
Pouvez-vous me dire comment implémenter la gestion des tabulations <tab> et <shift><tab> pour naviguer dans les cellules d'une CellTable composée de TextInputCell ?

Vous me seriez d'une grande aide.
Merci d'avance et bonne journée.

Autres pages sur : resolu gwt tabulation celltable composee textinputcell

23 Novembre 2012 12:55:30

Trouvé : il faut redéfinir le code de TextInputCell en remplaçant la ligne :
@Template("<input type=\"text\" value=\"{0}\" tabindex=\"-1\"></input>")
par celle-ci
@Template("<input type=\"text\" value=\"{0}\" tabindex=\"1\"></input>")
soit :
  1. package cnav.cotarr.client.ui.widget;
  2.  
  3. /*
  4.  * Copyright 2010 Google Inc.
  5.  *
  6.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7.  * use this file except in compliance with the License. You may obtain a copy of
  8.  * the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15.  * License for the specific language governing permissions and limitations under
  16.  * the License.
  17.  */
  18. //package com.google.gwt.cell.client;
  19.  
  20. import com.google.gwt.cell.client.AbstractInputCell;
  21. import com.google.gwt.cell.client.ValueUpdater;
  22. import com.google.gwt.core.client.GWT;
  23. import com.google.gwt.dom.client.Element;
  24. import com.google.gwt.dom.client.InputElement;
  25. import com.google.gwt.dom.client.NativeEvent;
  26. import com.google.gwt.safehtml.client.SafeHtmlTemplates;
  27. import com.google.gwt.safehtml.shared.SafeHtml;
  28. import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
  29. import com.google.gwt.text.shared.SafeHtmlRenderer;
  30.  
  31. /**
  32.  * An {@link AbstractCell} used to render a text input.
  33.  */
  34. public class TextInputCell2 extends
  35. AbstractInputCell<String, TextInputCell2.ViewData> {
  36.  
  37. interface Template extends SafeHtmlTemplates {
  38. @Template("<input type=\"text\" value=\"{0}\" tabindex=\"1\"></input>")
  39. SafeHtml input(String value);
  40. }
  41.  
  42. /**
  43.   * The {@code ViewData} for this cell.
  44.   */
  45. public static class ViewData {
  46. /**
  47.   * The last value that was updated.
  48.   */
  49. private String lastValue;
  50.  
  51. /**
  52.   * The current value.
  53.   */
  54. private String curValue;
  55.  
  56. /**
  57.   * Construct a ViewData instance containing a given value.
  58.   *
  59.   * @param value a String value
  60.   */
  61. public ViewData(String value) {
  62. this.lastValue = value;
  63. this.curValue = value;
  64. }
  65.  
  66. /**
  67.   * Return true if the last and current values of this ViewData object
  68.   * are equal to those of the other object.
  69.   */
  70. @Override
  71. public boolean equals(Object other) {
  72. if (!(other instanceof ViewData)) {
  73. return false;
  74. }
  75. ViewData vd = (ViewData) other;
  76. return equalsOrNull(lastValue, vd.lastValue)
  77. && equalsOrNull(curValue, vd.curValue);
  78. }
  79.  
  80. /**
  81.   * Return the current value of the input element.
  82.   *
  83.   * @return the current value String
  84.   * @see #setCurrentValue(String)
  85.   */
  86. public String getCurrentValue() {
  87. return curValue;
  88. }
  89.  
  90. /**
  91.   * Return the last value sent to the {@link ValueUpdater}.
  92.   *
  93.   * @return the last value String
  94.   * @see #setLastValue(String)
  95.   */
  96. public String getLastValue() {
  97. return lastValue;
  98. }
  99.  
  100. /**
  101.   * Return a hash code based on the last and current values.
  102.   */
  103. @Override
  104. public int hashCode() {
  105. return (lastValue + "_*!@HASH_SEPARATOR@!*_" + curValue).hashCode();
  106. }
  107.  
  108. /**
  109.   * Set the current value.
  110.   *
  111.   * @param curValue the current value
  112.   * @see #getCurrentValue()
  113.   */
  114. protected void setCurrentValue(String curValue) {
  115. this.curValue = curValue;
  116. }
  117.  
  118. /**
  119.   * Set the last value.
  120.   *
  121.   * @param lastValue the last value
  122.   * @see #getLastValue()
  123.   */
  124. protected void setLastValue(String lastValue) {
  125. this.lastValue = lastValue;
  126. }
  127.  
  128. private boolean equalsOrNull(Object a, Object b) {
  129. return (a != null) ? a.equals(b) : ((b == null) ? true : false);
  130. }
  131. }
  132.  
  133. private static Template template;
  134.  
  135. /**
  136.   * Constructs a TextInputCell that renders its text without HTML markup.
  137.   */
  138. public TextInputCell2() {
  139. super("change", "keyup");
  140. if (template == null) {
  141. template = GWT.create(Template.class);
  142. }
  143. }
  144.  
  145. /**
  146.   * Constructs a TextInputCell that renders its text using the given
  147.   * {@link SafeHtmlRenderer}.
  148.   *
  149.   * @param renderer parameter is ignored
  150.   * @deprecated the value of a text input is never treated as html
  151.   */
  152. @Deprecated
  153. public TextInputCell2(SafeHtmlRenderer<String> renderer) {
  154. this();
  155. }
  156.  
  157. @Override
  158. public void onBrowserEvent(Context context, Element parent, String value,
  159. NativeEvent event, ValueUpdater<String> valueUpdater) {
  160. super.onBrowserEvent(context, parent, value, event, valueUpdater);
  161.  
  162. // Ignore events that don't target the input.
  163. InputElement input = getInputElement(parent);
  164. Element target = event.getEventTarget().cast();
  165. if (!input.isOrHasChild(target)) {
  166. return;
  167. }
  168.  
  169. String eventType = event.getType();
  170. Object key = context.getKey();
  171. if ("change".equals(eventType)) {
  172. finishEditing(parent, value, key, valueUpdater);
  173. } else if ("keyup".equals(eventType)) {
  174. // Record keys as they are typed.
  175. ViewData vd = getViewData(key);
  176. if (vd == null) {
  177. vd = new ViewData(value);
  178. setViewData(key, vd);
  179. }
  180. vd.setCurrentValue(input.getValue());
  181. }
  182. }
  183.  
  184. @Override
  185. public void render(Context context, String value, SafeHtmlBuilder sb) {
  186. // Get the view data.
  187. Object key = context.getKey();
  188. ViewData viewData = getViewData(key);
  189. if (viewData != null && viewData.getCurrentValue().equals(value)) {
  190. clearViewData(key);
  191. viewData = null;
  192. }
  193.  
  194. String s = (viewData != null) ? viewData.getCurrentValue() : value;
  195. if (s != null) {
  196. sb.append(template.input(s));
  197. } else {
  198. sb.appendHtmlConstant("<input type=\"text\" tabindex=\"-1\"></input>");
  199. }
  200. }
  201.  
  202. @Override
  203. protected void finishEditing(Element parent, String value, Object key,
  204. ValueUpdater<String> valueUpdater) {
  205. String newValue = getInputElement(parent).getValue();
  206.  
  207. // Get the view data.
  208. ViewData vd = getViewData(key);
  209. if (vd == null) {
  210. vd = new ViewData(value);
  211. setViewData(key, vd);
  212. }
  213. vd.setCurrentValue(newValue);
  214.  
  215. // Fire the value updater if the value has changed.
  216. if (valueUpdater != null && !vd.getCurrentValue().equals(vd.getLastValue())) {
  217. vd.setLastValue(newValue);
  218. valueUpdater.update(newValue);
  219. }
  220.  
  221. // Blur the element.
  222. super.finishEditing(parent, newValue, key, valueUpdater);
  223. }
  224.  
  225. @Override
  226. protected InputElement getInputElement(Element parent) {
  227. return super.getInputElement(parent).<InputElement> cast();
  228. }
  229. }


et utiliser cette nouvelle classe dans la construction de la CellTable.

Vous pouvez clôturer ce topic; je ne sais pas comment le marquer "résolu".

Espérant que ceci servira à certains. Bonne journée.
Tom's guide dans le monde
  • Allemagne
  • Italie
  • Irlande
  • Royaume Uni
  • Etats Unis
Suivre Tom's Guide
Inscrivez-vous à la Newsletter
  • ajouter à twitter
  • ajouter à facebook
  • ajouter un flux RSS