Need flexibility in determining when an action should be visible (PopupMenu) [message #220969] |
Thu, 12 July 2007 07:19 |
Eclipse User |
|
|
|
Originally posted by: sourabhsj.mastek.com
Hi,
Currently in my plugin I have provided a menu which pops up when we
right click on any xml file. What I want is to know that is there any way
by which the menu's action is visible only when the user has right clicked
on a specific xml file. Also note that the name of this xml file is not
constant. It can be any thing. This is my popup extension. Here I have
tried using the visibility - a boolean expression to control whether the
menu item is visible in the menu.
<extension
id="com.mastek.jspx.plugin.project.popupmenus"
name="JSPX Pop-up Menus"
point="org.eclipse.ui.popupMenus">
<objectContribution
adaptable="true"
id="com.mastek.jspx.plugin.project.contribution"
nameFilter="*.xml"
objectClass="org.eclipse.core.resources.IFile">
<visibility>
<objectState
name="contentTypeId"
value="com.mastek.jspx.plugin.formDescriptor"/>
</visibility>
<menu
id="com.mastek.jspx.plugin.project.menu"
label="JSPX"
path="additions">
<separator name="group1"/>
</menu>
<action
class="com.mastek.jspx.plugin.actions.GenerateJSPXArtifacts "
enablesFor="5"
id="com.mastek.jspx.plugin.generateAction"
label="Generate"
menubarPath="com.mastek.jspx.plugin.project.menu/group1">
</action>
</objectContribution>
</extension>
I've contributed a custom ContentType
<extension
id="com.mastek.jspx.plugin.formDescriptor"
name="Form Descriptor Content Type"
point="org.eclipse.core.runtime.contentTypes">
<file-association content-type="org.eclipse.core.runtime.xml"
file-names=".project"/>
<content-type
id="formDescriptor"
name="%formDescriptorContentTypeName"
base-type="org.eclipse.core.runtime.xml"
priority="high"
file-extensions="xml">
<describer
class=" com.mastek.jspx.plugin.contentTypes.FormDescriptorContentDes criber ">
</describer>
</content-type>
</extension>
This is my ITextContentDescriber implementation
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.xerces.parsers.DOMParser;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.core.runtime.content.ITextContentDescriber;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/**
* A content describer that is used to discriminate between various content
* types in order to derive which editor should be used to edit the
content.
*
* @author Sourabh S Joshi
*/
public class FormDescriptorContentDescriber implements
ITextContentDescriber {
private static final String Invalid = "invalid";
private static final String Valid = "valid";
private List acceptableRootElements = new ArrayList();
private DOMParser parser = new DOMParser();
public FormDescriptorContentDescriber() {}
public int describe(Reader contents, IContentDescription description)
throws IOException {
String string = doDescribe(contents);
//String status = doDescribe(contents);
if (string.equalsIgnoreCase("invalid")) {
System.out.println("Returning Invalid");
} else {
System.out.println("Returning Valid");
}
return (string == null) ? INVALID : VALID;
//return (string == null) ? INVALID : VALID;
}
private boolean isValidFormDescriptor(Document doc) {
try {
NodeList nList = doc.getChildNodes();
if ((nList == null) || (nList.getLength() == 0)) {
return false;
} else {
String str = doc.getDocumentElement().getNodeName();
if (!str.equals("jspx")) {
return false;
} else {
NodeList nList1 =
doc.getDocumentElement().getChildNodes();
for (int i = 0; i < nList1.getLength(); i++) {
if (nList1.item(i).getNodeName().equals("forms")) {
return true;
}
}
nList1 = doc.getElementsByTagName("usercontrols");
if (nList1.getLength() > 0) {
return true;
}
}
}
} catch (Exception e) {
// TODO Exception handling needs to be done
e.printStackTrace();
}
return false;
}
private String doDescribe(Reader contents) throws IOException {
if (acceptableRootElements.isEmpty())
return Invalid;
InputSource source = new InputSource(contents);
try {
parser.setEntityResolver(null);
parser.setFeature(" http://apache.org/xml/features/nonvalidating/load-external-d td",
false);
parser.setFeature("http://xml.org/sax/features/validation",
false);
parser.setFeature("http://xml.org/sax/features/external-general-entities",
false);
parser.parse(source);
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
Document doc = parser.getDocument();
if (!isValidFormDescriptor(doc)) {
return Invalid;
}
} catch (Exception e) {
e.printStackTrace();
return Invalid;
}
return Valid;
}
public int describe(InputStream contents, IContentDescription description)
throws IOException {
return describe(new InputStreamReader(contents), description);
}
private String doDescribe(InputStream contents) throws IOException {
return doDescribe(new InputStreamReader(contents));
}
public QualifiedName[] getSupportedOptions() {
return null;
}
}
When I launch an Eclipse Application the menu item never shows upmin the
popup menu. Please help me.
|
|
|
|
Powered by
FUDForum. Page generated in 0.08769 seconds