43
IBM Software Group ® WebSphere ® Support Technical Exchange Adding a Graphical User Interface to your WebSphere Application Server Jython Scripts with Swing Bob Gibson <[email protected]> Advisory Software Engineer 8 November 2011

Add GUI to WAS Python Scripts

Embed Size (px)

Citation preview

Page 1: Add GUI to WAS Python Scripts

IBM Software Group

®

WebSphere® Support Technical Exchange

Adding a Graphical User Interface to your WebSphere Application Server Jython Scripts with SwingBob Gibson <[email protected]>Advisory Software Engineer8 November 2011

Page 2: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 2 of 43

Agenda

Intentions Starting simple Challenges and opportunities Jython advantages Using rapid prototyping Putting things together

Page 3: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 3 of 43

Background / impetus: ugly is easy

With little effort, we can show what ports are in use: print AdminTask.listServerPorts( 'nodeagent' )

Page 4: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 4 of 43

A simple script to show port numbers

Page 5: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 5 of 43

Simple text input / output: … a little harder

Text output is relatively easy, but user hostile

Page 6: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 6 of 43

Swing is all about Input & Output

High level presentation object include:Trees, Tables, and Lists

Low level presentation objects includeLabels, fields, text areasradio buttons, check boxes, sliderstrees, tables, buttons, images

Page 7: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 7 of 43

Components and containers

Most swing objects are components Some components are also containers Component class names start with J

Note: In order to be seen, a component must be within a container

Top level containers: heavyweightJFrame, JApplet, JWindow, JDialogCan’t be put into other containers

Lightweight containers: used to group components

Page 8: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 8 of 43

Good news / bad news

Good news:There are lots of swing books, articles,

tutorials, demonstration applications, web sites, and documentation

Bad news:Not all of the examples you see are good“Best practices” have changed (somewhat)

over time

Page 9: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 9 of 43

Learning to use swing

Look for swing examples, demos, tutorials Practice converting Java™ swing apps to Jython

Read, study, experiment (play), repeat Things to consider:

Some examples derive from ActionListenerJython doesn’t have anonymous inner classesSome examples don’t use dispatch thread

Note: Be prepared for bad examples

Page 10: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 10 of 43

A trivial Jython swing application

import the required Java & Swing classes Define our application class Define a Runnable class Use SwingUtilities.invokeLater() for thread safety

(to use dispatch thread cycle)Notes:

Application class is derived from JFrame Our class is instantiated by a Runnable class We need to keep script running (raw_input())

Page 11: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 11 of 43

Trivial Jython swing application (cont)

Page 12: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 12 of 43

Trivial application – what it can do

This application can already be:MovedResizedMaximizedMinimizeRestored, andClosed (terminated)

All of this with no additional code!

Page 13: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 13 of 43

Do we really need to invokeLater?

Swing applications are event drivene.g., keystrokes & mouse clicks

Events are generated and passed to the application

Events are received and processed by event handlers

Event handlers are executed on the event-dispatch thread, not your application thread

So, our applications should be implemented accordingly

Page 14: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 14 of 43

Challenges (part 1)

Fight the temptation to throw things together

Try to avoid the urge to keep everything in 1 class

Page 15: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 15 of 43

Opportunities

Jython scripts have complete access to all of the Java Swing object / classes / components

Converting Java Swing examples to Jython can be quite simple (as long as you can read & understand Java)

Remember:Jython isn’t strongly typed like JavaUnlike Java, Jython object attributes aren’t

fixed; we can add to, and remove from them

Page 16: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 16 of 43

Simple Button Example

Page 17: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 17 of 43

More challenges and opportunities Often, there are many ways to do something

e.g., Positioning application window using AWT

vs. using a possibly obscure method

There are many, many more examples

Page 18: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 18 of 43

Some Jython advantages

Java frequently requires many statements:

Jython lets us do more things at once:

Page 19: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 19 of 43

Positioning components

Given a container (e.g., JFrame), and some number of components, how do we position them?

By selecting a Layout Manager for the container

LayoutManager DescriptionFlowLayout Positions components left-to-right, top-to-bottomBorderLayout Positions components within the center or the borders of the

container (default)

GridLayout Lays out components within a gridBoxLayout Lays out components vertically or horizontally within a boxSpringLayout Lays out components subject to a set of constraints

Page 20: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 20 of 43

Layout can be somewhat …challenging

Page 21: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 21 of 43

Where were we?

Initial ideaDisplay AppServer port numbers using a GUI

Where should we start?How can we show the server in our cell?One way is to use a tabbed paneWhat would that look like?

Page 22: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 22 of 43

Using JTabbedPane

Page 23: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 23 of 43

What have we learned?

Advantages:Simple to implementJTabbedPane allows easy traversal using keys

DisadvantagesFor anything other than a simple (small) cell,

it’s easy to get lots (too many) tabsThe relationship between servers isn’t obvious

Can we display the cell hierarchy in a tree form?

Page 24: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 24 of 43

Using JTree to show the cell hierarchy

Page 25: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 25 of 43

JTree: Pro & Con

AdvantagesSimple to implementCell hierarchy maps well to this formatBuilt in keystroke traversalWrapping the JTree in a Scrolled Pane allows

for large cells to be displayed easily Disadvantages

You may not like the default JTree attributes

Page 26: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 26 of 43

Where do we show Server port info?

We can have the JTree shown on a split pane In the other side, we can decide what to display

For branch nodes (i.e., “cell” and “node”), we have to decide what we want to display

For servers, we can show a table containing the port details

Page 27: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 27 of 43

Adding a SplitPane

Start by showing the tree in the left pane:

Note: We don’t want multiple entries selectable

Page 28: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 28 of 43

How do we display port number info?

JTable is very function rich Swing component What do we need to do?

Instantiate it using two arraysData: 2 dimensional array of valuesColumn Headings: An array of strings

table = JTable( data, headings )

Page 29: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 29 of 43

Simple JTable to display port info

Page 30: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 30 of 43

JTable: Built in features / attributes

Editable cells Resizable:

Columns (width)Frame (width / height)

Draggable:Columns (position)

Cell selectable using keystrokes or mouse clicks Note: Use of DefaultTableModel

Page 31: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 31 of 43

With great power …

You can control things (e.g., size & resizing),col.setMinWidth( width )

col.setMaxWidth( width )

col.setPreferredWidth( width )

table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF )

The number of selectable rows, table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION )

Or columns, or just cells…table.setRowSelectionAllowed( BOOLEAN )

table.setColumnSelectionAllowed( BOOLEAN )

table.setCellSelectionEnabled( BOOLEAN )

And lots, lots more…

Page 32: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 32 of 43

comes great responsibility …

We have the ability to:Make only certain cells editable (e.g., port #)Fix column width, and positionMaybe even allow columns to be sortedDo we want to provide ways to filter the data

differently?

• Perhaps, but we don’t have time here

Page 33: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 33 of 43

For example

JTables have many properties… AutoResizeMode can be set to one of:

AUTO_RESIZE_OFFAUTO_RESIZE_NEXT_COLUMNAUTO_RESIZE_SUBSEQUENT_COLUMNSAUTO_RESIZE_LAST_COLUMNAUTO_RESIZE_ALL_COLUMNS

What does this really mean? Let’s find out

Page 34: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 34 of 43

JTable AutoResize example

Page 35: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 35 of 43

Table models

The table model used to create a JTable defines how the table may be used

You may want to define one for your application For example, to:

Define column widths,Prohibit column movement,Restrict cell editing,Define how cells should be displayed

(rendered)

Page 36: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 36 of 43

Table models (cont)

For an application with many similar tables, models can make life easy

Creating a class descending from TableModel lets us:Have all tables look and act the sameLocalize all of the table settingsExperiment with our settings easily

Page 37: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 37 of 43

An application (prototype)

Page 38: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 38 of 43

Are we done yet?

No! There is lots of room for improvement

Event Handler does too much & is too slowShouldn’t be able to select a server until the

port information is already availableWidths (e.g., column, table) are hard codedCurrently: no config changes are made

Are we ever done? … it depends ;-)

Page 39: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 39 of 43

Is it worth the time and effort?

Yes! Rapid prototyping can be quick and easy

Jython + swing lets us test to learn as we goWe can see the implications of our choices

Interaction with WebSphere configuration and environment is easy

Jython is easy to read and understand Iterative improvement is easy

Build the application & testing it as we go

Page 40: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 40 of 43

Summary

Graphical, event driven, applications are possible with Jython

Jython applications can access and use WebSphere administrative objects easily

Jython applications can access and use Java objects easily

Rapid prototyping allows us to determine where to spend our design, development, and testing time

wsadmin + Jython + Swing is a big win!

Page 41: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 41 of 43

Additional WebSphere Product Resources Learn about upcoming WebSphere Support Technical Exchange webcasts, and access

previously recorded presentations at:http://www.ibm.com/software/websphere/support/supp_tech.html

Discover the latest trends in WebSphere Technology and implementation, participate in technically-focused briefings, webcasts and podcasts at:http://www.ibm.com/developerworks/websphere/community/

Join the Global WebSphere Community: http://www.websphereusergroup.org

Access key product show-me demos and tutorials by visiting IBM® Education Assistant:http://www.ibm.com/software/info/education/assistant

View a webcast replay with step-by-step instructions for using the Service Request (SR) tool for submitting problems electronically:http://www.ibm.com/software/websphere/support/d2w.html

Sign up to receive weekly technical My Notifications emails:http://www.ibm.com/software/support/einfo.html

Page 42: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 42 of 43

Connect with us!

1. Get notified on upcoming webcastsSend an e-mail to [email protected] with subject line “wste subscribe” to get a list of mailing lists and to subscribe

2. Tell us what you want to learnSend us suggestions for future topics or improvements about our webcasts to [email protected]

3. Be connected!Connect with us on FacebookConnect with us on Twitter

Page 43: Add GUI to WAS Python Scripts

IBM Software Group

WebSphere® Support Technical Exchange 43 of 43

Questions and Answers