Programming NetBeans Platform with IntelliJ IDEA

Programming NetBeans Platform with IntelliJ IDEA

By Dan Lewis, OCI Principal Software Engineer

September 2013


Introduction

The聽 NetBeans Platform聽allows rapid construction of an application that has some advanced capabilities, including templates, undo/redo, auto update, and more. It is an alternative to Eclipse Platform for those that wish to use standard Java Swing user interface instead of the Eclipse SWT user interface. The NetBeans Platform grew organically from the lessons learned about application architecture learned by the聽 NetBeans IDE聽development team. Some of those lessons included the need to isolate the various parts of the application into "Modules" and the need to organize user interface items of interest into a hierarchy of "Nodes".

The NetBeans Platform enjoys use on many Java Swing-based Java UI applications. Unfortunately, the NetBeans Platform is coupled to the NetBeans IDE, and some developers prefer to use another IDE, such as聽 IntelliJ IDEA. Most examples of NetBeans Platform development are illustrated using the NetBeans IDE to perform the development. Fortunately, with the help of this article, you can develop your NetBeans Platform application using IntelliJ IDEA.

IntelliJ IDEA聽is an IDE that used to be a commercial-only tool, but was recently open-sourced. IntelliJ IDEA is favored by some developers because it focuses on productivity and refactoring support. For those IntelliJ developers that wish to develop on the NetBeans Platform, steps can be taken that will allow an IntelliJ developer to work effectively on a NetBeans Platform application.

NetBeans vs IntelliJ notion of a Module

NetBeans defines a module as a cohesive chunk of code or a 3rd-party library with carefully defined dependencies and available public packages. IntelliJ defines a module similarly, but without the ability to identify certain packages as internal only. This means that you will sometimes have the IntelliJ IDE able to compile the application, but NetBeans Platform will refuse to run it because one module is accessing a non-public package of another module. Another reason IntelliJ IDE will compile the application but it won't run is because the module dependencies are not set up properly. This means that IntelliJ has one module accessing a class in another module without the necessary configuration in NetBeans to have it work.

To illustrate, we will use聽 NetBeans Platform 7.3 Quick Start聽to quickly create a NetBeans Platform Application using NetBeans. The result of following along with the example is a NetBeans 7.3 Platform Application called "Word Processor" with five modules: WordEditorCore, WordEditorAPI, UppercaseFilter, LowercaseFilter, and WordHistory. We will use IntelliJ IDEA 12.1.4 for the illustrations.聽 IntelliJ IDEA聽is free and open source.

Opening Word Processor Application in IntelliJ

Figure 1: Create New Project
Figure 1: Create New Project

Figure 2: Create New Project (Continued...)

Figure 2: Create New Project Step 2

Figure 3: Create New Module

Figure 3: Create New Project Step 3

In figure 4 when we configure the module, we exclude the build directory. This prevents IntelliJ from overwriting compile outputs created by Ant or NetBeans.

Figure 4: Create New Module (Continued...)
Figure 4: Create New Project Step 4

Repeat the processes in Figures 3 and 4 for the remaining four modules: WordEditorAPI, UppercaseFilter, LowercaseFilter, and WordHistory. Figure 5 shows what the dialog looks like when done. We now have one IntelliJ module for each NetBeans module in our project.

Figure 5: All Modules Created
Figure 5: Create New Project Step 5

Compiling the Word Processor Application in IntelliJ

Now attempt to build the project in IntelliJ idea.

Figure 6a: First Compile of Word Processor Application in IntelliJ: Rebuild Project
Figure 6a: Rebuild Project

Figure 6b: First Compile of Word Processor Application in IntelliJ: Results

Figure 6b: Compile Step 1

Observe that IntelliJ doesn't know anything about the NetBeans Platform. How does the NetBeans build system know where to find the supporting jar files? The answer is in a file called聽$wordproc\nbproject\private\platform-private.properties. The variable聽$wordproc聽is the directory containing your word processor example program. On the author's system, this is聽c:\wordproc. It contains one line:

platform-private.properties

1.    user.properties.file=C:\\tools\\.netbeans\\7.3.1\\build.properties

Opening that file reveals two lines at the end:

build.properties

83.    nbplatform.default.harness.dir=${nbplatform.default.netbeans.dest.dir}/harness
84.    nbplatform.default.netbeans.dest.dir=C:\\tools\\netbeans-7.3.1-201306052037-javase

These lines tell the NetBeans build system where to find the NetBeans Platform jars and the IntelliJ project needs to know this information too. They are located in a subdirectory named platform in the NetBeans IDE installation. One can also obtain the smaller NetBeans Platform zip if the only requirement is to compile the project. However, certain ANT targets in the NetBeans build system for deployment, such as build-zip require something called a harness, which is not included in the Platform zip. For this example, we will create an IntelliJ library that includes all of the jar files in the NetBeans聽platform聽subdirectory, recursively, but excluding locale-specific jars.

Figure 7: Creating the IntelliJ library containing NetBeans Platform jars
Figure 7: Compile Step 2

Once the IntelliJ library called "NetBeans Platform" is created, we need to add it as a dependency of the IntelliJ modules in our application.

Figure 8: Adding the dependencies on NetBeans Platform.
Figure 8: Compile Step 4

After we have added the dependencies, we can rebuild the project. We have fewer compilation problems now, but we are still unhappy, as shown in the next figure.

Figure 9: Second Compile of Word Processor Application in IntelliJ
Figure 9: Compile Step 5

It turns out that the NetBeans Platform example makes use of compile time annotations. These annotations generate code as part of the compile step. We must tell IntelliJ to go ahead and run the annotations and place them in a spot where we can see the generated code from our IDE.

Figure 10: Enable Annotations Processing
Figure 10: Annotation Processor

After we have enabled annotation processing, we can rebuild the project. We have fewer compilation problems now, but we have yet more configuration to set up, as is shown in the next figure.

Figure 11: Third Compile of Word Processor Application in IntelliJ
Figure 11a: Compile Step 6

The IntelliJ module WordEditorCore cannot find the package org.word.editor.api. That package is inside the WordEditorAPI module. We need to add this dependency.

Figure 11: Adding Inter-Module Dependencies in IntelliJ
Figure 11b: Compile Step 7

Repeat this process so that modules LowercaseFilter and UppercaseFilter also depend upon WordEditorCore.

Figure 12: Fourth Compile of Word Processor Application in IntelliJ
Figure 12: Compile Step 8

While we do obtain a clean compilation, we still need to instruct IntelliJ where the location of the generated source code is so that we can navigate to it in the editor. For each of the five modules we will identify the "generated" subdirectory as a location of source code. The compilation is clean yet the red squiggly underline in WordTopComponent.java shows that the configuration is still incomplete.

Figure 13: Identifying annotation-generated sources in IntelliJ
Figure 13: Identifying Generated Sources

Observe that IntelliJ can now cleanly compile and navigate to the generated source code of the application.

Debugging the NetBeans Platform Application from IntelliJ

It is possible to develop a custom launcher program that will launch the NetBeans platform without the necessity to run either the NetBeans IDE or ANT-based NetBeans build system. In the absence of such a launcher program, launch your application using ANT and connect to it using a remote debugging profile. First, you will need to create a remote debugging run configuration in IntelliJ.

Figure 14a: Creating the Remote Debugging Profile
Figure 14a: Remote Debug 1

Figure 14b: Creating the Remote Debugging Profile (continued)

Figure 14b: Remote debug 2

Next, you will add the line with the debugging arguments to the suite's project.properties file. On the author's system, this is located at聽$wordproc/nbproject/project.properties.project.properties聽file. Remember to prefix the line that you copy paste from the IntelliJ dialog with "-J".

