Hybrid View

  1. #1
    Ext User
    Join Date
    Jun 2008
    Posts
    3
    Vote Rating
    0
    zhoujianghao is on a distinguished road

      0  

    Default How to clone a Field?

    How to clone a Field?


    Hi,
    I have a method with parameter abstract Field , i need to clone a new one and return it.
    just like:

    public Field cloneField(Field input)
    {
    return GWT.create(input.getClass());
    }
    It works fine in hostmode,but compile error in ant building with message:
    "Only class literals may be used as arguments to GWT.create()".
    Is that means abstract class or interface can't use in create method?
    Thanks.

  2. #2
    Ext GWT Premium Member gslender's Avatar
    Join Date
    Mar 2008
    Location
    Brisbane, Australia
    Posts
    1,572
    Vote Rating
    4
    gslender is on a distinguished road

      0  

    Default


    No, it requires... Field.class - but in saying that, as its abstract there is not really a way to use it.. you should be creating sub classes of Field.

    Why do you want to clone the field? Perhaps there is another simpler solution.

  3. #3
    Ext User
    Join Date
    Jun 2008
    Posts
    3
    Vote Rating
    0
    zhoujianghao is on a distinguished road

      0  

    Default


    Actually, i want to build a MultiField's subclass which can add a Field repeatly just like add attachments in email, and the field doesn't need to know what exactly it is and can be added repeatly.

    Thanks.

  4. #4
    Ext GWT Premium Member gslender's Avatar
    Join Date
    Mar 2008
    Location
    Brisbane, Australia
    Posts
    1,572
    Vote Rating
    4
    gslender is on a distinguished road

      0  

    Default


    ...still can't say I fully understand what you are doing - if you provide a small example/test case then I'd be happy to help you figure out another way to do what you need.

  5. #5
    Ext User
    Join Date
    Jun 2008
    Posts
    3
    Vote Rating
    0
    zhoujianghao is on a distinguished road

      0  

    Default


    Code:
    public class MyRepeatFieldGroup extends MultiField<Field> {
        
        private LayoutContainer lc = new LayoutContainer(new FormLayout());
        
        public BTRepeatEditorFieldGroup(final Field field) {
            setOrientation(Orientation.VERTICAL);
    
            lc.setLayoutOnChange(true);
            lc.add(new MyRepeatField(cloneField(field)));
            
            final LayoutContainer lc_btn = new LayoutContainer();
            final Button btn = new Button("Add");
            btn.setIconStyle("bt-icon-add");
            btn.addSelectionListener(new SelectionListener<ButtonEvent>(){
                public void componentSelected(ButtonEvent ce) {
                    lc.add(new BTRepeatEditorField(cloneField(field)));
                }});
            lc_btn.add(btn);
            add(new AdapterField(lc));
            add(new AdapterField(lc_btn));
        }
    }
    Code:
    public class MyRepeatField extends MultiField<Field> {
    
        private Field field;
        
        public MyRepeatField(Field field) {
            this.field = field;
            setHideLabel(true);
            field.setHideLabel(true);
            setOrientation(Orientation.HORIZONTAL);
            add(field);
            LabelField label = new LabelField("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
            label.setHideLabel(true);
            add(label);
            final Button btn = new Button("Delete");
            btn.setIconStyle("bt-icon-cancel");
            btn.addSelectionListener(new SelectionListener<ButtonEvent>(){
                public void componentSelected(ButtonEvent ce) {
                    MyRepeatField.this.removeFromParent();
                }});
            add(new AdapterField(btn));
        }
        
        public Object getRepeatValue()
        {
            return field.getValue();
        }
        
        public void setRepeateValue(Object obj)
        {
            field.setValue(obj);
        }
    }
    In the class MyRepeatFieldGroup's cloneField method i want to clone another one,and display repeatly.

  6. #6
    Ext GWT Premium Member gslender's Avatar
    Join Date
    Mar 2008
    Location
    Brisbane, Australia
    Posts
    1,572
    Vote Rating
    4
    gslender is on a distinguished road

      0  

    Default


    zhoujianghao,

    Can you provide a section of code that uses the above snippet - start with onModuleLoad and go from there... I'm happy to help, but you need to provide complete code examples that clearly show the problem you are having.

    Also, can you provide some background on what you are trying to do - what is the goal behind trying to create new fields where you don't know what kind of field it is ??

Thread Participants: 2