You are hereAria User Guide / Section III: In Depth / Styles

Styles


By luano - Posted on 14 January 2009

Most applications try to use consistent styling and Aria provides support for consistent coloring and typography through its style manager. Aria's style management allows all the information and details of colors and fonts to be maintained separately from the rest of the UI description. Not only does this make it easy to reference particular style elements but it also makes it easy to switch styles say, for example, when rebranding an application for a different sets of end-users.

Using styles

In the introduction section we saw how styles could be applied when adding components either via XML or within Java code. Aria provides interactive visual tools to help design and use styles. Within Aria a style can be applied by selecting a component or several components and clicking on the chosen style in the style palette. The style name is then linked to the component so that any subsequent change to the style will change the look of linked components. If a component has an associated style then the styles palette's selection will be updated to show the selected style. The style palette presents a preview of the style and if you move the mouse over an individual style you should see a tooltip presented using the appropriate style, including the style's font.

Changing a style

A style can be modified by selecting the style in the style palette and then right clicking to popup the style's context menu. For most styles the simplest thing to do is to choose the Edit... option and pick colors and fonts that suit the particular use. The style edit dialog allows you to choose the font face and style plus the foreground and background colors. The styles are then saved to the styles file whenever the project is saved.

Color schemes

Aria extends the notion of styles by including a color scheme chooser to provide coherent sets of styles.

You can edit a set of styles as a color scheme by choosing the Color Scheme... option from the Style Palette's popup menu. The Color Chooser provides a convenient way of specifying sympathetic systems of colors and styles. The chooser consists of a number of areas for choosing the colors and then applying these colors to a set of styles. The color chooser has two parts, the first allows the choice of a set of sympathetic colors and the second allows selection of these colors for various usages. On the left hand side is a color wheel where you can choose a base color. This base color can be manipulated using two sliders for brightness and saturation. The hue can also be modified by clicking at some point within the inner circle. The Hue, Saturation and Brightness values can also be entered directly as numeric values. Clicking on the selected color preview (just below the color wheel) pops up a list of system colors (see the image on the right). The system colors can also be selected whenever the color wheel is available. Once the color has been chosen the colors and the variants are shown in a preview area. The number and type of variants displayed depend on the color scheme chosen. By default the scheme is monochromatic but other schemes can be chosen from the drop down list at the top of the chooser. Below the preview area is a table of colors and you can click on any on these to use it as the base color. On the second page of the chooser is an area where you can configure the styles. Simply click on the various sample texts to modify the foreground color, the background color or the font. A set of radio buttons allow you choose from these options. Once the set of styles has been selected click OK to save the styles under the specified style name. If a new style is being created the styles will be added to the style palette. Alternatively if the style name is already in use then the styles will be updated once the chooser is closed.

Loading styles

The style details are stored in an XML file pointed to by the startup parameter ' StyleFile '. In the absence of an entry or a file name the files ' styles.xml ' is used. A typical style file includes color and font information as below: A sample style file

  1. <styles>
  2. <style name="banner">
  3. <color_back value="ffffff"/>
  4. <color_fore value="0000ff"/>
  5. <font_face value="arial"/>
  6. <font_size value="10"/>
  7. <font_weight value="0"/>
  8. <font_italic value="0"/>
  9. </style>
  10. <style name="base">
  11. <color_back value="ffffff"/>
  12. <color_fore value="0000ff"/>
  13. <font_face value="arial"/>
  14. <font_size value="10"/>
  15. <font_weight value="0"/>
  16. <font_italic value="0"/>
  17. </style>
  18. <style name="Caption">
  19. <color_back value="ffffff"/>
  20. <color_fore value="008800"/>
  21. <font_face value="arial"/>
  22. <font_size value="11"/>
  23. <font_weight value="0"/>
  24. <font_italic value="0"/>
  25. <style name="CaptionSmall">
  26. <font_size value="8"/>
  27. <font_italic value="1"/>
  28. </style>
  29. </style>
  30. <style name="footer">
  31. <color_back value="ffffff"/>
  32. ...
  33. </styles>

Styles are determined from this hierarchy so that in the above example the style ' CaptionSmall ' is of the same font and color except with a smaller point size and italicized. (as of v2.0.6) Within the style file XML a color can also be specified as a decimal RGB value, an alpha channel value can also be specified as an option decimal value or as a hexval: Using decimal RGBA values

  1. <styles>
  2. <style name="banner">
  3. <color_back value="255,255,255"/>
  4. <color_fore value="0,0,255"/>
  5. <font_face value="arial"/>
  6. <font_size value="10"/>
  7. <font_weight value="0"/>
  8. <font_italic value="0"/>
  9. </style>
  10. <style name="base">
  11. <color_back value="ffffff80"/>
  12. <color_fore value="0,0,255,192"/>
  13. ...
  14. </styles>

Using System colors

System colors can also be used by naming the system color in place of an RGB value, thus Using SystemColors

  1. <styles>
  2. <style name="banner">
  3. <color_back value="activeCaption"/>
  4. <color_fore value="activeCaptionText"/>
  5. <font_face value="arial"/>
  6. <font_size value="10"/>
  7. <font_weight value="0"/>
  8. <font_italic value="0"/>
  9. </style>

The system colors are dependant on the user preferences and may vary from user to user and machine to machine. The system color properties are:

System Colors

Name

Usage

desktop

The color rendered for the background of the desktop.

activeCaption

The color rendered for the window-title background of the currently active window.

activeCaptionText

he color rendered for the window-title text of the currently active window.

activeCaptionBorder

The color rendered for the border around the currently active window.

inactiveCaption

The color rendered for the window-title background of inactive windows.

inactiveCaptionText

The color rendered for the window-title text of inactive windows.

inactiveCaptionBorder

The color rendered for the border around inactive windows.

window

The color rendered for the background of interior regions inside windows.

windowBorder

The color rendered for the border around interior regions inside windows.

windowText

The color rendered for text of interior regions inside windows.

menu

The color rendered for the background of menus.

menuText

The color rendered for the text of menus.

text

The color rendered for the background of text control objects, such as textfields and comboboxes.

textText

The color rendered for the text of text control objects, such as textfields and comboboxes

textHighlight

The color rendered for the background of selected items, such as in menus, comboboxes, and text.

textHighlightText

The color rendered for the text of selected items, such as in menus, comboboxes, and text.

textInactiveText

The color rendered for the text of inactive items, such as in menus.

control

The color rendered for the background of control panels and control objects, such as pushbuttons.

controlText

The color rendered for the text of control panels and control objects, such as pushbuttons.

controlHighlight

The color rendered for light areas of 3D control objects, such as pushbuttons. This color is typically derived from the control background color to provide a 3D effect.

controlLtHighlight

The color rendered for highlight areas of 3D control objects, such as pushbuttons. This color is typically derived from the control background color to provide a 3D effect.

controlShadow

The color rendered for shadow areas of 3D control objects, such as pushbuttons. This color is typically derived from the control background color to provide a 3D effect.

controlDkShadow

The color rendered for dark shadow areas on 3D control objects, such as pushbuttons. This color is typically derived from the control background color to provide a 3D effect.

scrollbar

The color rendered for the background of scrollbars.

info

The color rendered for the background of tooltips or spot help.

infoText

The color rendered for the text of tooltips or spot help.

Extended Styles

Styles can be extended to add attributes other than the basic color and font attributes. However the use of these extra attributes is dependant upon the context such as the component or class using the style. The extra attributes are automatically added for the style when the style is loaded. Once the stle has been loaded it is marked as closed and any attempt to access an unrecognised style name will cause an error (this can be changed with the XStyleEx.setClosed(...) method). An example usage of the extended styles is the validation style, which uses three colors to specify the feedback colors for the validation states. The validations styles could thus be specified as: Specifying extended styles

  1. <style name="banner" extended="true">
  2. <colorNormal value="ffffff"/>
  3. <colorWarn value="ffffd5"/>
  4. <colorFail value="ffe6d5"/>
  5. </style>

Note the extended attribute for the style. By adding extra style attributes you may be able to provide additional consistency within the application as attributes for various classes can be specified on an application wide basis instead of via a class by class or component by component basis. In many cases you can specify the attribute values for components, for example alignments and borders. In applying a style the framework will attempt to set component attributes using the style name and value via either the XAttributedComponent interface or via Reflection. The basic types such as String, int, double, float and boolean values can be accomodated. The built-in styles are handled directly as described above and set the component's colors and font. Setting right aligned label styles and properties

  1. <styles></P>
  2. <style name="labelBold">
  3. <color_back value="255,255,255"/>
  4. <color_fore value="0,0,0"/>
  5. <font_face value="arial"/>
  6. <font_size value="10"/>
  7. <font_weight value="1"/>
  8. <font_italic value="0"/>
  9. <alignment value="Right"/>
  10. </style></P>
  11. ...

Applying styles

To apply a style to a component you just need to reference the style in the page's XML declaration. Thus applying style to an address form becomes: Style usage

  1. <Page>
  2. <Components>
  3. <Label x="42" y="61" w="103" h="20" content="First Name:" alignment="Right" style="Caption"/>
  4. <Label x="42" y="85" w="102" h="20" content="Last Name:" alignment="Right" style="Caption"/>
  5. <Label x="42" y="116" w="102" h="20" content="Telephone:" alignment="Right" style="Caption"/>
  6. <Label x="42" y="137" w="102" h="20" content="Fax:" alignment="Right" style="Caption"/>
  7. <Label x="42" y="161" w="102" h="20" content="Mobile:" alignment="Right" style="Caption"/>
  8. <Label x="42" y="196" w="102" h="20" content="Address:" alignment="Right" style="Caption"/>
  9. <Edit name="firstNameEdit" x="154" y="62" w="100" h="20"/>
  10. <Edit name="lastNameEdit" x="154" y="85" w="100" h="20"/>
  11. <Edit name="phoneEdit" x="155" y="115" w="100" h="20"/>
  12. <Edit name="faxEdit" x="155" y="137" w="100" h="20" />
  13. <Edit name="mobileEdit" x="154" y="160" w="100" h="20" />
  14. <Edit name="address1Edit" x="155" y="198" w="248" h="20" />
  15. <Edit name="address2Edit" x="155" y="222" w="248" h="20" />
  16. <Edit name="address3Edit" x="155" y="246" w="249" h="20" />
  17. </Components>
  18. </Page>

Related issues

Colors and fonts are not the only style related things that should be consistent within an application. Layouts, naming, data representations, translations and so on should also be consistent. Consistency will make it easier for a user to grasp the concepts behind the application and it will make it easier to understand the mechanism used within the application for various tasks. Logically grouping elements can also assist readability. So, for example placing related components together within a framed panel gives a visual clue as to the association of those components. Subtle shading of panels can also assist in giving the necessary visual clues about the role of an individual panel particularly if the coloring is reinforced by the behavior you code into the applications.

Layout

In terms or readability it is often said that the average person can optimally perceive about seven pieces of information. In an application it is often difficult to limit the number of items or components displayed at once. Therefore style can play an important role in reducing apparent clutter and confusion. The style manager can go along way to helping produce a harmonious color scheme and if judiciously employed such a scheme may allow you make better use of the on-screen space. To maximize such benefit it is important that your layout is consistent. Careful consideration should be given to alignments, spacing and grouping of components. All of these features can greatly assist in the smooth flow of action within an application. A later chapter (See Layout.) details how layouts can be controlled within an application. With dynamic content it is always important to consider how the content will appear. Aria makes it easy to dynamically modify the content of a component but this generally doesn't mean that the layout of that component will be modified. Particularly for elements that display text it is important to note the interaction of fonts and layouts. Some fonts are narrower than others or have different distributions of character widths and therefore it is important to test your application with a representative set of data.

Flow

Just as layout is important, the flow of text and components can have a major impact on the user experience. Generally it is best to keep it simple: left, right, top to bottom. Keep it simple, reinforce the visual signals, update consistently.

Naming

Styles should be named consistently for two reasons. Firstly from a programming point of view a consistent naming convention will make reading of code easier and hence development and maintenance will be easier. We recommend that you use a convention similar to the naming convention you would use in programming Java. Secondly a consistent style naming convention is important where styles are being modified and particularly where the Color Chooser is being used. The chooser can modify existing styles depending on the style name and therefore a consistent naming convention will help avoid confusion when styles are modified in this way.

Data/formatting

Some values such as numeric, date or monetary values may be formatted specially and styles can be used to reinforce or highlight important values such as negative monetary values.

Setting the system look and feel

The system look and feel can be set up using a look and feel (LAF) installer. The LAF installer is controlled by a startup parameter " LAF ". The value of this parameter refers to a helper class that installs the LAF and takes care of the initialization. This setting is only appropriate for Swing applications as changing the LAF is not supported in the AWT.

Setting the Look and Feel

In Swing you can alter the look and feel of an application by setting the 'Look and Feel' manager for the application. The look and feel (LAF) affects the visual appearance of components and some of the soft behavioral attributes, like highlighting, rounding of button edges and so on. The LAF may also have a broader impact by changing indentations, component sizes and layout. Setting the LAF is fairly straightforward within Aria. The project settings pages include the option to choose the LAF. (it is on the files page as somewhat tenuously you need to choose a configuration file for the synth LAF) Adding a new LAF is also possible and Aria makes this even easier than normal by allowing you to simply plug-in the look and feel via a startup parameter. Aria comes with support for a few popular LAFs, but it is also easy to add support for your favorite LAF. The component class for setting the LAF is part of the Open Source Aria and hence you can review the source code for the LAF installer to see how to build an installer for some other LAF. The LAF is set with the following startup parameter

Setting the look and feel

LAF=org.formaria.laf.SynthInstaller

This setting refers to a helper class that installs the LAF and takes care of any initialization that is necessary. Aria lists a few popular LAFs, including the default LAF, JGoodies Looks LAF, Windows and the Synth LAF. Of these, the Synth LAF is probably the most interesting as it is configured via XML.

Configuring Synth

The Synth LAF requires a number of extra parameters to be configured to operate correctly.

Synth parameters in the start-up file

SynthConfigFile=demo.xml SynthResourceLoader=mypackage.MyClass

These parameters may vary significantly depending on the content of the Synth configuration file. The SynthResourceLoader is the name of a class that will be used to load the resources used by the Synth LAF. The class does not need to do anything special as it merely provides a (classloader) route to the resources. When the Synth LAF is chosen a number of new styles are added to the project if they do not already exist. These styles are used by the LAF and you can customize them to alter the look. Note that the Synth LAF provides rendering for many aspects of the components and the LAF may therefore override the styles you have chosen for individual components. Some of the built-in components have an option to suppress the LAF and use the styles you explicitly apply to the component.

SkinBuilder

