Search in sources :

Example 16 with IStyleSheet

use of com.servoy.j2db.util.IStyleSheet in project servoy-client by Servoy.

the class TemplateGenerator method applyBaseComponentProperties.

protected static BorderAndPadding applyBaseComponentProperties(BaseComponent component, Form form, TextualStyle styleObj, Insets defaultPadding, Insets defaultBorder, IServiceProvider sp) {
    Pair<IStyleSheet, IStyleRule> styleInfo = ComponentFactory.getStyleForBasicComponent(sp, component, form);
    Insets insetsBorder = null;
    Border designBorder = null;
    if (component.getBorderType() != null) {
        insetsBorder = ComponentFactoryHelper.createBorderCSSProperties(component.getBorderType(), styleObj);
        designBorder = ComponentFactoryHelper.createBorder(component.getBorderType());
    }
    Insets insetsMargin = null;
    if (component instanceof ISupportTextSetup) {
        insetsMargin = ((ISupportTextSetup) component).getMargin();
    }
    if (styleInfo != null) {
        IStyleSheet ss = styleInfo.getLeft();
        IStyleRule s = styleInfo.getRight();
        if (ss != null && s != null) {
            List<String> attributeNames = s.getAttributeNames();
            for (String s_attr : attributeNames) {
                // Skip margin related attributes. Margin is computed separately below, and rendered as padding.
                if (s_attr.toLowerCase().contains("margin"))
                    continue;
                // do not add any border attributes if set on component
                if (s_attr.toLowerCase().contains("border") && component.getBorderType() != null)
                    continue;
                String val = s.getValue(s_attr);
                if (s_attr.equals("font-size")) {
                    String tmp = val;
                    if (tmp.endsWith("px")) {
                        int size = Utils.getAsInteger(tmp.substring(0, tmp.length() - 2));
                        // 9 should be defined hard. Because 12 (1.33*9) is to big.
                        if (size == 9) {
                            size = 11;
                        } else {
                            size = (int) (size * (4 / (double) 3));
                        }
                        styleObj.setProperty(s_attr, size + "px");
                    } else {
                        int size = 0;
                        if (tmp.endsWith("pt")) {
                            size = Utils.getAsInteger(tmp.substring(0, tmp.length() - 2));
                        } else {
                            size = Utils.getAsInteger(tmp);
                        }
                        // 9 should be defined hard. Because 6 (0.75*9) is to small.
                        if (size == 9) {
                            size = 7;
                        } else {
                            size = (int) (size * (3 / (double) 4));
                        }
                        styleObj.setProperty(s_attr, size + "pt");
                    }
                } else {
                    if (val.toString() != null)
                        styleObj.setProperty(s_attr, s.getValues(s_attr), false);
                }
            }
            if (component.getBorderType() == null) {
                if (ss.hasBorder(s)) {
                    Border b = ss.getBorder(s);
                    if (b != null) {
                        try {
                            insetsBorder = ComponentFactoryHelper.getBorderInsetsForNoComponent(b);
                        } catch (Exception e) {
                            Debug.error("for border " + b + " no insets could be extracted.", e);
                        }
                        TextualStyle borderStyle = new TextualStyle();
                        ComponentFactoryHelper.createBorderCSSProperties(ComponentFactoryHelper.createBorderString(b), borderStyle);
                        Enumeration<Object> cssAttributes = borderStyle.keys();
                        while (cssAttributes.hasMoreElements()) {
                            String att = (String) cssAttributes.nextElement();
                            // put the default values, if not all specified in css
                            styleObj.setProperty(att, borderStyle.getProperty(att), false);
                        }
                    }
                }
            }
            if (insetsMargin == null) {
                if (ss.hasMargin(s)) {
                    insetsMargin = ss.getMargin(s);
                }
            }
        }
    }
    if (component.getFontType() != null) {
        Pair<String, String>[] props = PersistHelper.createFontCSSProperties(component.getFontType());
        if (props != null) {
            for (Pair<String, String> element : props) {
                if (element == null)
                    continue;
                styleObj.setProperty(element.getLeft(), element.getRight());
            }
        }
    }
    if (component.getForeground() != null) {
        styleObj.setProperty("color", PersistHelper.createColorString(component.getForeground()));
    }
    if (component.getTransparent()) {
        styleObj.setProperty("background-color", IStyleSheet.COLOR_TRANSPARENT);
    } else if (component.getBackground() != null) {
        styleObj.setProperty("background-color", PersistHelper.createColorString(component.getBackground()));
    }
    if (insetsBorder == null)
        insetsBorder = defaultBorder;
    if (insetsMargin == null && defaultPadding != null)
        insetsMargin = defaultPadding;
    BorderAndPadding bp = new BorderAndPadding(insetsBorder, insetsMargin);
    styleObj.setProperty("padding", (designBorder instanceof EmptyBorder && !(designBorder instanceof MatteBorder)) ? createInsetsText(bp.getSum()) : createInsetsText(bp.getPadding()));
    return bp;
}
Also used : IStyleSheet(com.servoy.j2db.util.IStyleSheet) Insets(java.awt.Insets) ISupportTextSetup(com.servoy.j2db.persistence.ISupportTextSetup) Point(java.awt.Point) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) MatteBorder(javax.swing.border.MatteBorder) IStyleRule(com.servoy.j2db.util.IStyleRule) EmptyBorder(javax.swing.border.EmptyBorder) MatteBorder(javax.swing.border.MatteBorder) Border(javax.swing.border.Border) TitledBorder(javax.swing.border.TitledBorder) EmptyBorder(javax.swing.border.EmptyBorder) Pair(com.servoy.j2db.util.Pair)

Example 17 with IStyleSheet

use of com.servoy.j2db.util.IStyleSheet in project servoy-client by Servoy.

the class AbstractFormLayoutProvider method fillPartStyle.

public void fillPartStyle(TextualStyle partStyle, Part part) {
    Pair<IStyleSheet, IStyleRule> pairStyle = ComponentFactory.getStyleForBasicComponent(sp, part, f);
    if (pairStyle != null) {
        addBackgroundImageAttributeIfExists(pairStyle.getRight(), partStyle);
    }
    fillPartBackground(partStyle, part);
    if (part.getPartType() == Part.BODY) {
        // $NON-NLS-1$
        partStyle.setProperty("overflow-x", getCSSScrolling(f.getScrollbars(), true));
        // $NON-NLS-1$
        partStyle.setProperty("overflow-y", getCSSScrolling(f.getScrollbars(), false));
    } else {
        // $NON-NLS-1$ //$NON-NLS-2$
        partStyle.setProperty("overflow", "hidden");
    }
    int spaceUsedOnlyInPrintAbove = 0;
    int spaceUsedOnlyInPrintBelow = 0;
    Iterator<Part> allParts = f.getParts();
    while (allParts.hasNext()) {
        Part otherPart = allParts.next();
        if (Part.rendersOnlyInPrint(otherPart.getPartType())) {
            int otherPartHeight = otherPart.getHeight() - f.getPartStartYPos(otherPart.getID());
            if (part.getPartType() > otherPart.getPartType())
                spaceUsedOnlyInPrintAbove += otherPartHeight;
            if (part.getPartType() < otherPart.getPartType())
                spaceUsedOnlyInPrintBelow += otherPartHeight;
        }
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    partStyle.setProperty("position", "absolute");
    fillPartLayoutCSS(partStyle, part, spaceUsedOnlyInPrintAbove, spaceUsedOnlyInPrintBelow);
    if (pairStyle != null && pairStyle.getLeft() != null && pairStyle.getRight() != null) {
        copyBorderAttributes(pairStyle.getRight(), partStyle);
    }
}
Also used : IStyleSheet(com.servoy.j2db.util.IStyleSheet) Part(com.servoy.j2db.persistence.Part) IStyleRule(com.servoy.j2db.util.IStyleRule)

Example 18 with IStyleSheet

use of com.servoy.j2db.util.IStyleSheet in project servoy-client by Servoy.

the class ComponentFactory method getStyleForBasicComponentInternal.

private static Pair<IStyleSheet, IStyleRule> getStyleForBasicComponentInternal(IServiceProvider sp, AbstractBase bc, Form form, Set<Integer> visited) {
    if (bc == null || sp == null)
        return null;
    // Protection agains cycle in form inheritance hierarchy.
    if (!visited.add(new Integer(form.getID())))
        return null;
    Style repos_style = getStyleForForm(sp, form);
    Pair<IStyleSheet, IStyleRule> pair = null;
    if (repos_style != null) {
        IStyleSheet ss = getCSSStyle(sp, repos_style);
        String lookupName = getLookupName(bc);
        if (lookupName != null) {
            String formLookup = "";
            ISupportChilds parent = bc.getParent();
            if (parent instanceof Form) {
                String styleClass = ((Form) parent).getStyleClass();
                if (styleClass != null && styleClass.length() != 0) {
                    formLookup = "form." + styleClass;
                } else {
                    formLookup = "form";
                }
            } else if (parent instanceof Portal) {
                String styleClass = ((Portal) parent).getStyleClass();
                if (styleClass != null && styleClass.length() != 0) {
                    formLookup = "portal." + styleClass;
                } else {
                    formLookup = "portal";
                }
                parent = ((Portal) parent).getParent();
                if (parent instanceof Form) {
                    styleClass = ((Form) parent).getStyleClass();
                    if (styleClass != null && styleClass.length() != 0) {
                        formLookup = "form." + styleClass + ' ' + formLookup;
                    } else {
                        formLookup = "form " + formLookup;
                    }
                }
            }
            IStyleRule s = null;
            String styleClass = (bc instanceof BaseComponent) ? ((BaseComponent) bc).getStyleClass() : null;
            if (bc instanceof Part) {
                styleClass = ((Part) bc).getStyleClass();
            }
            if (lookupName.equals("check") || lookupName.equals("combobox") || "radio".equals(lookupName)) {
                if (styleClass != null && styleClass.length() != 0) {
                    lookupName += '.' + styleClass;
                }
                lookupName = formLookup + ' ' + lookupName;
                s = ss.getCSSRule(lookupName);
                if (s.getAttributeCount() > 0)
                    return new Pair<IStyleSheet, IStyleRule>(ss, s);
                else
                    lookupName = "field";
            }
            if (styleClass != null && styleClass.length() != 0) {
                lookupName += '.' + styleClass;
            }
            lookupName = formLookup + ' ' + lookupName;
            s = ss.getCSSRule(lookupName);
            pair = new Pair<IStyleSheet, IStyleRule>(ss, s);
        // see BoxPainter for getBorder/getInsets/getLength examples
        }
    }
    if ((pair == null || pair.getRight() == null || (pair.getRight()).getAttributeCount() == 0)) {
        if (sp.getFlattenedSolution() != null) {
            List<Form> formHierarchy = sp.getFlattenedSolution().getFormHierarchy(form);
            for (int i = 1; i < formHierarchy.size(); i++) {
                pair = getStyleForBasicComponentInternal(sp, bc, formHierarchy.get(i), visited);
                if (pair != null && pair.getRight() != null && (pair.getRight()).getAttributeCount() != 0) {
                    break;
                }
            }
        }
    }
    return pair;
}
Also used : BaseComponent(com.servoy.j2db.persistence.BaseComponent) IStyleSheet(com.servoy.j2db.util.IStyleSheet) ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) IForm(com.servoy.j2db.IForm) Form(com.servoy.j2db.persistence.Form) Part(com.servoy.j2db.persistence.Part) Style(com.servoy.j2db.persistence.Style) IStyleRule(com.servoy.j2db.util.IStyleRule) Portal(com.servoy.j2db.persistence.Portal) RuntimePortal(com.servoy.j2db.ui.scripting.RuntimePortal)

