Top Banner
THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.
48

THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

THE MERGING POINT OF ANDROID AND SWING

David QiaoJIDE Software, Inc.

Page 2: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Myself

• 10-year old Swing developer• Author of most components offered by JIDE

Software

• 1-year old Android developer• Author of several popular Android apps:

Advanced Ruler (free) and Advanced Ruler Pro (paid)

Page 3: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

What is Android?

• Android is a software stack for mobile devices that includes an operating system, middleware and key applications

• Allows any Java developers to make phone apps– It’s in “Java”!– Familiar dev environment – Eclipse, NetBeans or

IntelliJ IDEA

Page 4: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

ComparisonSwing Android

JComponent View

Container ViewGroup

AbsoluteLayout FrameLayout

FlowLayout LinearLayout

Component.addMouseListener View.setOnClickListenerJTable.setCellRenderer GridView.setAdapter

Window.setContentPane Activity.setContentView

Page 5: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Topics

• Resource Management• Theme and Style

Page 6: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Resource Management

Page 7: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Resources

• What are resources? – Images– Strings– Layouts– Values (colors, dimensions, integers etc.)– Animations– Audios and videos

• Externalize the resources

Page 8: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Android Project

Page 9: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Different Locales

• res/values/strings.xmlContains English text for all the strings that the application uses

• res/values-fr/strings.xmlContains French text for all the strings

• res/values-ja/strings.xmlContains Japanese text for all the strings

Page 10: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Different Devices

• res/drawable/Contains default graphics.

• res/drawable-small-land-stylus/Contains graphics optimized for use with a device that expects input from a stylus and has a QVGA low-density screen in landscape orientation.

Page 11: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Resource Management Comparison

Swing Android

Strings Properties file at any folder XML file under res/values/strings.xml

Images Any folder Image files under res/drawable/ folder

Layouts Mostly in Java source code, or form files of some GUI builders

XML files under res/layout/(res/layout-hori/ or layout-land/)

Colors In Java code or in UIDefaults

XML files under res/values/color.xml

Audios and videos Any folder Under res/raw/

Page 12: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

What is Missing in Swing?

• Give too many choices to the developers• The more choices, the more chances of

making mistakes, and frustration

Page 13: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

James Gosling

•“The biggest problem with Swing was that it can do so much that it's kind of become like the 747 cockpit if you like APIs. There's a lot of complexity there, and the hard part is figuring out how to use it”

Page 14: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.
Page 15: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

But

• Not everyone is a pilot!!!

Page 16: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

The Simplest is the Best

Page 17: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.
Page 18: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Developer Experience ComparisonTasks Swing Android

Missing resources Runtime error Compile time error

Resource Lookup Look up in folder or file Code completion

Refactoring Change file name then change references

IDE support

Locations Multiple locations Single location (under res folder)

Learning Curve Steep Almost none

Page 19: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Android ID’nize all resources

• Assign a unique int ID for each resource so that all resources can be referred using a unique ID.

• On Android, a tool called aapt will do it

Page 20: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Resource Class on Android

Page 21: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Using ID’nized Resources

• getWindow().setBackgroundDrawableResource(R.drawable.my_background_image) ;

• getWindow().setTitle(getResources().getText(R.string.main_title));• getWindow().setTitle(R.string.main_title);

• // Load a custom layout for the current screen• setContentView(R.layout.main_screen);

Page 22: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

What to Learn from Android?

Page 23: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Resource to ID

• A utility, just like Android’s aapt to parse all resources and create resource classes that contains the unique IDs.– Under /com/company/project/res

• Could be an ant target, a plug-in to IDE so that it will run automatically when needed– Generate com.company.project.res.R class

Page 24: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Resource Location?

• A single location as Android does?– Probably not.

Page 25: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

API Changes

• AbstractButton only has setText(String)• There is no setText(int id)

Page 26: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

API Changes

• ResourceUtils res = ResourceUtils.getInstance(new R());

• button.setText(String)• New: res.setText(AbstractButton, int id)

• component.setForeground(Color)• New: res.setForeground(JComponent, int id)

Page 27: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

ResourceUtils

• Placeholder for methods that set resource related properties

• Listen to locale changes and when locale changes, it will automatically update the component text or tooltip or icon based on the ID

Page 28: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Methods

• setText (JLabel or AbstractButton)• setToolTipText (JComponent)• setIcon (JLabel and JButton),• setDisabledIcon (Rollover, Pressed, Selected

etc. for AbstractButton)• Mnemonic using Window convention (i.e.

“&Title” is Title).

Page 29: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Benefit

• Code completion can be used to find the resource without looking into the properties files or the image folders

• Compile time error if the resource is missing• Change locale on fly

Page 30: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

DemoResource Management

Page 31: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Theme and Style

Page 32: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

A TextView in Android

Page 33: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

After using Style

Page 34: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Style

• On Android, “style” is nothing but a bunch of properties for a control

• Support simple inheritance

Page 35: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Theme

• When style is used on an application or activity, it becomes theme

Page 36: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

A Sample Theme

Page 37: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Java Look And Feel

• Theme is equivalent to Java L&F.• UIDefaults table in Swing is the same as the

style XML file in Android• Synth LookAndFeel already uses XML file to

define L&F

Page 38: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Synth XML

Page 39: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Problems with Synth

• Too complex– Unnecessary hierarchy

• Limited binding– Static binding only

• i.e. can’t do thing like tabbedPane.loadStyle(“newStyle.xml”);

– By region or name only• i.e tabbedPane.setName(“newStyle”). What if the name

us used for other purpose?

• Poor extensibility for 3rd party components

Page 40: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Nimbus LookAndFeel

• Built on top of Synth• The first pure vector based L&F• Dynamic binding (can define new state)• Flat

Page 41: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Problems with Nimbus

• Very complex– You will be surprised if you try to read nimbus

source code– No XML

• Too ambitious– Pure vector based L&F– Instead of using vector, I can create three images

at different resolutions which will cover for at least next five years

Page 42: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

What is REALLY a LookAndFeel

• A bunch of colors– control, controlText, Label.foreground, …

• A bunch of fonts– Label.font, TextField.font, …

• A bunch of painters– To paint components in different states.

• More flags for each component– To fine tune the L&F, such as iconTextGap, insets.

Page 43: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

XML? Or just plain text?

• I don’t really care• I choose properties file in favor of simplity

Page 44: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Creating a new L&F

• load(packageName + “colors”); • load(packageName + “button”); // painters

and flags for button• load(packageName + “comboBox”); // painters

and flags for combobox• …

Page 45: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Extending a L&F

• Customize a LookAndFeel– Just call load(packageName + “myApp”), overwrite

existing UIDefaults• Extend a LookAndFeel– Just call load(packageName + “newComponent”), a

file with new properties and add to UIDefaults• Customize a particular component or panel– apply(component, packageName + “myComp”) – applyRecursively(panel, packageName + “myPanel”)

Page 46: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Painter

• Painter is the way to go• Painter could be image based or Java code

(vector)– Concrete Implementations: ColorPainter,

GradientPainter, ImagePainter– For different resolutions, load a different set of

properties files

Page 47: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

DemoLook and Feel

Page 48: THE MERGING POINT OF ANDROID AND SWING David Qiao JIDE Software, Inc.

Q & A