1. #11
    Ext User bigmountainben's Avatar
    Join Date
    Apr 2009
    Posts
    46
    Vote Rating
    0
    bigmountainben is on a distinguished road

      0  

    Default


    Sure, that would work too:

    Code:
    enum MyEnum {
    
        Admin(Util.getMessages().adminDesc());
        User(Util.getMessages().userDesc());
    ...
    }
    etc.

    where
    Code:
    final class Util {
    
        static MyMessages messages;
    
        static {
           messages = GWT.create(MyMessages.class);
        }
    
        static public MyMessages getMessages() { return messages; }
    
    ...
    }
    Nice thing too is you can always user enum.name() and Enum.valueOf() as a way to go back and forth from a string rep (which is what I use for xml serialization, for example), and it continues to work with i18n.

  2. #12
    Sencha User
    Join Date
    Jun 2009
    Posts
    47
    Vote Rating
    0
    sape is on a distinguished road

      0  

    Question


    Quote Originally Posted by Colin Alworth View Post
    My quick and dirty solution would be to use a simple modeldata as sven suggested, then use a custom Converter class to transfer back and forth.

    Try this as a model object - this assumes that the toString() method of the enum has been overridden to display something useful (you aren't just displaying the PascalCased words on your otherwise pretty UI are you? Alternatively, you could make the constructor take an additional String param if you like...

    Code:
    public class EnumWrapper<E extends Enum<E>> extends BaseModelData {
        public EnumWrapper(E enumValue) {
            set("enum", enumValue);
            set("value", enumValue.toString());
        }
    }
    Then the Converter subclass looks something like this:

    Code:
    public class EnumConverter<E> extends Converter {
        public Object convertFieldValue(Object value) {
            return new EnumWrapper<E>((E)value);
        }
        public Object convertModelValue(Object value) {
            return ((EnumWrapper)value).get("enum");
        }
    }
    I think this is a good question, one which I will need answered for myself in the coming weeks, so if anyone has good/bad luck with this approach or finds another one, please share!



    FWIW, I am find a lot of cases where a Converter class is essential to get many faucets of GXT working well. They are ugly to have to patch in, as you only want a single instance of each one, and Java's syntax makes them rather verbose - at least 6 lines of code before you even start adding custom code to the thing... Anyone have better ideas on how some of this could be implemented?

    I am aware that it is a big deal to get the ComboBox working well, especially if you want it to contain complex data, but this whole idea of using a Map<String, Object> (i.e. ModelData) feels like you are trying to write Javascript components in Java. And while yes, it will all be compiled to Javascript in the end, the entire point of using Java is to stay far away from Javascript ...
    I tried this approach with the classes you provided but the EnumConverter doesn't even compile. Can you maybe provide me with a working example of the code and how to use it in practice?

  3. #13
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,732
    Vote Rating
    90
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      0  

    Default


    It would help to know what doesnt build... After I posted, I copied my own code into our project in case someone needed it later, and after brief visual inspection, I don't see any differences between my code that builds and the sample I posted...

    The only thing I see is the make the generic parameters the same in both classes, but that can also be achieved by making one the inner class of the other.

  4. #14
    Sencha User
    Join Date
    Jun 2009
    Posts
    47
    Vote Rating
    0
    sape is on a distinguished road

      0  

    Question


    Quote Originally Posted by Colin Alworth View Post
    It would help to know what doesnt build... After I posted, I copied my own code into our project in case someone needed it later, and after brief visual inspection, I don't see any differences between my code that builds and the sample I posted...

    The only thing I see is the make the generic parameters the same in both classes, but that can also be achieved by making one the inner class of the other.
    The compiles starts to complain about the generics on the line where you create a new EnumWrapper of type E which will complain about that it's not a compatible type.

    When I change the the E to
    Code:
    public class EnumConverter<E extends Enum<E>> extends Converter {
    
        public Object convertFieldValue(Object value) {
            return new EnumWrapper<E>((E) value);
        }
    
        public Object convertModelValue(Object value) {
            return ((EnumWrapper) value).get("enum");
        }
    }
    Than the code compiles but the convertion goes wrong. I guess I am doing something wrong.

    I created the EnumConverter like this:
    Code:
            final FieldBinding fieldBinding = new FieldBinding(tarrifType, "tarrifType");
            fieldBinding.setConvertor(new EnumConverter<TarrifType>());
            
            binding.addFieldBinding(fieldBinding);
    With TarrifType being the Java enum on my POJO.

    Used this code to populate the combobox (which doesn't work):
    Code:
            final TarrifType[] tarrifTypes = TarrifType.values();
            
            ListStore<ModelData> store = new ListStore<ModelData>();
            for (TarrifType type : tarrifTypes) {
                EnumWrapper<TarrifType> tarrifTypeWrapper = new EnumWrapper<TarrifType>(type);
                store.add(tarrifTypeWrapper);
            }
            
            tarrifType.setStore(store);
    So can you please point me out what I am doing wrong here?

    Thanks

  5. #15
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,732
    Vote Rating
    90
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      0  

    Default


    I am assuming that you are using a ComboBox for this then, probably ComboBox<EnumWrapper<TarrifType>>. Did you call ComboBox.setDisplayField("value")? or perhaps change the wrapper to call set("text", enumValue.toString())? ComboBox assumes that there is a property called text, or that you will set a custom template (or display field) to handle the model objects.

  6. #16
    Sencha User
    Join Date
    Jun 2009
    Posts
    47
    Vote Rating
    0
    sape is on a distinguished road

      0  

    Default


    Quote Originally Posted by Colin Alworth View Post
    I am assuming that you are using a ComboBox for this then, probably ComboBox<EnumWrapper<TarrifType>>. Did you call ComboBox.setDisplayField("value")? or perhaps change the wrapper to call set("text", enumValue.toString())? ComboBox assumes that there is a property called text, or that you will set a custom template (or display field) to handle the model objects.
    Ah ok thanks.. that was one of the problems but I also needed to change the code to look like this instead:
    Code:
    public class EnumConverter<E extends Enum<E>> extends Converter {
    
        @SuppressWarnings("unchecked")
        public Object convertFieldValue(Object value) {
            return ((EnumWrapper) value).get("enum");
        }
    
        @SuppressWarnings("unchecked")
        public Object convertModelValue(Object value) {
            return new EnumWrapper<E>((E) value);
        }
    }
    This because my Model has the enum TarrifType and not a EnumWrapper. The code you posted was the other way around.

    Thanks again

  7. #17
    Ext User
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    961
    Vote Rating
    1
    Arno.Nyhm is on a distinguished road

      0  

    Default


    i found also this idea from gslender:

    Quote Originally Posted by gslender View Post
    I use this...
    Code:
    public enum FreqType {
        DAILY("Daily"), WEEKLY("Weekly"), FORTNIGHTLY("Fortnightly"), MONTHLY("Monthly"), QUARTERLY("Quarterly"), YEARLY("Yearly");
    
        private String name;
    
        FreqType(String name) {
            this.name = name;
        }
    
        public String toString() {
            return name;
        }
    }
    and then I use this in SimpleCombo and POJO like...

    Code:
     freqTypeFld = new SimpleComboBox<FreqType>();
     freqTypeFld.setFieldLabel("Frequency");
     freqTypeFld.add(Arrays.asList(FreqType.values()));
     freqTypeFld.setEmptyText("Choose...");
     freqTypeFld.setEditable(false);
     freqTypeFld.setAllowBlank(false);
    
     ....
    
     freqTypeFld.setSimpleValue(mypojo.getFreq());
    see: http://www.extjs.com/forum/showthrea...382#post277382
    This forum needs your help: you got hints from the community and now you have fixed your code? dont just reply with "now its fixed" or "i found the error"! please take the time to post also an detailed answer with the working code.

    GreaseMonkey Script for a GXT-only Forum: it hides ExtJs here: New Posts • Search Results • Advanced Search form • Category overview http://www.extjs.com/forum/showthrea...041#post410041

  8. #18
    Ext User
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    961
    Vote Rating
    1
    Arno.Nyhm is on a distinguished road

      0  

    Default


    Quote Originally Posted by bigmountainben View Post
    You can also use the BeanModelMarker method:
    Code:
    @BeanModelMarker.BEAN(MyEnum.class)
    public class MyEnumModel implements BeanModelMarker { }
    And then you can use the BeanModel in your combo box. Add a getDescription to your Enum:
    Code:
    enum UserType {
    
       User("A User"),
       Admin("An Admin");
    
       String description;
    
       UserType(String desc) { this.description = desc; }
      
       private String getDescription() { return description; }
    }
    And set the combo box's displayField to be 'description'.

    comboBox.getValue().getBean() will get you back the Enum.

    Ben
    i like this suggestion, but how i use this enum with the combobox?


    it start some code but this not works:

    PHP Code:
    ComboBox cb = new ComboBox<UserType>(); 
    maybe i need to use the BeanModelReader? but i found only examples with the RpcProxy. but i dont need the enum via a proxy? or need i to write a enum->bean proxy?

    maybe someone have a hint for me how to solve this!?
    This forum needs your help: you got hints from the community and now you have fixed your code? dont just reply with "now its fixed" or "i found the error"! please take the time to post also an detailed answer with the working code.

    GreaseMonkey Script for a GXT-only Forum: it hides ExtJs here: New Posts • Search Results • Advanced Search form • Category overview http://www.extjs.com/forum/showthrea...041#post410041

  9. #19
    Ext User
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    961
    Vote Rating
    1
    Arno.Nyhm is on a distinguished road

      0  

    Default


    maybe the idea with the beanmodel is not working?

    i get this compile errors:

    PHP Code:
          Refreshing TypeOracle
             Processing types in compilation unit
    file:/C:/NetBeansProjects/GXTTest1/src/java/com/mycompany/myapplication/client/customer/test/UserTypeBeanModel.java
                Found type 
    'UserTypeBeanModel'
                   
    Resolving annotation '@BeanModelMarker.BEAN(value = UserType.class)'
                      
    [ERROR
    java.lang.ClassNotFoundExceptioncom.mycompany.myapplication.client.customer.test.UserType
            at java
    .net.URLClassLoader$1.run(URLClassLoader.java:200)
            
    at java.security.AccessController.doPrivileged(Native Method)
            
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
            
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
            
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
            
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
            
    at java.lang.Class.forName0(Native Method)
            
    at java.lang.Class.forName(Class.java:247)
            
    at com.google.gwt.dev.javac.TypeOracleMediator.getClassLiteral(TypeOracleMediator.java:763)
            
    at com.google.gwt.dev.javac.TypeOracleMediator.getAnnotationElementValue(TypeOracleMediator.java:674)
            
    at com.google.gwt.dev.javac.TypeOracleMediator.createAnnotationInstance(TypeOracleMediator.java:442)
            
    at com.google.gwt.dev.javac.TypeOracleMediator.resolveAnnotation(TypeOracleMediator.java:836)
            
    at com.google.gwt.dev.javac.TypeOracleMediator.resolveAnnotations(TypeOracleMediator.java:857)
            
    at com.google.gwt.dev.javac.TypeOracleMediator.resolveTypeDeclaration(TypeOracleMediator.java:1384)
            
    at com.google.gwt.dev.javac.TypeOracleMediator.addNewUnits(TypeOracleMediator.java:389)
            
    at com.google.gwt.dev.javac.TypeOracleMediator.refresh(TypeOracleMediator.java:417)
            
    at com.google.gwt.dev.javac.CompilationState.refresh(CompilationState.java:179)
            
    at com.google.gwt.dev.javac.CompilationState.<init>(CompilationState.java:93)
            
    at com.google.gwt.dev.cfg.ModuleDef.getCompilationState(ModuleDef.java:264)
            
    at com.google.gwt.dev.Precompile.precompile(Precompile.java:283)
            
    at com.google.gwt.dev.Compiler.run(Compiler.java:170)
            
    at com.google.gwt.dev.Compiler$1.run(Compiler.java:124)
            
    at com.google.gwt.dev.CompileTaskRunner.doRun(CompileTaskRunner.java:88)
            
    at com.google.gwt.dev.CompileTaskRunner.runWithAppropriateLogger(CompileTaskRunner.java:82)
            
    at com.google.gwt.dev.Compiler.main(Compiler.java:131)

    ...
       
    Scanning for additional dependenciesjar:file:/C:/NetBeansProjects/GXTTest1/lib/gwt/gxt-2.0.1/gxt.jar!/com/extjs/gxt/ui/client/data/BeanModelLookup.java
          Computing all possible rebind results 
    for 'com.extjs.gxt.ui.client.data.BeanModelLookup'
             
    Rebinding com.extjs.gxt.ui.client.data.BeanModelLookup
                Invoking 
    <generate-with class='com.extjs.gxt.ui.rebind.core.BeanModelGenerator'/>
                   [
    ERROR] Class com.extjs.gxt.ui.client.data.BeanModelLookup not found.
    java.lang.NullPointerException
            at com
    .extjs.gxt.ui.rebind.core.BeanModelGenerator.getMarkerBean(BeanModelGenerator.java:184)
            
    at com.extjs.gxt.ui.rebind.core.BeanModelGenerator.generate(BeanModelGenerator.java:54)
            
    at com.google.gwt.dev.cfg.RuleGenerateWith.realize(RuleGenerateWith.java:49)
            
    at com.google.gwt.dev.shell.StandardRebindOracle$Rebinder.tryRebind(StandardRebindOracle.java:113)
            
    at com.google.gwt.dev.shell.StandardRebindOracle$Rebinder.rebind(StandardRebindOracle.java:62)
            
    at com.google.gwt.dev.shell.StandardRebindOracle.rebind(StandardRebindOracle.java:172)
            
    at com.google.gwt.dev.shell.StandardRebindOracle.rebind(StandardRebindOracle.java:161)
            
    at com.google.gwt.dev.Precompile$DistillerRebindPermutationOracle.getAllPossibleRebindAnswers(Precompile.java:204)
            
    at com.google.gwt.dev.jdt.WebModeCompilerFrontEnd.doFindAdditionalTypesUsingRebinds(WebModeCompilerFrontEnd.java:128)
            
    at com.google.gwt.dev.jdt.AbstractCompiler$CompilerImpl.process(AbstractCompiler.java:151)
            
    at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:444)
            
    at com.google.gwt.dev.jdt.AbstractCompiler$CompilerImpl.compile(AbstractCompiler.java:85)
            
    at com.google.gwt.dev.jdt.AbstractCompiler$CompilerImpl.compile(AbstractCompiler.java:181)
            
    at com.google.gwt.dev.jdt.AbstractCompiler$CompilerImpl.access$400(AbstractCompiler.java:71)
            
    at com.google.gwt.dev.jdt.AbstractCompiler.compile(AbstractCompiler.java:473)
            
    at com.google.gwt.dev.jdt.WebModeCompilerFrontEnd.getCompilationUnitDeclarations(WebModeCompilerFrontEnd.java:73)
            
    at com.google.gwt.dev.jjs.JavaToJavaScriptCompiler.precompile(JavaToJavaScriptCompiler.java:259)
            
    at com.google.gwt.dev.Precompile.precompile(Precompile.java:300)
            
    at com.google.gwt.dev.Compiler.run(Compiler.java:170)
            
    at com.google.gwt.dev.Compiler$1.run(Compiler.java:124)
            
    at com.google.gwt.dev.CompileTaskRunner.doRun(CompileTaskRunner.java:88)
            
    at com.google.gwt.dev.CompileTaskRunner.runWithAppropriateLogger(CompileTaskRunner.java:82)
            
    at com.google.gwt.dev.Compiler.main(Compiler.java:131)
       [
    ERRORErrors in 'jar:file:/C:/NetBeansProjects/GXTTest1/lib/gwt/gxt-2.0.1/gxt.jar!/com/extjs/gxt/ui/client/data/BeanModelLookup.java'
          
    [ERRORLine 33:  Failed to resolve 'com.extjs.gxt.ui.client.data.BeanModelLookup' via deferred binding 
    This forum needs your help: you got hints from the community and now you have fixed your code? dont just reply with "now its fixed" or "i found the error"! please take the time to post also an detailed answer with the working code.

    GreaseMonkey Script for a GXT-only Forum: it hides ExtJs here: New Posts • Search Results • Advanced Search form • Category overview http://www.extjs.com/forum/showthrea...041#post410041

  10. #20
    Sencha User chalu's Avatar
    Join Date
    Feb 2008
    Location
    Benin City, Nigeria
    Posts
    480
    Vote Rating
    1
    chalu is on a distinguished road

      0  

    Default


    Hmmm.... all this just to load a Combo with values from a construct (Enum) that itemizes things. I think something is wrong somewhere, I really hope this gets simplified. Thanks fro your suggestions.