Easy Integration of Scripting Languages in NetBeans 6.0

netbeans.org

Easy Integration of Scripting Languages in NetBeans 6.0

Beyond JavaBlog by JanJancura, thelead NetBeansengineer forSchliemannblogs.sun.com/hanzBListing 1. NBS file snippet.## NBS Template## definition of tokensTOKEN:keyword:( “while” | “if” | “else”)TOKEN:operator:( “{“ | “}” | “(“ | “)” )TOKEN:identifier:( [“a”-”z”] [“a”-”z” “0”-”9”]* )TOKEN:whitespace:( [“ “ “\t” “\n” “\r”]+ )# parser should ignore whitespacesSKIP:whitespace# definition of grammarS = (Statement)*;Statement = WhileStatement | IfStatement |ExpressionStatement;WhileStatement = “while” “(“ ConditionalExpression “)”Block;IfStatement = “if” “(“ ConditionalExpression “)” Block;Block = “{“ (Statement)* “}”;ConditionalExpression = ;ExpressionStatement = ;# code foldingFOLD:Block# navigator supportNAVIGATOR:WhileStatement:”{$ConditionalExpression}”# brace completionCOMPLETE “{:}”COMPLETE “(:)”# indentation supportINDENT “{:}”INDENT “(:)”INDENT “\\s*(((if|while)\\s*\\(|else\\s*|else\\s+if\\s*\\(|for\\s*\\(.*\\))[^{;]*)”bproject.Once tokens are defined, one can already begin assigning features.For example, this single statement would fill the Navigator with thevalues provided by the “keyword” token:NAVIGATOR:keywordReaders who are familiar with the NetBeans Navigator API can onlybe amazed at this drastic simplification! However, normally you wouldlike more robust support for a language and to provide a grammarin addition to tokens. The grammar that the Schliemann approachrequires is also highly simplified. It is comparable to JavaCC or AntLR.Ideally, one would wish that the grammar provided by JavaCC andAntLR could be directly integrated into NetBeans IDE. Unfortunately,however, these grammars are not tailored to usage within an IDE. Forthis reason, a conversion process needs to take place, from AntLR orJavaCC (or from a similar approach) to the Schliemann NBS format.Early experiments have shown that both a manual and an automaticsolution for this process is feasible. However, this aspect ofthe Schliemann project is definitely the areawhere most work needs to be done. A unified,simple approach to integrating grammarsprovided by AntLR, JavaCC, and thelike, is needed in order for the Schliemannproject to reach its full potential.In the NBS code shown before, you cansee, in addition to the tokens, that the grammarforms the basis of both the Navigatorimplementation and the code folding implementation.In the case of code folding, theBlock grammar definition determines eachcode fold, while the Navigator is populatedby values conforming to the WhileStatementdefinition.Finally, notice that the code also showshow brace completion and indentation isdefined, all within the same single file, andthat one can fine-tune further by specifyingthat white space should be skipped by theparser.Hence, when the NBS file in Listing 1 isassociated with a MIME type, documentscorresponding to the MIME type immediatelyhave the following features:• Syntax coloring• Navigator• Code folding• Brace matching• IndentationIn similar ways, a wide range of otherlanguage-support features can be created,including code completion, which is frequentlyvery high up on the list of featuresthat language programmers want to providesupport for.Getting startedNow that we have a general flavor ofthe Schliemann approach, let’s put it into18 N NetBeans Magazine