A sample project ` SkinBuilder ' is included in Aria 3.0, this application is another demo of Aria, but it can be used to configure the Synth Look and Feel, including choosing artwork, setting properties and colorizing the LAF . See the accompanying documentation for more details.

Using styles with Synth

While the Synth look and feel can be used out of the box it is often desirable to connect the look and feel to the application's styles for added consistency. The Synth look and feel is configured via an XML file and this configuration file can itself be preprocessed to include colors and fonts from the applications look and feel. Some special expressions can be embedded in the XML and when processed these are converted into the color and font declarations expected by Synth. Scalable Vector Graphics ( SVG) images can also be used with Synth by way of the preprocessor. Aria can convert a referenced SVG into a PNG raster graphic for use in the final application. Using SVG in this way allows you to create sophisticated icons and buttons for your application and have these images scale to the required size.

Synth substitution expression

Expression

Example Result

Usage

${getFontFace(styleName)}

Arial

Gets the font face name from the style

${getFontSize(styleName)}

10

Get the font point size from the style

${getColorBackground(styleName)}

F0C2E9

Get the foreground color from the style

${getColorForeground(styleName)}

F00000

Get the background color from the style

${getFontWeight(styleName)}

BOLD

Get the font weight from the style

${getFontItalic(styleName)}

ITALIC

Get the font italic flag from the style

${svgToPng(styleName)}

images/button.png

Convert an SVG image to a PNG. Optional width and height attributes can also be specified after the source image name. If they are omitted a 50x50 image is created. A PNG image sometimes includes an optional preview of the image.

${svgToTiff(styleName)}

images/button.tif

Convert an SVG image to a TIFF. Optional width and height attributes can also be specified after the source image name. If they are omitted a 50x50 image is created.

Using SVG and Synth

Just as the Synth file can be processed so to can the preprocessed SVG files. SVG uses a slightly different definition of colors so different expressions are required but the concepts are the same. The substitution expressions also come in a number of variations to help support generation of gradients from a single style.

SVG substitution expression

Expression

Example Result

Usage

${getColorForegroundRgb(styleName)}

rgb(108,128,255)

Gets the RGB foreground color from the style

${getColorForegroundHex(styleName)}

F0C2E9

Gets the HEX foreground color from the style

${getColorBackgroundRgb(styleName)}

rgb(108,128,255)

Gets the RGB background color from the style

${getColorBackgroundHex(styleName)}

F00000

Gets the HEX backgroundcolor from the style

${getHsbColorForegroundRgb(styleName)}

rgb(108,128,255)

Gets the RGB foreground color from the style

${getHsbColorForegroundHex(styleName)}

F0C2E9

Gets the HEX foreground color from the style

${getHsbColorBackgroundRgb(styleName)}

rgb(108,128,255)

Gets the RGB background color from the style

${getHsbColorBackgroundHex(styleName)}

F0C2E9

Gets the HEX background color from the style

Each expression in the table above can have just a style name as an argument or it can have the style name plus percentage values for each color channel. Thus an expression such as getColorForegroundRgb(HeadlineStyle,50,60,80) obtains the color from the style, splits out the RGB color channels and returns a color that scales the channel values by the percentages given. So if the red value was 240 the example expression would return a color with a red value of 50% or 120. Similarly if the percentage given is between 100% and 200% the color value is scaled up to 255. In this way a red value of 120 and a percentage of 150 would result in a color with a red value of 187 (i.e. 120 + (255-120)*50/100). The expression set also includes a variation where instead of scaling the RGB values the HSB values are scaled. In some cases it may be easier to specify the gradient color stops as variations in saturation or brightness values. The scaling of the HSB values works in the same way as the RGB values. Aria automatically adds a set of styles for Synth when you select the Synth LAF. These styles are the styles that are used in the preconfigured Synth LAF that is part of Aria. However you can further customize the look and feel to use whatever graphics files and styles that you like, the configuration file is just another XML file that you can edit. To add another style to the Synth LAF just add the matching style within Aria or within the style file. Then embed the relevant expression from the list above in your Synth configuration file. The Synth LAF requires the Batik SVG engine to process SVG images. The libraries for Batik must be part of your project for this process to work correctly. More information on Batik can be found on the Batik website.

Panel backgrounds

Synth can be used to paint many components including the panel backgrounds but in some cases a different look or style is desirable for painting panels to help distinguish part of an application. A custom painter class can be used for such painting, the code for configuring such a background painter is shown below:

Setting a background painter

The example below shows a number of panels using this custom painter to display a gradient background with a watermark logo.

Painters

Aria 2.0 introduced the notion of painters much like the painters used within the Synth look and feel, and since then the use of painters has become more widespread. Aria 3.0 adopted the SwingLabs painter API in an effort to conform to standards and to make a wider range of painters available. The painters API is straightforward.

The painters API

public void paint( Graphics2D g2d, JComponent comp, int w, int h );

and can be used in a varient of Aria and Aria components. The painter is normally responsible for painting the entire client area of the component, but of course it is open to configuration. The painter class need not extend a component and therefore it acts purely as helper class. Some painters included in Aria are:

 

Painter

Appearance/Role

BandPainter

Fills the client area with a number of coloured bands, graduating from the foreground colour to the background colour.

FlarePainter

Fills the client area with a radial gradient using the foreground and background colours

TitlePainter

Extends the GradientBackground by adding a caption

GradientBackground

Fills the background with a gradient. The gradient uses the background and foreground colours. The painter can be configured to `nest' its gradient into the parent coordinate space so that multiple panels appear to share the same gradient.

SvgPainter

A painter based on an SVG images. See the section SVG Support for more details. The painter can interact with the mouse for various effects.

ImagePainter

Paints an image in the client area.

Style guidelines

A full discussion of styles is beyond the scope of this guide, however a number of style guidelines have been published and depending on your target platform and personal tastes it may be worth consulting these guidelines for clues as to best practice.

Look and feel resources

Java Look and Feel Design Guidelines: http://java.sun.com/products/jlf/

Introduction to the Apple Human Interface Guidelines: http://developer.apple.com/documentation/UserExperience/Conceptual/OSXHIGuidelines/

Official Guidelines for User Interface Developers and Designers: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/welcome.asp

Palm OS User Interface Guidelines: www.palmos.com/dev/support/docs/uiguidelines.pdf

SwingLabs Painter http://javadesktop.org/swinglabs/build/weekly/latest/swingx-HEAD/javadoc/org/jdesktop/swingx/painter/package-summary.html