很抱歉,最近总是很忙,一个开头之后,这么久没了下文 ,并不是为了调大家的胃口,工作所迫,大家多多理解,好了,废话少讲,开始正文.

3.

SpeedStudio开发步骤

(1).建立eclipse插件工程.

因为我们从弹出菜单(popupmenu)开始开发,所以建立插件工程时,我们可以在工程向导中选择相应模板,自动生成相应的plugin.xml内容.

新建插件工程

选择弹出菜单的模板:

下面设置详细的信息,目标对象的类指弹出菜单项在何种类型的对象上点击右键后出现,有IFile, IResource等,在SpeedStudio中我们选择IResource,即所有资源对象都会有此弹出菜单项,操作类,是点击弹出菜单项后出发的动作,是对动作的相应类.



点击完成后,就会生成相应的配置文件和资源文件,请看plugin.xml生成的内容大致如下:

<extension   
         point="org.eclipse.ui.popupMenus">   
      <objectContribution   
            id="org.speedframework.studio.actions.ConfigGenAction"   
            nameFilter="*"   
            adaptable="true"   
            objectClass="org.eclipse.core.resources.IResource">   
         <menu   
               id="org.speedframework.studio.popmenus"   
               label="Speedframework Studio"   
               path="additions">   
               <separator name="groupSub"/>   
         </menu>   
         <action   
               class="org.speedframework.studio.actions.ConfigGenAction"   
               enablesFor="+"   
               id="org.speedframework.studio.actions.ConfigGenAction"   
               label="生成speedframework配置文件"   
               menubarPath="org.speedframework.studio.popmenus/groupSub"/>   
      </objectContribution>   
</extension>

以上就是弹出菜单扩展的对应的配置代码.这样一个eclipse插件工程就建立成功,并且加了弹出菜单扩展和点击菜单项后动作实现类(不过他什么也没有实现).

(2).实现弹出菜单相关功能.

[1]动作类实现.

上面我们定义了弹出菜单行为的动作类,该类实现IObjectActionDelegate接口,该接口扩

展了接口IActionDelegate,我们实现了run方法和selectionChanged方法.

       run 方法在右击活动被调用时执行,在这里我们实例化一个向导并显示它, selectionChanged方法帮助我们检测右键选择了那个节点(ISelection).我们的ConfigGenAction拥有一个IStructuredSelection属性,用于实例化向导.代码大概如下:

 

/**   
     * 描述:   
     * @author Stone yang   
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)   
     */   
    public void run(IAction action) {   
       // TODO 自动生成方法存根   
       IWizard wizard = new ConfigGenWizard(structSelect);     
       Dialog wizardDlg = new WizardDialog(Activator.getShell(),wizard);   
       wizardDlg.create();   
       wizardDlg.open();   
    }   
   
      
  
    /**   
     * 描述:   
     * @author Stone yang   
     * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)   
     */   
    public void selectionChanged(IAction action, ISelection selection) {   
       // TODO 自动生成方法存根   
       structSelectIStructuredSelection) selection;   
    }

[2]向导类实现.

       上面我们创建了ConfigGenWizard向导类,扩展了Wizard抽象类,主要是重载了addPages方法和performFinish方法.

    Wizard可以说是一个容器,具体的内容展现来自内部”组件”page, addPages方法就是为该容器添加组件的,请容许我使用这个不是很准确的比方来讲解. performFinish方法是在用户点击”完成”按钮后需要实现的动作.

该类具体内容如下:

public class ConfigGenWizard extends Wizard{   
       
    private IStructuredSelection structSelect;   
   
      
  
    private ConfigGenPage configGenPage;   
       
    private IPackageFragmentRoot targetSrcFolder;   
       
    private IFileGenerator iFileGenerator;   
       
    public ConfigGenWizard(IStructuredSelection structSelect) {   
       // TODO 自动生成构造函数存根   
       super();   
       this.structSelect = structSelect;   
    }   
   
      
  
    @Override   
    public void addPages()   
    {   
       super.addPages();   
       configGenPage = new ConfigGenPage(   
              "生成speed.cfg.xml", structSelect);   
       addPage(configGenPage);   
   
      
  
    }   
       
    @Override   
    public booleanperformFinish() {   
       ConfigGenInfo configGenInfo = getConfigGenInfo();   
          
       try {   
           processConfigGen(configGenInfo);   
       } catch (CodeGenException e) {   
           // TODO 自动生成 catch 块   
           e.printStackTrace();   
           return false;   
       }   
          
       // TODO 自动生成方法存根   
       return true;   
    }   
   
      
  
    /**   
     *    
     * 描述:   
     * @author Stone yang   
     * 创建时间:2007-6-13   
     * @throws CodeGenException    
     */   
    private void processConfigGen(ConfigGenInfo configGenInfo) throws CodeGenException {   
       // TODO 自动生成方法存根   
       targetSrcFolder = configGenPage.getPackageFragmentRoot();   
        iFileGenerator = configGenPage.getFileGenerator();   
       iFileGenerator.setTargetSourceFolder(targetSrcFolder);   
          
       iFileGenerator.generateFile(configGenInfo);   
    }   
   
      
  
    /**   
     * 描述:   
     * @author Stone yang   
     * 创建时间:2007-6-13    
    */   
    private ConfigGenInfo getConfigGenInfo() {   
       ConfigGenInfo configGenInfo = new ConfigGenInfo();   
       configGenInfo.setDriveClassName(configGenPage.getDriveClassName());   
       configGenInfo.setHostIp(configGenPage.getIp());   
       configGenInfo.setUserName(configGenPage.getUserName());   
       configGenInfo.setPassword(configGenPage.getPassword());   
        configGenInfo.setSpeedId(ConfigGenConstant.driveClass_SpeedIdMap.get(configGenPage.getDriveClassName()));   
       configGenInfo.setCharset(configGenPage.getCharset());   
       configGenInfo.setDbName(configGenPage.getDbNameAndPort());   
          
       return configGenInfo;   
    }   
   
      
  
}

从上面的代码可以看到,该向导类是在开发者设定了相关的设置后进行配置文件的生成. ConfigGenWizard 拥有一个page页面属性,addPages方法实例化了该page页面属性configGenPage,可以在整个类中拥有该页面类句柄,用于获取设置信息.