Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with Eclipse plugin (copying a file)
    primarykey
    data
    text
    <p>Ok, so I've written a plugin for Eclipse (ok, so it's mostly copied code from ADT). It is a new project wizard which does essentially the same thing as ADT's new Project wizard, only it copies over a library, adds it to the build path, and copies over some other files. This all works fine.</p> <p>The library is called AltBridge, which was built from App Inventor's Java Bridge library. This library essentially changes how you write an activity a little bit, and I wanted to add a menu option to create a new Form (which is a modified Activity).</p> <p>This is where Eclipse is being bothersome. I got it to work fine if I hit F11. It creates the new file no problem. However, when I export the plugin, and try it, it doesn't work.</p> <p>Here's what I have so far:</p> <pre><code>public class NewFormMenuSelection extends AbstractHandler implements IHandler { private static final String PARAM_PACKAGE = "PACKAGE"; private static final String PARAM_ACTIVITY = "ACTIVITY_NAME"; private static final String PARAM_IMPORT_RESOURCE_CLASS = "IMPORT_RESOURCE_CLASS"; private Map&lt;String, Object&gt; java_activity_parameters = new HashMap&lt;String, Object&gt;(); public Object execute(ExecutionEvent event) throws ExecutionException { IStructuredSelection selection = (IStructuredSelection) HandlerUtil .getActiveMenuSelection(event); String directory = ""; Object firstElement = selection.getFirstElement(); if (firstElement instanceof IPackageFragment) { InputDialog dialog = new InputDialog(HandlerUtil.getActiveShell(event), "Name of the new Form to create?", "Please enter the name of the new form to create.", "NewForm", null); if ( dialog.open()==IStatus.OK ) { String activityName = dialog.getValue(); String packageName = ((IPackageFragment) firstElement).getElementName(); if (activityName != null) { String resourcePackageClass = null; // An activity name can be of the form ".package.Class", ".Class" or FQDN. // The initial dot is ignored, as it is always added later in the templates. int lastDotIndex = activityName.lastIndexOf('.'); if (lastDotIndex != -1) { // Resource class if (lastDotIndex &gt; 0) { resourcePackageClass = packageName + "." + AdtConstants.FN_RESOURCE_BASE; //$NON-NLS-1$ } // Package name if (activityName.startsWith(".")) { //$NON-NLS-1$ packageName += activityName.substring(0, lastDotIndex); } else { packageName = activityName.substring(0, lastDotIndex); } // Activity Class name activityName = activityName.substring(lastDotIndex + 1); } java_activity_parameters.put(PARAM_IMPORT_RESOURCE_CLASS, ""); java_activity_parameters.put(PARAM_ACTIVITY, activityName); java_activity_parameters.put(PARAM_PACKAGE, packageName); if (resourcePackageClass != null) { String importResourceClass = "\nimport " + resourcePackageClass + ";"; //$NON-NLS-1$ // $NON-NLS-2$ java_activity_parameters.put(PARAM_IMPORT_RESOURCE_CLASS, importResourceClass); } if (activityName != null) { // create the main activity Java file // get the path of the package IPath path = ((IPackageFragment) firstElement).getPath(); String pkgpath = path.toString(); // Get the project name String activityJava = activityName + AdtConstants.DOT_JAVA; String projname = ((IPackageFragment) firstElement).getParent().getJavaProject().getElementName(); // Get the project IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projname); IFolder pkgFolder = project.getFolder(pkgpath); IFile file = pkgFolder.getFile(activityJava); if (!file.exists()) { try { copyFile("java_file.template", file, java_activity_parameters, false); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } } return null; } private void copyFile(String resourceFilename, IFile destFile, Map&lt;String, Object&gt; parameters, boolean reformat) throws CoreException, IOException { // Read existing file. String template = AltBridge.readEmbeddedTextFile( "templates/" + resourceFilename); // Replace all keyword parameters template = replaceParameters(template, parameters); if (reformat) { // Guess the formatting style based on the file location XmlFormatStyle style = XmlFormatStyle.getForFile(destFile.getProjectRelativePath()); if (style != null) { template = reformat(style, template); } } // Save in the project as UTF-8 InputStream stream = new ByteArrayInputStream(template.getBytes("UTF-8")); //$NON-NLS-1$ destFile.create(stream, true /* force */, null); } private String replaceParameters(String str, Map&lt;String, Object&gt; parameters) { if (parameters == null) { AltBridge.log(IStatus.ERROR, "NPW replace parameters: null parameter map. String: '%s'", str); //$NON-NLS-1$ return str; } else if (str == null) { AltBridge.log(IStatus.ERROR, "NPW replace parameters: null template string"); //$NON-NLS-1$ return str; } for (Entry&lt;String, Object&gt; entry : parameters.entrySet()) { if (entry != null &amp;&amp; entry.getValue() instanceof String) { Object value = entry.getValue(); if (value == null) { AltBridge.log(IStatus.ERROR, "NPW replace parameters: null value for key '%s' in template '%s'", //$NON-NLS-1$ entry.getKey(), str); } else { str = str.replaceAll(entry.getKey(), (String) value); } } } return str; } private String reformat(XmlFormatStyle style, String contents) { if (AdtPrefs.getPrefs().getUseCustomXmlFormatter()) { XmlFormatPreferences formatPrefs = XmlFormatPreferences.create(); return XmlPrettyPrinter.prettyPrint(contents, formatPrefs, style, null /*lineSeparator*/); } else { return contents; } } </code></pre> <p>The part I seem to be having the problem with is under where it says // create the main activity java file.</p> <p>In order to get it to work when hitting F11, I had to drop the first two entries of the package (I know I could have probably done this a better way, but it works). Otherwise, for some reason, it added the project name to the path at the beginning (doubling it). If left this way, it won't work when exported and tried outside of the testing environment. It gives an error saying that the project name must be in the path. So, if I remove the section that drops that, it doesn't work when hitting F11, and outside the build environment, it doesn't work, and doesn't give any kind of error. Any clues on what I may be doing wrong?</p> <p>Here's the copy file code (again, this is just copied from the ADT plugin) :</p> <pre><code>public static String readEmbeddedTextFile(String filepath) { try { InputStream is = readEmbeddedFileAsStream(filepath); if (is != null) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line; StringBuilder total = new StringBuilder(reader.readLine()); while ((line = reader.readLine()) != null) { total.append('\n'); total.append(line); } return total.toString(); } } catch (IOException e) { // we'll just return null AltBridge.log(e, "Failed to read text file '%s'", filepath); //$NON-NLS-1$ } return null; } public static InputStream readEmbeddedFileAsStream(String filepath) { // attempt to read an embedded file try { URL url = getEmbeddedFileUrl(AdtConstants.WS_SEP + filepath); if (url != null) { return url.openStream(); } } catch (MalformedURLException e) { // we'll just return null. AltBridge.log(e, "Failed to read stream '%s'", filepath); //$NON-NLS-1$ } catch (IOException e) { // we'll just return null;. AltBridge.log(e, "Failed to read stream '%s'", filepath); //$NON-NLS-1$ } return null; } public static URL getEmbeddedFileUrl(String filepath) { Bundle bundle = null; synchronized (AltBridge.class) { if (plugin != null) { bundle = plugin.getBundle(); } else { AltBridge.log(IStatus.WARNING, "ADT Plugin is missing"); //$NON-NLS-1$ return null; } } // attempt to get a file to one of the template. String path = filepath; if (!path.startsWith(AdtConstants.WS_SEP)) { path = AdtConstants.WS_SEP + path; } URL url = bundle.getEntry(path); if (url == null) { AltBridge.log(IStatus.INFO, "Bundle file URL not found at path '%s'", path); //$NON-NLS-1$ } return url; } </code></pre> <p>Ok, the issue seems to be with IProject, or IFolder. The path gets returned as "test/src/com/test" for example. Then, when I use pkgfolder.getFile, it adds another /test/ in front of the path (this is when testing by hitting F11 in eclipse).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload