gamelan
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Compare Prices
Car Donations
Best Price
Promotional Pens
Memory
Hurricane Shutters
Shop
Home Improvement
Corporate Awards
Memory Upgrades
Build a Server Rack
Rackmount LCD Monitor
Compare Prices
PDA Phones & Cases

 
 
Search
 



Storage Networking , Part 1
eBook: A storage network is any network that's designed to transport block-level storage protocols. But understanding the ins and outs of networked storage takes you deep into several of protocols. This guide covers SANs, Fibre Channels, Disk Arrays, Fabric, and IP Storage. »

Storage Networking 2, Configuration and Planning
eBook: Picking up where Part 1 left off, Part 2 of our look at storage networking examines configurations for SAN-attached servers and disk arrays, and also includes a look at the future of IP storage. »

Storage Management Costs in the Enterprise: A Comparison of Mid-Range Array Solutions
Whitepaper: Many factors contribute to the ownership cost for enterprise storage. These include (but are not limited to): physical capacity relative to physical space requirements, performance capacity for data transfer and system reaction time, software maintenance and updates, expandability and flexibility, and much more. »

Storage Is Changing Fast  Be Ready or Be Left Behind
PDF: The storage landscape is headed for dramatic change, thanks to new technologies like Fibre Channel over Ethernet (FCoE), pNFS, object-based storage and SAS that will affect everything from NAS and SANs to disk drives. Get the knowledge you need to make the most of your storage environment, now and in the future. »

HP StorageWorks EVA4400
Demo: Dont settle for an expensive and complex array that lacks functionality. The HP StorageWorks EVA4400 delivers virtual storage with enterprise class functionality at an affordable price. »

Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Project Management Guide: Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.

Switching to the Mac for the Java Developer, Part 2
By Benoît Marchal

This series discusses techniques to package Java applications for Mac OS X. Thanks to an excellent Java VM, pure Java applications run immediately on the Mac. Still it's a very polished machine and users have high expectations. The series discusses simple steps you should take to make your Java application more Mac friendly. The series will be useful to Java developers who want to write more portable applications.

Menus and the Mac

In the previous article, you learned about differences between the Mac user interface (Aqua) and other GUIs such as Windows/KDE/Gnome. You also learned how to package JAR files as application bundles. Application bundles replace the Start menu of other platforms, as well as the installer for most applications.

The next issue to consider when fine-tuning a Java application for Aqua is the menu bar. Aqua has a single menu bar that always appears at the top of the screen. It is shared between the applications and the system. On most other operating systems, individual windows have their own menus. You see the difference when you compare figure 1, a Windows screenshot, with figure 2, the Mac OS equivalent.



Figure 1: Under Windows, individual windows have their own menus.


Figure 2: Under Mac OS, the menu is shared between the applications and the system.

Finally look at the application menu. It appears on the right of the Apple menu and is labelled with the application name. Every application has one. The system controls half of the entries and your application the other half. On figure 3, entries for the application are highlighted.


Figure 3: The application is managed by the system and the application.

Support the Menu Bar

Porting to Mac with AWT is easy. Since AWT uses native controls, it automatically supports the Aqua bar. Although, the setMenuBar() method is defined on the Frame object, the menus are regular Mac menus.

Swing is less convenient. Since Swing draws the controls in Java, it attaches menus to windows in a non-portable way. To use the Aqua menu with Swing applications, you need to set the com.apple.macos.useScreenMenuBar system property to true. The following code excerpt demonstrates it:

System.setProperty("com.apple.macos.useScreenMenuBar","true");
int keyModifier = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JMenuItem fileNew = new JMenuItem("New");
fileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,keyModifier));
JMenu fileMenu = new JMenu("File");
fileMenu.add(fileNew);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
menuBar.add(file);

Although it sets an Apple-specific property, this code remains portable. The property is silently ignored on non-Apple platforms. Alternatively, you can set the property from the command-line with -D option:

java -Dcom.apple.macos.useScreenMenuBar=true -jar menu.jar

or using the MRJAppBuilder introduced in the previous article.

This example also demonstrates the use of getMenuShortcutKeyMask(). To activate menu shortcuts, Macs use the Command key, other platform use the Alt or the Control key. getMenuShortcutKeyMask(), which is part of the JDK, returns the proper key for the current platform. It's good practice to use en Java.

Support the Application Menu

As you have seen, it's easy to use the Aqua menu bar even with Swing. Alas supporting the application menu is more work. The application menu is very specific to Aqua and there is zero support for it in the JDK. We will have to turn to extensions that Apple ships with its JDK.

The application menu has three items that may interested your application: About, Preferences (also known as Options on some platforms) and Quit. Care must be taken not to duplicate those in the regular menus. Figure 4 illustrates what you should avoid: this application has Quit and Preferences in its regular menus whereas the user will look for them under the application menu. In practice your application should test whether it runs on a Mac and registers with the application menu accordingly.
Figure 4: Avoid duplicating menus.

The following code demonstrates how to register for the Quit menu:

import com.apple.mrj.*;

// ... some code deleted

if(System.getProperty("mrj.version") == null)
{
   fileMenu.addSeparator();
   fileQuit = new JMenuItem("Quit");
   fileQuit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,keyModifier));
   fileQuit.addActionListener(new ActionListener()
   {
      public void actionPerformed(ActionEvent e)
      {
         if(promptTheUser())
            System.exit(0);
      }
   });
   fileMenu.add(fileQuit);
}
else
{
   MRJApplicationUtils.registerQuitHandler(new MRJQuitHandler()
   {
      public void handleQuit()
      {
         SwingUtilities.invokeLater(new Runnable()
         {
            public void run()
            {
               if(promptTheUser())
                  System.exit(0);
            }
         });
         throw new IllegalStateException("Stop Pending User Confirmation");
      }
   });
}

Let's review the code step by step:

  1. 1. it tests the platform. The mrj.version property is always defined on a Mac (MRJ stands for MRJ stands for "Mac OS Runtime for Java");
  2. 2. if not running on a Mac, it register the quit menu as a regular menu item;
  3. 3. if running on Mac, it registers an MRJQuitHandler with the application menu.

    Aqua will call handleQuit() when the users selects the Quit menu. An additional annoyance is that it is not safe to call AWT or Swing methods from the handler itself, you have to run user interface code in a different thread. The easiest workaround is to use SwingUtilities.invokeLater() (or EventQueue.invokeLater()) to return to the the user interface thread.

    Finally handleQuit throws an IllegalStateException exception. There is a technical note from Apple on this topic. In a nutshell, Quit attempts to terminate the application unless the handler throws an IllegalStateException exception.

    In the com.apple.mrj package, you will find two additional interface for the Preferences menu (MRJPrefsHandler) and the About menu (MRJAboutHandler). They do not differ much from MRJQuitHandler and should be self-explanatory.

    Towards More Portable Code

    Unfortunately to support the application menu, you have to turn to Apple extensions. Which means that, when deploying on other platforms, the VM needs access to the Apple class definitions to load your application. Since it's unlikely Sun will include Apple extensions in the standard JDK, you will want to add a copy of MRJToolkitStubs.zip to the classpath. You don't need this file on the Mac, of course.

    An alternative is to organize your code so that only Mac-specific classes use the com.apple.mrj package. If you are careful not to load these classes on other platforms, you don't need to ship MRJToolkitStubs.zip with your application.

    I have found it convenient to write a small library that abstract the application menu in a more portable way and takes care of loading the MRJ extensions. The library is available at ananas.org/mac under an open source license. You may want to grab a copy.

    Take Another Byte

    Although pure Java applications run on the Mac without changes, you can make a few adjustements so that they run more smoothly. In the previous article, you learned how to package a Java application. In this article, you learned how to use the Aqua menus.

    Benoît Marchal is a Belgian developer. He is the author of XML by Example and other XML books. He was originally attracted to Mac OS X for digital photography but he has learned to enjoy the robust Java implementation. Benoît is available to help you with your projects.


    Tools:
    Add www.developer.com to your favorites
    Add www.developer.com to your browser search box
    IE 7 | Firefox 2.0 | Firefox 1.5.x
    Receive news via our XML/RSS feed


    Other Java Archives

    Five Trends for Application Development & Program Management. Download Complimentary Report Now.
    Generate Complete .NET Web Apps in Minutes . Download Iron Speed Designer today.
    Guide to Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.
    Best Practices for Developing a Web Site. Checklists, Tips & Strategies. Download Exclusive eBook Now.
    Whitepaper: Embeddable Content Platform for OEM's



  4. JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES