Getting to Know ABook You've used NetBeans to analyze the source code for the Address Book application over the past couple of months. It is now time to actually look and see what the application can do. Since a database comes integrated with the 1.6 runtime, you don't have to configure any MySQL or ODBC target to get started. Just run the application. Download Address Book in .zip format. Once you unpack the file, the ABook.jar file is found in dist subdirectory of the Abook.zip file. Run it with the -jar option to the java command: > java -jar ABook.jar This will show a brief splash screen: Figure 1. Splash Screen | Before going to the main application screen: Figure 2. Main Application Screen | The image to use for the splash screen is specified by including a line in the manifest file: SplashScreen-Image: abook/images/abook-splash.gif The application looks like a typical, though fairly basic, address book manager. You can add, edit, and delete contacts, as well as filter the contact list. You may also notice an icon on the status tray when the application is running. Right clicking on the status tray icon will bring up its popup menu, with an Exit option on it. The code for this is found in the ContactSystemTray class. Here is what the key parts of the class looks like, setting up the tray and menu. SystemTray tray = SystemTray.getSystemTray(); URL url = ContactList.class.getResource("images/tray.gif"); Image image = Toolkit.getDefaultToolkit().getImage(url); ActionListener exitListener = new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Exiting..."); System.exit(0); } }; PopupMenu popup = new PopupMenu(); MenuItem defaultItem = new MenuItem("Exit"); defaultItem.addActionListener(exitListener); popup.add(defaultItem); TrayIcon trayIcon = new TrayIcon(image, "Address Book", popup); tray.add(trayIcon); Pressing the New Contact button will bring up the "Email Contacts" screen, for entry of a new person with potentially three tabs worth of associated information, including a picture. Figure 3. Email Contacts | Pressing the Add button brings up a JFileDialog, prompting to enter a GIF, JPG, or PNG image, smaller than 64K. The preview panel only shows a small portion of the image, so if you're face shot happens to be too big, the image isn't scaled. Figure 4. Contact Photo | Saving of the image is done via a Blob column in the database. The getPicture() method of the Contact record returns a byte array, which is converted to a ByteArrayInputStream for storing as the blob. if(record.getPicture() != null){ ByteArrayInputStream pictureByteStrm = new ByteArrayInputStream(record.getPicture()); stmtUpdateExistingRecord.setBlob(18, pictureByteStrm, record.getPicture().length); pictureByteStrm.close(); } else { stmtUpdateExistingRecord.setBlob(18, null, 0); } If you're wondering where the database is stored between runs, the program keeps the database files in the .addressbook subdirectory of your home directory, the user.home system property. See the setDBSystemDir() method of the AddressBookDao and ContactDao classes for where that is configured: private void setDBSystemDir() { // decide on the db system directory String userHomeDir = System.getProperty("user.home", "."); String systemDir = userHomeDir + "/.addressbook"; System.setProperty("derby.system.home", systemDir); // create the db system directory File fileSystemDir = new File(systemDir); fileSystemDir.mkdir(); } Derby is the name of the Apache project which provided the basis for the Java DB in Java SE 6. One last feature worth highlighting is related to the email, website and notes fields. If you look at all the data for a specific contact, you'll notice these three fields are in bold on the data tab. Figure 5. The Data Tab | The reason behind that is you can click on the data in the form and the application associated with the data will be launched. Click on the email address to start writing an email to the person. Clicking on the Website text will launch the browser, with the Notes text opening you're local text editor. Here's the ContactInfoPanel method code behind the mailing operation: private void openMailTo(String mailURI) { if(!Desktop.isDesktopSupported()){ System.out.println("Class java.awt.Desktop is not supported on " + "current platform. Farther testing will not be performed"); System.exit(0); } Desktop desktop = Desktop.getDesktop(); if (!desktop.isSupported(Desktop.Action.MAIL)) { System.out.println("Desktop does not support the action of MAIL."); System.exit(0); } /* * launch the mail composing window without a mailto URI. */ try { System.out.println("Testing desktop.mail()"); URI mailToURI = new URI("mailto:" + mailURI); desktop.mail(mailToURI); } catch (IOException e) { System.err.println("EXCEPTION: " + e.getMessage()); e.printStackTrace(); } catch (URISyntaxException uriEx) { System.err.println("URISyntaxException: " + uriEx.getMessage()); uriEx.printStackTrace(); } } The method first checks to see if the Desktop concept is supported on your platform before checking if the MAIL action is supported. It then creates a mailto: string to eventually be used to start up your email client. Similar code is behind the other two operations. There is much more going on here with the address book application. You'll be looking at more over the upcoming weeks. This was a quick tour of the application and helps you to understand some of the things revealed by the prior source code analysis. Do try out the application some more to get a better feel for its feature set. |