Example 19 with IStyleSheet

use of com.servoy.j2db.util.IStyleSheet in project servoy-client by Servoy.

the class ComponentFactory method applyBasicComponentProperties.

public static void applyBasicComponentProperties(IApplication application, IComponent c, BaseComponent bc, Pair<IStyleSheet, IStyleRule> styleInfo) {
    // flag for border set by style config
    boolean isBorderStyle = false;
    // by default it is not transparent
    c.setOpaque(true);
    // apply any style
    if (styleInfo != null) {
        IStyleSheet ss = styleInfo.getLeft();
        IStyleRule s = styleInfo.getRight();
        if (ss != null && s != null) {
            if (s.hasAttribute(CSS.Attribute.COLOR.toString())) {
                Color cfg = ss.getForeground(s);
                if (cfg != null)
                    c.setForeground(cfg);
            }
            Object sbackground_color = s.getValue(CSS.Attribute.BACKGROUND_COLOR.toString());
            if (sbackground_color != null) {
                if (IStyleSheet.COLOR_TRANSPARENT.equals(sbackground_color.toString())) {
                    c.setOpaque(false);
                } else {
                    Color cbg = ss.getBackground(s);
                    if (cbg != null)
                        c.setBackground(cbg);
                }
            }
            // else c.setOpaque(false); // no background-color means transparent
            if (ss.hasFont(s)) {
                Font f = ss.getFont(s);
                if (f != null)
                    c.setFont(f);
            }
            if (ss.hasBorder(s)) {
                Border b = ss.getBorder(s);
                if (b != null) {
                    c.setBorder(b);
                    isBorderStyle = true;
                }
            }
            if (ss.hasMargin(s)) {
                Insets i = ss.getMargin(s);
                if (i != null && c instanceof IButton)
                    ((IButton) c).setMargin(i);
            }
        }
    }
    // We intentionally leave the location setting to DataRenderers, since thats the context and might substract part heights from location!
    java.awt.Dimension dim = bc.getSize();
    if (dim != null)
        c.setSize(bc.getSize());
    javax.swing.border.Border border = ComponentFactoryHelper.createBorder(bc.getBorderType());
    if ((c instanceof JCheckBox || /* DataCheckBox */
    c instanceof JRadioButton) && (border != null || isBorderStyle)) {
        ((AbstractButton) c).setBorderPainted(true);
        if (c instanceof JCheckBox) {
            ((JCheckBox) c).setBorderPaintedFlat(false);
        }
    }
    if (border != null) {
        if (border instanceof TitledBorder && Utils.isAppleMacOS()) {
            // apple bug.. i have to set the font again (as new!!)
            TitledBorder tb = (TitledBorder) border;
            Font f = tb.getTitleFont();
            if (f != null) {
                tb.setTitleFont(new Font(f.getName(), f.getStyle(), f.getSize()));
            }
            c.setBorder(border);
        } else {
            c.setBorder(border);
        }
    }
    // if (c instanceof IDelegate)
    // {
    // c = (JComponent)((IDelegate)c).getDelegate();
    // }
    String fontString = bc.getFontType();
    if (fontString != null) {
        Font f = PersistHelper.createFont(fontString);
        if (f != null)
            c.setFont(f);
    }
    java.awt.Color bg = bc.getBackground();
    if (bg != null)
        c.setBackground(bg);
    java.awt.Color fg = bc.getForeground();
    if (fg != null)
        c.setForeground(fg);
    String name = bc.getName();
    if (name != null)
        c.setName(name);
    // only use component property value if it is "checked" to be transparent
    if (bc.getTransparent())
        c.setOpaque(false);
    c.setComponentEnabled(bc.getEnabled());
    c.setComponentVisible(bc.getVisible());
    if (Utils.isSwingClient(application.getApplicationType())) {
        // special code for smart client LAFs, like BizLaf
        String delegateStyleClassNamePropertyKey = application.getSettings().getProperty("servoy.smartclient.componentStyleClassDelegatePropertyKey");
        if (delegateStyleClassNamePropertyKey != null && c instanceof JComponent) {
            if (c instanceof IScriptableProvider && ((IScriptableProvider) c).getScriptObject() instanceof IRuntimeComponent) {
                // special case since putClientProperty can delegate properties but cannot be overridden we relay on the scripting equivalent
                ((IRuntimeComponent) ((IScriptableProvider) c).getScriptObject()).putClientProperty(delegateStyleClassNamePropertyKey, bc.getStyleClass());
            } else {
                ((JComponent) c).putClientProperty(delegateStyleClassNamePropertyKey, bc.getStyleClass());
            }
        }
    }
}
Also used : Color(java.awt.Color) IStyleSheet(com.servoy.j2db.util.IStyleSheet) Insets(java.awt.Insets) JRadioButton(javax.swing.JRadioButton) AbstractButton(javax.swing.AbstractButton) Color(java.awt.Color) JComponent(javax.swing.JComponent) TitledBorder(javax.swing.border.TitledBorder) Font(java.awt.Font) Border(javax.swing.border.Border) JCheckBox(javax.swing.JCheckBox) Dimension(java.awt.Dimension) IButton(com.servoy.j2db.ui.IButton) IStyleRule(com.servoy.j2db.util.IStyleRule) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IRuntimeComponent(com.servoy.j2db.ui.runtime.IRuntimeComponent) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider) Border(javax.swing.border.Border) TitledBorder(javax.swing.border.TitledBorder)

