2014-12-07 64 views
3

我创建了一个IntelliJIdea插件,使用行为条目plugin.xml如何IntelliJIdea插件发展的行动仅添加的strings.xml文件的Android

<actions> 
    <!-- Add your actions here --> 
    <group id="AL.Localize" text="_Localize" description="Localize strings" > 
     <add-to-group group-id="ProjectViewPopupMenu"/> 

     <action id="AL.Convert" class="action.ConvertToOtherLanguages" text="Convert to other languages" 
       description="Convert this strings.xml to other languages that can be used to localize your Android app."> 

     </action> 
    </group> 
    </actions> 

使用此设置,我的动作将用户后出现右键单击文件。就像这样: right-click popup menu

的问题是,Convert to other languages菜单显示的时候,我只希望这个菜单显示在string.xml文件时,用户单击鼠标右键,像Open Translation Editor(Preview)菜单一样。 (Open Translation Editor(Preview)是Android Studio在version 0.8.7中推出的功能)

我该怎么办?

回答

4

不知道是否有一种方法可以纯粹在XML中完成此操作,所以如果您知道方式,其他人可以随意插入,但有一种方法可以在Java中执行此操作。

在您的动作的更新方法中,您可以使用动作的Presentation对象根据文件的名称设置动作是否可见。这是一个基于Android Studio中的ConvertToNinePatchAction一个例子:

package com.example.plugin; 

import com.intellij.openapi.actionSystem.AnAction; 
import com.intellij.openapi.actionSystem.AnActionEvent; 
import com.intellij.openapi.actionSystem.CommonDataKeys; 
import com.intellij.openapi.vfs.VirtualFile; 

import org.jetbrains.annotations.Nullable; 

public class ConvertToOtherLanguages extends AnAction { 

    public ConvertToOtherLanguages() { 
     super("Convert to other languages"); 
    } 

    @Override 
    public void update(AnActionEvent e) { 
     final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext()); 
     final boolean isStringsXML = isStringsFile(file); 
     e.getPresentation().setEnabled(isStringsXML); 
     e.getPresentation().setVisible(isStringsXML); 
    } 

    @Contract("null -> false") 
    private static boolean isStringsFile(@Nullable VirtualFile file) { 
     return file != null && file.getName().equals("string.xml"); 
    } 

    @Override 
    public void actionPerformed(AnActionEvent e) { 
     // Do your action here 
    } 
} 

然后在XML,你的行动应该是这样的:

<action id="AL.Convert" class="com.example.plugin.ConvertToOtherLanguages"> 
    <add-to-group group-id="ProjectViewPopupMenu" anchor="last" /> 
</action> 
+1

太好了!非常感谢。 – Wesley 2014-12-07 09:20:38

相关问题