The Last Mile to Desktop Java™ Technology
Anton Kast Chief Architect Light Crafts Inc. www.lightcrafts.com
TS-3938
2007 JavaOneSM Conference | Session 3938 | Desktop Java Technology Works Great It is Mostly Possible for a Small Group to Develop Cross-platform Desktop Software Products in Java Technology
ThingsT That Work Great: h ● i AWT, Swing, SWT ● RT Framework s ● Component abstraction ● Collections
● Preferences t ● Event abstraction ● I/O (File, network) a ● Widgets l ● k The VM
i ● Exceptions
s ● Synchronization
a ● Garbage collection (if you get the policy right) b 2007 JavaOneSM Conference | Session 3938 | 2 What Is LightZone? LightZone is advanced photo editing made simple Requirements
Big image files (100MB+) Maximum Power Many image processing layers Fast rendering
Desktop integration User Focus Conformant look-and-feel Standard behaviors
This talk is about the stuff we had to add to Java technology to make this product
2007 JavaOneSM Conference | Session 3938 | 3 DEMO
Briefly see what LightZone is about
2007 JavaOneSM Conference | Session 3938 | 4 Huge Universe of Java Technology UI Kits L&F's AWT Metal “Foundation classes” Swing Windows SWT (Eclipse) Aqua Quaqua javadesktop.org GTK Swing Depot (component libraries) Winlaf Project Looking Glass Substance blogs/discussion Alloy Nimbus SwingLabs ... SwingX (widgets) Java technology-based Desktop Integration Components
Toolkits is a vigorous specialty, and it is expanding Leaders and experts may be seated near you
2007 JavaOneSM Conference | Session 3938 | 5 Deployment How to get the app running in the user’s environment?
Windows Macintosh
● Can’t assume Java technology is ● Java technology is always present, so present, so we can’t control the Applet WebStart Java technology version (Argh!)
● “DMG” A file system image holds an Deploy Java technology itself “application bundle,” including all icons and desktop logic
● Desktop icon, document associations, controlled launching
INSTALLER
NSIS/InstallShield is popular, but we like Install4J
2007 JavaOneSM Conference | Session 3938 | 6 Launching
Absolutely Critical:
● Heap Limit (“-Xmx*M”)
● In general, this must depend on physical memory
● Desktop Events
● Handle drag-and-drop onto desktop icons, taskbar or dock
● Single Instance
● Prevent two running copies of the app
Need a native app, just to launch the Java Virtual Machine (JVM™)
The terms “Java Virtual Machine” and “JVM” mean a Virtual Machine for the Java™ platform. 2007 JavaOneSM Conference | Session 3938 | 7 Java Technology’s 2GB Heap Limit Actually, it’s less (~1.5GB on Windows)
We made a native allocator of ByteBuffers, and we swap image data to it:
// Allocate a chunk outisde the Java heap. for instance, native long alloc(int size); “return new char[size];” // Free a chunk outside the Java heap. native void free(long addr, int size); for instance, “delete[] addr;” // Construct a ByteBuffer inside a chunk. native ByteBuffer getNativeByteBuffer(long addr, int size); // JNI to instantiate the ByteBuffer: JNIEXPORT jobject JNICALL getNativeByteBuffer( JNIEnv *env, jclass, jlong jAddr, jint jSize ) { void *addr = reinterpret_cast
2007 JavaOneSM Conference | Session 3938 | 8 File Choosers Users spend a lot of time with these: Shortcuts Search Preview Forward/back Native Shortcuts
(Key Bindings) Solutions:
●Another L&F (Quaqua, Winlaf) Better, but still not authentic
●java.awt.FileDialog Native, but restrictive API
●Native code
JFileChooser The cadillac of file choosers
2007 JavaOneSM Conference | Session 3938 | 9 File System Presentation LightZone has a folder tree Windows Macintosh
● These common displays require lots of trickery in Java:
● Icons: (Windows: native code; Mac: Quaqua L&F)
● Hidden folders: (C:\WINDOWS; /bin /etc /usr etc.)
● Shortcuts/Aliases: (Native code in all cases)
● Java technology-based Swing provides some help: javax.swing.filechooser.FileSystemView
2007 JavaOneSM Conference | Session 3938 | 10 Licensing License enforcement in Java technology is hard because bytecode is readable Story of Cracking LightZone
● We wanted hard licensing
● 30-day trials, credit cards, copy-paste your key, etc.
● We implemented this in native code, for obscurity
● Within two months of release, a hacked version was on bittorrent
● Our mistake: the license check was called from Java technology
● Someone just decompiled the bytecode and bypassed the license check Two Solutions Cool, but a hassle if you like stack traces
● Obfuscation—See ProGuard More native code
● Digital signing—Sign your jars, obfuscate the verification
2007 JavaOneSM Conference | Session 3938 | 11 A Word on Desktop GC The word is: -XX:+UseParallelGC We’ve had problems: 1.GC fails to keep up with the user 2.GC blocks the app intermittently
● (Maybe server GC gets more love)
● Parallel GC smoothed the ride in LightZone
● Treatise on GC configuration at http://java.sun.com
2007 JavaOneSM Conference | Session 3938 | 12 Standard APIs We Replaced We had some specific requirements, mostly related to image processing
Printing
● Needed sticky printer options, reliable imageable areas ImageIO
● Needed faster image I/O and full image metadata
● Didn’t need diverse image formats (Just TIFF, JPEG, and RAW) Color Management
● Needed thread-safe color space conversions, to run on Java Advanced Imaging (JAI) API tile threads JavaHelp™ Tool
● JavaHelp tool’s searching, indexing, and UI coupling are all great
● But its HTML renderer is nonstandard
2007 JavaOneSM Conference | Session 3938 | 13 Another Standard API We Replaced JOptionPane
● Mac alert dialogs are a joy to use:
App icon gives context Simple question in bold
Destructive option Boring explanation separated on left in fine print
(Accelerator keys work) Default option throbs
● The Quaqua Swing L&F does a good job with this on Mac
● On Windows, JOptionPane can be warped to a close imitation
default JOptionPane.showConfirmDialog()
2007 JavaOneSM Conference | Session 3938 | 14 More Native Tips and Tricks
● Discover “My Documents” on Windows
● It’s localized
● Find the color profile of a display device
● Resolve file “shortcuts” and “aliases”
● We cash file data, so we need a truly unique path for every file (File.getCanonicalPath() won’t do)
● Bring a Window to the front
● Evil?
2007 JavaOneSM Conference | Session 3938 | 15 Mac-Specific Tips and Tricks
● Screen menu bar (“-Dapple.laf.useScreenMenuBar=true”)
● Application menu
● Use a native launcher to control the menu name
● About, Preferences, Quit items: try MRJAdapter at java.net
● Sheets
● Someone please try this with the new Java Platform v.6 frame “modalities”
● Apple’s specialized system properties
● (showGrowBox, fileDialogForDirectories, brushMetalLook, etc.)
● Use Quaqua, not Aqua
2007 JavaOneSM Conference | Session 3938 | 16 Why Was LightZone Written in Java Technology? Because we find Java technology to be a fun and productive framework
● Almost all the code is cross-platform, including all the GUI
● JAI API works great
● We can easily integrate third-party libraries when needed
● We can easily integrate native code when needed
2007 JavaOneSM Conference | Session 3938 | 17 For More Information
GC configuration in Sun Vms:
● http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html Mac system properties:
● http://developer.apple.com/documentation/Java/Conceptual/ JavaPropVMInfoRef Swing Toolkits:
● http://community.java.net/javadesktop
● http://swinglabs.org Apple’s MRJAdapter
● https://mrjadapter.dev.java.net EJ-Technologies “install4j” Java installer
● http://www.ej-technologies.com
2007 JavaOneSM Conference | Session 3938 | 18 Q&A
Anton Kast Chief Architect Light Crafts Inc. www.lightcrafts.com
2007 JavaOneSM Conference | Session 3938 | 19 The Last Mile to Desktop Java™ Technology
Anton Kast Chief Architect Light Crafts Inc. www.lightcrafts.com
TS-3938
2007 JavaOneSM Conference | Session 3938 |