Presentation is loading. Please wait.

Presentation is loading. Please wait.

Presenter Name presenter@address.com This presentation introduces Swing Application Framework and Beans Binding JSRs. These are two new JSRs which greatly.

Similar presentations


Presentation on theme: "Presenter Name presenter@address.com This presentation introduces Swing Application Framework and Beans Binding JSRs. These are two new JSRs which greatly."— Presentation transcript:

1 Presenter Name presenter@address.com
This presentation introduces Swing Application Framework and Beans Binding JSRs. These are two new JSRs which greatly simplify desktop Java development. We will also cover new GUI Building features in NetBeans 6 as a part of this presentation. Presenter Name

2 Agenda Introduction JSR 296 (Swing App. Framework)
JSR 295 (Beans Binding) Putting it all together Conclusion The agenda is pretty straightforward: 1. Introduce Swing Application Framework and do a demo of an application that uses it to learn the basics of this JSR. 2. Introduce Beans Binding and do a demo of an application that uses it to learn the basics of this JSR. 3. Create an application which uses both JSRs to show how they can be reused in real-world applications. 4. Conclusion – what do we gain from these JSRs.

3 Introduction Goals of this session:
Introduce new JSRs for desktop Java: JSR 296 and 295 Demonstrate building an application using these frameworks The goals are two-fold: 1. Technology-oriented presentation of JSR 295 and 296 2. Show the tools for these JSRs and also show the latest and greatest Matisse features in meanwhile.

4 Agenda Introduction JSR 296 (Swing App. Framework)
JSR 295 (Beans Binding) Putting it all together Conclusion We'll start with Swing Application Framework.

5 Swing App. Framework Swing v 0.1 debuts in 1997
It's widely adopted now But there is no standard framework Explain the motivation for Swing application framework – Swing has been very succesful despite the fact that it is still missing a standard framework.

