GWT 2.5 - 自定义EditTextCell - 不允许在TextCell中输入空格

时间:2013-09-26 05:32:39

标签: gwt gwt-2.5

我通过扩展EditTextCell创建了自定义单元格,我无法在EditTextCell中输入“SPACE”,如果我喜欢,则通过从文本单元格编辑中保留焦点来选择行单元格。

这是我的代码,

package com.skit.compiere.client.widgets.fields;

import static com.google.gwt.dom.client.BrowserEvents.BLUR;
import static com.google.gwt.dom.client.BrowserEvents.DBLCLICK;
import static com.google.gwt.dom.client.BrowserEvents.KEYDOWN;
import static com.google.gwt.dom.client.BrowserEvents.KEYUP;

import java.util.HashMap;
import java.util.Map;

import org.compiere.vos.FieldVO;

import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.client.SafeHtmlTemplates.Template;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.text.shared.SafeHtmlRenderer;
import com.google.gwt.text.shared.SimpleSafeHtmlRenderer;
import com.skit.compiere.client.panel.MultiViewPanel;

public class SkitMultiTextCell extends EditTextCell {
    Map<Object, SkitMultiTextCell.ViewData> viewDataMap = new HashMap<Object, SkitMultiTextCell.ViewData>();
    private SafeHtmlRenderer<String> renderer;
    public boolean editable = true;
    public String SelectedValue = null;
    ValueUpdater<String> supervalueup ;
    Element superparent ;
    NativeEvent superevent;
    ViewData viewdata;
    Context supercontext ;
    String supervalue ;
    Template template = GWT.create(Template.class);
    SafeHtmlBuilder superhtml = new SafeHtmlBuilder();
    Context ctx =null;
    String val;
    int i=-1;
    interface Template extends SafeHtmlTemplates {
        @Template("<input type=\"text\" value=\"{0}\"></input>")//tabindex=\"-1\"
        SafeHtml input(String value);
    }
    /**
     * Renders EditTextCell contents
     */
    public void render(com.google.gwt.cell.client.Cell.Context context,
            String value, SafeHtmlBuilder sb) {     
        super.render(context, value, sb);
    } 
    @Override
    public void onBrowserEvent(Context context, Element parent, String value,
            NativeEvent event, ValueUpdater<String> valueUpdater) {

        if (DBLCLICK.equals(event.getType())) {

            super.onBrowserEvent(supercontext, superparent, supervalue, superevent, supervalueup);  
        }
        else{
            this.supervalueup = valueUpdater;
            this.superparent = parent;
            this.superevent = event;
            this.supercontext = context;
            this.supervalue = value;
            viewdata = new ViewData(supercontext.getKey().toString());
            String type = event.getType();
            boolean keyUp = KEYUP.equals(type);
            boolean keyDown = KEYDOWN.equals(type);
            setviewData(supercontext.getKey(), viewdata);       

            if (keyUp || keyDown) {
                this.editEvent(supercontext, superparent, supervalue,viewdata, superevent, supervalueup);
            }

            else if (BLUR.equals(type)) {
                // Commit the change. Ensure that we are blurring the input element and
                // not the parent element itself.
                EventTarget eventTarget = event.getEventTarget();
                if (Element.is(eventTarget)) {
                    Element target = Element.as(eventTarget);
                    if ("input".equals(target.getTagName().toLowerCase())) {
                        commit(supercontext, superparent, viewdata, supervalueup);
                    }
                }
            }           
        }
    }
    public ViewData getviewData(Object key) {
        return (key == null) ? null : viewDataMap.get(key);
    }
    /**
     * Fires KeyPress Events
     * @param context
     * @param parent
     * @param value
     * @param viewData
     * @param event
     * @param valueUpdater
     */
    private void editEvent(Context context, Element parent, String value,
            ViewData viewData, NativeEvent event, ValueUpdater<String> valueUpdater) {
        String type = event.getType();
        System.out.println(type);
        boolean keyUp = KEYUP.equals(type);
        boolean keyDown = KEYDOWN.equals(type);
        //          int keyCodes = event.getKeyCode();
        //         if(event.getKeyCode()==32){
        //               event.preventDefault();
        //               return;
        //           }
        if (keyUp || keyDown) {
            int keyCode = event.getKeyCode();

            if ( keyCode == KeyCodes.KEY_ENTER ||keyCode == KeyCodes.KEY_ESCAPE) {
                // Commit the change.
                commit(context, parent, viewData, valueUpdater);
            } 

            else if (keyUp && keyCode == KeyCodes.KEY_ESCAPE) {
                // Cancel edit mode.
                String originalText = viewData.getOriginal();
                if (viewData.isEditingAgain()) {
                    viewData.setText(originalText);
                    viewData.setEditing(false);
                } else {
                    setviewData(context.getKey(), null);
                }
                cancel(context, parent, value);
            } else {
                // Update the text in the view data on each key.
                //                if(event.getKeyCode()==32){
                //                    viewdata.setText(viewdata.getText()+" ");
                //                    viewdata.setEditing(true);
                //                 }
                updateViewData(parent, viewData, true);
            }
        } 

    }
    @Override
    public boolean isEditing(Context context, Element parent, String value) {
        SafeHtmlBuilder safehtml = new SafeHtmlBuilder();
        return false;

    }
    private void cancel(Context context, Element parent, String value) {
        clearInput(getInputElement(parent));
        setValue(context, parent, value);
    }
    private native void clearInput(Element input) /*-{
    if (input.selectionEnd)
      input.selectionEnd = input.selectionStart;
    else if ($doc.selection)
      $doc.selection.clear();
  }-*/;

    private InputElement getInputElement(Element parent) {
        return parent.getFirstChild().<InputElement> cast();
    }

    private String updateViewData(Element parent, ViewData viewData,
            boolean isEditing) {
        InputElement input = (InputElement) parent.getFirstChild();
        String value = input.getValue();
        viewData.setText(value);
        viewData.setEditing(isEditing);
        return value;
    }

    private void commit(Context context, Element parent, ViewData viewData,
            ValueUpdater<String> valueUpdater) {
        String value = updateViewData(parent, viewData, false);
        clearInput(getInputElement(parent));
        setValue(context, parent, value);
        if (valueUpdater != null) {
            valueUpdater.update(value);
        }
    }
    public void setValue(Context context, Element parent, String value) {
        SafeHtmlBuilder sb = new SafeHtmlBuilder();
        renderFields(context, value, sb);
        parent.setInnerSafeHtml(sb.toSafeHtml());
    }
    private void renderFields(com.google.gwt.cell.client.Cell.Context context,
            String value, SafeHtmlBuilder sb) {
        this.renderer = SimpleSafeHtmlRenderer.getInstance();
        Object key = context.getKey();
        ViewData viewData = getviewData(key);
        if (viewData != null && !viewData.isEditing() && value != null
                && value.equals(viewData.getText())) {
            clearViewData(key);
            viewData = null;
        }

        String toRender = value;
        if (viewData != null) {
            String text = viewData.getText();
            if (viewData.isEditing()) {
                /*
                 * Do not use the renderer in edit mode because the value of a text
                 * input element is always treated as text. SafeHtml isn't valid in the
                 * context of the value attribute.
                 */
                 sb.append(template.input(value));
                 return;
            } else {
                // The user pressed enter, but view data still exists.
                toRender = text;
            }
        }

        if (toRender != null && toRender.trim().length() > 0) {
            sb.append(renderer.render(toRender));
        } else {
            /*
             * Render a blank space to force the rendered element to have a height.
             * Otherwise it is not clickable.
             */
            sb.appendHtmlConstant("\u00A0");
        }

    }
    public void setviewData(Object key, ViewData viewData) {
        if (key == null) {
            return;
        }

        if (viewData == null) {
            clearViewData(key);
        } else {
            viewDataMap.put(key, viewData);
        }
    }
    static class ViewData {

        private boolean isEditing;

        /**
         * If true, this is not the first edit.
         */
        private boolean isEditingAgain;

        /**
         * Keep track of the original value at the start of the edit, which might be
         * the edited value from the previous edit and NOT the actual value.
         */
        private String original;

        private String text;

        /**
         * Construct a new ViewData in editing mode.
         *
         * @param text the text to edit
         */
        public ViewData(String text) {
            this.original = text;
            this.text = text;
            this.isEditing = true;
            this.isEditingAgain = false;
        }

        @Override
        public boolean equals(Object o) {
            if (o == null) {
                return false;
            }
            ViewData vd = (ViewData) o;
            return equalsOrBothNull(original, vd.original)
                    && equalsOrBothNull(text, vd.text) && isEditing == vd.isEditing
                    && isEditingAgain == vd.isEditingAgain;
        }

        public String getOriginal() {
            return original;
        }

        public String getText() {
            return text;
        }

        @Override
        public int hashCode() {
            return original.hashCode() + text.hashCode()
                    + Boolean.valueOf(isEditing).hashCode() * 29
                    + Boolean.valueOf(isEditingAgain).hashCode();
        }

        public boolean isEditing() {
            return isEditing;
        }

        public boolean isEditingAgain() {
            return isEditingAgain;
        }

        public void setEditing(boolean isEditing) {
            boolean wasEditing = this.isEditing;
            this.isEditing = isEditing;

            // This is a subsequent edit, so start from where we left off.
            if (!wasEditing && isEditing) {
                isEditingAgain = true;
                original = text;
            }
        }

        public void setText(String text) {
            this.text = text;
        }

        private boolean equalsOrBothNull(Object o1, Object o2) {
            return (o1 == null) ? o2 == null : o1.equals(o2);
        }
    }
    public void setEditable(boolean flag)
    {
        editable = flag;
    }
    public void SetSelectedValue (String Value)
    {
        SelectedValue = Value;
    }
      String oldvalue = null;  static FieldVO m_fieldVO = null; 
      /**
       * Sets FieldVO, ListOptions
       * @param fieldvo
       * @param val
       * @param pinfo
       */
      public void SetFieldVO(FieldVO fieldvo) 
      {     
        m_fieldVO = fieldvo;
       }   

       public static  MultiViewPanel mv=null;
       /**
        * Sets MultiViewpanel value
        * @param mvp
        */
        public void setMVP(MultiViewPanel mvp)
        {
          this.mv = mvp;
        }
}

1 个答案:

答案 0 :(得分:1)

将此代码用于太空问题......

    if(keyCode == 32)
 {
            InputElement element = event.getEventTarget().cast();
            final int start = element.getPropertyInt("selectionStart");
            final int end = element.getPropertyInt("selectionEnd");
            if (start == end) {
                    String before = element.getValue();
                    String after = before.substring(0, start) + " "
                                    + before.substring(start);
                    element.setValue(after);
                    element.setPropertyInt("selectionStart", start + 1);
                    element.setPropertyInt("selectionEnd", start + 1); 
                }
    }

我希望这可以帮助你...