转载请保留作者信息:
作者:88250
Blog:http:/blog.csdn.net/DL88250
MSN & Gmail & QQ:DL88250@gmail.com
NetBeans IDE 是基于NetBeans Platform搭建的,它的所有功能都是以插件的方式实现的。我们也可以基于NetBeans Platform实现自己的应用,这一点,与Eclipse RCP是等同的。首先,我们先来熟悉一下NetBeans Module的开发 :-)。这个例子是一个用于配置NetBeans启动参数的插件,用它可以使用图形界面配置NetBeans启动参数。当然了,这这是一个例子,Bugs比较多,要使用同种功能的插件,看这里!
准备
点此访问NetBeans下载站点。下载最新的Java基本开发套件就可以了。开始
1. 创建工程
新建工程,选择模块开发:![](https://p-blog.csdn.net/images/p_blog_csdn_net/DL88250/273209/o_NewProject.png)
工程命名为CustomStartup,其他的使用默认配置。
2. 编写测试用例
插件随小,无脏俱全哦~ 再说,本着TDD的原则,我们还是先编写一下测试用例吧。![](https://p-blog.csdn.net/images/p_blog_csdn_net/DL88250/273209/o_Screenshot-Custom%20Startup%20-%20NetBeans%20IDE%206.1.png)
测试代码如下:
/*
*@(#)NBConfFileJUnitTest.java
*Author:88250<DL88250@gmail.com>, http://blog.csdn.net/DL88250
*CreatedonMay17,2008,11:19:21AM
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
package cn.edu.ynu.sei.customstartup.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openide.util.Exceptions;
import static org.junit.Assert. * ;
/**
*NBConfFileTestCase.
* @author 88250<DL88250@gmail.com>, http://blog.csdn.net/DL88250
* @version 1.0.0.0,May17,2008
*/
public class NBConfFileJUnitTest{
private static cn.edu.ynu.sei.customstartup.NBConfFileinstance;
public NBConfFileJUnitTest(){
instance = new cn.edu.ynu.sei.customstartup.NBConfFile();
}
@BeforeClass
public static void setUpClass() throws Exception{
// simulatetheNetBeanslunchertosetsystemproperty
System.setProperty( " netbeans.home " ,
" /home/daniel/Work/netbeans-6.1/platform8 " );
}
@AfterClass
public static void tearDownClass() throws Exception{
}
@Before
public void setUp(){
}
@After
public void tearDown(){
}
@Test
public void getLocation(){
System.out.println( " getLocation " );
assertEquals( " /home/daniel/Work/netbeans-6.1/etc/netbeans.conf " ,
instance.getLocation());
}
@Test
public void getFile(){
System.out.println( " getFile " );
Filenetbeans_conf = instance.getFile();
assertTrue(netbeans_conf.exists());
assertEquals( " netbeans.conf " ,netbeans_conf.getName());
BufferedReaderreader;
// Stringexpect=
// "#${HOME}willbereplacedbyJVMuser.homesystemproperty";
Stringexpect = " ###propertieswrittenbyCustomStartupmodule " ;
Stringactual = null ;
try {
reader = new BufferedReader( new FileReader(netbeans_conf));
actual = reader.readLine();
} catch (IOExceptionex){
Exceptions.printStackTrace(ex);
}
assertEquals(expect,actual);
}
@Test
public void backupConfFile(){
System.out.println( " backupConfFile " );
instance.backupConfFile();
assertTrue(instance.getBackupFile().exists());
}
@Test
public void getBackupFile(){
System.out.println( " getBackupFile " );
Fileactual = instance.getBackupFile();
assertEquals( " netbeans.conf.backup " ,actual.getName());
}
@Test
public void getParameters(){
System.out.println( " getParameters " );
Propertiesparas = instance.getParameters();
Enumeration <?> keys = paras.propertyNames();
while (keys.hasMoreElements()){
Stringkey = keys.nextElement().toString();
Stringvalue = paras.getProperty(key);
System.out.println(key + " = " + value);
}
assertEquals( " "${HOME}/.netbeans/6.1" " ,
paras.getProperty( " netbeans_default_userdir " ));
}
@Test
public void getNetBeansDefaultOptions(){
System.out.println( " getNetBeansDefaultOptions " );
List < String > options = instance.getNetBeansDefaultOptions();
for (Stringoption:options){
System.out.println(option);
}
assertEquals( " -J-client " ,options.get( 0 ));
assertEquals( " --fontsize10 " ,options.get(options.size() - 1 ));
}
@Test
public void getNetBeansJDKHome(){
System.out.println( " getNetBeansJDKHome " );
Stringexpect = " /usr/lib/jvm/java-6-sun/ " ;
Stringactual = instance.getNetBeansJDKHome();
assertEquals(expect,actual);
}
@Test
public void setNetBeansJDKHome(){
System.out.println( " setNetBeansJDKHome " );
StringjdkHome = " /usr/lib/jvm/java-6-sun/ " ;
instance.setNetBeansJDKHome(jdkHome);
assertEquals(jdkHome,instance.getNetBeansJDKHome());
}
@Test
public void addNetBeansDefaultOptions(){
System.out.println( " addNetBeansDefaultOptions " );
Stringpara = " --fontsize15 " ;
instance.addNetBeansDefaultOptions(para);
assertEquals(para,instance.getNetBeansDefaultOptions().get(instance.
getNetBeansDefaultOptions().
size() - 1 ));
}
@Test
public void removeNetBeansDefaultOptions(){
System.out.println( " removeNetBeansDefaultOptions " );
instance.removeNetBeansDefaultOptions( 0 );
assertEquals( " -J-Xss16m " ,instance.getNetBeansDefaultOptions().get( 0 ));
}
@Test
public void editNetBeansDefaultOptions(){
System.out.println( " editNetBeansDefaultOptions " );
instance.editNetBeansDefaultOptions( 0 , " --test10 " );
assertEquals( " --test10 " ,instance.getNetBeansDefaultOptions().get( 0 ));
}
}
*@(#)NBConfFileJUnitTest.java
*Author:88250<DL88250@gmail.com>, http://blog.csdn.net/DL88250
*CreatedonMay17,2008,11:19:21AM
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
package cn.edu.ynu.sei.customstartup.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openide.util.Exceptions;
import static org.junit.Assert. * ;
/**
*NBConfFileTestCase.
* @author 88250<DL88250@gmail.com>, http://blog.csdn.net/DL88250
* @version 1.0.0.0,May17,2008
*/
public class NBConfFileJUnitTest{
private static cn.edu.ynu.sei.customstartup.NBConfFileinstance;
public NBConfFileJUnitTest(){
instance = new cn.edu.ynu.sei.customstartup.NBConfFile();
}
@BeforeClass
public static void setUpClass() throws Exception{
// simulatetheNetBeanslunchertosetsystemproperty
System.setProperty( " netbeans.home " ,
" /home/daniel/Work/netbeans-6.1/platform8 " );
}
@AfterClass
public static void tearDownClass() throws Exception{
}
@Before
public void setUp(){
}
@After
public void tearDown(){
}
@Test
public void getLocation(){
System.out.println( " getLocation " );
assertEquals( " /home/daniel/Work/netbeans-6.1/etc/netbeans.conf " ,
instance.getLocation());
}
@Test
public void getFile(){
System.out.println( " getFile " );
Filenetbeans_conf = instance.getFile();
assertTrue(netbeans_conf.exists());
assertEquals( " netbeans.conf " ,netbeans_conf.getName());
BufferedReaderreader;
// Stringexpect=
// "#${HOME}willbereplacedbyJVMuser.homesystemproperty";
Stringexpect = " ###propertieswrittenbyCustomStartupmodule " ;
Stringactual = null ;
try {
reader = new BufferedReader( new FileReader(netbeans_conf));
actual = reader.readLine();
} catch (IOExceptionex){
Exceptions.printStackTrace(ex);
}
assertEquals(expect,actual);
}
@Test
public void backupConfFile(){
System.out.println( " backupConfFile " );
instance.backupConfFile();
assertTrue(instance.getBackupFile().exists());
}
@Test
public void getBackupFile(){
System.out.println( " getBackupFile " );
Fileactual = instance.getBackupFile();
assertEquals( " netbeans.conf.backup " ,actual.getName());
}
@Test
public void getParameters(){
System.out.println( " getParameters " );
Propertiesparas = instance.getParameters();
Enumeration <?> keys = paras.propertyNames();
while (keys.hasMoreElements()){
Stringkey = keys.nextElement().toString();
Stringvalue = paras.getProperty(key);
System.out.println(key + " = " + value);
}
assertEquals( " "${HOME}/.netbeans/6.1" " ,
paras.getProperty( " netbeans_default_userdir " ));
}
@Test
public void getNetBeansDefaultOptions(){
System.out.println( " getNetBeansDefaultOptions " );
List < String > options = instance.getNetBeansDefaultOptions();
for (Stringoption:options){
System.out.println(option);
}
assertEquals( " -J-client " ,options.get( 0 ));
assertEquals( " --fontsize10 " ,options.get(options.size() - 1 ));
}
@Test
public void getNetBeansJDKHome(){
System.out.println( " getNetBeansJDKHome " );
Stringexpect = " /usr/lib/jvm/java-6-sun/ " ;
Stringactual = instance.getNetBeansJDKHome();
assertEquals(expect,actual);
}
@Test
public void setNetBeansJDKHome(){
System.out.println( " setNetBeansJDKHome " );
StringjdkHome = " /usr/lib/jvm/java-6-sun/ " ;
instance.setNetBeansJDKHome(jdkHome);
assertEquals(jdkHome,instance.getNetBeansJDKHome());
}
@Test
public void addNetBeansDefaultOptions(){
System.out.println( " addNetBeansDefaultOptions " );
Stringpara = " --fontsize15 " ;
instance.addNetBeansDefaultOptions(para);
assertEquals(para,instance.getNetBeansDefaultOptions().get(instance.
getNetBeansDefaultOptions().
size() - 1 ));
}
@Test
public void removeNetBeansDefaultOptions(){
System.out.println( " removeNetBeansDefaultOptions " );
instance.removeNetBeansDefaultOptions( 0 );
assertEquals( " -J-Xss16m " ,instance.getNetBeansDefaultOptions().get( 0 ));
}
@Test
public void editNetBeansDefaultOptions(){
System.out.println( " editNetBeansDefaultOptions " );
instance.editNetBeansDefaultOptions( 0 , " --test10 " );
assertEquals( " --test10 " ,instance.getNetBeansDefaultOptions().get( 0 ));
}
}
3. 编写逻辑实现代码
/*
*@(#)NBConfFile.java
*Author:88250<DL88250@gmail.com>, http://blog.csdn.net/DL88250
*CreatedonMay17,2008,11:23:53AM
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
package cn.edu.ynu.sei.customstartup;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import org.openide.util.Exceptions;
/**
*NetBeansstartupconfigurationfiledescription.
* @author 88250<DL88250@gmail.com>, http://blog.csdn.net/DL88250
* @version 1.0.0.0,May17,2008
*/
public class NBConfFile{
/**
*configurationfilelocation(fullpath,fullname)
*/
private Stringlocation;
/**
*configurationfile
*/
private Fileself;
/**
*Defaultconstructor.
*/
public NBConfFile(){
initLocation();
}
/**
*AddanewNetBeansstartupparameter.
* @param parathenewparametertobeadded
*/
public void addNetBeansDefaultOptions(Stringpara){
List < String > options = getNetBeansDefaultOptions();
options.add(para);
persistNBOptions(options);
}
/**
*EditaNetBeansstartupparameter.
* @param indexparameterindex
* @param newValuethenewparametervalue
*/
public void editNetBeansDefaultOptions( int index,StringnewValue){
List < String > options = getNetBeansDefaultOptions();
if (index >= options.size()){
return ;
}
options.set(index,newValue);
persistNBOptions(options);
}
/**
*RemoveaNetBeansstartupparameter.
* @param indexaparametertobedeleted
*/
public void removeNetBeansDefaultOptions( int index){
List < String > options = getNetBeansDefaultOptions();
if (index >= options.size()){
return ;
}
options.remove(index);
persistNBOptions(options);
}
/**
*Backupconfigurationfile.Thebackupnamed"netbeans.conf.backup",and
*inthesamepathwithoriginal.
*/
public void backupConfFile(){
InputStreaminFile = null ;
OutputStreamoutFile = null ;
try {
inFile = new FileInputStream(location);
outFile = new FileOutputStream(location + " .backup " );
org.openide.filesystems.FileUtil.copy(inFile,outFile);
} catch (IOExceptionex){
Exceptions.printStackTrace(ex);
} finally {
try {
inFile.close();
outFile.close();
} catch (IOExceptionex){
Exceptions.printStackTrace(ex);
}
}
}
/**
*Gettheconfigurationbackupfile.
* @return afilenamed"netbeans.conf.backup"
*/
public FilegetBackupFile(){
Fileret = new File(location + " .backup " );
if (ret.exists()){
return ret;
} else {
throw new RuntimeException( " netbeans.conf.backupinexists! " );
}
}
/**
*Gettheconfigurationfile.
* @return { @link #self}
*/
public FilegetFile(){
self = new File(location);
if (self.exists()){
return self;
} else {
throw new RuntimeException( " netbeans.confinexists! " );
}
}
/**
*Gettheconfigurationfilelocation.
* @return { @link #location}
*/
public StringgetLocation(){
return location;
}
/**
*Getallvaluesdescribedin"netbeans_default_options",file"netbeans.conf".
* @return ifnetbeans_default_options="-J-client-J-Xss16m-J-Xms128m-J-XX:PermSize=256m<b>--fontsize10</b>...",
*returnalistlikethis:{"-J-client","-J-Xss16m","-J-Xms128m","-J-XX:PermSize=256m",<b>"--fontsize10"</b>...}
*/
public List < String > getNetBeansDefaultOptions(){
String[]allOptions = getParameters().getProperty(
" netbeans_default_options " ).split( " /s " );
int last = allOptions.length - 1 ;
// removethefirstdoublequotation
allOptions[ 0 ] = allOptions[ 0 ].substring( 1 ,allOptions[ 0 ].length());
// removethelastdoublequotation
allOptions[last] =
allOptions[last].substring( 0 ,
allOptions[last].length() - 1 );
List < String > ret = new ArrayList < String > ();
for ( int i = 0 ;i < allOptions.length;i ++ ){
Stringitem = allOptions[i];
// confirmthestartupparameter'sbeginning
if (item.length() >= 2 ){
Stringbeginning = item.substring( 0 , 2 );
if (beginning.equals( " -J " )){
// JVMParameters
ret.add(item);
} else if (beginning.equals( " -- " )){
// IDEParameters
if (item.startsWith( " --laf " )){
// --lafui_class_name:UseagivenclassastheIDE'slookandfeel.
ret.add(item + " " + allOptions[i + 1 ]);
} else if (item.startsWith( " --fontsize " )){
// --fontsizesize:UseagivensizeinpointsasthebasicfontsizefortheIDEuserinterface.
ret.add(item + " " + allOptions[i + 1 ]);
}
} else {
// TODOother'sconditions
}
}
}
return ret;
}
/**
*GetJDKHOMEbyNetBeansusing.
* @return theJDKHOMEpath,suchas"/usr/lib/jvm/java-6-sun/"
*/
public StringgetNetBeansJDKHome(){
Stringret = getParameters().getProperty( " netbeans_jdkhome " );
// removethedoublequotation
ret = ret.substring( 1 ,ret.length() - 1 );
return ret;
}
/**
*Getallstartupparametersinfile"netbeans.conf".Everyparameterlikethis:
*<p>
*-J-Xss16m
*-J-XX:PermSize=256m
*-J-Xverify:none
*--lafjavax.swing.plaf.metal.MetalLookAndFeel
*--fontsize10
*</p>
* @return
*/
public PropertiesgetParameters(){
Propertiesparas = new Properties();
BufferedReadernb_confReader = null ;
try {
nb_confReader = new BufferedReader( new FileReader(getFile()));
StringaLine = null ;
while ((aLine = nb_confReader.readLine()) != null ){
if (aLine == null || aLine.equals( "" ) || aLine.charAt( 0 ) == ' # ' ){
continue ;
}
Stringkey = aLine.substring( 0 ,aLine.indexOf( ' = ' ));
Stringvalue = aLine.substring(aLine.indexOf( ' = ' ) + 1 ,
aLine.length());
paras.setProperty(key,value);
}
} catch (IOExceptionex){
Exceptions.printStackTrace(ex);
throw new RuntimeException( " netbeans.confreaderror!! " );
}
return paras;
}
/**
*SetNetBEansJDKHomepath.
*<p>
*NOTE:thespecifiedjdkHomepathisfullpathwithoudoublequotation.
*,suchas<center>/usr/lib/jvm/java-6-sun/</center>
*</p>
* @param jdkHomeJDKHomepath
*/
public void setNetBeansJDKHome(StringjdkHome){
Propertiesparas = getParameters();
paras.setProperty( " netbeans_jdkhome " , " " " + jdkHome + " " " );
setParameters(paras);
}
/**
*PersistNetBeansOptions.
* @param optionsoptionslist
*/
private void persistNBOptions(List < String > options){
Propertiesparas = getParameters();
StringoptionsStr = "" ;
for (Stringoption:options){
optionsStr += option + " " ;
}
optionsStr = optionsStr.trim();
paras.setProperty( " netbeans_default_options " , " " " + optionsStr + " " " );
setParameters(paras);
}
/**
*Setallstartupparametersinfile"netbeans.conf".
* @param parastheparametersstorebya<code>Properties<code>
*/
private void setParameters(Propertiesparas){
BufferedWriterwriter;
try {
writer = new BufferedWriter( new FileWriter(location));
writer.write( " ###propertieswrittenbyCustomStartupmodule " );
writer.newLine();
Enumeration <?> keys = paras.propertyNames();
while (keys.hasMoreElements()){
Stringkey = keys.nextElement().toString();
Stringvalue = paras.getProperty(key);
writer.write(key + " = " + value);
writer.newLine();
}
writer.close();
} catch (IOExceptionex){
Exceptions.printStackTrace(ex);
}
}
/**
*Initializenetbeans.conf'slocation.
*/
private void initLocation(){
location = System.getProperty( " netbeans.home " );
// remove"/platform8"
location = location.substring( 0 ,location.lastIndexOf(File.separator));
// add"/etc/netbeans.conf"
location += File.separator + " etc " + File.separator + " netbeans.conf " ;
// TODOOSsettings
// System.out.println(org.openide.util.Utilities.isUnix());
// System.out.println(org.openide.util.Utilities.isWindows());
}
}
*@(#)NBConfFile.java
*Author:88250<DL88250@gmail.com>, http://blog.csdn.net/DL88250
*CreatedonMay17,2008,11:23:53AM
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*itunderthetermsoftheGNUGeneralPublicLicenseaspublishedby
*theFreeSoftwareFoundation;eitherversion3oftheLicense,or
*(atyouroption)anylaterversion.
*
*Thisprogramisdistributedinthehopethatitwillbeuseful,
*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
*GNULibraryGeneralPublicLicenseformoredetails.
*
*YoushouldhavereceivedacopyoftheGNUGeneralPublicLicense
*alongwiththisprogram;ifnot,writetotheFreeSoftware
*Foundation,Inc.,59TemplePlace-Suite330,Boston,MA02111-1307,USA.
*/
package cn.edu.ynu.sei.customstartup;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import org.openide.util.Exceptions;
/**
*NetBeansstartupconfigurationfiledescription.
* @author 88250<DL88250@gmail.com>, http://blog.csdn.net/DL88250
* @version 1.0.0.0,May17,2008
*/
public class NBConfFile{
/**
*configurationfilelocation(fullpath,fullname)
*/
private Stringlocation;
/**
*configurationfile
*/
private Fileself;
/**
*Defaultconstructor.
*/
public NBConfFile(){
initLocation();
}
/**
*AddanewNetBeansstartupparameter.
* @param parathenewparametertobeadded
*/
public void addNetBeansDefaultOptions(Stringpara){
List < String > options = getNetBeansDefaultOptions();
options.add(para);
persistNBOptions(options);
}
/**
*EditaNetBeansstartupparameter.
* @param indexparameterindex
* @param newValuethenewparametervalue
*/
public void editNetBeansDefaultOptions( int index,StringnewValue){
List < String > options = getNetBeansDefaultOptions();
if (index >= options.size()){
return ;
}
options.set(index,newValue);
persistNBOptions(options);
}
/**
*RemoveaNetBeansstartupparameter.
* @param indexaparametertobedeleted
*/
public void removeNetBeansDefaultOptions( int index){
List < String > options = getNetBeansDefaultOptions();
if (index >= options.size()){
return ;
}
options.remove(index);
persistNBOptions(options);
}
/**
*Backupconfigurationfile.Thebackupnamed"netbeans.conf.backup",and
*inthesamepathwithoriginal.
*/
public void backupConfFile(){
InputStreaminFile = null ;
OutputStreamoutFile = null ;
try {
inFile = new FileInputStream(location);
outFile = new FileOutputStream(location + " .backup " );
org.openide.filesystems.FileUtil.copy(inFile,outFile);
} catch (IOExceptionex){
Exceptions.printStackTrace(ex);
} finally {
try {
inFile.close();
outFile.close();
} catch (IOExceptionex){
Exceptions.printStackTrace(ex);
}
}
}
/**
*Gettheconfigurationbackupfile.
* @return afilenamed"netbeans.conf.backup"
*/
public FilegetBackupFile(){
Fileret = new File(location + " .backup " );
if (ret.exists()){
return ret;
} else {
throw new RuntimeException( " netbeans.conf.backupinexists! " );
}
}
/**
*Gettheconfigurationfile.
* @return { @link #self}
*/
public FilegetFile(){
self = new File(location);
if (self.exists()){
return self;
} else {
throw new RuntimeException( " netbeans.confinexists! " );
}
}
/**
*Gettheconfigurationfilelocation.
* @return { @link #location}
*/
public StringgetLocation(){
return location;
}
/**
*Getallvaluesdescribedin"netbeans_default_options",file"netbeans.conf".
* @return ifnetbeans_default_options="-J-client-J-Xss16m-J-Xms128m-J-XX:PermSize=256m<b>--fontsize10</b>...",
*returnalistlikethis:{"-J-client","-J-Xss16m","-J-Xms128m","-J-XX:PermSize=256m",<b>"--fontsize10"</b>...}
*/
public List < String > getNetBeansDefaultOptions(){
String[]allOptions = getParameters().getProperty(
" netbeans_default_options " ).split( " /s " );
int last = allOptions.length - 1 ;
// removethefirstdoublequotation
allOptions[ 0 ] = allOptions[ 0 ].substring( 1 ,allOptions[ 0 ].length());
// removethelastdoublequotation
allOptions[last] =
allOptions[last].substring( 0 ,
allOptions[last].length() - 1 );
List < String > ret = new ArrayList < String > ();
for ( int i = 0 ;i < allOptions.length;i ++ ){
Stringitem = allOptions[i];
// confirmthestartupparameter'sbeginning
if (item.length() >= 2 ){
Stringbeginning = item.substring( 0 , 2 );
if (beginning.equals( " -J " )){
// JVMParameters
ret.add(item);
} else if (beginning.equals( " -- " )){
// IDEParameters
if (item.startsWith( " --laf " )){
// --lafui_class_name:UseagivenclassastheIDE'slookandfeel.
ret.add(item + " " + allOptions[i + 1 ]);
} else if (item.startsWith( " --fontsize " )){
// --fontsizesize:UseagivensizeinpointsasthebasicfontsizefortheIDEuserinterface.
ret.add(item + " " + allOptions[i + 1 ]);
}
} else {
// TODOother'sconditions
}
}
}
return ret;
}
/**
*GetJDKHOMEbyNetBeansusing.
* @return theJDKHOMEpath,suchas"/usr/lib/jvm/java-6-sun/"
*/
public StringgetNetBeansJDKHome(){
Stringret = getParameters().getProperty( " netbeans_jdkhome " );
// removethedoublequotation
ret = ret.substring( 1 ,ret.length() - 1 );
return ret;
}
/**
*Getallstartupparametersinfile"netbeans.conf".Everyparameterlikethis:
*<p>
*-J-Xss16m
*-J-XX:PermSize=256m
*-J-Xverify:none
*--lafjavax.swing.plaf.metal.MetalLookAndFeel
*--fontsize10
*</p>
* @return
*/
public PropertiesgetParameters(){
Propertiesparas = new Properties();
BufferedReadernb_confReader = null ;
try {
nb_confReader = new BufferedReader( new FileReader(getFile()));
StringaLine = null ;
while ((aLine = nb_confReader.readLine()) != null ){
if (aLine == null || aLine.equals( "" ) || aLine.charAt( 0 ) == ' # ' ){
continue ;
}
Stringkey = aLine.substring( 0 ,aLine.indexOf( ' = ' ));
Stringvalue = aLine.substring(aLine.indexOf( ' = ' ) + 1 ,
aLine.length());
paras.setProperty(key,value);
}
} catch (IOExceptionex){
Exceptions.printStackTrace(ex);
throw new RuntimeException( " netbeans.confreaderror!! " );
}
return paras;
}
/**
*SetNetBEansJDKHomepath.
*<p>
*NOTE:thespecifiedjdkHomepathisfullpathwithoudoublequotation.
*,suchas<center>/usr/lib/jvm/java-6-sun/</center>
*</p>
* @param jdkHomeJDKHomepath
*/
public void setNetBeansJDKHome(StringjdkHome){
Propertiesparas = getParameters();
paras.setProperty( " netbeans_jdkhome " , " " " + jdkHome + " " " );
setParameters(paras);
}
/**
*PersistNetBeansOptions.
* @param optionsoptionslist
*/
private void persistNBOptions(List < String > options){
Propertiesparas = getParameters();
StringoptionsStr = "" ;
for (Stringoption:options){
optionsStr += option + " " ;
}
optionsStr = optionsStr.trim();
paras.setProperty( " netbeans_default_options " , " " " + optionsStr + " " " );
setParameters(paras);
}
/**
*Setallstartupparametersinfile"netbeans.conf".
* @param parastheparametersstorebya<code>Properties<code>
*/
private void setParameters(Propertiesparas){
BufferedWriterwriter;
try {
writer = new BufferedWriter( new FileWriter(location));
writer.write( " ###propertieswrittenbyCustomStartupmodule " );
writer.newLine();
Enumeration <?> keys = paras.propertyNames();
while (keys.hasMoreElements()){
Stringkey = keys.nextElement().toString();
Stringvalue = paras.getProperty(key);
writer.write(key + " = " + value);
writer.newLine();
}
writer.close();
} catch (IOExceptionex){
Exceptions.printStackTrace(ex);
}
}
/**
*Initializenetbeans.conf'slocation.
*/
private void initLocation(){
location = System.getProperty( " netbeans.home " );
// remove"/platform8"
location = location.substring( 0 ,location.lastIndexOf(File.separator));
// add"/etc/netbeans.conf"
location += File.separator + " etc " + File.separator + " netbeans.conf " ;
// TODOOSsettings
// System.out.println(org.openide.util.Utilities.isUnix());
// System.out.println(org.openide.util.Utilities.isWindows());
}
}
4. 开始插件咯~
好了,上面贴了很多代码,貌似和NetBeans的插件开发没什么联系。现在我们正式开始介绍插件开发(模块开发)!a. 添加需要的依赖库,也就是Platform的一些APIs:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/DL88250/273209/o_CSLibraies.png)
b.建立Options Panel类型的文件:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/DL88250/273209/o_Screenshot-New%20File.png)
c. 打开Panel,设计界面:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/DL88250/273209/o_CSPanel.png)
d. 主要事件处理代码:
private void confirmJDKHomeBtnActionPerformed(java.awt.event.ActionEventevt){
StringjdkHomeInput = jdkHomeTextField.getText();
nBConfFile.setNetBeansJDKHome(jdkHomeInput);
}
private void addParasBtnActionPerformed(java.awt.event.ActionEventevt){
StringnewValue = (String) this .argumentsTbl.getCellEditor(
this .argumentsTbl.getEditingRow(), 0 ).getCellEditorValue();
Vector < String > newRow = new Vector < String > ();
newRow.add(newValue);
this .argsTblModel.addRow(newRow);
}
private void removeParasBtnActionPerformed(java.awt.event.ActionEventevt){
int selectedRow = this .argumentsTbl.getSelectedRow();
this .argsTblModel.removeRow(selectedRow);
nBConfFile.removeNetBeansDefaultOptions(selectedRow);
}
private void argumentsTblPropertyChange(java.beans.PropertyChangeEventevt){
int selectedRow = this .argumentsTbl.getSelectedRow();
if (selectedRow >= 0 && selectedRow <= nBConfFile.getNetBeansDefaultOptions().
size()){
// editparameter
StringnewValue = this .argsTblModel.getValueAt(selectedRow,
0 ).toString();
nBConfFile.editNetBeansDefaultOptions(selectedRow,newValue);
} else if (selectedRow >= 0 && this .argsTblModel != null &&
this .argsTblModel.getValueAt(selectedRow, 0 ) != null ){
// addparameter
StringnewValue = this .argsTblModel.getValueAt(selectedRow,
0 ).toString();
nBConfFile.addNetBeansDefaultOptions(newValue);
}
}
5. 测试
直接运行工程就可以了:![](https://p-blog.csdn.net/images/p_blog_csdn_net/DL88250/273209/o_CSTestGUI.png)