Tuesday Screenies

What?  You were expecting WoW screenies today?  Well, I do have one but I won’t post it right now.  I’ve been playing WoW but I haven’t done anything exciting enough to warrant a screenie.  Instead I have screenies of my other project:  To show everyone how awesome I am at making small GUI apps in Java.

The database login dialog

The database login dialog

The app is a small accounting program to help me balance the checkbook.  Right now, I use Excel and it has problems.  I’ve got the database backend set up already.  Now I’m designing the GUI forms.

Login Success

Login Success

It just uses the MySQL server to authenticate using JDBC.  As I haven’t bothered to set up users for this db yet, I’ve been using the MySQL root user to test.  I know it’s bad policy but I plan on changing the password when I’m done testing anyway.

Login Failure

Login Failure

When you type in a bad password (or username) or point it at a server or instance that doesn’t exist, you get a nice error message (and since I’m running it from within NetBeans, you get the exception details in the output window).

The login form in Sun's new Nimbus look and feel

The login form in Sun's new Nimbus look and feel

I really loved the Ocean theme for Metal when it came out.  I still do love it.  But I have to admit that Nimbus is damn cool.

Would look right nice on my Sun box

Would look right nice on my Sun box

I’ve always loved Motif and I’m very sad that Oracle is dropping it in favor of GTK.  IBM and HP are the only Motif vendors anymore.

So far all I have done is the logon box.  I can, however, show off the preferences backend:

It's a dotfile!

It's a dotfile!

The UNIX-y way of handling preferences is by using dotfiles – text files in a user’s home directory whose names begin with a . (UNIX hides dotfiles by default).  The syntax is very simple:  key=value1,value2,value3,etc.  For example, if I changed db_server=dustpuppy to db_server=dustpuppy,localhost, I would see it listed in the servers dropdown.  Similarly, if I typed in a different instance in the Schema dropdown, it would be added to the list in the prefs file.  Here’s the code:

The makeLine(int) and save() methods

The makeLine(int) and save() methods

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package checkbook;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 * Preferences File Class
 * @author Imbrius
 */
public class PrefsFile {

 private static final String PREFS_FILE = ".checkbook";
 private static File prefsFile = null;
 private static ArrayList<String> keys;
 private static ArrayList<ArrayList<String>> values;

 static {
 try {
 prefsFile = new File(PREFS_FILE);

 try {
 prefsFile.createNewFile();
 } catch (Exception ex) {
 // explicit noop
//                JOptionPane.showMessageDialog(null, ex.getMessage(),
//                        "Preferences File", JOptionPane.ERROR_MESSAGE);
 }

 keys = new ArrayList<String>();
 values = new ArrayList<ArrayList<String>>();
 FileReader fr = new FileReader(PREFS_FILE);
 BufferedReader br = new BufferedReader(fr);

 while (br.ready()) {
 ArrayList<String> valueList = new ArrayList<String>();
 String line = br.readLine();
 keys.add(line.split("=")[0]);
 line = line.split("=")[1];
 line.replace(" ", "");

 for (String value : line.split(",")) {
 valueList.add(value);
 }

 values.add(valueList);
 }

 br.close();
 fr.close();
 } catch (Exception ex) {
 // explicit noop
//            JOptionPane.showMessageDialog(null, ex.getLocalizedMessage(),
//                    "Preferences File", JOptionPane.ERROR_MESSAGE);
 }
 }

 private static int lookupKeyIndex(String key) {
 int i = 0;
 boolean found = false;

 try {

 for (String lKey : keys) {
 if (lKey.equalsIgnoreCase(key)) {
 found = true;
 break;
 }

 i++;
 }

 } catch (java.lang.NullPointerException npex) {
 // explicit noop
 }

 if (!found) {
 return -1;
 } else {
 return i;
 }
 }

 public static String[] getValues(String key) {
 int i = lookupKeyIndex(key);
 if (i == -1) {
 return null;
 } else {
 return (values.get(i).toArray(new String[1]));
 }
 }

 public static void addKey(String key, String[] lValues) {
 try {

 for (String lKey : keys) {
 if (lKey.equalsIgnoreCase(key)) {
 setValues(key, lValues);
 return;
 }
 }

 } catch (java.lang.NullPointerException npex) {
 // explicit noop
 }

 keys.add(key);
 ArrayList<String> alValues = new ArrayList<String>();

 for (String value : lValues) {
 alValues.add(value);
 }

 values.add(alValues);
 }

 public static void setValues(String key, String[] lValues) {
 ArrayList<String> alValues = new ArrayList<String>();
 int i = lookupKeyIndex(key);

 for (String lValue : lValues) {
 alValues.add(lValue);
 }

 if (i == -1) {
 addKey(key, lValues);
 } else {
 try {
 values.set(i, alValues);
 } catch (java.lang.IndexOutOfBoundsException ioobex) {
 values.add(i, alValues);
 }

 }
 }

 public static void addValue(String key, String value) {
 int i = lookupKeyIndex(key);

 if (i == -1) {
 addKey(key, new String[]{value});
 } else {
 for (String lValue : getValues(key)) {
 if (lValue.equalsIgnoreCase(value)) {
 return;
 }
 }

 values.get(i).add(value);
 }
 }

 private static String makeLine(int i) {
 String line = "";
 line += keys.get(i);
 line += "=";

 for (String value : values.get(i)) {
 line += value + ",";
 }

 line = line.substring(0, line.length() - 1);
 line += '\n';
//        JOptionPane.showMessageDialog(null, line, "Prefs File",
//                JOptionPane.INFORMATION_MESSAGE);

 return line;
 }

 public static void save() {
 try {
 FileWriter fw = new FileWriter(prefsFile);

 for (int i = 0; i < keys.size(); i++) {
 fw.write(makeLine(i));
 }

 fw.close();
 } catch (IOException ex) {
 Logger.getLogger(PrefsFile.class.getName()).log(Level.SEVERE, null, ex);
 }

 }
}


Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: