Home > Java, NB Platform, NetBeans > Simple Validation API & DialogDescriptor

Simple Validation API & DialogDescriptor

This post shows you how the Simple Validation API (HowTo, Project@Kenai) can be used together with a NetBeans Platform standard dialog (using a DialogDescriptor).

The Simple Validation API gives you a convenience method to show a dialog like (excerpt from the BasicDemo):

JPanel panel = new JPanel();
... // setup panel

ValidationPanel vp = new ValidationPanel();
vp.setInnerComponent(panel);
... // setup validation

// Convenience method to show a simple dialog
if (vp.showOkCancelDialog("URL")) {
   System.exit(0);
} else {
   System.exit(1);
}

To use a standard NetBeans Platform dialog, the sample above can be replaced with:

... // as above
DialogDescriptor dd = ValidationUtil.createDialogDescriptor(vp, panel, "URL");
Object ret = DialogDisplayer.getDefault().notify(dd);
if (DialogDescriptor.OK_OPTION.equals(ret)) {
   System.exit(0);
} else {
   System.exit(1);
}
public class ValidationUtil {

   private ValidationUtil() {
      // default constructor suppressed for non-instantiability
   }

   public static DialogDescriptor createDialogDescriptor(ValidationPanel vp, Object innerPane, String title) {
      final DialogDescriptor dd = new DialogDescriptor(innerPane, title);

      ValidationUI okButtonEnabler = new ValidationUI() {
         private NotificationLineSupport nls = dd.createNotificationLineSupport();

         public void showProblem(Problem problem) {
            if (problem != null) {
               switch (problem.severity()) {
                  case FATAL:
                     nls.setErrorMessage(problem.getMessage());
                     dd.setValid(false);
                     break;
                  case WARNING:
                     nls.setWarningMessage(problem.getMessage());
                     dd.setValid(true);
                     break;
                  default:
                     nls.setInformationMessage(problem.getMessage());
                     dd.setValid(true);
               }
            } else {
               nls.clearMessages();
               dd.setValid(true);
            }
         }

         public void clearProblem() {
            showProblem(null);
         }
      };

      vp.getValidationGroup().addUI(okButtonEnabler);
      vp.getValidationGroup().performValidation();
      return dd;
   }
}
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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: