【问题标题】:Eclipse: Accessing a editor template from plugin codeEclipse:从插件代码访问编辑器模板
【发布时间】:2009-01-14 06:50:48
【问题描述】:

假设我在编辑器首选项中定义了一个编辑器模板(它插入一些任意的 sn-p 代码)。

我想以编程方式访问该模板。 我该怎么做?

我知道存在 TemplateStore、TemplatePreferencesPage 和 TemplatePersistentData 类,但我无法将它们组合成任何工作。

是否有任何示例代码可以让我通过 Java 代码访问我的编辑器模板?

【问题讨论】:

    标签: java eclipse eclipse-plugin


    【解决方案1】:

    可能是这个JavaPlugin class(在 eclipse 的 org.eclipse.jdt.internal.ui 包中)可能会为您提供第一个线索。

     /**
      * Returns the template store for the code generation templates.
      *
      * @return the template store for the code generation templates
      * @since 3.0
      */
     public TemplateStore getCodeTemplateStore() {
         if (fCodeTemplateStore == null) {
             IPreferenceStore store= getPreferenceStore();
             boolean alreadyMigrated= store.getBoolean(CODE_TEMPLATES_MIGRATION_KEY);
             if (alreadyMigrated)
                 fCodeTemplateStore= new ContributionTemplateStore(getCodeTemplateContextRegistry(), store, CODE_TEMPLATES_KEY);
             else {
                 fCodeTemplateStore= new CompatibilityTemplateStore(getCodeTemplateContextRegistry(), store, CODE_TEMPLATES_KEY, getOldCodeTemplateStoreInstance());
                 store.setValue(CODE_TEMPLATES_MIGRATION_KEY, true);
             }
    
             try {
                 fCodeTemplateStore.load();
             } catch (IOException JavaDoc e) {
                 log(e);
             }
    
             fCodeTemplateStore.startListeningForPreferenceChanges();
    
             // compatibility / bug fixing code for duplicated templates
             // TODO remove for 3.0
            CompatibilityTemplateStore.pruneDuplicates(fCodeTemplateStore, true);            
         }
    
         return fCodeTemplateStore;
     }
    

    从那里,您可以找到一些使用该函数的类:

    NewASInterfaceWizard 似乎需要访问那些代码模板:

    private String resolveTemplate(String templateName) {
    
            Template template = ASEditorPlugin.getDefault().getCodeTemplateStore().findTemplate(templateName);
            if (template == null) {
                showErrorBox("Could not resolve template (" + templateName +").");
                return "";
            }
    
            // Create the template context
            TemplateContext templeteContext = new TemplateContext(new ASContextType()) {
    
                public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
                    TemplateTranslator translator = new TemplateTranslator();
                    TemplateBuffer buffer = translator.translate(template);
                    getContextType().resolve(buffer, this);
                    return buffer;
                }
    
                public boolean canEvaluate(Template template) {
                    return true;
                }
    
            };
    
            try {
                return templeteContext.evaluate(template).getString();
            } catch (BadLocationException e) {
                logger.error("Couldnt evaluate template",e);
            } catch (TemplateException e) {
                logger.error("Couldnt evaluate template",e);
            }
           return "";
    
    }
    

    这样使用:

            private static final String FILE_HEADER_TEMPLATE = "file_header";
            // Header
            String header = resolveTemplate(FILE_HEADER_TEMPLATE);
            if (header.length() > 0) {
                content.append(header + "\n");
            }
    

    【讨论】:

      【解决方案2】:

      嗯,我就是这样做的。

      /**
       * Get the Template Store of the JDT UI.
       * 
       * @return the JDT template store
       */
      private TemplateStore getTemplateStore() {
          if (templateStore == null) {
              System.out.println("templateStore is null - Creating a new one");
      
              final ContributionContextTypeRegistry registry = new ContributionContextTypeRegistry(JavaUI.ID_CU_EDITOR);
              final IPreferenceStore store = PreferenceConstants.getPreferenceStore();
      
              templateStore = new ContributionTemplateStore(registry, store, TEMPLATES_KEY);
      
              try {
                  templateStore.load();
              } catch (IOException e) {
                  WSConsole.e(e);
              }
              templateStore.startListeningForPreferenceChanges();
          }
          return templateStore;
      }
      

      上述方法返回 TemplateStore。您使用商店来添加、删除、查找模板。

      private void filterTemplates() {
          templateStore = getTemplateStore();
      
          deleteTemplate(templateStore, "org.eclipse.jdt.ui.templates.sysout");
      
          try {
              templateStore.save();
          } catch (IOException e) {
          }
      }
      
      private void deleteTemplate(TemplateStore templateStore, String id) {
          TemplatePersistenceData templateData = templateStore.getTemplateData(id);
          if (templateData != null) {
              templateStore.delete(templateData);
          }
      }
      

      这是对 TemplateStore 执行操作的方法。如果要查找模板的模式、名称、描述,可以通过从 TemplateStore 获取 Template 对象来获取。如果要查找模板的 id,可以从 TemplatePersistantData 对象中获取。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多