Saturday January 06, 2007
Plugin: Latitude & Longitude of Your City
Let's look at Winston Prakash's Netbeans 5.5 Tip: Create a Webservice client to find Latitude & Longitude of your city. Very nice. How about turning it into a plugin (a.k.a. "module") for NetBeans?
- Complete Winston's steps, but use "JAX-RPC", instead of "JAX-WS". (I couldn't get this example to work as a NetBeans module for JAX-WS, only for JAX-RPC, I don't know why, yet.) Try the result. Now build the project and look in the Files window. Expand the dist folder. You should see this:

- Create a module suite project. (In the New Project wizard, choose Module Suite Project in the NetBeans Plug-in Module category.) Name it "WSDeploymentUnit".
- Expand the deployment unit and right-click on the "Modules" node in the Projects window. Choose "Add New Library". Browse to the dist folder shown in the previous step. Select LocationService.jar, click Next, Next, and Finish. Repeat this process, this time selecting the entire content of the dist/lib folder (use Shift-Click and you can select all the JAR files in one go).
- Right-click on the "Modules" node in the Projects window. This time, choose "Add New". Create a project called "WSFunctionality".
- Use the Coding the Module section in the Google Toolbar Tutorial, to create an action that displays a toolbar that looks like this:

- Now right-click the deployment unit and choose "Run". A new instance of your IDE starts up and you have a new toolbar:

- Let's now create something that will display the latitude and longitude. Create a Window Component (i.e., a subclass of TopComponent) using the template in the New File wizard. Set its Window Position to "properties" and set its Class Name Prefix to "DisplayLoc". Drag and drop a JTextArea called "displayArea" on the new TopComponent. You can run the deployment unit again and look at your new TopComponent. If it does not open automatially, you can open it from a new menu item that you will find under the Window menu.
- Now, let's call the web service from the "Search" button and display the result in the TopComponent! Just take Winston's code and paste it in the button's actionPerformed event. Tweak it slightly, because you want to display the result in the TopComponent. (The only difference between the JAX-WS and JAX-RPC code is that in JAX-WS, the first line below needs "_Impl" appended, while JAX-WS does not use this ending.)
try { // Call Web Service Operation locationsvc.TerraService_Impl service = new locationsvc.TerraService_Impl(); locationsvc.TerraServiceSoap port = service.getTerraServiceSoap(); // TODO initialize WS operation arguments here locationsvc.Place place = new locationsvc.Place(); place.setCity(jTextFieldCity.getText()); place.setState(jTextFieldState.getText()); place.setCountry(jTextFieldCountry.getText()); // TODO process result here locationsvc.LonLatPt result = port.convertPlaceToLonLatPt(place); DisplayLocTopComponent.setText( "City: " + place.getCity() + "\nState: " + place.getState() + "\nCountry: " + place.getCountry() + "\nLatitude = " + result.getLat() + "\nLongitude: " + result.getLon()); } catch (Exception ex) { DisplayLocTopComponent.setText("Failed!"); }You'll notice red underline markings. Declare dependencies on the two modules you created earlier. (Right-click the functionality module, choose Properties, and browse for 'LocationService' and 'activation' in the Libraries panel.) The red underline markings should now disappear. Also make sure to create a dependency in the 'LocationService' module, on the 'activation' module, because the former depends on the latter. Finally, notice that you need to create a setText() method in the TopComponent, to display the data retrieved from the web service.
- Now run the deployment unit again, type values in the toolbar's text fields and observe the result in the new window on the right side of the IDE:

From now onwards, whenever you're in the middle of coding and you suddenly realize: "Oh my god, I don't know the latitude and longitude of my current location!", you'll have NetBeans to jump to your assistance.
Jan 06 2007, 01:44:28 AM PST Permalink