Schliemann: Easy Integration of Scripting Languages in NetBeans 6.0A 2A1 completed the wizard, you have a single new file, inwhich we will do all our coding for this module (seeFigure 4).Now, let’s begin! Unlike in the previous section, thesyntax we are dealing with here has the notion of state.By state we mean that if we know in which token wefind ourselves, we can always know where we are inrelation to all the other tokens. So, for example, if weare in the “key” part of a key/value statement in a Manifest,we know that when we reach the colon we areentering the “value” part of the statement. As a result, we can defineour tokens in the context of their states. Below you see how this isdone. Not much of this should be foreign to you if you are familiarwith regular expressions:TOKEN:key:( [^”#”] [^ “:” “\n” “\r”]* ):AFigure 1Creating a newmodule project.AFigure 2Result of the NewProjects window:Plugin SourceStructurepractice and create an NBS file for JavaManifests. Manifests, as you know, areconstructed from key/value pairs. In theIDE, there is no language support for Manifests,not even syntax coloring. Let’s providethat... and a lot more besides.We begin as one always does when creatinga plug-in for the IDE: by creating anew module project (see Figure 1). Next,in the New Project wizard, name the project“ManifestEditorFeatures” and specify“org.netbeans.modules.manifesteditorfeatures”as the Code Name Base. At the endof the wizard, after having clicked Finish,you’ll see that the IDE has created a basicsource structure, as it does for every Net-Beans module (see Figure 2).Next, we can use the Generic LanguagesFramework wizard to generate the NBStemplate discussed in the previous section.This template is found in the NetBeansModule Development section in the NewFile wizard (see Figure 3). Once you’veA 4A 3AFigure 3Generic LanguagesFrameworkTemplate.AFigure 4Result of the NewFile wizard: Oneadditional file!Issue Three N 19


Beyond JavaAFigure 7Navigator. {TOKEN:whitespace:( [“\n” “\r”]+ ):TOKEN:operator:( “:” ):} {TOKEN:whitespace:( [“\n” “\r”]+ ):TOKEN:value:( [^ “\n” “\r”]* )}A 7CGeertjan Wielenga(geertjan.wielenga@sun.com) is a technicalwriter for NetBeansIDE and a co-authorof the book “RichClient Programming:Plugging into theNetBeans Platform”.He is passionate aboutNetBeans and blogsabout it daily atblogs.sun.com/geertjan.AFigure 5A Manifestfile withsyntaxcoloring.AFigure 6AST window.Notice that we start out by saying that we are not in a key if the firstcharacter is a hash (#). In that case we are, in fact, in a comment. Itwould also be good to provide a specific syntax color for comments,so let’s define a token for comments:TOKEN:comment:( “#” [^ “\n” “\r”]* [“\n” “\r”]+ )Right now, without going any further, we can already assign colors.Again we do so declaratively:COLOR:key: {foreground_color: “blue”;}COLOR:operator: {foreground_color: “black”;}COLOR:value: {foreground_color: “magenta”;}Apart from the foreground color, there are many other attributesthat we can set per token, such as the style and background color.Without going much further, though, we can already install our moduleand then we’ll have syntax coloring (see Figure 5)! It couldn’t bemuch simpler. Before we do so, however, we need to create a MIMEtype resolver, which is a small XML file that specifies the file extensionof the files we want to deal with.A 5A 6If you use the New File Type wizard, youcan let the IDE generate such a MIME type resolverfor you. You then need to register boththe resolver and the NBS file in the XML layerfile and declare a dependency on the GenericLanguages Framework API. Eventually, theGeneric Languages Framework template willdo all of this for you, one imagines; but at thetime of writing this is not the case.After installing the module, we can developit further. To help you, NetBeans 6.0 will providea number of developer tools, such asthe new AST window (see Figure 6), whichlets you analyze a file, based on the tokensyou have assigned to its MIME type. Ultimately,for Manifests, you could create a verydetailed Navigator (see Figure 7), amongother useful features for the end user.ConclusionsHopefully this broad introduction givesyou a flavor of what NetBeans 6.0 will dofor scripting languages. Quickly and withoutmuch fuss, language developers willbe able to integrate their favorite scriptinglanguages into the IDE, thus turningNetBeans more and more into their own,customized development environment.In short, just like HeinrichSchliemann, NetBeans IDE will beable to pick up new languagesand expand its usefulness acrossmore and more developmentcommunities.20 N NetBeans Magazine


Mobile DevelopmentA A 5Figure 5Applicationflow with asplash screen.AFigure 6Splash screenin the deviceemulator.Mobility Packfor CLDC/MIDPdocumentationindexnetbeans.org/kb/55/mobility.htmlIDE automatically makes it available to us from the Resources folderin the Navigator window, on the lower left side of the IDE.) Drag aSplashScreen component from the Tools Palette on the right of thewindow on to the Flow Designer. Next, Drag the image1[Image] fromthe Navigator window and drag it on top of the splash screen.Presto! Our splash screen has been created. You can look at itby double-clicking on the SplashScreen component, which takesyou into the Screen Designer. The splash screen should look likeFigure 4.Now let’s make the splash screen part of the application flow. Clickthe Flow Design button to go back to the Flow Designer. Grab thetip of the Start Point Arrow and drag it over to the splashScreen1component. Click on the orange square next to Dismiss and dragthe arrow over the helloForm component. The flow should now looksomething like Figure 5.Let’s just make one more quick change to the program to illustratethe Screen Designer. Double click on the form component. In theScreen Designer, click on the “Hello, World!” text and change it tosomething else, say “Hello, Universe!”Now go ahead and run the MIDlet again. Click the button under“Launch,” and the emulator displays the splash screen as shown inFigure 6. Then you see the new message.That was just a simple example of howyou can quickly design the screens and flowof your application in the VMD. The Splash-Screen component is one of three customcomponents, along with WaitScreen andTableItem, created by the NetBeans MobilityPack team and added to the palette tomake visual programming easier. You canalso create your own custom componentsand add them to the palette.Another important VMD feature is its supportfor Scalable Vector Graphics (SVG).SVG is an XML-based standard definedby the W3C and supported by MIDP 2.0through JSR 226. The compact size andconsistent appearance across differentplatforms and screen resolutions makesit an attractive graphics format for mobiledevelopers. SVG also enables scripting andanimation that allows users to interact withA 634 N NetBeans Magazine


Write Once, Deploy Anywherethe visual content. To use SVG, you’ll needthe Sun Java Wireless Toolkit 2.5, whichis currently available as a module fromNetBeans Update Center, and is bundledwith the Mobility Pack starting with version5.5.1.In the VMD, you can add an external SVGediting tool, such as Hyperion or Ikivo, anduse it to create your initial SVG graphic.Like we did in the example we just lookedat, you can create a splash screen, an interactivemenu, or a wait screen by draggingand dropping components into theFlow Designer. You can drop the graphic oranimation on the component, and inspectthe behavior of the graphic or animation asthe application is run.A 7Figure 7 shows the VMD inspecting ananimated SVG menu. You can use the VMDflow designer interface to link each menuitem to a separate screen that would becalled when the menu item is selected.Reducing fragmentationwith project configurationsOne of the most difficult aspects of developingapplications is device fragmentation. Mobile devices differin a variety of attributes, such as screen size, color depth, and theproprietary or optional APIs they support. These differences oftenrequire special code or project settings for successful deployment.One solution is to create separate source code for each deviceyou’re programming to, which is almost guaranteed to be a logisticsnightmare. We’ve already touched on the Mobility Pack solutionfor device fragmentation – project configurations.Project configurations enable you to define the execution environmentfor each target device. With project configurations and codepre-processing, you can write an application and – using a singleset of source code – customize, debug, and deploy a separatedistribution JAR for each target device. If you need to customizeyour MIDlet for more devices, you add a new configuration for eachdevice, modify the project properties, add some pre-processingcode, then build and deploy the application. In most cases, youshould create one configuration for each distribution JAR you planto build for your project. For example, if you are planning to supportthree different screen sizes using two sets of vendor specific APIs,you should create six configurations.Before we look at deploying our MIDlet to different devices, let’sexamine the three main concepts behind project configurations.The Emulator PlatformAn emulator platform simulates the execution of an application onone or more target devices. It enables you to understand the userThe Sun Java Wireless Toolkit 2.5The Sun Java Wireless Toolkit 2.5 includes all of the advanceddevelopment features found in earlier versions, such as MIDletsigning, certificate management, integrated OTA emulation,push registry emulation, and more. New features includesupport for the Mobile Service Architecture (JSR-248) platform.Although this JSR does not define any new APIs, it doesstandardize many existing ones into a common API stack, toincrease interoperability and make mobile development easier.There are also many new APIs supported, such as Security andTrust Services (JSR 177), Location (JSR 179), SIP (JSR 180),Content Handler (JSR 211), Scalable 2D Vector Graphics (JSR226), Payment (JSR 229), and several others.AFigure 7An interactiveSVG-basedmenu.netbeans.org/kb/55/quickstart-mobilitycdc.htmlMobility Pack forCDC Quick StartGuideIssue Three N 35


Mobile DevelopmentAFigure 8The Java PlatformManager.AFigure 9Abilities page.experience for an application on a particular device, and to test theportability of the application across different devices. As you haveseen, the J2ME Wireless Toolkit 2.2, bundled with Mobility Pack 5.5for CLDC/MIDP, provides several sample devices, like the Default-ColorPhone (and you can easily update to WTK 2.5).It is important, however, to remember that an emulator can onlyapproximate a device’s performance. Environmental variables likeprocessing speed or the strength of the wireless signal can affectperformance on a real device, and should be taken into account.A very important feature of the Mobility Pack is its ability to workA 8A 9with the emulators provided by major manufacturers,such as Nokia, Sony Ericsson,Siemens, and Motorola. Using the JavaPlatform Manager (see Figure 8), you caneasily add any emulator that supports theUnified Emulator Interface (UEI) standards.Simply choose Tools>Java Platform Manager,select the J2ME Platform, and thewizard detects the emulator platforms installedon your system. Other emulatorscan also be added with a little more effort.Project propertiesWe can use the project properties to definemany aspects of the program. As youcan see in Figure 9, the property categoriesare on the left, and the properties forthat category are on the right. A short listof things you can do in properties includes:defining the emulator platform, setting/checking Configuration and Profile versionsand optional APIs the device supports; addingor removing the contents of the JAR andJAD files; setting the Push Registry; settingobfuscation and optimization levels; addingsigning and security certificates; andsetting deployment options. The Abilitiesshown in Figure 9 list attributes that mightbe shared by different devices, and thereforemight be shared when you are addingpre-processing code.Let’s take a look at this using the two configurationswe’ve created for our TestMIDlet.The first is the DefaultConfiguration we’vebeen using so far. The second is the MediaControlSkinthat you selected when firstcreating the project. Before we discussproject properties and pre-processingcode, let’s take a quick look at the Media-ControlSkin device emulator.To switch configurations, choose the36 N NetBeans Magazine


Write Once, Deploy AnywhereMediaControlSkin from the Configurationdrop-down menu in the IDE toolbar. Thenchoose Run>Run Main Project. It’s theA11AFigure 11Preprocessorcode.same TestMIDlet as before, but this timethe device emulator and its display arethinner – so we lose a little bit of the splashscreen, as shown in Figure 10.Preprocessing codePreprocessing modifies the code in yoursource files before the code is parsed bythe compiler. The preprocessor modifiesthe code according to preprocessor directivesyou insert into the code as codeblocks with beginning and ending directives.These code blocks are marked visuallyin the Source Editor and are includedA 10(or excluded) when you build the JAR for a specific project configurationor ability. You can use these code blocks to create, manage,and track code that is specific to one or more project configurationsor abilities.Now that we’ve defined the key concepts of project configurations,let’s go back to our example and say now that we want touse a different graphic for the MediaControlSkin device. We alreadyhave our two project configurations defined, so what we need todo is add some pre-processing code so that the compiler knowsit should use one image for DefaultConfiguration and another forMediaControlSkin.Click the Source button to view the TestMIDlet source code. Thenscroll down to the get_image1() method. Notice that some sectionshave a blue background – these are “guarded blocks” that are generatedby the VMD, and cannot be edited. Highlight the code beginningwith “//Insert pre-init code here and ending with “//Insertpost-init code here.” Right click on the selection and choose PreprocessorBlocks>Create If/Else Block. A menu with all the availableconfigurations and abilities appears. Double click on DefaultConfiguration.Your code should look like Figure 11.Notice that the //#else directive has a pink background. Thiscode can be edited. Change the graphic name “/mobileduke.png” to the name of another graphic (such as “/veryproudduke.AFigure 10TheMediaControlSkinEmulator.netbeans.org/kb/55/mobilitycdc.htmlMobilityPack for CDCdocumentationindexIssue Three N 37


Mobile Developmentpng”). Now, when you run our MIDlet using the DefaultConfiguration,the emulator will display the “mobile duke” image. Whenyou run the MIDlet using the MediaControlSkin configuration, orany other configuration you add later, the emulator will displaythe second graphic. This is a very simple example of what’s possiblewith preprocessor blocks, but it hopefully gives you a tasteof what they can do.Deploying tomultiple devicesNow that you have an application that works with two devices, it’stime to deploy it. The deployment property page shown in Figure 12shows the different deployment methods available.Because deployment is set in the project Properties, you can definea different deployment for each configuration. When you havechosen your deployment method, choose Build>Build All Main ProjectConfigurations. Then you’ll have a JAR for each target deviceyou’re programming for.Other featuresOur example was purposefully kept simple to focus on the design,configuration, and deployment features of the Mobility Pack forCLDC. But there are two other importantfeatures we’d like to mention quickly beforewe move on to the CDC Pack.The End-to-End Bridge technology is a setof two wizards that enable you to quicklymodify your MIDlet to consume WebServices. The J2ME Web Service Clientcreates a client-side proxy that connectsdirectly to Web Services that support theJSR-172 (J2ME Web Services) specification.The Mobile Client to Web ApplicationGenerator generates a servlet that connectsto a web application that includes aWeb Service client.Another important feature is JMUnit testingsupport. The Mobilty Pack for CLDC providesbuilt-in JMUnit support for generatingand executing unit tests for MIDP/CLDC applications.You can generate and navigateto tests by selecting any class or packagenode in the Projects window and choosingfrom the Tools>JUnit menu.AFigure 12Deploymentproperties.A 1238 N NetBeans Magazine


Write Once, Deploy AnywhereNetBeansMobility Pack for CDCThe Mobility Pack for CDC (ConnectedDevice Configuration) makes it possible tocreate, test, and deploy applications forseveral CDC platforms including the SunJava CDC Toolkit, Sony Ericsson CDC Platform1, Nokia S80, SavaJe and Ricoh MFP,as well as Windows CE using NSIcom’sCrEme CDC virtual machine. Although itis not yet as complete as the MIDP/CLDCPack, many new features will be addedwhen Mobility Pack 6.0 is released.You create CDC projects in the samemanner as MIDP/CLDC projects, using theNew Project Wizard. Before you begin, youwill want to install the Sun Java Toolkit forCDC or an emulator platform from one ofthe growing list of manufacturers the MobilityPack supports. You can find this list inthe NetBeans MobilityPack for CDC Quick Start Guide.To get started on a project, Choose File>New Project. Choose thecategory CDC, project type CDC Application. The wizard will guideyou through the rest of the steps for creating your Main Project.Once you’ve created a project, you can use the Matisse GUI Builderwith either the AGUI toolkit or the Personal Profile 1.0, in thesame way you would use it for regular Java SE development. ForPersonal Profile GUI development, right click the Main.java form inthe GUI Builder, and choose Set Layout>Free Layout. Then dragand drop components from the Palette window into the Design Areaof the GUI Builder. You can also take advantage of JUnit testing andother key features of the NetBeans IDE when developing CDC applicationsin the Mobility Pack for CDC.What’s coming in NetBeansMobility Pack 6.0The NetBeans Mobility Pack has many dramatic changes comingup. One of the most significant changes, as we’ve mentioned before,is that the CLDC/MIDP and CDC Packs will be merged into asingle UI, making it easier to create end-to-end applications. Otherexciting new features include:• New custom components to simplify programming, includinga File Browser, an SMS Composer, a Login Screen and a PersonalInformation Manager (PIM) Browser• VMD support for the MIDP 2.x Game API that allows creatingtiled and animated layers for environment design, and support foranimated character and sprites.• Improved VMD UI, including support for non-visual componentsand a design analyzer.• CDC support for project configurations and pre-processingblocks.ConclusionsThis article was intended to give you a hands-on sense of thecapabilities of the Mobility Pack for CLDC/MIDP and CDC, and arunning start on building your first mobile application. You can learnmore about the Mobility Pack by reading the tutorials and articleson the NetBeans website, joining the NetBeans community of developers,and most importantly, by going out there and creating greatmobile applications!CAnatole Wilson(anatole.wilson@sun.com) livesin Pittsburgh,Pennsylvania andhas been the SeniorTechnical Writer forthe Mobility Packsince its inception.He has worked forvarious high-techcompanies, includingIBM and Oracle, and,as a freelance writer,has written articlesfor publication invarious magazines.Issue Three N 39


A New HandbookSchliemann:forEasy Integration of Scripting Languages in NetBeans 6.0BooksNetBeans PlatformDevelopmentto the worldof rich client developmenton the Net-“WelcomeBeans Platform.”So begins the new book on the NetBeansPlatform, called “Rich Client Programming:Plugging into the NetBeans Platform”. Writtenby three stalwarts of the NetBeans IDE,Tim Boudreau, Jaroslav Tulach, and GeertjanWielenga, this new title from PrenticeHall introduces you to the central conceptsof the NetBeans Platform.The book begins by discussing the rationalefor modular programming. “Loose coupling”,whereby spaghetti code is avoidedthrough a set of related but independentmodules, is discussed with reference to aset of concrete examples. Gradually, theneed for this approach to robust programmingis introduced and, piece by piece, theauthors highlight NetBeans’ responses tothis need. The tooling for modular programmingsince the release of NetBeans IDE 5.0has proven its value to numerous programmers,and the reasons for this quickly becomeclear in the book. Typical stumblingblocks that new developers come across,such as “nodes” and “cookies” are exploredin detail.The second part of the book deals witha set of concrete NetBeans API scenarios.How, for example, can one provide codecompletion? Or hyperlinks in the Source Editor?And what about palettes with items that can bedragged and dropped? These and other scenariosare discussed in detail. Each chapter travelsthrough a very specific example scenario, basedon the example in the CD that accompanies thebook. At the end of this part, the reader shouldunderstand some of the basic APIs that are typicallyimplemented by developers making use ofthe NetBeans Platform.The book closes with two chapters contributedby two developers with years of experience with the Net-Beans Platform. First, Jens Trapp, from Germany, discusseshow he integrated the HTML Tidy project into NetBeansIDE. In doing so, he brings together many of the principlesand APIs discussed in the preceding chapters. Next, USbasedRich Unger describes a complete application builton top of the NetBeans Platform, for editing WAV files.Typical concerns involved in creating Platform-based applications arediscussed in this chapter. Together Jens and Rich provide the two “usecases” of the NetBeans Platform – allowing you to extend NetBeansIDE with new features, and creating completely separate applications,which in turn could constitute the platform of still other applications.This is the first book since Tim Boudreau’s and Jesse Glick’s “Net-Beans: The Definitive Guide” – the popular title from some years agowhich had a large section on the NetBeans Platform – to cover thelength and breadth of the NetBeans Platform. Judging from the level ofinterest shown in the Safari Rough Cuts version of the book, which providesan early draft in PDF format, “Rich Client Programming: Plugginginto the NetBeans Platform” promises to take the Swing developmentworld by storm. If you want to leverage the full potential of the Net-Beans Platform, this is definitely not a book that you want to miss!Rich Client Programming:Plugging into theNetBeans PlatformTim Boudreau,Jaroslav Tulach,Geertjan Wielenga(Prentice Hall)ISBN-13: 978-0-13-235480-6640 pagesIssue Three N 21

More magazines by this user
Similar magazines