I am trying to develop a simple Eclipse plugin to understand how it works.
I have two questions about this:
How can I get the content of the active editor?
Do you have a good documentation about life cycle plugin and co? I can't find real good documentation on Google.
Tonny Madsen's answer is fine, but perhaps a little more transparent (getAdapter() is very opaque) is something like:
public String getCurrentEditorContent() {
final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.getActiveEditor();
if (!(editor instanceof ITextEditor)) return null;
ITextEditor ite = (ITextEditor)editor;
IDocument doc = ite.getDocumentProvider().getDocument(ite.getEditorInput());
return doc.get();
}
Regarding the content of the current editor, there are several ways to do this. The following code is not tested:
public String getCurrentEditorContent() {
final IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.getActiveEditor();
if (activeEditor == null)
return null;
final IDocument doc = (IDocument) activeEditor.getAdapter(IDocument.class);
if (doc == null) return null;
return doc.get();
}
I'm assuming you're already familiar with using Eclipse as an IDE.
Create a new plug-in project using the New Plug-in Project Wizard.
On the Templates panel, choose "Plug-in with an editor"
Read the generated code
If you're serious about writing Eclipse plug-ins, the book, "Eclipse Plug-ins" by Eric Clayberg and Dan Rubel is invaluable. I couldn't make sense of the eclipse.org write-ups until after I read the book.
Related
I am currently having all project names in my workspace like this.
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
Is there a way to get project name which currently open in the editor or selected one without using ISelection or IStructuredSelection.
Eclipse has no concept of a 'current project' or anything like that.
You either have to use the selection service to find out the current selection in a view such as 'Project Explorer' or you look at the active editor and see what that is editing (see for example here)
Found one way
private IFile getFullPath(URI uri)
{
String platformstring = uri.toPlatformString(true);
IFile f = (IFile)ResourcesPlugin.getWorkspace().getRoot().findMember(platformstring);
return f;
}
//and then call
IFile projectFile = getFullPath(uri);
String projectName = projectFile.getProject().getName();
I'm making an Eclipse plug-in that requires access to code written in the Eclipse editor. I've followed the process mentioned in the link.
Accessing Eclipse editor code
But it's showing filepath instead of code in the message box. The getEditorInput() of IEditorEditor class isn't doing what it should do according to the link. Here's my code. Please help me find what i'm doing wrong.
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
IEditorPart editor = ((IWorkbenchPage) PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()).getActiveEditor();
IEditorInput input = (IEditorInput) editor.getEditorInput(); // accessing code from eclipse editor
String code = input.toString();
MessageDialog.openInformation(
window.getShell(),
"Project",
code);
return null;
}
And here's snap of the output.
You can do this two different ways. This way works regardless of whether the contents are backed by a file on disk or not.
This method gets the text IDocument from the editor, which is how the contents are stored and accessed for most uses. StyledText is a widget, and unless you are doing something with Widgets and Controls, it's not the right way in. For that you're going to go from the editor part, through the ITextEditor interface, and then use the IDocumentProvider with the current editor input. This is skipping the instanceof check you'd want to do beforehand, as well as anything you might have to do if this is a page in a MultiPageEditorPart (there's no standard way for handling those).
org.eclipse.jface.text.IDocument document =
((org.eclipse.ui.texteditor.ITextEditor)editor).
getDocumentProvider().
getDocument(input);
You can get, and modify, the contents through the IDocument.
I'm currently writing an eclipse plugin, and in it, I have a new project creation and a new file creation wizard.
In the new project wizard, I create it, so I have no problem getting it and creating new files in it. (like creating the Main class for your project)
But when I'm in my New file wizard, I have literally no idea how to select the right project, and I would like some help please.
Since it is a wizard, I would like to avoid needing an opened editor, and since it's a new wizard, it doesn't have a handler so I can't get it from there...
Thank you in advance,
Cordially,
Okay, I looked at it from another approach and it feels quite stupid right now.
When you initialize your wizard, you get an init method that contains the workbench and the selection, so you can keep it.
private IWorkbench wb;
private IStructuredSelection sel;
#Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
wb = workbench;
sel = selection;
}
Then, I had found a code snippet earlier on Eclipse website, linked under here, that I had to change a bit, and it does what I want it to do.
// Get selected resource (can get project from it)
// https://wiki.eclipse.org/FAQ_How_do_I_access_the_active_project%3F
private IResource extractSelection() {
Object element = sel.getFirstElement();
if (element instanceof IResource)
return (IResource) element;
if (!(element instanceof IAdaptable))
return null;
IAdaptable adaptable = (IAdaptable)element;
Object adapter = adaptable.getAdapter(IResource.class);
return (IResource) adapter;
}
With this, I can get my project in my wizard by doing
IProject project = extractSelection().getProject();
I am currently developing a plugin for eclipse that analyzes dependencies and references between projects within the Eclipse Workspace and displays them in its own View in a UML-like diagram.
To increase the usefulness of my plugin, I wish to add interactivity to the diagram by allowing users to open a project in the package explorer and if applicable open it in an editor by clicking on the graph displayed.
However, my problem is that while I know how to obtain a given selection from the package explorer, I have not been able to find a way to change the selection or simply open up a project in the package explorer programmatically.
Does anyone have a solution for this problem?
This answer extends what the accepted answer states but takes it further for folks who mind the "Discouraged Access" warning on the use of PackageExplorerPart.
Exact warning (more for easier searching off Google) that you see is
Discouraged access: The type PackageExplorerPart is not accessible due
to restriction on required library
/eclipse_install_path/eclipse/plugins/org.eclipse.jdt.ui_3.9.1.v20130820-1427.jar
Code Sample:
final IWorkbenchPart activePart = getActivePart();
if (activePart != null && activePart instanceof IPackagesViewPart) {
((IPackagesViewPart) activePart).selectAndReveal(newElement);
}
Supporting Code:
private IWorkbenchPart getActivePart() {
final IWorkbench workbench = PlatformUI.getWorkbench();
final IWorkbenchWindow activeWindow = workbench.getActiveWorkbenchWindow();
if (activeWindow != null) {
final IWorkbenchPage activePage = activeWindow.getActivePage();
if (activePage != null) {
return activePage.getActivePart();
}
}
return null;
}
I have found the solution. Eclipse does offer direct access to the package explorer in org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart , but it is discouraged.
import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
...
PackageExplorerPart part= PackageExplorerPart.getFromActivePerspective();
IResource resource = /*any IResource to be selected in the explorer*/;
part.selectAndReveal(resource);
This will highlight whatever IResource resource is and expand the tree as necessary.
I have a regular expression (\d{4}\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}) that I need to validate the input of a text field against when the user clicks the OK button or moves the cursor to another field. That I know how to do, writing the code. However, I'm interested in if it's possible to have the NetBeans GUI editor do some of the work for me, especially since I'm moving away from Eclipse and towards NetBeans as my IDE of choice, and I would like to take full advantage of the tools it provides.
Open the Properties of your JTextField, in the Properties tab look for inputVerifier. Open it
Now you'll be asked to introduce the InputVerifier code.
ftf2.setInputVerifier(new InputVerifier() {
public boolean verifyText(String textToVerify) {
Pattern p = Pattern.compile("your regexp");
Matcher m = p.matcher(textToVerify);
if (m.matches()) {
setComponentValue(textToVerify);
return true;
}
else {
return false;
}
}
});
I haven't compiled this code, so could contain errors. But I think you get the idea ;)
This isn't the easiest solution, but it is a very powerful one:
try spring rich client, where validation could be reached via:
public class Validation extends DefaultRulesSource {
private Constraint NAME_VALIDATION = all(new Constraint[]{minLength(3), required()});
public void load() {
addRules(new Rules(Person.class) {
#Override
protected void initRules() {
add("name", NAME_VALIDATION);
}
});
...
and the gui form is easily created via:
TableFormBuilder formBuilder = getFormBuilder();
formBuilder.add("firstName");
formBuilder.add("name");
formBuilder.row();
E.g. look here for validation or here for more infos. I am using this sucessfully in my open source project ...
This way you could create a more general swing component which could be added to the netbeans component palette