Simple Plugin - Know the path of a file [message #660550] |
Sat, 19 March 2011 09:02 |
Giancarlo Messages: 5 Registered: March 2011 |
Junior Member |
|
|
Hello!!!
I'm new with Eclipse plugin development !!!
I'm studying this tutorial : http:// www.vogella.de/articles/EclipsePlugIn/article.html#contribut e , but I'd like to create a plugin so when I right-click on a file (for example an html file) and choose for example the Option "Modify HTML" (that I've created), I can open and modify that file.
I think I need only to know the path of the file that I right-click, and so I can open and modify that file using other java-classes.
The code used in this tutorial is :
public class Convert extends AbstractHandler {
private QualifiedName path = new QualifiedName("html", "path");
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IStructuredSelection selection = (IStructuredSelection) HandlerUtil
.getActiveMenuSelection(event);
DirectoryDialog fileDialog = new DirectoryDialog(HandlerUtil
.getActiveShell(event));
String directory = "";
Object firstElement = selection.getFirstElement();
if (firstElement instanceof ICompilationUnit) {
ICompilationUnit cu = (ICompilationUnit) firstElement;
IResource res = cu.getResource();
boolean newDirectory = true;
directory = getPersistentProperty(res, path);
if (directory != null && directory.length() > 0) {
newDirectory = !(MessageDialog.openQuestion(HandlerUtil
.getActiveShell(event), "Question",
"Use the previous output directory?"));
}
if (newDirectory) {
directory = fileDialog.open();
}
if (directory != null && directory.length() > 0) {
analyze(cu);
setPersistentProperty(res, path, directory);
write(directory, cu);
}
} else {
MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
"Information", "Please select a Java source file");
}
// iterator.next();
// }
return null;
}
protected String getPersistentProperty(IResource res, QualifiedName qn) {
try {
return (String) res.getPersistentProperty(qn);
} catch (CoreException e) {
return "";
}
}
// TODO: Include this in the HTML output
private void analyze(ICompilationUnit cu) {
// Cool JDT allows you to analyze the code easily
// I don't see really a use case here but I just wanted to do this here
// as I consider this as cool and
// what to have a place where I can store the data
try {
IType type = null;
IType[] allTypes;
allTypes = cu.getAllTypes();
/**
* Search the public class
*/
for (int t = 0; t < allTypes.length; t++) {
if (Flags.isPublic((allTypes[t].getFlags()))) {
type = allTypes[t];
break;
}
}
String classname = type.getFullyQualifiedName();
IMethod[] methods = type.getMethods();
} catch (JavaModelException e) {
e.printStackTrace();
}
}
protected void setPersistentProperty(IResource res, QualifiedName qn,
String value) {
try {
res.setPersistentProperty(qn, value);
} catch (CoreException e) {
e.printStackTrace();
}
}
private void write(String dir, ICompilationUnit cu) {
try {
cu.getCorrespondingResource().getName();
String test = cu.getCorrespondingResource().getName();
// Need
String[] name = test.split("\\.");
System.out.println(test);
System.out.println(name.length);
String htmlFile = dir + "\\" + name[0] + ".html";
System.out.println(htmlFile);
FileWriter output = new FileWriter(htmlFile);
BufferedWriter writer = new BufferedWriter(output);
writer.write("<html>");
writer.write("<head>");
writer.write("</head>");
writer.write("<body>");
writer.write("<pre>");
writer.write(cu.getSource());
writer.write("</pre>");
writer.write("</body>");
writer.write("</html>");
writer.flush();
} catch (JavaModelException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
}
The example given in the tutorial is similar, but with that code I can only open java files.
I think the problem is ICompilationUnit that represents an entire Java compilation unit, but I hadn't understood how I can modify that code to re-use it.
Who can help me ????
Thanks.
[Updated on: Sun, 20 March 2011 11:54] Report message to a moderator
|
|
|
|
|
Re: Simple Plugin - Know the path of a file [message #661876 is a reply to message #660550] |
Mon, 28 March 2011 11:24 |
|
On 03/19/2011 05:03 AM, giancarlo.capone@yahoo.com wrote:
> Hello!!!
> I'm new with Eclipse plugin development !!!
> I'm studying this tutorial :
> http://www.vogella.de/articles/EclipsePlugIn/article.html#co ntribute ,
> but I'd like to create a plugin so when I right-click on a file (for
> example an html file) and choose for example the Option "Modify HTML"
> (that I've created), I can open and modify that file. I think I need
> only to know the path of the file that I right-click, and so I can open
> and modify that file using other java-classes.
You always deal with IFiles for files within a workspace. In the
example, because they knew it was an ICompilationUnit they could get the
resource:
IResource res = cu.getResource();
The other way is to adapt to the resource, using the adapter manager:
org.eclipse.core.runtime.Platform.getAdapterManager().getAda pter(firstElement,
IFile.class);
If you want to *write* your output in the same directory ... don't use
that example, that's not what it is doing. Use the IFile to get the
parent directory, and then create a new IFile and write to that.
Later,
PW
--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02452 seconds