6 Motivation public static void main(String args[]) { // good luck! }
Developers are left alone – there's no standard framework, no typical way how to create a Swing application. So lots of developers repeat same mistakes or write similar code, which could have existed in a framework.

7 Framework Goals As small as possible Explain it all in an hour
Work well for small/medium apps Out of scope: Modular system, docking, filesystems, data models, help system, updates, etc. Not a replacement for NetBeans platform Explain the goals of Swing application framework and compare it with NetBeans platform, which is larger and can be also used as a framework. Both approaches have their advantages and disadvantages – a small framework is easy to learn, but provides less features. Big framework such as a platform provides really lots of features but is much harder to learn. Since learning curve has been the biggest problem with rich client platforms, a small framework was needed.

8 Framework Features Lifecycle support Resources Actions Tasks
Session state These are the main features of Swing application framework. Actually there's not much besides this, so you can see that the framework is really lightweight. You can learn it in several days, not like a platform which takes weeks. We'll look at each of the features one by one.

9 Framework Architecture
Let's look at the architecture of the framework first. As you can see, it is quite simple. There are two main classes in the API - Application and ApplicationContext. There's a 1-to-1 mapping between them. You extend the Application (e.g. by extending SingleFrameApplication which is a subclass of Application). Then you call Application.launch method to initialize your GUI on the Event dispatch thread. You can use the ApplicationContext services to work with resources, actions, tasks, storage, etc.

10 Lifecycle Support public class MyApp extends SingleFrameApplication {
@Override protected void startup() { JLabel label = new JLabel("Hello World"); show(label); } public static void main(String[] args) { Application.launch(MyApp.class, args); // SingleFrameApplication is a subclass of Application Here's an example of how you use Swing application framework to initialize your application. Your application needs to extend the SingleFrameApplication which is a subclass of an Application. There will be a class for multi-frame applications in the final version of the JSR. You call the Application.launch method in your main method. You provide your class as an argument There are several methods you can override: startup, initialize, ready and shutdown. In this example we override the startup method to define what should happen on the startup. In this simple UI we just show a “Hello world” label.

11 Resources Defined with regular ResourceBundles
ResourceManager class is used Three options: Manually load, use ResourceMap objects Automatically inject resources into UI components Automatically inject resources into object fields Swing application framework uses the regular ResourceBundles - which are familiar to all Java developers. It provides a class called ResourceManager which takes care of them (similar to EntityManager in JPA). You can handle resources manually using ResourceMap objects, which is similar to the old way of loading resources. Alternatively the framework provides very convenient ways of resource injections, we will see it in the code. The frameworks also supports field injection - it annotations to mark the fields. This is used to e.g. inject parametrized strings into longer messages using %s.

12 Manual Loading # resources/MyForm.properties aString = Just a string
aMessage = Hello {0} anInteger = 123 aBoolean = True anIcon = myIcon.png aFont = Arial-PLAIN-12 colorRGBA = 5, 6, 7, 8 color0xRGB = #556677 ApplicationContext c = ApplicationContext.getInstance(); ResourceMap r = c.getResourceMap(MyForm.class); r.getString("aMessage", "World") => "Hello World" r.getColor("colorRBGA") => new Color(5, 6, 7, 8) r.getFont("aFont") => new Font("Arial", Font.PLAIN, 12) As you can see the resource files are defined as expected. On the bottom of the slide there is code which uses the ResourceMap to load each of the properties. You can see that it can handle various types including colors, fonts and other comples properties.

13 Resource Injection Resources are injected automatically!
Resource file: btnShowTime.text = Show current time! btnShowTime.icon = refresh.png Java class: btnShowTime = new JButton(); txtShowTime = new JTextField(); btnShowTime.setName("btnShowTime"); txtShowTime.setName("txtShowTime"); You can also use automatic resource injection. It is very simple - you just use default names of the properties file and then setNames of the components. The default name of the properties file is: resources/NameOfClass.properties The framework handles resource injection for you, so you don't need to write code to extract the string from the resource bundle, etc. The resources can have arbitrary names and the injection will work as long as you use the same name of the resource (e.g. btnShowTime) and the parameter of the setName() method (“btnShowTime”). Resources are injected automatically!

14 Object Field Injection
Resource file: MyPanel.greetingMsg = Hello, %s, a string was injected! Java class: @Resource String greetingMsg; ResourceMap resource = ctxt.getResourceMap(MyPanel.class); resource.injectFields(this); String personalMsg = String.format(greetingMsg, txtName.getText()); JOptionPane.showMessageDialog(this, personalMsg); To perform object field injection you need to mark the field annotation. Then you call the injectFields() method and you can use the resource with a parameter in your code, as you can see on the example. For more details see:

15 Actions Possible simple logic Instead of ActionListeners
Asynchronous actions are easy @Action annotation Possible simple logic ActionListeners are pain due to annonymous inner classes and other complexities. Swing application framework introduces actions which are denoted by annotation. Asynchronous actions are possible and much easier than before, they return a Task (we'll discuss that in a while). Simple logic is possible, so for instance the action in the example will be available only if the getChangesPending method returns true. The actions are integrated into the GUI, so if the method returns false the JButton for save will be disabled. @Action(enabledProperty = "changesPending") public void save() { ... } public boolean getChangesPending() { ... }

16 Actions // define sayHello Action – pops up message Dialog
@Action public void sayHello() { String s = textField.getText(); JOptionPane.showMessageDialog(s); } // use sayHello – set the action property Action sayHello = getAction("sayHello"); textField.setAction(sayHello); button.setAction(sayHello); In this example we define a sayHello action which displays a message. To get access to the action we can call the getAction method from the ApplicationContext. Then we can set this action to any of the Swing components like buttons and we are done. When the user clicks on the button the action is executed - no listeners are needed. One action can be reused for multiple components. In NetBeans actions are now available so you can define new actions and assign them to components instead of calling actionPerformed() methods etc.

17 Tasks Title, Start/Done, etc. Inherit the SwingWorker API
Support for monitoring Title, Start/Done, etc. return Tasks Easy to handle non-blocking actions Tasks inherit the SwingWorker API. That guarantees that the tasks are NOT executed on the Event dispatch thread. There is an API for monitoring of tasks - you can get the title of the task, start it, finish it, etc. Asynchronous actions return tasks and you can override the methods of the task to perform some action both on action invocation and on completion of the task. So it is easy now to handle non-blocking actions, the user can click on the button and the overriden method “doInBackground” is executed. Once the task is finished it will call the “succeeded” method again overriden by you to reflect the status in the GUI. We will see background tasks in the demo.

18 Session State Window size Application position
Easily save properties: Window size Application position Sizes of columns in jTable, etc. ApplicationContext.getSessionStorage() Save(RootComponent, filename) The application framework can also handle session state. It can save important properties of your application like window size, position of the window, sizes of individual columns, etc. The framework provides the getSessionStorage() method which returns the stored values. All values are stored in XML. Similarly there is a save value which saves the properties of components into the XML file. If you extend the SingleFrameApplication saving and retrieving of session state happens automagically.

19 JSR 296 vs. NB Platform JSR 296 vs. NetBeans Platform
Platform is better for: Applications that can reuse platform APIs Editor-based applications If you need update centers, window system, file systems, data systems, help system, visual designers, explorers, etc. JSR 296 is much easier to learn One of the frequent questions about Swing application framework is how does it relate to NetBeans platform. Both frameworks are Swing based and they have some features in common. The biggest difference is in the size - NetBeans platform is a complex framework with many features. NB platform solves opening multiple files, updating of the application, docking of windows and there are many APIs available you can reuse. However the disadvantage is that you need to invest a lot of time in learning the platform and the APIs. If you just want a simple framework and your application is small/medium sized, JSR 296 may be a better option. If your application is complex and can reuse various features of NetBeans platform, then it's probably a better choice.

20 Demo JSR Flickr Demo Let's look at Swing application framework in action and how it is supported by NetBeans 6.

21 Agenda Introduction JSR 296 (Swing App. Framework)
JSR 295 (Beans Binding) Putting it all together Conclusion We'll continue with the second part of this presentation - Beans Binding.

22 Beans Binding Keeps two properties of two objects in sync
Source properties specified using Expression Language: ex: “${customer}” or “${employee.salary}” Does not require special object types: new Binding(object, "${property}", textField, "text"); The main idea of Beans Binding is to keep two properties of two objects in sync. Using this approach you can bind the properties and when value of one of them updates, the framework makes sure that the value of the other one is updated. Thanks to this visual binding is possible - e.g. when a slider is moved an image is resized. Properties can be specified using expression language - the same language that is used in Java EE 5. Beans binding works for regular object types, so no new object types are necessary.

23 Builds Upon... Beans PropertyChangeListeners
Doesn't require strict following of spec Collection classes Standard way to encapsulate common data types Beans binding takes advantage of Beans - it uses PropertyChangeListeners to detect changes. It doesn't require strict following of the spec - for example to treat Map as a bean with dynamic properties. Beans binding also builds on collection classes which provide a standard way to encapsulate common data types.

24 Features Different update strategies
Read once, read only from source, keep source and target in sync Ability to do validation Ability to transform value String to Color, Date to String There are different update strategies, you can choose them in NetBeans as an option of the binding. Sometimes you need just to bind the value once, or you can need only a one-way binding, or keep both source and target in sync. Beans binding can also handle validation - which makes sure that the value bound to the property is sane. Beans binding can also transform values, e.g. a String to Color and Date to String.

25 Example Let's look at a simple example of property binding.

26 Example The user moves the slider and a ChangeListener is called.

27 Example The property is bound to the image and so the setFaceStyle method is called to change the value to the new value.

28 Example Similarly if other process changes the value of the faceStyle, a propertyChangeListener is called and the slider.setValue() method is called. The slider is updated visually as well.

29 Example - Before 295 faceSlider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { caricature.setFaceStyle(faceSlider.getValue()); } }); caricature.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName() == “faceStyle”) { faceSlider.setValue(caricature.getFaceStyle()); } }); This is the how this kind of binding was handled before JSR 295 existed. This code is very ugly.

30 Example - JSR 295 Binding binding = new Binding(
caricature, “${faceStyle}”, // source faceSlider, “value”); // target binding.bind(); This is how the code looks like using JSR 295. As you can see, much cleaner and simpler code.

31 Demo JSR Ticker Demo This demo shows how JSR 295 is supported in NetBeans in the GUI builder.

32 Agenda Introduction JSR 296 (Swing App. Framework)
JSR 295 (Beans Binding) Putting it all together Conclusion Now that we've learned about both JSRs and how they are supported in NetBeans, let's look at an example which uses both of them.

33 Demo JSR 295 + JSR 296 + Matisse improvements in NB 6.0
This demo shows a database-drive application created with NetBeans and JSR 295/6. It also demonstrates some of the new features introduced in NetBeans 6.0 to make swing GUI building easier.

34 Conclusion JSR 295 and 296 are making Swing development fun again
Both are very well supported in NetBeans GUI builder (project Matisse) Desktop Java becaming interesting for “Delphi” developers Desktop Java is well and alive! We've seen in the demos that JSR 295 and 296 help us create Swing applications faster and it is fun to do Swing development now. Both JSRs are very well supported by the GUI builder and there's a lot of new features in NetBeans 6.0 for Swing developers. Thanks to the simplifications of the standards and to the features of the GUI builder even “Delphi”-type of developers can find Java platform attractive for building desktop applications. ... and Desktop Java is well and alive!

35 Thank you.


Download ppt "Presenter Name presenter@address.com This presentation introduces Swing Application Framework and Beans Binding JSRs. These are two new JSRs which greatly."

Similar presentations


Ads by Google