Friday, August 22, 2014

Get Involved in Your Kid's School

My wife and I just signed our oldest daughter up for Kindergarten this year.  We choose a local private school only because we really like the curriculum and since that's where she went to preschool, we already know and love the teachers there.  It is a good fit for us and my daughter gets to keep some of the same friends that she has been around for the last couple years.  However, It wasn't until we signed her up that I realized that they required parents to volunteer within the school.

What!?  Me volunteer?

I think not.  I have things to do, work, a life...

It wasn't until I started thinking about it that I realized what a wonderful idea this was. My wife is a middle school teacher and she tells me these stories of kids and their parents, and how hard it is to contact the parents in times that their kids really need them.  During most parent-teacher conferences it usually ends up with 80% of the parents being a no-show.  Where are the parents?  Don't we care about our kids' education?  We should.



I can't explain the rationale for previous generations but my generation needs to get off there butts and attend a school function.  Be active in your child's life.  There is a saying that I've heard often:
"Be here now."
To me it means to be active in the present.  Stop daydreaming and pay attention to what is going on around you.  Be observant of your surroundings.  At this point we've lived through some troubling times;  the decline in the economy in 2008, a high unemployment rate, an ever increasing national debt, a Congress that can't work together, and the list goes on.  How can we expect our children to make good choices in their future if they are uneducated?  We must be active parents and stop being lazy.

Originally I thought that the idea of volunteering was burdensome, but I realize the value of participation.  How would you have felt if your parents participated a little more while you were growing up?  By helping out we can improve the quality of our local education systems, reduce classroom violence, and make a positive influence on our children's quality of life.

NHibernate 4.0 Released

Just to let everyone know that NHibernate 4.0 is out!

Get it here:
http://sourceforge.net/projects/nhibernate/?source=directory


Creating a Blank Report in iReport Designer 5.6.0

I recently set out to create a blank report using the Jaspersoft iReport Designer 5.6.0. When I googled for articles trying to find out how to do this (because it wasn't totally obvious to me from the app) I didn't find any good articles explaining this. Moreover, my question posed on Stack Overflow didn't have very many responses. But thanks to Mike Noland for suggesting I use an empty datasource to configure my report, because that's what set me on the path to find what worked.



To set this up, go to the Report Datasources and click the New button.  This will bring up the following dialog.


Select 'Empty data source' and next.  Then choose to add one empty row and set it as your default data source.  Now if you click the Preview button you'll see all your text but with empty data.  This comes in really handy.  Now all you have to do is remove any parameters you might have so your user doesn't get prompted.  That's assuming you're using a report that originally asked for them.

This was much easier to implement then I was originally making it out to be but I thought I'd help the next guy out.  :)

Saturday, May 4, 2013

Printing the Contents of a JTextArea

Last week I needed to print the contents of a JTextArea.  When I looked online for examples it was scarce and what I thought would be an easy task (like 5 minutes) turned into a 3 hour event.

There were a few reasons for this.  The first is that there were examples, but they were all dated. They were created for Java 1.4 and who still develops with Java 1.5 or older?  Secondly, some of the examples, particularly on Stack Overflow, took you down the path of using Graphics2D which isn't what I wanted since it only printed the displayable portion of your JTextArea.

Then I found the TextAreaPrintingDemo on the Java Tutorials site.  It helped out a lot.  Here's my final result.

private class PrintingTask extends SwingWorker<Object, Object> {

    private final MessageFormat headerFormat;
    private final MessageFormat footerFormat;
    private final boolean interactive;
    private volatile boolean complete = false;
    private volatile String message;

    public PrintingTask(MessageFormat header, MessageFormat footer,
            boolean interactive) {
        this.headerFormat = header;
        this.footerFormat = footer;
        this.interactive = interactive;
    }

    @Override
    protected Object doInBackground() {
        try {
            reportTextPane.setFont(printableFont);
            PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
            attributes.add(new MediaPrintableArea(0.5f, 0.5f, 7.5f, 10.0f, MediaPrintableArea.INCH));
            complete = reportTextPane.print(headerFormat, footerFormat,
                    true, null, attributes, interactive);
            reportTextPane.setFont(viewableFont);
            message = "Printing " + (complete ? "complete" : "canceled");
        } catch (PrinterException ex) {
            message = "Sorry, a printer error occurred";
        } catch (SecurityException ex) {
            message = "Sorry, cannot access the printer due to security reasons";
        }
        return null;
    }

    @Override
    protected void done() {
        message(!complete, message);
    }
}

private void message(boolean error, String msg) {
    int type = (error ? JOptionPane.ERROR_MESSAGE
            : JOptionPane.INFORMATION_MESSAGE);
    JOptionPane.showMessageDialog(this, msg, "Printing", type);
}

private void error(String msg) {
    message(true, msg);
}

public void print() {
    MessageFormat header = new MessageFormat("This is the Title");
    MessageFormat footer = new MessageFormat("Page {0}");
    boolean interactive = true;
    boolean background = true;
    PrintingTask task = new PrintingTask(header, footer, interactive);
    if (background) {
        task.execute();
    } else {
        task.run();
    }
}
I did find that once I started printing that the text size didn't render on the printer the same as viewed in the JTextArea. A hack I did to work around this was to set the font smaller (10) and return it to the original size (12) when it was finished. I'd like a cleaner method for rendering the print size a bit better. Any suggestions?

Monday, March 25, 2013

Test Driven Development in C


Found a good slide-show of Test Driven Development in C that doesn't use any heavy-weight libraries and figured I'd share...


Wednesday, March 20, 2013

RC2 Encryption in C

I recently had to implement a the RC2 Rivest (Block) Cipher in C. I didn't find too many examples so I figured I'd post mine here. This is a 64-bit block cipher with a variable key size.

This example uses the OpenSSL library.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "openssl/rc2.h"

#define BITS 64

UBYTE *rc2_encrypt(const UBYTE *key,  UINT32 key_size,
       const UBYTE *data, UINT64 data_size)
{
    RC2_KEY rc2_ks;
    UBYTE *output;
    UBYTE iv[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

    if (key_size % 8 != 0)
    {
        printf("RC2 key size should be a multiple of 8!\n");
        return NULL;
    }

    output = (UBYTE *) malloc(data_size);
    if (output == NULL)
    {
        printf("Out of memory!\n");
        return NULL;
    }

    RC2_set_key(&rc2_ks, key_size, key, BITS);
    RC2_cbc_encrypt(data, output, data_size, &rc2_ks, iv, RC2_ENCRYPT);

    return output;
}

UBYTE *rc2_decrypt(const UBYTE *key,  UINT32 key_size,
       const UBYTE *data, UINT64 data_size)
{
    RC2_KEY rc2_ks;
    UBYTE *output;
    UBYTE iv[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

    if (key_size % 8 != 0)
    {
        printf("RC2 key size should be a multiple of 8!\n");
        return NULL;
    }

    output = (UBYTE *) malloc(data_size);
    if (output == NULL)
    {
        printf("Out of memory!\n");
        return NULL;
    }

    RC2_set_key(&rc2_ks, key_size, key, BITS);
    RC2_cbc_encrypt(data, output, data_size, &rc2_ks, iv, RC2_DECRYPT);

    return output;
}

Cheers!

Cherry Blossoms Bloom Dates Have Changed

Washington DC in beautiful in the spring.  Usually come the beginning of April I look forward to taking my wife and daughter to see the cherry blossoms.  We were excited to go this Saturday because they were expecting an early bloom.  But I just found out that they expect the bloom period to be April 3-6.  I guess we'll have to wait a couple weeks. :(