OnSave actions in NetBeans 6.9 - netbeans

Is there any way to tell NetBeans to execute a specific action when saving a file? e.g. removing unused imports while saving a source file?

This was an interesting question... as I believe you would have to write a custom NetBeans plugin to do what you're wanting (the functionality isn't available out-of-the-box), and I've been looking for an excuse to explore NetBeans plugin development.
However, after spending a couple of hours reading tutorials and crawling through the javadocs... it's become clear that this subject is quite a big bite to chew, and probably way more involved than you're wanting.
I think the BEST suggestion is forget about removing unused imports at save-time, and instead perform this step at build-time. NetBeans offers great integration with Ant and/or Maven (for build purposes it's basically just a GUI wrapper around those tools), and there are a number of Ant tasks out there that can do what you're wanting. See:
http://ant.apache.org/external.html
(look for the "CleanImports" and "Importscrubber" tasks)
If your NetBeans project(s) are Maven-based, then you can always plug in one of these Ant tasks there using the AntRun plugin for Maven.
If you're not used to dealing with Ant or Maven directly in NetBeans, then just switch to the "Files" tab and look at your project's root directory. If its a Maven project, the build script will be named pom.xml. Otherwise, your project will generally be Ant-based and the build script will be named build.xml. The documentation for these items above should make it fairly clear how to move forward from there.
I notice that those two Ant tasks haven't been updated in awhile, so if you run into issues you might want to check out the very popular and up-to-date PMD system, which has its own documentation for integrating with NetBeans. However, the issue there is PMD is primarily for generating reports... I don't know if it can be used to actually take action and change source files.

Not exactly an answer to your question, but note that NB 7.1 lets you fix imports on the whole project at once: http://wiki.netbeans.org/NewAndNoteworthyNB71#Organize_Imports_Hint

This is not a good practice and NetBeans does not support it.

I resurrect this topic.
Well this code code is tested with Netbeans 7.4.
here I'm overriding the default save action in the actionPerformed method.
If you choose to do this by yourself create a new Action using the wizard then call the save action inside actionPerformed method.
package yourpackage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;
#ActionID(
category = "File",
id = "BZ.SaveAction"
)
#ActionRegistration(
iconBase = "BZ/Save.png",
displayName = "#CTL_SaveAction"
)
#ActionReferences({
#ActionReference(path = "Menu/File", position = 750),
#ActionReference(path = "Toolbars/File", position = 0),
#ActionReference(path = "Shortcuts", name = "D-S")
})
#Messages("CTL_SaveAction=Save")
public final class SaveAction implements ActionListener {
org.openide.actions.SaveAction sa = org.openide.util.actions.CallbackSystemAction.get(org.openide.actions.SaveAction.class);
#Override
public void actionPerformed(ActionEvent e) {
// custom code
JOptionPane.showMessageDialog(null, "custum message ");
sa.performAction();
}
}

Goto Tools-> Options select Editor there select On Save Tab now select Java from drop down menu. So, now select Organize Imports option. Hope this will help you.

Related

Using ISVNClientAdapter for svn operations in eclipse plugin

I am trying to use ISVNClientAdapter from org.tigris.subversion.svnclientadapter to invoke svn operations from my eclipse plugin. It seems to offer support for various operations, but it is unclear to me how to use them, starting from a project given as IProject or an SVNTeamProvider.
Can anyone give me a short example how to apply operations (like commit or getStatus)?
One way to go seems to be (If project is the object of type IProject):
ISVNClientAdapter adapter = SVNProviderPlugin.getPlugin().getSVNClient();
File file = new File(project.getLocation().toString());
ISVNStatus[] status = adapter.getStatus(file, true, false);
Together with the absolute path stored in file, adapter can perform different operations on the svn.

Is there auto-import functionality for typescript in Visual Studio Code?

Is there any shortcut that will allow me to auto generate my typescript imports? Like hitting ctrl+space next to the type name and having the import declaration placed at the top of the file. If not, what about intellisense for filling out the module reference path so that I wont have to do it manually? I would really love to use vscode but having to do manual imports for typescript is killing me.
There are rumors of making it support tsconfig.json (well, better than rumors). This will allow us to be able to use all files for our references.
Your feature would be to create an auto import of all commonly used 3rd party libs into the typings. Perhaps auto scan the files and create a list of ones to go gather. Wouldn't it be fine to just have a quick way to add several of these using tsd directly from Code (interactively)?
I believe the plugin called "TypeScript Importer" does exactly what You mean: https://marketplace.visualstudio.com/items?itemName=pmneo.tsimporter .
Automatically searches for TypeScript definitions in workspace files and provides all known symbols as completion item to allow code completion.
With it You can truly use Ctrl+Space to choose what exactly You would like to be imported.
You can find and install it from Ctrl+Shift+X menu or just by pasting ext install tsimporter in Quick Open menu opened with Ctrl+P.
I know a solution for Visual Studio (not Visual Studio Code, I'm using the 2015 Community edition, which is free), but it needs some setup and coding -- however, I find the results to be adequate.
Basically, in Visual Studio, when using the Web-Essentials extension, .ts files can be dragged into the active document to automatically generate a relative reference path comment:
/// <reference path="lib/foo.ts" />
With which of course we might as well wipe it, because it's an import statement we need, not a reference comment.
For this reason, I recently wrote the following command snippet for Visual Commander, but it should be easily adaptable to other use casese as well. With Visual Commander, your drag the needed imports into the open document, then run the following macro:
using EnvDTE;
using EnvDTE80;
using System.Text.RegularExpressions;
public class C : VisualCommanderExt.ICommand
{
// Called by Visual Commander extension.
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
TextDocument doc = (TextDocument)(DTE.ActiveDocument.Object("TextDocument"));
var p = doc.StartPoint.CreateEditPoint();
string s = p.GetText(doc.EndPoint);
p.ReplaceText(doc.EndPoint, this.ReplaceReferences(s), (int)vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers);
}
// Converts "reference" syntax to "ES6 import" syntax.
private string ReplaceReferences(string text)
{
string pattern = "\\/\\/\\/ *<reference *path *= *\"([^\"]*)(?:\\.ts)\" *\\/>";
var regex = new Regex(pattern);
var matches = Regex.Matches(text, pattern);
return Regex.Replace(text, pattern, "import {} from \"./$1\";");
}
}
When running this snippet, all reference comments in the active document will be replaced with import statements. The above example is converted to:
import {} from "./lib/foo";
This has just been released in version 1.18.
From the release notes:
Auto Import for JavaScript and TypeScript
Speed up your coding with auto imports for JavaScript and TypeScript. The suggestion list now includes all exported symbols in the current project. Just start typing:
If you choose one of the suggestion from another file or module, VS Code will automatically add an import for it. In this example, VS Code adds an import for Hercules to the top of the file:
Auto imports requires TypeScript 2.6+. You can disable auto imports by setting "typescript.autoImportSuggestions.enabled": false.
The files attributes in the tsconfig.json file allows you to set your reference imports in your whole project. It is supported with Visual Studio Code, but please note that if you're using a specific build chain (such as tsify/browserify) it might not work when compiling your project.

How to hijack files from clearcase from eclipse plugin

I am developing a component which generates code based on templates inside java class. The project use clearcase as SCM. After the code update, the files are in read-only state. If i am adding anything to any java class, i have to make it hijack and paste the source code templates inside the class. Let's suppose the jAutoDoc plugin which is used for adding comment. If user select a class, click on generate comment. The comment will not paste if the file is not in write mode.
Clearcase Plugin Vendor : IBM Rational.
Eclipse Version : 3.5
Please help. Is there any way to do hijack a file from java code?
Thanks in advance..
Thanks VonC.
For making a java file in write mode through eclipse JDT API. This method will set the preference "read only" of the resource to "false".
private static void setCompilationUnitWriteMode(ICompilationUnit cu) throws CoreException {
ResourceAttributes resourceAttributes = cu.getResource().getResourceAttributes();
if (resourceAttributes != null) {
// Setting Writemode true
resourceAttributes.setReadOnly(false);
cu.getResource().setResourceAttributes(resourceAttributes);
}
}
For Non Java Resource
First create the IFile object, set the preference "read only" of the resource to "false".
IFile file = path.getFile()
file.getFile().getResourceAttributes();
if (resourceAttributes != null) {
resourceAttributes.setReadOnly(false);
file.setResourceAttributes(resourceAttributes);
}
Hijacking files only means making them read - write through an OS-based operation. (for snapshot view only, not dynamic ones)
The question is though: do you need to version your classes completed by your plugin?
Because in that case, a cleartool checkout is more appropriate.
Otherwise, read write is enough, changing file attribute through java.

Remove custom code workflow programmatically

For a variety of reasons too convoluted to explain here, I find myself in a position where I need to be able to remove custom code workflows, while leaving the solution there.
I essentially have the same code as the plugin registration tool
Namely
service.Delete("plugintype", new Guid(info));
where info is the workflow id [running on a foreach loop but that's beside the point]
However, while the tool removes the workflows without any issues, my code complains about dependencies.
EM:
Additional information: The PluginType(a0b2dcf7-cf2a-111e-7da9-003021880a42) component cannot be deleted because it is referenced by 1 other components. For a list of referenced components, use the RetrieveDependenciesForDeleteRequest.
which I duly did
RetrieveDependenciesForDeleteRequest req = new RetrieveDependenciesForDeleteRequest();
req.ComponentType = 90; //plugintype
req.ObjectId = new Guid(info);
RetrieveDependenciesForDeleteResponse resp = (RetrieveDependenciesForDeleteResponse)OrgService.Execute(req);
This retrieves an optionvalueset, but there is little I can do with it as I cannot remove it from the solution as the solution is managed.
The only difference I can see is the way the OrganizationServiceProxy gets instantiated. The plugin registration tool includes a way to refresh the securitytoken, but as far as I can tell it's not doing much (I've stepped through the code, but it's possible I missed something)
Are you sure that it is an OptionSetValue that is the dependency? It's much easier to use GUI to determine what the dependencies are. Fire up your solution in CRM 2011, click 'plug-in assemblies', select the relevant assembly then go through each custom workflow/plugin item and click 'Show Dependences'.
In my case I had another workflow (created within CRM) that was referencing a custom workflow preventing removal of the assembly.
You won't be able to remove components from a Managed solution... Are you the author of the solution originally?
As an aside, does your workflow fire on change of the OptionSet that is showing as a dependency?

When creating an Eclipse-based Product, how can I set the default workspace?

I've built an eclipse-based product, and I want to set the default workspace used by the Product. Currently, when the "Workspace Launcher" pops up for the first time, the default workspace location is just in the same directory as the Eclipse Product executable. I'd like to change to something like USER_HOME/myworkspace.
I can't seem to find a setting for this, but I'm guessing / hoping its a setting in my product_configuration.ini.
Cheers!
here is a more easy way
Once you have Eclipse up and running you can open Window-->Preferences-->Editors-->Startup and Shutdown. Click the first box that says Prompt for workspace on startup.
Or In your config.ini file ull've this line (or look in configuration.settings\org.eclipse.ui.ide.prefs)
//The default workspace location
Osgi.instance.area.default=#user.home/workspace
try changing this
Here is what needs to be done.
Wherever eclipse is installed go to the "configuration" directory and open the config.ini file in there.
Windows paths normally look like this:
C:\Users\Wilbert\Documents\Installers\Eclipse\eclipse
You will probably find something like this in the config.ini file:
osgi.instance.area.default=#user.home/workingspace
You need to change that to[Getting rid of the "#" and using forward slashes instead of back slash]:
osgi.instance.area.default=C:/Users/Wilbert/Documents/Programs/CS111B(Java)/Practice Programs/Projects
I just did it and it worked.
In your product (.product), go to the "Configuration" tab. Under the "Properties" section, add the property 'osgi.instance.area.default' with a value of '#user.home/myworkspace'. When you export your product, this property will be automatically added to your product's configuration file (just as ayush and Wilbert Sequeira were manually doing).
Note that only an exported product will use that configuration. When running your product in the Eclipse IDE, the workspace location will be overridden by your IDE's configurations.
The now-defunct Symbian WRT product did this. Looking through the sources, it seems to be done by a p2.inf file in the product package. See the screenshot below:
The first yellow arrow is for Windows and the second for Mac and Linux
In your .product file you can specify this as part of the programArgs element.
<programArgs>-data #user.home/MyWorkspace</programArgs>
Note that you can customize config.ini for individual platforms in the product descriptor (*.product) editor. But it never worked for me - hence that hack using P2. It may be working now as I was working with either 3.5 or early 3.6 when I last tried it.
Have a look at the following tutorial: http://hexapixel.com/2009/01/12/rcp-workspaces.
You said in your comment to the question "I just want to prepopulate the selector window with a certain default location".
You can do just that in PickWorkspaceDialog's (from the tutorial) getWorkspacePathSuggestion() method:
private String getWorkspacePathSuggestion() {
StringBuffer buf = new StringBuffer();
String uHome = System.getProperty("user.home");
if (uHome == null) {
uHome = "c:" + File.separator + "temp";
}
buf.append(uHome);
buf.append(File.separator);
buf.append("My App Name");
buf.append("_Workspace");
return buf.toString();
}
For this to work, you do have to create your own dialog though, and I can't tell if that's an option from your question...
In your .product file within the block add:
<property name="osgi.instance.area.default" value="#user.home/workspace" />
And when you build your product, the default config.ini will have this property set.
Details are in the Eclipse docs regarding the various variables.
To set the workspace location programmatically, use:
Platform.getInstanceLocation().set(new URL(...));