project.properties

  1. app.name=${branding.token}
  2. app.title=WordProcessor
  3. modules=\
  4. ${project.org.word.editor.core}:\
  5. ${project.org.word.editor.api}:\
  6. ${project.org.word.editor.uppercase}:\
  7. ${project.org.word.editor.lowercase}:\
  8. ${project.org.word.editor.history}
  9. project.org.word.editor.api=WordEditorAPI
  10. project.org.word.editor.core=WordEditorCore
  11. project.org.word.editor.history=WordHistory
  12. project.org.word.editor.lowercase=LowercaseFilter
  13. project.org.word.editor.uppercase=UppercaseFilter
  14. run.args.extra=-J-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

Common Pitfalls

Failure to synchronize NetBeans and Intellij module dependencies.

If a module A depends upon a module B in the NetBeans universe, then the same should be true in the IntelliJ universe. The same is true the other way. If you are editing code in IntelliJ and you add a dependency between two modules, you need to make an analogous change in your NetBeans module dependencies. This can be done by hand in the modules' nbproject/project.xml file for the modules in question or using the NetBeans IDE in the normal way.

Failure to add new packages to the list of public packages in a module.

If you add a new package to a module that you would like to be visible within other packages, you need to edit the modules' nbproject/project.xml in the element to include that package. Note that this is necessary because IntelliJ does not have the ability to restrict visibility at the package level. This is a NetBeans capability that is enforced by the NetBeans Platform runtime.

Summary

Some developers have acquired proficiency with the IntelliJ IDE and may wish to maintain or develop an application based on the NetBeans Platform without having to use the NetBeans IDE. This article has shown the necessary steps to allow the IntelliJ developer to edit, compile, and debug a NetBeans Platform application from within IntelliJ IDE.

References

secret

两个鬼故事10月5日是什么星座高性价比抗日之铁血远征军物流运输公司名称起名大全属鼠人起名字大全男孩医路坦途小说photoshop字体下载机械设备起名矿泉水起名1518免费起名测长坂坡之战给课堂起个高雅的名字胡姓起名胡姓起名大全精益求精是什么意思长寿商会网店取名起名属鼠起名的忌讳猩球崛起猩猩名字正青春免费观看全集ebank.spdb.com.cn有创意的环保科技公司起名大全集面相起名普姓女孩起名金牛起名字需要注意什么钟易轩暗黑破坏神2修改器udietoo取名起姓名大全山崩地裂造句明星孩子取名起名大全劳务公司起名免费取名少年生前被连续抽血16次?多部门介入两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”淀粉肠小王子日销售额涨超10倍高中生被打伤下体休学 邯郸通报单亲妈妈陷入热恋 14岁儿子报警何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言张家界的山上“长”满了韩国人?男孩8年未见母亲被告知被遗忘中国拥有亿元资产的家庭达13.3万户19岁小伙救下5人后溺亡 多方发声315晚会后胖东来又人满为患了张立群任西安交通大学校长“重生之我在北大当嫡校长”男子被猫抓伤后确诊“猫抓病”测试车高速逃费 小米:已补缴周杰伦一审败诉网易网友洛杉矶偶遇贾玲今日春分倪萍分享减重40斤方法七年后宇文玥被薅头发捞上岸许家印被限制高消费萧美琴窜访捷克 外交部回应联合利华开始重组专访95后高颜值猪保姆胖东来员工每周单休无小长假男子被流浪猫绊倒 投喂者赔24万小米汽车超级工厂正式揭幕黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发当地回应沈阳致3死车祸车主疑毒驾恒大被罚41.75亿到底怎么缴妈妈回应孩子在校撞护栏坠楼外国人感慨凌晨的中国很安全杨倩无缘巴黎奥运校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变王树国卸任西安交大校长 师生送别手机成瘾是影响睡眠质量重要因素国产伟哥去年销售近13亿阿根廷将发行1万与2万面值的纸币兔狲“狲大娘”因病死亡遭遇山火的松茸之乡“开封王婆”爆火:促成四五十对奥巴马现身唐宁街 黑色着装引猜测考生莫言也上北大硕士复试名单了德国打算提及普京时仅用姓名天水麻辣烫把捣辣椒大爷累坏了

两个鬼故事 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化