このエントリーをはてなブックマークに追加

Editorのサンプル2

リストデーターは、 所有だけしている。編集はcelltableで行っている。

@Path("colors") 
HasDataEditor<ColorData> editor;

cellに更新があると、そこからeditorをupdateしている

HasEditorDelegateを実装している。

それで、問題があるとエラーを返すようにしている。


private EditorDelegate<ColorDataList> delegate;
        @Override
        public void setDelegate(EditorDelegate<ColorDataList> delegate) {
            this.delegate=delegate;

        }
        @Override
        public void flush() {
            LogUtils.log("flush");
            if(nameEditor.getText().length()<=0){
                delegate.recordError("name must have value", nameEditor, "");
            }
            /*
             * validate here?
             */
        }

flush後にエラーを気にかけている

ColorDataList data=colorDataDriver.flush();

        /*
         * don't call before flush;
         */
        boolean erro=colorDataDriver.hasErrors();
        if(erro){
            for(EditorError err:colorDataDriver.getErrors()){
                LogUtils.log("err:"+err.getMessage());
            }

            updateLabel.setText("error");
            replace.setVisible(true);
            //updateListTable();
            //select(lastSelection,true);
            return;
        }else{
            replace.setVisible(false);
        }

ColorDataEditor.java

public class ColorDataEditor extends VerticalPanel implements Editor<ColorDataList>,HasEditorDelegate<ColorDataList>,ValueAwareEditor<ColorDataList>{
        TextBox nameEditor;
        @Path("colors") 
        HasDataEditor<ColorData> editor; 

        public ColorDataEditor(){
        nameEditor=new TextBox();
        add(nameEditor);
        nameEditor.addChangeHandler(new ChangeHandler() {

            @Override
            public void onChange(ChangeEvent event) {

                updateEdit();
            }
        });

        //have limit
        final CellTable<ColorData> table = new CellTable<ColorData>(100, (CellTable.Resources)GWT.create(TableResources.class),KEY_PROVIDER);

         final TextInputCell nameCell = new TextInputCell();

            Column<ColorData, String> nameColumn = new Column<ColorData, String>(nameCell) {
              @Override
              public String getValue(ColorData object) {
                return object.name;
              }
            };

            table.addColumn(nameColumn, "Name");

            nameColumn.setFieldUpdater(new FieldUpdater<ColorData, String>() {
                public void update(int index, ColorData object, String value) {
                  // Validate the data.
                  if (value.length() < 3) {
                    Window.alert("Names must be at least three characters long.");

                    /*
                     * Clear the view data. The view data contains the pending change and
                     * allows the table to render with the pending value until the data is
                     * committed. If the data is committed into the object, the view data
                     * is automatically cleared out. If the data is not committed because
                     * it is invalid, you must delete.
                     */
                    nameCell.clearViewData(KEY_PROVIDER.getKey(object));

                    // Redraw the table.
                    table.redraw();
                    return;
                  }

                  // Inform the user of the change.
                  //Window.alert("You changed the name of " + object.name + " to " + value);

                  // Push the changes into the Contact. At this point, you could send an
                  // asynchronous request to the server to update the database.
                  object.name = value;

                  // Redraw the table with the new data.
                  table.redraw();
                  updateEdit();
                }
              });








         final TextInputCell beforeCell = new TextInputCell();

            FieldUpdater<ColorData, String> beforeChecker=new FieldUpdater<ColorData, String>() {
                public void update(int index, ColorData object, String value) {
                  // Validate the data.
                  if (!value.startsWith("#")) {
                    Window.alert("color must be start#");


                    beforeCell.clearViewData(KEY_PROVIDER.getKey(object));


                    //GWT.log(data.getLastValue()););
                    //GWT.log(data.getLastValue());

                    table.redraw();
                    return;
                  }
                  object.before = value;
                  table.redraw();
                  updateEdit();
                }
              };

         Column<ColorData,String> beforeColumn = new Column<ColorData,String>(beforeCell) {
            @Override
            public String getValue(ColorData object) {
                return object.before;
            }
        };
        table.addColumn(beforeColumn, "Before");
        beforeColumn.setFieldUpdater(beforeChecker);

        final TextInputCell afterCell = new TextInputCell();


                FieldUpdater<ColorData, String> afterChecker=new FieldUpdater<ColorData, String>() {
                    public void update(int index, ColorData object, String value) {
                      // Validate the data.
                      if (!value.startsWith("#")) {
                        Window.alert("color must be start#");
                        afterCell.clearViewData(KEY_PROVIDER.getKey(object));
                        table.redraw();
                        return;
                      }
                      object.after = value;
                      table.redraw();
                      updateEdit();
                    }
                  };

        Column<ColorData,String> afterColumn = new Column<ColorData,String>(afterCell) {
            @Override
            public String getValue(ColorData object) {
                return object.after;
            }
        };
        table.addColumn(afterColumn, "After");
        afterColumn.setFieldUpdater(afterChecker);



         FieldUpdater<ColorData, String> simpleUpdater=new FieldUpdater<ColorData, String>() {
                public void update(int index, ColorData object, String value) {   
                  object.filter = value;
                  table.redraw();
                  updateEdit();
                }
              };

        final TextInputCell filterCell = new TextInputCell();
        Column<ColorData,String> filterColumn = new Column<ColorData,String>(filterCell) {
            @Override
            public String getValue(ColorData object) {
                return object.filter;
            }
        };
        table.addColumn(filterColumn, "Filter");
        filterColumn.setFieldUpdater(simpleUpdater);

         final SingleSelectionModel<ColorData> selectionModel = new SingleSelectionModel<ColorData>();
            table.setSelectionModel(selectionModel);
            selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
              public void onSelectionChange(SelectionChangeEvent event) {
                  ColorData selected = selectionModel.getSelectedObject();
                if (selected != null) {
                  Window.alert("You selected: " + selected.name+","+selected.before+","+selected.after+","+selected.filter);
                }
              }
            });

            table.setRowCount(DATAS.size(), true);

            // Push the data into the widget.
            table.setRowData(0, DATAS);

            // Add it to the root panel.
            add(table);

            editor = HasDataEditor.of(table);

            //table made smaller
            table.setWidth("320px", true);
            nameColumn.setCellStyleNames("short");
            beforeColumn.setCellStyleNames("short");
            afterColumn.setCellStyleNames("short");
            filterColumn.setCellStyleNames("short");
        }

        private EditorDelegate<ColorDataList> delegate;
        @Override
        public void setDelegate(EditorDelegate<ColorDataList> delegate) {
            this.delegate=delegate;

        }
        @Override
        public void flush() {
            LogUtils.log("flush");
            if(nameEditor.getText().length()<=0){
                delegate.recordError("name must have value", nameEditor, "");
            }
            /*
             * validate here?
             */
        }
        @Override
        public void onPropertyChange(String... paths) {
            // TODO Auto-generated method stub

        }
        @Override
        public void setValue(ColorDataList value) {
            LogUtils.log("value");
            beforeData=value.cloneTo();
        }

        private ColorDataList beforeData;

        public ColorDataList getBeforeData() {
            return beforeData;
        }
    }

単純に値を保持するだけのEditor

ClipDataEditor

public static class ClipDataEditor extends VerticalPanel implements LeafValueEditor<ClipData>,ClipDataHolder{
        private ClipData clipData;
        private Image image;
        public ClipDataEditor(){
            image = new Image();
            image.setVisible(false);
            add(image);

            Button edit=new Button("reedit clip position",new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    ImageClip.get().editClipData(ClipDataEditor.this,clipData);
                }
            });
            add(edit);
        };
        @Override
        public void setValue(ClipData value) {
            this.clipData=value;
            if(value!=null && value.getClipDataUrl()!=null){
                image.setVisible(true);
                image.setUrl(value.getClipDataUrl());
            }else{
                image.setVisible(false);
            }
        }

        @Override
        public ClipData getValue() {
            // TODO Auto-generated method stub
            return clipData;
        }
        @Override
        public void updateData() {
            ImageClip.get().selectPhraseTab();
            image.setUrl(clipData.getClipDataUrl());
        }

    }