Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    SimpleTypes and CSS styling

    Hi,

    What is the recommended approach to specify the CSS style to be used when displaying a given SimpleType?

    The SimpleType API supports specifying formatters, but as far as I understand, they are intended for formatting the content, and specifying the style. (Although technically I could generate HTML code here, which could reference and CSS style, but then I must manually implement all rendering, even if I only wanted to change some CSS properties.)

    Or I could specify getCellStyle overrides on the compoments using the type, but then I have to do this manually everywhere the SimpleType is used, which is exactly what the SimpleType system is intended to avoid..

    What is the right solution here?

    Thank you for explaining:

    Csillag

    #2
    Typically you'd create a FormItem subclass intended for editing or viewing the SimpleType and apply the css to that (and connect it with the SimpleType via setEditorType())

    Comment


      #3
      I tried to create a subclass of StaticTextItem to use as default viewer, and connected it to the type with setReadOnlyEditorType.

      On the subclass I tried to use setTextBoxStyle() to configure the CSS, but that did not work. I also tried overriding getTextBoxStyle(), but that did not work either.

      So, how do I configure the CSS on a StaticTextItem subclass that is to be used as a viewer for a SimpleType?

      Thank you for explaining:

      Csillag

      Comment


        #4
        Use "setInitHandler(...)" to add a handler that fires on init and calls 'setTextBoxStyle' on the item at init time.

        Comment


          #5
          The configured editor's configured initHandler does not seem to fire.
          The simple type itself is registered and invoked as expected, because in the ListGrid, I can see the values formatted by the formatter ... but the InitHandler's debug message does not appear.

          What am I missing? Here is the code:

          Code:
          public class PriorityType extends SimpleType {
          	
          public final static String NAME = "priority";
          	
          SimpleTypeFormatter formatter = new SimpleTypeFormatter() { @Override public String format(Object valueObject, DataClass field, DataBoundComponent component, Record record) {
            boolean value = Boolean.parseBoolean((String)valueObject);
            return value ? "urgent" : "usual";
          }};
          	
          FormItem editor = new StaticTextItem() {{
            setInitHandler(new FormItemInitHandler() { 
              @Override public void onInit(FormItem item) {
                SC.logWarn("Called priority cell's formatter's init handler");
                item.setTextBoxStyle("priorityCell");
              }
            });
          }};
          
          public PriorityType() {
            super(NAME, FieldType.TEXT);
            setNormalDisplayFormatter(formatter);
            setShortDisplayFormatter(formatter);
            setEditFormatter(formatter);
            setReadOnlyEditorType(editor);
            setEditorType(editor);
          }
          
          }
          Thank you for explaining:

          Csillag

          ps. This is EE-Eval from 2011-10-24

          Comment


            #6
            This appears to be working for us.
            Complete test case below.
            If you continue to see it in your application / usage, please post a complete runnable test case so we can reproduce what you're seeing.

            Thanks

            Code:
            public class SimpleTypeEditorIssue implements EntryPoint {
                
                public class PriorityType extends SimpleType {
                    
                    public final static String NAME = "priority";
                        
                    SimpleTypeFormatter formatter = new SimpleTypeFormatter() { 
            
                        @Override
                        public String format(Object valueObject, DataClass field,
                                DataBoundComponent component, Record record) {
                            boolean value = Boolean.parseBoolean((String)valueObject);
                            return value ? "urgent" : "usual";
                        }
                    };
                        
                    FormItem editor = new StaticTextItem() {{
                      setInitHandler(new FormItemInitHandler() { 
                        @Override public void onInit(FormItem item) {
                          SC.logWarn("Called priority cell's formatter's init handler");
                          // Use a style from the skin so its very obvious
                          item.setTextBoxStyle("cellSelectedOver");
                        }
                      });
                    }};
            
                    public PriorityType() {
                      super(NAME, FieldType.TEXT);
                      setNormalDisplayFormatter(formatter);
                      setShortDisplayFormatter(formatter);
                      setEditFormatter(formatter);
                      setReadOnlyEditorType(editor);
                      setEditorType(editor);
                    }
            
                }
            
                @Override
                public void onModuleLoad() {
                    
                    DataSource PriorityDS = new DataSource();
                    DataSourceField PriorityField = new DataSourceField();
                    PriorityField.setType(new PriorityType());
                    PriorityField.setName("x");
                    PriorityDS.setFields(PriorityField);
                    
                    DynamicForm f = new DynamicForm();
                    f.setDataSource(PriorityDS);
                    
                    f.draw();
                    
                }
            
            }

            Comment

            Working...
            X