Example 20 with IStyleSheet

use of com.servoy.j2db.util.IStyleSheet in project servoy-client by Servoy.

the class ComponentFactory method getCSSStyle.

public static IStyleSheet getCSSStyle(IServiceProvider sp, Style s) {
    if (s == null)
        return null;
    Map<Style, IStyleSheet> parsedStylesMap;
    if (sp != null && Boolean.TRUE.equals(s.getRuntimeProperty(MODIFIED_BY_CLIENT))) {
        // style changed by client, cache parsed style per client.
        parsedStylesMap = (ConcurrentMap<Style, IStyleSheet>) sp.getRuntimeProperties().get(PARSED_STYLES);
        if (parsedStylesMap == null) {
            parsedStylesMap = new ConcurrentHashMap<Style, IStyleSheet>();
            sp.getRuntimeProperties().put(PARSED_STYLES, parsedStylesMap);
        }
    } else {
        // static style, cache globally.
        parsedStylesMap = parsedStyles;
    }
    IStyleSheet ss = parsedStylesMap.get(s);
    if (ss == null) {
        ss = new ServoyStyleSheet(s.getCSSText(), s.getName());
        parsedStylesMap.put(s, ss);
    }
    return ss;
}
Also used : IStyleSheet(com.servoy.j2db.util.IStyleSheet) Style(com.servoy.j2db.persistence.Style) ServoyStyleSheet(com.servoy.j2db.util.ServoyStyleSheet)

Aggregations

IStyleSheet (com.servoy.j2db.util.IStyleSheet)22 IStyleRule (com.servoy.j2db.util.IStyleRule)17 Point (java.awt.Point)8 Border (javax.swing.border.Border)7 Part (com.servoy.j2db.persistence.Part)6 Color (java.awt.Color)6 Dimension (java.awt.Dimension)6 Insets (java.awt.Insets)6 FormController (com.servoy.j2db.FormController)4 IForm (com.servoy.j2db.IForm)4 IDataRenderer (com.servoy.j2db.ui.IDataRenderer)4 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)4 TitledBorder (javax.swing.border.TitledBorder)4 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)3 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)3 PrototypeState (com.servoy.j2db.dataprocessing.PrototypeState)3 AbstractBase (com.servoy.j2db.persistence.AbstractBase)3 RepositoryException (com.servoy.j2db.persistence.RepositoryException)3 Style (com.servoy.j2db.persistence.Style)3 ISupportRowStyling (com.servoy.j2db.ui.ISupportRowStyling)3