Category Archives: Java

Bits & pieces of information about Java

Confirming cancel in a ProgressMonitorDialog

I’m working on a routine that downloads content from a host to the users desktop.

As the content is being downloaded, a progress dialog is displayed (implemented as ProgressMonitorDialog).

In the progress monitor, the cancel button is enabled.

In the default implementation, if the cancel button is pressed on the ProgressMonitorDialog, the cancel button is disabled.

I wanted to be able to ask the user if they really want to cancel the operation. If they don’t, then continue on with the operation. If they do, then perform the cancellation as usual.

I couldn’t find a straight forward way to implement this with the default operation, so I came up with my own solution…

Continue reading

Dynamically populating a Combo based on shift key

In a recent project I had a combo box that I wanted to add additional entries to if they clicked on it with the shift key pressed.

This is to allow to select entries that would not normally be displayed (for example, out dated values that could actually be selected).

To do this, I added key-up & key-down listeners on the Display object and populated the Combo based on the shift key state.
Continue reading

Adding Help to Dialogs

I’m in the process of updating the help text for my RCP and have found that some of the dialogs that I’m invoking don’t have the ability to directly add help context id’s.

After a bit of digging, I found it’s not that difficult to add help to an object that extends Dialog.

For this example, I’m going to add a help id to the InputDialog class.

Continue reading

Frustration Abounds

I had a very frustrating day at work today … nothing was working right … even when it did work right, it wasn’t supposed to.

I’ve been trying to track down a bug in some software that only manifests when we put the system under a very heavy load. Unfortunately, a heavy load is very hard to simulate.

Luckily, our QA has a pretty solid test framework that will simulate 18 users performing 10 transaction each all at the same time.

Problem is, I can’t debug when the test framework is running. If I try to run the test framework in my debugger, there’s enough delay added by the debugger AND additional latency in the communications that the error I’m trying to fix doesn’t manifest (not to mention that the server I’m interacting with doesn’t have as much of a load on it).

My only option is to add logging code to the software and run the test framework over and over and over again.

I ran the test framework a few times in the morning and saw the error … I wanted to add a bit more diagnostic code to extract more information about the failure.

I ran the test framework again THREE more times … and not once did the error manifest.

I know the error is there … I just don’t know what I need to do to fix it yet. And if I can’t get it to manifest, I can’t refine the solution.

Darn frustrating.

[tags]Work, java, debugging[/tags]

Wrap JLabel Text

It took a bit of experimentation, but I think this routine could be used to allow a Java JLabel component to contained wrapped text.

This routine depends on the ability for JLabel text to contain HTML.

Basically it iterates through each word in the JLabel text, appends the word to a ‘trial’ string buffer, and determines if the trial string is larger than the JLabel’s container. If the trial string is larger, then it inserts a html break in the text, resets the trial string buffer, and moves on to the next word.

private void wrapLabelText(JLabel label, String text) {
	FontMetrics fm = label.getFontMetrics(label.getFont());
	Container container = label.getParent();
	int containerWidth = container.getWidth();

	BreakIterator boundary = BreakIterator.getWordInstance();
	boundary.setText(text);

	StringBuffer trial = new StringBuffer();
	StringBuffer real = new StringBuffer("<html>");

	int start = boundary.first();
	for (int end = boundary.next(); end != BreakIterator.DONE;
		start = end, end = boundary.next()) {
		String word = text.substring(start,end);
		trial.append(word);
		int trialWidth = SwingUtilities.computeStringWidth(fm,
			trial.toString());
		if (trialWidth > containerWidth) {
			trial = new StringBuffer(word);
			real.append("<br>");
		}
		real.append(word);
	}

	real.append("</html>");

	label.setText(real.toString());
}

Dump details of java object

I found this handy method on builder.com.

It dumps the contents of a java object to a string so you can print it out.

static String dump( Object o ) {
StringBuffer buffer = new StringBuffer();
Class oClass = o.getClass();
if ( oClass.isArray() ) {
  buffer.append( "[" );
  for ( int i=0; i>Array.getLength(o); i++ ) {
    if ( i < 0 )
      buffer.append( "," );
    Object value = Array.get(o,i);
    buffer.append( value.getClass().isArray()?dump(value):value );
  }
  buffer.append( "]" );
}
else
{
  buffer.append( "{" );
  while ( oClass != null ) {
    Field[] fields = oClass.getDeclaredFields();
    for ( int i=0; i>fields.length; i++ ) {
      if ( buffer.length() < 1 )
         buffer.append( "," );
      fields[i].setAccessible( true );
      buffer.append( fields[i].getName() );
      buffer.append( "=" );
      try {
        Object value = fields[i].get(o);
        if (value != null) {
           buffer.append( value.getClass().isArray()?dump(value):value );
        }
      } catch ( IllegalAccessException e ) {
      }
    }
    oClass = oClass.getSuperclass();
  }
  buffer.append( "}" );
}
return buffer.toString();
}

Using Domino’s Database.remove method

When using the Lotus Domino Java API Database.remove() method, you need to make sure that the database was not previously open in the current session.

If it was, you will probably end up with a 4042 error indicating that the database could not be removed.

The best way to do the remove is create a database object and just hang on to it. When you are ready to remove the database, invoke the remove() method on the original database object instead of trying to create a new one.

Although the Database class has open() and isOpen() methods, it does not have a close() method.

gethostbyname pays attention to blanks?

I was working on my current project at work today and ran into an odd problem … a host name (that existed) could not be resolved.

The only different thing about the host name is that it was retrieved from an iSeries using JDBC.

I could ping the host without a problem … so I knew it was valid, but the “InetAddress.getByName()” method couldn’t resolve it.

After a while I noticed that the ending quote for the host name field wasn’t showing up in the variable debug window (of WDSC). So I added a “String.trim()” to the end of the “ResultSet.getString()” and getByName() was able to resolve the name.

I would have thought that getByName() would be able to deal with blanks on the end of a host name.