Adding Styles To Components: January 2006 (Revision Number: V2-1)
Adding Styles To Components: January 2006 (Revision Number: V2-1)
Adding Styles To Components: January 2006 (Revision Number: V2-1)
This tutorial explains how to use Sun Java Studio Creator integrated development environment (the IDE) to add styles to both a single component and several instances of the component. This tutorial also describes how to create a new style class and how to create and apply a new style sheet to your project. Contents - About Styles and the IDE - Opening a Sample Project - Applying a Style to a Single Component - Creating a New Style Rule to Apply to Selected Instances of a Component - Editing an Existing Style Rule to Change All Instances of a Component - Creating a New Style Sheet - Advanced Tips - Summary Example used in this tutorial Vehicle Incident Report Application provided in the samples directory of the IDE
First level of precedence. Goes to in-line style. In the Java Studio Creator IDE, the in-line style applies to rules you add to a component's style property, which you can edit using the Style Editor (see Figure 1). When you use in-line style, you override both the default CSS file, stylesheet.css, and, in most cases, the default theme, defaulttheme.jar. Setting styles on a per-component basis is good if you want to make one component different from the rest of the components on the page. For example, you might want to make an Order button bigger than the rest of the buttons in the application. However, setting styles on a per-component basis is not recommended as a general practice. For example, to change the background-color of that Order button, you need not only to set the style property's background-color but also you need to set the background-image to null to override what has been set by the css_master.css .Btn2 class. See the section in this tutorial, Advanced Tips, for more information.
Second level of precedence. Goes to the default CSS file which the Java Studio Creator IDE adds to every project that you create, and the IDE also includes a link to the CSS file within every page that you add to a project. Using a CSS file enables you to write styles that apply to many components instead of just a single component. Then, when you change the style definition in one place, the style is immediately reflected on all of the same component types. The IDE has a CSS Editor that you can use to edit the CSS file and a Style Rule Editor (see Figure 2) that you can use to create a new style rule. The IDE includes a styleClass Property Editor (see Figure 3) that you can
use to add style classes to the styleClass property or to bind the styleClass property to a JavaBean that determines the style classes applied to the component at runtime.
q
Third level of precedence. Goes to the theme. The IDE applies a default theme to each project. A theme consists of images, JavaScript, CSS files, and messages that are used by components to define the look and feel of the components. You cannot edit a theme, but if you know a theme rule that applies to a component, you can override it by creating a rule with the same name in the default CSS file. The section of this tutorial Editing an Existing Style Rule to Change All Instances of a Component includes instructions for finding the component's theme rule.
This tutorial demonstrates all three levels of CSS precedence and shows you how to modify the style rules in your project. Note: There are exceptions and the final result of the styling is based on the computed style as defined at http://www.w3. org/TR/CSS21/cascade.html.
You can see a preview of the new styles in the CSS Style text area at the bottom of the Style Editor, as shown in Figure 1.
Figure 1: Style Editor 4. Click OK to apply the style and close the Style Editor. 5. To verify the style change, click the Preview in Browser icon page in the default browser. in the Visual Designer toolbar and view the
3. In the text field under the Class radio button, type fancybutton in the text field after the period (.), as shown in Figure 2.
4. Click the
button to move the fancybutton class to the Style Rule Hierarchy box, and then click OK.
Notice the period (.) before the rule name in the style sheet. This CSS syntax defines a class rule rather than an HTML element rule or an element ID rule. 5. In the CSS Editor, select the following Font styles:
r r
Code Sample 1: fancybutton Style .fancybutton { font-family: Georgia,'Times New Roman',times,serif; font-size: 24px }
6. In the Projects window, double-click Login.jsp to open the file in the Visual Designer. 7. In the Properties window for the New User button, click the ellipsis button (...) next to the styleClass property. The styleClass property editor opens, as shown in Figure 3. The styleClass property editor shows all style classes that are available to the CSS files.
Figure 3: styleClass Property Editor 8. In the Filter field, type fan. You see the fancybutton style class listed.
9. Select the fancybutton style class and click the add this style class to the property.
The New User button font type and size changes to the fancybutton style class.
Note: To add the fancybutton style rule to more than one button at a time, in the Visual Designer, you can select all the buttons to which you want to apply the style and add fancybutton to the styleClass property just one time. 10. To view the style change, right-click the Login page and choose Preview in Browser.
7. Click the
button to move the Btn2Hov class to the Style Rule Hierarchy box, and then click OK.
8. In the Style Sheet Editor below the editing pane, set the Font to italic in the Style drop-down. The following rule is applied to the style sheet: Code Sample 2: Btn2Hov Style .Btn2Hov { font-style: italic }
9. Deploy the project to verify the changes by clicking from the main menu Run > Run Main Project. In the web browser, by default, the first page that opens is the Search page. Note that when you hover the mouse over the Search button, it does not change to italic because its style is taken from the theme's CSS file rule. 10. Click the Search button to go to the list of vehicles page and hover the mouse over the buttons in the table.
All non-primary buttons change to italic when you hover the mouse over them. Any new button that you create has the Btn2Hov style applied to it.
Advanced Tips
The following tips are provided for developers who are comfortable using and developing CSS styles.
You can view the rendered HTML by using the Mozilla DOM feature. Another method to use for viewing rendered HTML is to select a component, press and hold Ctrl-Alt, and click the mouse button. When properly activated, the component outline changes to red. In the JSF section of the Properties window, click the ellipsis button (...) next to RenderedHTML to see the rendered HTML for the component. In both Attributes and Rendered HTML you can find the active style class name. For example, the Search button yields a class="Btn2". To find the style properties defined in class Btn2, go to the theme's read-only css_master.css file: 1. In the Projects window, expand Libraries > Default Theme > com.sun.rave.web.ui.defaultheme.css. 2. Double-click the css_master.css file to open the file. 3. Search the file for the style class of interest, such as Btn2. For example, to set a Btn2 background-color, both background-color and backgroundimage must be defined to override the existing style class. Since you cannot make edits to the read-only css_master.css, use the style property editor to set the background-image to null and the background-color to the one desired for your component.
Another way to add a style sheet to your project is to drag and drop the style sheet file from a file manager window onto the page. The link to the style sheet is automatically added to the JSP file.
Summary
q q q
You can edit an individual component's style properties to override the default style sheet. You can create a new style rule and apply it to selected components. You can edit an existing style rule that applies to all new or existing components of the same type that you use in your project. You can create your own style sheet and apply it to your project.
See Also:
q q
Using Page Fragments, a tutorial available on the Java Studio Creator portal. Cascading Style Sheets, level 2, revision 1, CSS 2.1 Specification, a specification defined by the World Wide Web Consortium.
Sun Java Studio Creator integrated development environment (IDE) Sun Java System Application Server version number (Application Server) Java Platform, Standard Edition technology (Java SE(tm) platform) JavaServer(tm) Faces technology JavaServer Pages(tm) technology (JSP(tm) technology) Sun Java System Web Server version number (Web Server) Java Database Connectivity software (JDBC software) Enterprise JavaBeans(tm) specification (EJB(tm) specification) Solaris(tm) Operating System software (Solaris OS software)
The following third-party trademarked terms might be used in the Sun Java Studio Creator tutorials:
q q
Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is described in this document. In particular, and without limitation, these intellectual property rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents or pending patent applications in the U.S. and in other countries. U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its supplements. Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and the Java Coffee Cup logo are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.This product is covered and controlled by U.S. Export Control laws and may be subject to the export or import laws in other countries. Nuclear, missile, chemical biological weapons or nuclear maritime end uses or end users, whether direct or indirect, are strictly prohibited. Export or reexport to countries subject to U.S. embargo or to entities identified on U.S. export exclusion lists, including, but not limited to, the denied persons and specially designated nationals lists is strictly prohibited. Note: Sun is not responsible for the availability of third-party web sites mentioned in this document and does not endorse and is not responsible or liable for any content, advertising, products, or other materials on or available from such sites or resources. Sun will not be responsible or liable for any damage or loss caused or alleged to be caused by or in connection with use of or reliance on any such content, goods, or services available on or through any such sites or resources.
Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, tats-Unis. Tous droits rservs. Sun Microsystems, Inc. dtient les droits de proprit intellectuels relatifs la technologie incorpore dans le produit qui est dcrit dans ce document. En particulier, et ce sans limitation, ces droits de proprit intellectuelle peuvent inclure un ou plus des brevets amricains lists l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires ou les applications de brevet en attente aux tats-Unis et dans les autres pays. L'utilisation est soumise aux termes de la Licence. Sun, Sun Microsystems, le logo Sun, Java et le logo Java Coffee Cup sont des marques de fabrique ou des marques dposes de Sun Microsystems, Inc. aux tats-Unis et dans d'autres pays.Ce produit est soumis la lgislation amricaine en matire de contrle des exportations et peut tre soumis la rglementation en vigueur dans d'autres pays dans le domaine des exportations et importations. Les utilisations, ou utilisateurs finaux, pour des armes nuclaires,des missiles, des armes biologiques et chimiques ou du nuclaire maritime, directement ou indirectement, sont strictement interdites. Les exportations ou rexportations vers les pays sous embargo amricain, ou vers des entits figurant sur les listes d'exclusion d'exportation amricaines, y compris, mais de manire non exhaustive, la liste de personnes qui font objet d'un ordre de ne pas participer, d'une faon directe ou indirecte, aux exportations des produits ou des services qui sont rgis par la lgislation amricaine en matire de contrle des exportations et la liste de ressortissants spcifiquement dsigns, sont rigoureusement interdites. Sun Microsystems n'est pas responsable de la disponibilit de tiers emplacements d'enchanement mentionns dans ce document et n'approuve pas et n'est pas responsable ou iresponsable d'aucun contenu, de la publicit, de produits, ou d'autres matriaux dessus ou fournis par de tels emplacements ou ressources. Sun ne sera pas responsable ou iresponsable d'aucuns dommages ou perte causs ou allgus pour tre caus par ou en liaison avec l'utilisation de ce produit ou la confiance dans des tels contenu, marchandises, ou services disponibles sur ou par des tels emplacements ou ressources.