I have a plug-in and want to detect when projects are added to workspace, to set some project settings from my plug-in code, Any Ideas.
Specially i want to call setHidden in some resources that are derived files, as this settings seems to not be part of the project, i mean whenever a resources is hidden seems to not persist if i import the project in a new workspace.
Ironically, I just wrote something like this yesterday. It is a bit more complicated than you would like. Here is a code snippet for you to play with:
public class ProjectListener implements IResourceChangeListener {
public void resourceChanged(IResourceChangeEvent event) {
if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
List<IProject> projects = getProjects(event.getDelta());
// do something with new projects
}
}
private List<IProject> getProjects(IResourceDelta delta) {
final List<IProject> projects = new ArrayList<IProject>();
try {
delta.accept(new IResourceDeltaVisitor() {
public boolean visit(IResourceDelta delta) throws CoreException {
if (delta.getKind() == IResourceDelta.ADDED &&
delta.getResource().getType() == IResource.PROJECT) {
IProject project = (IProject) delta.getResource();
if (project.isAccessible()) {
projects.add(project);
}
}
// only continue for the workspace root
return delta.getResource().getType() == IResource.ROOT;
}
});
} catch (CoreException e) {
// handle error
}
return projects;
}
Then, you need to add this ProjectListener to the Workspace, preferably in the start method of your plugin activator:
ResourcesPlugin.getWorkspace().addResourceChangeListener(ProjectListener.LISTENER, IResourceChangeEvent.POST_CHANGE);
And then you want to remove it in the stop method. I literally just wrote this code yesterday. I hope it helps.
You can define a resourcelistener to the workspace, and look for changes in the resource root. See the following article for details: http://www.eclipse.org/articles/Article-Resource-deltas/resource-deltas.html
Related
I am trying to update the view content when I switch or open a new file in the Eclipse project explorer. I tried using "IResourceChangeListener" but it is not triggered (only on changed on the file itself). I also looked looked at https://www.wideskills.com/eclipse-plugin-tutorial (specific on the view part), and Eclipse forums, without success. I thought it would be easy thing... Any Ideas ? small code example will be great !
After hours of searching (and based on #greg-449 comment) I was able to generate a small example that get notified when a new file is opened and is currently visible in the editor:
in the derived ViewPart class:
#Override
public void createPartControl(Composite parent) {
.
.
.
workbench.getPartService().addPartListenr(new IPartListener2() {
public void partOpened(IWorkbenchPartReference partRef) {
String part = partRef.toString();
showMessage("partOpened is " + part);
}
public void partVisible(IWorkbenchPartReference partRef) {
String part = partRef.toString();
showMessage("partVisible is " + part);
}
});
}
what I want to do:
In my RCP an E3/E4 hybrid I have a project and library based on sirius tree. The User can drag an drop item from the library tree to the project tree. This works fine and was no great problem to build in. So now I want to make the UI more usable. It should looks like this layout:
what works:
After application startup I open my library presentation with the DialectUIManager.
final DialectEditor editor = (DialectEditor)
DialectUIManager.INSTANCE.openEditor(siriusSession, description, monitor);
Okay, this works. But it open it in the editor in the part market as org.eclipse.ui.editorss. This it not what I want
what does not work:
I want to show it in the "Library Part". I can move it manually with the mouse after open the editor, but how can i tell DialectUIManager to open it direct there. Or how can I programmatically it move there.
I do a lot of google research but i don't found a solution. The only thing I found was a hint Pierre-Charles David https:// www. eclipse.org/forums/index.php?t=msg&th=998476&goto=1631138&#msg_1631138
If you need is simply to show the editor outside of the main editor
area, this is possible since Eclipse 4.2 (e4 does not really treat the
main editor area as something special), so you can have your editor
"around" another editor in the middle of other views.
But at this step I stuck. I also ask it in the Sirius Forum but they say its a Eclipse E4 problem
Thanks for help, code snippets or links to correct part of manual.
I've found a solution. It's not very nice, but it works. I execute these code here after the editors have opened.
What the code does:
He is looking for the MPlaceholder which has the ID: org. eclipse. ui. editorss. There he descends until he is with the parts. These are in the Compatibly editor mode. Then he chooses the part we wants to move out of and Attach them to the MPartStack target.
public static void movePart(MApplication application,
EModelService modelService) {
MPart partToMove = null;
MUIElement muiElement =
modelService.find("org.eclipse.ui.editorss", application);
if (muiElement instanceof MPlaceholder) {
MPlaceholder placeholder = (MPlaceholder) muiElement;
MUIElement ref = placeholder.getRef();
if (ref instanceof MArea) {
MArea area = (MArea) ref;
List<MPartSashContainerElement> children = area.getChildren();
for (MPartSashContainerElement mPartSashContainerElement
: children) {
if (mPartSashContainerElement instanceof MPartStack) {
MPartStack partStack = (MPartStack) mPartSashContainerElement;
List<MStackElement> children2 = partStack.getChildren();
for (MStackElement mStackElement : children2) {
if (mStackElement instanceof MPart) {
MPart part = (MPart) mStackElement;
// Library is the Editor Name wiche I want to move
if (part.getLabel().equals("Library")) {
partToMove = part;
break;
}
}
}
}
}
}
}
if (partToMove != null) {
moveElement(modelService, application, partToMove);
}
}
private static void moveElement(EModelService modelService,
MApplication application, MPart part) {
// target PartStack
MUIElement find = modelService.find("de.bsg.onesps.rcp.
partstack.library", application);
if (find instanceof MPartStack) {
MPartStack mPartStack = (MPartStack) find;
mPartStack.getChildren().add(part);
mPartStack.setSelectedElement(part);
}
}
I need to show annotations on files outside of Workspace.
I am able to show annotations on files present in Worspace
When i try to do the same for files outside the Workspace I need to create resource and ifile object. How do i achieve the same?
How do i read contents of file outside workspace since i am unable to create a ifile object.
Here is what i am doing right now:
IEditorPart editor =(IEditorPart) wins[i].getPartService().getActivePart()
IEditorInput input = editor.getEditorInput();
IPath path = ((FileEditorInput)input).getPath();
IFile file= workspace.getRoot().getFileForLocation(path);
You cannot annotation files outside of workspace. An IFile is only defined for workspace files.
Just in case you don't know, files and projects don't have to the under the workspace's folder on the file system to be in the workspace.
Maybe you can surreptitiously add a hidden project to the workspace and link the file as a resource in it. I don't think that an external file converted to an internal file loses any behaviors. But, it could gain more than you want. One that I can think of is that source control plugins might then detect it.
Or, you could point out to the user that there are advantages to adding the file to the workspace and let them make the choice. You might be able to show a dialog asking which new or existing project/folder to add it to. Of course, if they decline then you should remember and not ask again about that external file.
UPDATE:
FileStoreEditorInput represents a file that is not part of the current workspace. To listen for external files being opened, subscribe with an IPartListener2 on each window.
public class Activator extends AbstractUIPlugin implements IStartup {
#Override
public void earlyStartup() {
final PartListener partListener = new PartListener();
for (final IWorkbenchWindow window : getWorkbench().getWorkbenchWindows()) {
window.getPartService().addPartListener(partListener);
}
getWorkbench().addWindowListener(new IWindowListener() {
#Override
public void windowOpened(IWorkbenchWindow window) {
window.getPartService().addPartListener(partListener);
}
});
}
private class PartListener implements IPartListener2 {
#Override
public void partOpened(final IWorkbenchPartReference partRef) {
if (partRef.getPart(false) instanceof EditorPart) {
final EditorPart editor = (EditorPart) partRef.getPart(false);
if (editor.getEditorInput() instanceof FileStoreEditorInput) {
final FileStoreEditorInput input = (FileStoreEditorInput) editor.getEditorInput();
System.out.println(input.getURI());
}
}
}
}
i'm working on an Eclipse RCP4 project. I have different perspectives showing a set of Parts to choose informations from. After selecting what i want to see, a new Part opens and displays the objet i want to edit / view attibutes of.
I can open many parts of the same type. If i close the application, the eclipse framwork persists the position of all opened Parts. If i restart the application all previously opened Parts are open but without informations.
-How to prevent Eclipseframwork from persisting the state of Parts?
-How to close Parts on exit?
I'm searching for a way to add an "removeOnExit" tag to a Part and than close such a marked Part on exit.
Thanks in advance :)
With this you can close MParts with a specific Tag.
It seems you have to switch to the Perspective the Part is on, else it's not removed from the context wich will cause nullpointer exceptions.
#Inject
#Optional
public void doEvent(#EventTopic(EventConstants.EventTopics.CLOSE_PARTS_WITH_TAGS) String tagToClose, MApplication app,
EModelService eModelService, EPartService ePartService) {
MUIElement activePart = ePartService.getActivePart();
EPartService activePeServcie = null;
MPerspective activePerspective = null;
if (activePart instanceof MPart) {
activePeServcie = ((MPart) activePart).getContext().get(EPartService.class);
activePerspective = eModelService.getPerspectiveFor(activePart);
}
List<String> tags = new ArrayList<String>();
tags.add(tagToClose);
List<MPart> elementsWithTags = eModelService.findElements(app, null, MPart.class, tags);
for (MPart part : elementsWithTags) {
try {
logger.info("Closing part " + part.toString());
EPartService peService = part.getContext().get(EPartService.class);
peService.switchPerspective(eModelService.getPerspectiveFor(part));
peService.hidePart(part);
} catch (Exception e) {
logger.error(e);
}
}
if (activePart instanceof MPart && activePeServcie != null && activePerspective != null) {
activePeServcie.switchPerspective(activePerspective);
}
}
We too tried to migrate from Eclipse 3 to Eclipse 4.We used the comp layer and had a lot of problems with the migration. I had a similar problem with persistent store of the eclipse workbench. So parts and views had the same position as before restart.
The persistence paradigma in Eclipse 4 has changed. Please take a look here:
As far as I remember the call of configurer.setSaveAndRestore(false) does not work in Eclipse 4 correctly.
I have developed an application using net bean,
1) I'm making project in java API 6. I'm using "Net Beans 7.1".
2) I want to use JInternalFrame in my project
3) I made another package and made "JInternalFrame" there. And then call it in my main application window by firing action performed event on "JMenuItem".
4) It works fine but only one problem occurs that is, if i click on "JMenuItem" again and again, new "JInternalFrame" of same instance are opening, How can i stop that?
5) I want that, if I open "JInternalFrame" once and then i again click on "JMenuItem" to open the same "JInternalFrame", it Should do nothing or it shows the window which already opened and minimized
sample code:
<code>
private void empDataActionPerformed(java.awt.event.ActionEvent evt) {
Emps employees = new Emps();
desktop.add(employees);
employees.setVisible(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
employees.setBounds(230, 40, screenSize.width / 2 - 80, screenSize.height / 2 + 105);
}
<code>
please I need help.
Here is may sample code. hope this help.
Menu action to call internal frame in main application where JdesktopPane in it.
private void YourJinternalFrameMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
YourJinternalFrame nw = YourJinternalFrame.getInstance();
nw.pack();
//usefull part for you.. if open shows, if not creates new one
if (nw.isVisible()) {
} else {
desktopPane.add(nw);
nw.setVisible(true);
}
try {
nw.setMaximum(true);
} catch (PropertyVetoException ex) {
Logger.getLogger(MainApplication.class.getName()).log(Level.SEVERE, null, ex);
}
}
put this inside of your YourJinternalFrame
private static YourJinternalFrame myInstance;
public static YourJinternalFrame getInstance() {
if (myInstance == null) {
myInstance = new YourJinternalFrame();
}
return myInstance;