The Artima Developer Community
Sponsored Link

Artima Developer Spotlight Forum
A CSS Engine for Swing

4 replies on 1 page. Most recent reply: Jul 23, 2008 4:31 PM by John Zabroski

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

A CSS Engine for Swing Posted: Jul 18, 2008 9:57 PM
Reply to this message Reply
Advertisement

Although several UI toolkits use the Cascading Stylesheet Language (CSS) for customizing user interface components, Swing's component styling relies on a combination of constant values passed in at runtime and development-time component settings. That approach is not very flexible, argues Ethan Nicholas in a recent blog post, Introducing Java CSS, where he introduces a Java CSS engine that allows CSS-based styling of Swing components:

There already exists a powerful, flexible stylesheet language that millions of people are familiar with. It's called CSS, and various (mostly very poor) implementations already exist for Java. With a proper CSS engine, you wouldn't need to bury the configuration in your code or burn it into proprietary configuration files... The existing CSS implementations for Java are generally very poor. That's why I ended up creating a new one, which (hopefully) far exceeds previous CSS engines in many respects.

When a stylesheet is first applied to a component, applicable styles affect the component and all of its descendents. Certain individual properties, such as font and foreground, are inheritable, meaning that any descendent components automatically inherit these settings even when not specifically applied to them. The rule:

JFrame { foreground: red }

will set the default foreground of every component in the frame to red, due to property inheritance. Most properties are not inherited— when you set a border on a panel, you generally don't want that same border also applied to each individual component within the panel.

Once a stylesheet has been applied, the CSS engine tracks changes to the component heirarchy. Any newly added components are automatically styled, and removed components have their styles removed. You can additionally set one stylesheet as the global stylesheet. The global stylesheet is automatically applied to windows as they are displayed, but does not affect existing unstyled windows.

What do you think of Nicholas' Swing CSS engine?


John Zabroski

Posts: 272
Nickname: zbo
Registered: Jan, 2007

Re: A CSS Engine for Swing Posted: Jul 23, 2008 9:06 AM
Reply to this message Reply
Now this is a code-base I want to read through and an idea I want to evaluate!

John Zabroski

Posts: 272
Nickname: zbo
Registered: Jan, 2007

Re: A CSS Engine for Swing Posted: Jul 23, 2008 10:57 AM
Reply to this message Reply
By the way, Frank, what do you think of Nicholas' Swing CSS engine?

I'm curious to hear your thoughts.

Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Re: A CSS Engine for Swing Posted: Jul 23, 2008 3:48 PM
Reply to this message Reply
> By the way, Frank, what do you think of Nicholas' Swing
> CSS engine?
>
> I'm curious to hear your thoughts.

I haven't tried it yet, but it does sound very promising. My biggest problem with Swing has always been that it was difficult to make Swing applications look really good. And, if delivered as an applet, it was really difficult to make a Swing app blend in with other design elements of a site. This includes not only basic colors, but also things like fonts, re-sizing, etc. By contrast, all of that is very easy to do with Flex, precisely because Flex components can be styled via CSS. It is even possible to share a CSS stylesheet between HTML pages and Flex components. If this new API allows Swing components to be styled similarly, it could make a big difference, I think, in how Swing-based apps, and especially applets, are perceived.

John Zabroski

Posts: 272
Nickname: zbo
Registered: Jan, 2007

Re: A CSS Engine for Swing Posted: Jul 23, 2008 4:31 PM
Reply to this message Reply
My initial reaction was, "Finally, someone who wants to completely get rid of *.properties files!" Yet, I'm not sure CSS is the right approach. It's familiar, though, and people tend to prefer things that they trust instead of preferring things that are of high quality. Unless a developer is trained to know what to look for in a DSL, they will invariably choose a DSL that looks familiar to them.

Currently, I am developing a Silverlight 2 application. Through no choice of my own, I am using XAML. Being forced to use a language has a strange way of forcing you to become an insider and assess the language fairly. Initially, I did not like XAML at all. After using it for three months, it has become familiar and -- surprise -- is now my preferred template language. Funny how adoption works.

The question I've been asking myself is, "Was it just a matter of becoming familiar with the language that changed my opinion about it?" I don't think so. I still see major problems with the way Microsoft expects developers to write XAML. My two biggest complaints are that XAML out-of-the-box violates the Open-Closed Principle and promotes Pollution of the Knowledge Space.

For instance, if I want to adjust the style of a column header for a DataGrid control, then I have to copy-and-paste a multiple line ControlTemplate off MSDN and edit one line within that ControlTemplate. All I want to do is adjust a Layout Property like VerticalContentAlignment, yet the out-of-the-box way of writing XAML forces me to pollute the Style setter with information unrelated to Layout Properties. In doing so, I have polluted the knowledge space by providing excess details to accomplish the primary objective of the Style setter. These extraneous details are orthogonal concerns that I should not have to worry about at that particular point in time.

From what I have seen on the Internet, most criticism of XAML is an ad hominem attack against Microsoft. For instance, they argue that we already have XForms, CSS, XHTML, XUL, SVG and sXBL. From their perspective, it looks like Microsoft is re-inventing the wheel and that XAML is just another weapon for an Embrace, Extend, Extinguish coup d’état. Such a point of view is sad, but a reflection of the fact that Microsoft has continuously changed the standard way of doing things. For synchronization, it used to be Hailstorm and now it is LiveMesh. For database technologies, it has evolved from ODBC to OLE DB to RDO to ADO to ADO.NET. For any given technology Microsoft has touched, there has been disruptive evolution.

Yet, in XAML, Microsoft appears to have created something permanent. It is a single, unified language that closely obeys Elliotte Rusty Harold's stringent Effective XML guidelines. The biggest negative is that it is tightly coupled to the CLR; the Application Pack URI format is just one example of its tight integration with Microsoft virtual machine technology.

So my thoughts lately have been, "If XAML is here to stay, how can I make it more pleasant to work with such that I am not violating the open-closed principle and promoting pollution of the knowledge space?" This is where attempts like Nicholas' Swing CSS engine might be able to teach me something useful, even if the answer is, "This is a mistake. Don't do the same thing in XAML."

Flat View: This topic has 4 replies on 1 page
Topic: A CSS Engine for Swing Previous Topic   Next Topic Topic: Coverity Introduces Static Analyzer for C#

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use