Can I display an eclipse plugin/action based on a preference? - eclipse

I have a eclipse plug-in, to be more precise I have an action which is triggered via the context menu of any folder. I do also have a Preferences page where I have a checkbox. I want to only display the action if that checkbox is enabled.
My plugin.xml:
<action
class="com.something.MyClass"
enablesFor="1"
id="com.something.MyClass"
label="Label
menubarPath="path/group.smth">
<enablement>
<objectClass
name="org.eclipse.core.resources.IFolder">
</objectClass>
</enablement>
</action>
So. Is there any possibility to display the action based on a specific preference?
Thanks in advance.

I don't think you can do that using (deprecated) actions as the enablement expression is rather limited.
You can also use the org.eclipse.ui.menus extension point to contribute to context menus. In this case you can use the test element to invoke a 'property tester' defined using the org.eclipse.core.expressions.propertyTesters extension point. I don't think there are any predefined property testers for preferences but testers are not difficult to write.

Related

How do I have my custom toolbar have its items updated via IElementUpdater

I've implemented a custom toolbar in my RCP application. I'm also using the org.eclipse.ui.menus extension to contribute commands to the custom toolbar.
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="toolbar:com.my.custom.toolbar.identifier.toolbar">
<command
commandId="com.my.command.id"
icon="icons/my_icon.png"
style="toggle"
tooltip="My Toggle Tooltip">
</command>
</menuContribution>
</extension>
I've written the code that finds these IConfigurationElements and turns them into CommandContributionItems which I use to add them to my custom toolbar.
I have some toolbar items that are of type toggle, and I need these to update their UI using the IElementUpdater interface that the command's handler implements.
I also contribute this command to a standard Eclipse menu... like "toolbar:org.eclipse.ui.main.toolbar" and its toggle state is updated via the IElementUpdater when appropriate.
I'm probably missing something in my custom toolbar that hooks it up to the IElementUpdater, but I have no idea where to start looking to make this work.
I've used the IElementUpdater, but not for cases to update the toggle state, as this should be done by the connection to the respective Command. There exist, however, some problems in Eclipse 4 which I have already started to point out in a blog article.
What Eclipse variant are you using? 3.x or 4.x? There exists a bug in 4.x concerning the synchronization of contributions and their command states.

Override builtin perspective layout in Eclipse-RCP product

Is it possible to override the layout of a built-in perspective in my Eclipse-RCP product?
In particular, I wish to add a custom view and change the layout of the Debug perspective. I know how to do it with a custom perspective (IPerspectiveFactory.createInitialLayout()). I'd want that my custom layout to be permanent -survive the "Reset perspective" command.
Create a class that implements IPerspectiveFactory.
Add a perspectives extension to your plugin.xml. Here's one of mine.
<extension point="org.eclipse.ui.perspectives">
<perspective
class="gov.bop.cobolsupport.perspectives.CobolPerspectiveFactory"
icon="icons/ispf_editor.gif"
id="gov.bop.cobolsupport.CobolPerspective"
name="Cobol"/>
</extension>
Your users can change your perspective, and save their changes if they wish. That's built into Eclipse.
However, when you extend your perspective, the Reset Perspective command resets the perspective to how you defined it in your Perspectivefactory class.
Extending a perspective is possible by using the extension point org.eclipse.ui.perspectiveExtensions.
Plug-ins can add their own action sets, views, and various shortcuts
to existing perspectives by contributing to the
org.eclipse.ui.perspectiveExtensions extension point.
To extend the default debug perspective paste the following code in your plugin.xml:
<extension
point="org.eclipse.ui.perspectiveExtensions">
<perspectiveExtension
targetID="org.eclipse.debug.ui.DebugPerspective">
<view
ratio="0.5"
relative="org.eclipse.ui.views.TaskList"
relationship="right"
id="com.jens.customdebug.views.SampleView">
</view>
</perspectiveExtension>
</extension>
You have to define a relative view (in my case the task view named org.eclipse.ui.views.TaskList) and the id of your own view (in my case com.jens.customdebug.views.SampleView)
Source:
To get further information how to use this extension point, take a look here.
For the configuration markup of this extension point you may also take a look at this page.

try to avoid multiple instances of eclipse editor opening same file

I registered a custom editor with eclipse extension point "org.eclipse.ui.editors" as follows.
<extension
point="org.eclipse.ui.editors">
<editor
class="com.xxx.designer.scxml.ui.ScxmlDiagramEditor"
id="com.xxx.designer.scxml.ui.ScxmlDiagramEditor"
extensions="scdiagram"
name="SCXML Editor">
</editor>
It works fine, this editor is associated with .scdiagram files. However, every time I click on one same .scdiagram file, it opens a new instance for me instead of highlighting the opened file. Any idea on this?
Thanks,
Jie
All you need to do is make your IEditorInput return true for the same files. You could implement this by comparing the canonical names of your files. Don't forget to also override the hashCode() method. What issues should be considered when overriding equals and hashCode in Java?
See Lars Vogel's excellent tutorial Eclipse Editor Plugin Tutorial for more details on using Editors.
Here's how my question was resolved eventually. It's related to Graphiti and solution is also tied to the framework.
Since I subclassed Graphiti's diagram editor, all I need to do is to set org.eclipse.graphiti.ui.editor.DiagramEditorMatchingStrategy as the editor's matching strategy in plugin.xml.

How to disable the rename option from everywhere (i.e, from file menu & context menu) in eclipse plugin?

I am developing a plug-in which is basically create a new project, project contains more than one folder, and the folder contains our own customized editors. Now I dont want to allow the user to rename of the project from anywhere. I can see the rename option in file menu, context menu of project explorer, context menu of package explorer, context menu of navigator. Is it possible to disable/customize the rename option globally. I have tried to use global action handler for rename action,which is basically doing my own job whenever the rename action occurs. But since I want to block/disable the rename option at the time plug-in activator get started, I am able get active view site on the activator class, which doesn't seems to help me because of the active view might be anyother than above mentioned view at sometimes. Hence I tried to using 'Rename Participant' through ltk's renameparticipant extension and I wrote my own class which extending RenameParticipant, but the class never get initiated even I have given the correct class name in plug-in.xml file.
Since our product is plug-in
Could anyone please advise me that how should I proceed further? I hope that someone has already walk on this path to block the rename option globally.
You can specify your own handler to global rename (id="org.eclipse.ui.edit.rename") and Java Rename element (id="org.eclipse.jdt.ui.edit.text.java.rename.element") commands using Handlers extension point. However, it is not possible to disable the context menu item (Refactor > Rename...) in Package Explorer because it is hard-coded.
I removed from context menu using that class
public class MoveActionProvider extends RefactorActionProvider {
//#Override
public void fillContextMenu(IMenuManager menu) {}
}
and declaring this into the plugin.xml
<extension point="org.eclipse.ui.navigator.navigatorContent">
<actionProvider
class="com.totvs.tds.ordinechaos.providers.action.MoveActionProvider"
id="com.totvs.tds.ordinechaos.providers.action.MoveActionExtension"
overrides="org.eclipse.ui.navigator.resources.actions.RefactorActions"
priority="highest">
<enablement>
<!-- A hack to allways be enabled -->
<not>
<systemTest
property="MyApp"
value="WONT-EVER-BE-SET">
</systemTest>
</not>
</enablement>
</actionProvider>
</extension>

different editors for different perspectives in Eclipse [duplicate]

I'm developing tow eclipse plugin, I have the next problem:
I have two perspective that manages the same files. I would like to make an association between file extension - editor - perspective.
I mean if I open the file extension .XXX in perspective 1 it uses the editor A, but if I open the same file extension .XXX in perspective 2, it uses the editor B.
is it possible? Since now, I used the launcher but now I need more differentiation.
Thanks.
(Sorry, this is one of those "don't do that!" non-answers. :))
As mentioned in the comments, I'd recommend against opening a different editor depending on the current perspective. I think that goes against the expectations of the user, and has some unintuitive consequences, e.g. when I create my own perspectives.
I'd recommend going the path of Eclipse' XML/Plug-in manifest editors, for example. Tabs at the bottom allow the user to choose between the different views, independent of any perspective choice or configuration.
While I agree that this seems a little strange to have the default editor be different for the same file based on the open perspective, here is how you could do it.
Create two new Content Type extensions
Register your first editor as default editor for 1st new Content Type
Register your 2nd editor as the default editor for the 2nd new Content Type
For each content type, you have a 'content type describer'. In these describer classes, have it check the active workbench page for the current perspective ID and if it matches the expected value, then VALID, if perspective id doesn't match, return INVALID.
For both editors you need to associate those editors with a content-type instead of a file-extension or filename
Now only one content type will match at a time depending on which perspective is open. Make sure that one of the content types is the 'default' so that it will always match if the user has some other perspective open.
Update #1 added some examples
There are some online tutorials for this. But here is some example code to make it easier to see what work is required. Here is how you declare your content types (you would need two of them)
<plugin>
<extension
point="org.eclipse.core.contenttype.contentTypes">
<content-type
base-type="org.eclipse.core.runtime.xml"
describer="com.liferay.ide.core.FirstContentTypeDescriber"
id="com.liferay.ide.core.contentType1"
name="First Content Type"
priority="normal">
</content-type>
</extension>
</plugin>
Then in the Describer class you would do your matching logic. Then in the editor extension point you reference a content type instead of a file-name or extension like this:
<extension
point="org.eclipse.ui.editors">
<editor
class="com.liferay.ide.ui.FirstEditor"
default="false"
id="com.liferay.ide.ui.editor1"
name="My First Editor">
<contentTypeBinding
contentTypeId="com.liferay.ide.core.firstContentType">
</contentTypeBinding>
</editor>
</extension>
I would recommend to rethink your approach, and take some cues from WindowBuilder: have one editor associated with the file type which opens a tabbed editor; if a second plugin is added, have it create a separate tab on the same editor.
Other option may be programmatically change file type association with Java code shown in
Eclipse RCP: programmatically associate file type with Editor?
Then there is only a question how to execute that code on perspective change event.