SimpleGUIBasedParser

Simple GUI-based Parser using StringTokenizer

Contributed By; Samir Kumar Mishra


Innovation is what can be the other name of Netbeans IDE 6.1 , true to the fact as it says "The only IDE you need".

The notable features of the Netbeans IDE 6.1 can be found highlighted at Netbeans Release Overview.

Perhaps, the best thing about the IDE is that it gives us a chance to realize our new ideas by building plugins on to the Netbeans platform.

Introduction


We will therefore have a short view of how to make a simple GUI-based parser.This makes use of a class of java.util package called the StringTokenizer. Lets get started then, have a look at the pre-requisites, before you go any further.

Pre-requisites


  • Java Standard Development Kit (JDK™) version 1.4.2 OR 5.0
  • NetBeans Platform
  • If you don’t have prior experience with NetBeans Platform, read the plugin quickstart tutorial. That's a must! Now, after you have done that, you might like to explore a bit more. So, you should give the following a read as well(if you want you can skip it for a while and read it along with my tutorial).
  • This overview will quickly familiarize you with how NetBeans plug-in modules interact with the NetBeans infrastructure and with each other.
Now, you're ready for plugin-development. So, lets get started then!

Getting Started


The processing of text often consists of parsing a formatted input string.Parsing is the division of the text into a set of discrete parts or tokens, which in a certain sequence can convey a semantic meaning.The
StringTokenizer
class provides the first step in the parsing process, often called the lexer(aka lexical analyzer) OR scanner. {StringTokenizer} class implements the Enumeration interface. To use
StringTokenizer
, we specify an input string and a string that contains delimiters.Delimiters are characters that separate tokens.By default, whitespace characters:space,tab,newline and carriage return are considered delimiters.

Creating Project


So, here are the following steps to create a NetBeans Module;

  • Choose File > New Project. Under Categories, select NetBeans Plug-in Modules.
NetBeans plug-in module support provides three Project types;
1. Module Project- Creates a template for a standalone plug-in module.
2. Library Wrapper Module Project- Creates a plug-in module for an external JAR file required by one or more plug-in modules.
3. Module Suite Project- Creates a template for a set of interdependent plug-in modules and library wrapper modules, which you want to deploy together.
  • Select Module Project. Click Next.
  • In the Name and Location panel, type
    Stokenizer
    in Project Name.
  • Leave the Standalone Module radio button and the Set as Main Project checkbox selected. Click Next. (see Figure)

File:page2_SimpleGUIBasedParser.bmp

  • In the Basic Module Configuration panel, replace yourorghere in Code Name Base with myorg, so that the whole code name base is
    org.myorg.Stokenizer.Notice
    , where the localizing bundle and the XML layer will be stored in the {package org.myorg.Stokenizer}.
These files do the following;
1. Localizing Bundle- Specifies language-specific strings for internationalization.
2. XML Layer- Registers items such as menus and toolbar buttons in the NetBeans System Filesystem.
  • Click Finish. The IDE creates the Stokenizer project. The project contains all of your sources and project metadata, such as the project's Ant build script. The project opens in the IDE. You can view its logical structure in the Projects window (Ctrl-1) and its file structure in the Files window (Ctrl-2)

Using the Window Component Wizard


  • Right click the Stokenizer project node and select New > File/Folder
  • In the New File Wizard, choose NetBeans Module Development under Categories and then, select Window Component under File Types. Click Next.
  • In the Basic Settings panel of the New Window, check the option "Open On Application Start" and click Next.The place of its opening is kept "output " to default.
  • In the Name, Icon, Location Panel, fill up the name as
    Tokenizer
    and click Finish.
  • Your Project window will have some files generated like
    TokenizerAction.java
    , {TokenizerTopComponent.java} and some XML files. (see Figure)

File:page3_SimpleGUIBasedParser.jpg

  • The TokenizerTopComponent.java has a source view and a design view.Using palette on the righthand side, just drag and drop components in to the design and resize it.

File:Page4 SimpleGUIBasedParser.bmp

  • Right click on the jButton1 and choose Events, then Action, then ActionPerformed() so that the event listener is registered with the jButton1.
  • Copy and paste the following code in the method jButton1ActionPerformed().This will come immediately after your selection of the event in the step 7.
String str1=jTextField1.getText();
String str2=jTextField2.getText();

StringTokenizer st= new StringTokenizer(str1,str2);

while(st.hasMoreTokens()) {
jTextArea1.append(st.nextToken()+"\n");
}
  • You will see an error in the line:
StringTokenizer st= new StringTokenizer(str1,str2);
  • Click on the "X"(cross) symbol at the left of the line.the line is now highlighted with a bulb symbol glowing. Click on the bulb.It will show an option to "add import java.util.StringTokenizer". ;: Click it, this way Netbeans helps you to fix errors in your programYou could have also just pressed Alt+Shift+F to fix the necessary imports.
  • Press Ctrl+ Shift + F to reformat your code.
  • Right click on the Stokenizer project node and select the Install/Reload in Target Platform
  • Final out put will look like this in the place where the output window was-

File:Page5 SimpleGUIBasedParser.bmp

Install/Reload and Use


  • Right-click Project Node, select Install/Reload in Target Platform, see Target Platform boots and soon, your plugin is loaded into it.
  • Enter Hello + Samir,;Minakshi,(krishna)*,Rama$,hari as the Input String
  • Enter the delimiters string as +;()*$
  • Press the Button Parse.
  • The output will be
   Hello
       Samir,
       Minakshi,
       krishna
      ,Rama
      ,hari
  • Note that we have deliberately left the comma(,) and therefore it appears.

Reference


In this way, we have built a plugin very easily into Netbeans platform. You can refer this blog entry, from where this tutorial has been recreated. Also, this blog entry is a contender for winning prize in Sun Student Views and Reviews - blogging contest.


Version Compatability


Works with NetBeans 6.1 as said above. Try this out on your version, and make sure to notify us if anything goes wrong!

Thank you!

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo