The Source for Java Technology Collaboration
User: Password:



Evan Summers's Blog

April 2007 Archives


Splash Screen

Posted by evanx on April 25, 2007 at 01:44 AM | Permalink | Comments (0)

If it's gonna take a while to start up our Swing app, then we probably wanna display a splash screen.

It's easy with Java6 - just add a -splash command-line option.



Code Snippet

We initialise a SplashScreen instance and get its Graphics2D as follows.

public class SplashTest {
    static Logger logger = Logger.getLogger(SplashTest.class.getSimpleName());
    final SplashScreen splash = SplashScreen.getSplashScreen();
    Rectangle splashBounds;
    Graphics2D splashGraphics;
    
    protected void initSplash() throws Exception {
        if (splash == null) {
            throw new Exception("no splash image specified eg. -splash:mysplash.png");
        }
        splashBounds = splash.getBounds();
        splashGraphics = (Graphics2D) splash.createGraphics();
        ...
    }
    
    protected void updateSplash(String status, int progress) {
        if (splash == null) return;
        drawSplash(splashGraphics, status, progress);
        splash.update();
    }
    ...
}    

where in drawSplash() we draw a progress bar ourselves over the splash image using the Graphics2D instance.




Encryptic

Posted by evanx on April 19, 2007 at 04:17 AM | Permalink | Comments (0)

Earlier we considered using straight Base-64 for hiding sensitive data from prying eyes.

Let's take it a step further and actually encrypt the data using a known password.


Code Snippet

We implement the following class to support password-based encryption using the DES algorithm.

public class PBECipher {
    private static final String pbeAlgorithm = "PBEWithMD5AndDES";
    private static final String defaultPassword = "ssh ssh!";
    ...
    public PBECipher() {
        this(defaultPassword);
    }
    
    public PBECipher(String password) {
        try {
            parameterSpec = new PBEParameterSpec(salt, iterationCount);
            secretKey = createSecretKey(password);
            encryptCipher = createEncryptCipher();
            decryptCipher = createDecryptCipher();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    ...
    public String decrypt(String string) {
        try {
            return new String(decryptCipher.doFinal(Base64.decode(string)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }    
}    

where we instantiate a PBEParameterSpec with an arbitrary 8-byte salt and iterationCount.




Embedded Text via XML Annotations

Posted by evanx on April 16, 2007 at 05:08 AM | Permalink | Comments (8)

We hear talk of introducing "native XML" into Java7. I'm not sure what form that might take?

Embedding HTML and SQL (and JavaScript et al) "text" into Java is cumbersome eg. using multi-line concatenated string literals. And this doesn't lend itself towards leveraging IDEs eg. for auto-completion, error-detection and syntax-highlighting. What might work for me is allowing "annotations" that are correctly formed multi-line XML text, as a mechanism to embed "structured" text into Java code in a way that is easy to type (and cut and paste) and easy to read (especially if the IDE does some syntax highlighting).

Then i could use these annotations for HTML snippets as follows.

public class HTMLBuilder {
   ...
   <html xmlns="html" id="sectionHeadingDiv">
      <div class="sectionHeadingStyle" />
   </html>
   public String getSectionHeadingHTML(String heading) {
      HTML snippet = getAnnotationHTML("sectionHeadingDiv");
      snippet.getChildElement("div").setText(heading);
      return snippet.toString();
   }
}

Also for SQL...

public class PersonDao {
   ...
   <sql xmlns="sql" id="selectPersonByPersonId">
      <query>
         select * from person 
         where person_id = :personId
      </query>
   </sql>
   public Person getPerson(String personId) throws SQLException {
      SQL query = getAnnotationSQL("selectPersonByPersonId");
      query.setParameter("personId", personId);
      return (Person) query.fetch(Person.class);
   }
}

and also other any other cases where i might wanna embed multi-line text into a Java class, eg. maybe some dynamic scripting stuff.

The compiler might just treat these as String annotations, and an Ant task might verify the XML at compile time, and IDE's could hopefully join in the fun as well (with auto-completion, highlighting, verification). Waddaya think?



Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds