How to override apply and restore defaults - eclipse

I want to configure some appearance settings for a type of file. So I created a new entry in the General->Appearance->Colors and Fonts.
My plugin.xml looks like this:
<extension point="org.eclipse.ui.themes">
<themeElementCategory
id="com.example.themeElementCategory"
label="My specific settings">
<description>
Control the appearance of .example files
</description>
</themeElementCategory>
<colorDefinition
categoryId="com.example.themeElementCategory"
id="com.example.colorDefinition"
label="Some Color"
value="COLOR_DARK_BLUE">
<description>
Your description goes here
</description>
</colorDefinition>
<fontDefinition
categoryId="com.example.themeElementCategory"
id="com.example.fontDefinition"
label="Some Font"
value="Lucida Sans-italic-18">
<description>
Description for this font
</description>
</fontDefinition>
</extension>
Now in the Colors and Fonts I have a new entry were I can set the color and the font.
How can I extend the preferences window so I can override the Restore defaults, Apply and Apply and Close buttons?
In my <themeElementCategory> I will have to add a class=MyHandlingClasswhich would override the performApply(), but what should that class extend/implement?
Same as 1, but add a PropertyChangeEvent, still don't know what should extend/implement
Less likely, create a new preference page which extends the PreferencePage and implements IWorkbenchPreferencePage
How can I achieve one of the first two options?
UPDATE FOR CLARIFICATION
Currently the color and the font for a specific file extension are hardcoded in a class( I KNOW).
When the file is opened in the editor, the informations are read from that static class and visible in the editor.
What I wanted to do:
In a static{} block, read the settings configured in the preferences and update the static fields from my class.
If the user changes those settings from the preferences, on apply I wanted to update the static fields from the class and "repaint" the editor.

If you just want to know when theme items change value use the addPropertyChangeListener method of IThemeManager to add a listener for changes:
IThemeManager manager = PlatformUI.getWorkbench().getThemeManager();
manager.addPropertyChangeListener(listener);
The PropertyChangeEvent passed to the propertyChanged method of IPropertyChangeListener contains the id, old and new value of the changed theme item.

Related

Annotations in custom Eclipse text editor don't show up

I'm trying to write a custom text editor with a reconciler that will update the AST and create markers for the errors, if there are any. I managed to add markers that show up in the left ruler of the editor and in the problems view. However, I also want those errors to be underlined in the text. That doesn't work. None of the text is underlined. If I double click on one of the errors in the problem view however, the corresponding text in the text editor is selected. As far as I understand, I need to add an annotation in addition to the marker. This is what I have tried so far:
final IResource resource = ResourceUtil.getResource(getEditorInput());
final IMarker marker = resource.createMarker("com.test.myproblemmarker");
marker.setAttribute(IMarker.MESSAGE, "hello");
marker.setAttribute(IMarker.CHAR_START, 2);
marker.setAttribute(IMarker.CHAR_END, 10);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
final IDocumentProvider idp = getDocumentProvider();
final IDocument document = idp.getDocument(getEditorInput());
final IAnnotationModel iamf = idp.getAnnotationModel(getEditorInput());
final SimpleMarkerAnnotation ma = new SimpleMarkerAnnotation("org.eclipse.ui.workbench.texteditor.error", marker);
ma.setText("hello");
iamf.connect(document);
iamf.addAnnotation(ma, new Position(2, 8));
ma.update();
iamf.disconnect(document);
ma.update();
The relevant parts from plugin.xml:
<extension id="com.test.problemmarker" point="org.eclipse.core.resources.markers" name="A Problem">
<super type="org.eclipse.core.resources.problemmarker"/>
<super type="org.eclipse.core.resources.textmarker"/>
<persistent value="true"/>
</extension>
<extension point="org.eclipse.ui.editors.markerAnnotationSpecification" id="myannotationspecification" name="MyAnnotation">
<specification
annotationType="com.test.problemannotation"
label="MyAnnotation"
icon="icons/icon16x16.png"
overviewRulerPreferenceKey="clruler"
overviewRulerPreferenceValue="true"
colorPreferenceKey="clcolor"
colorPreferenceValue="255,0,0"
textPreferenceKey="cltext"
textPreferenceValue="true"
verticalRulerPreferenceKey="clvertical"
verticalRulerPreferenceValue="true"
textStylePreferenceKey="clstyle"
textStylePreferenceValue="UNDERLINE"
presentationLayer="4">
</specification>
</extension>
<extension point="org.eclipse.ui.editors.annotationTypes">
<type
markerSeverity="2"
super="org.eclipse.ui.workbench.texteditor.error"
name="com.test.problemannotation"
markerType="com.test.problemmarker"/>
</extension>
The custom text editor inherits from TextEditor and the reconciler is a MonoReconciler. Seems like a pretty standard setup, when I look at existing editor / reconciler implementations. I appreciate any help!
Ok, I was able to fix it myself. The problem was, that I set my own preference store in the constructor of the TextEditor subclass:
setPreferenceStore(MyPlugin.getDefault().getPreferenceStore());
I set a few breakpoints in the AnnotationPainter class and realized that all
annotations were disabled. When I removed this line from the editor, the annotations appeared.
I also noticed that it is sufficient to just create a marker. A corresponding annotation will be added automatically.

How to create custom image decorator in java file using eclipse plugin

I would like to use some decorators in my Eclipse plugin. I have created a plugin where I created my own editor for my own .test file. If the user edits .test file and saves it. The file must show some decorations not only the .test file but also the project must show the decorator. I am stuck with this problem I can't find any good tutorial to create decorators.
I Have seen some of the websites like https://www.eclipse.org/articles/Article-Decorators/decorators.html but I couldn't get the exact point.
Will some one please tell me how to create custom decorator for my eclipse plugin.
A ILightweightLabelDecorator is the most common type of decorator
Declare this in your plugin.xml with something like this:
<extension point="org.eclipse.ui.decorators">
<decorator
id="my.decorator.id"
class="my.decorator.Decorator"
label="My Decorator"
state="true"
lightweight="true"
adaptable="true">
<enablement>
<objectClass name="org.eclipse.core.resources.IResource"/>
</enablement>
</decorator>
</extension>
You will have to adjust the enablement to suit what you want.
Your decorator class would be something like:
public class Decorator extends BaseLabelDecorator implements ILightweightLabelDecorator
{
#Override
public void decorate(final Object element, final IDecoration decoration)
{
// TODO call decoration methods to add overlay images or prefix / suffix text
}
}

Select editor programmatically

I have written an Eclpse plugin that contains several editor types. For each editor a content type is associated. In some situations I have two possible editors for the same contenttype. I know that I can select the editor via "Open with". But I would like to decide automatically which editor should be opened.
Here is an example
In one plugins I have got the following extension point definitions:
<extension
point="org.eclipse.ui.editors">
<editor
class="de.dstg.adsplus.editors.program.MacroEditor"
contributorClass="de.dstg.adsplus.editors.core.ADSTextEditorActionContributor"
default="true"
icon="icons/macro.png"
id="de.dstg.adsplus.editors.editors.macroEditor"
name="ADS Macro Editor">
<contentTypeBinding
contentTypeId="de.dstg.adsplus.macro">
</contentTypeBinding>
</editor>
</extension
In another plugin I have this definiton:
<extension
point="org.eclipse.ui.editors">
<editor
class="de.dstg.adsplus.focusededitor.FocusedEditor"
contributorClass="de.dstg.adsplus.focusededitor.FocusedEditorContributor"
icon="icons/dveditor.png"
id="de.dstg.adsplus.focusededitor"
name="ADS Focused Editor">
<contentTypeBinding
contentTypeId="de.dstg.adsplus.macro">
</contentTypeBinding>
When I double click on a file with this content type I would like to decide programmatically which editor to open depending on the content of the file or the location where it is stored.
I have tried to find a method that I could implement but without success.
You could use two different content types both using the same file extensions and use a 'content type describer' to distinguish the files.
The content type describer is defined in the content type extension point:
<extension point="org.eclipse.core.contenttype.contentTypes">
<content-type id="ABC"
base-type="org.eclipse.core.runtime.xml"
file-extensions="a,b,c">
<describer class="org.eclipse.core.runtime.content.XMLRootElementContentDescriber2">
</describer>
</content-type>
The content describer class implements IContentDescriber or ITextContentDescriber. Eclipse calls the describe method to find out if the file is valid for the content type (and to get information about other attributes of the file).

Populate extension point's element attribute during runtime

Here is my use case. I have an Editor that different plugins can use to display their data. These plugins can create (with intervention from user - wizards) different files that Editor can accept: .p1, .p2, etc. Each plugin contributes file extensions of files that it produces and this data is available via custom extension point. I need a way, if possible to "inject" this data into org.eclipse.ui.editors extension point's extensions attribute.
One way I was thinking about doing this is in a plugin that is called early enough collect file extensions from all plugins that use my extension point and write these values into plugin.properties file's key like supportedFileExtension and extension point org.eclipse.ui.editors will in turn consume this file:
<extension
point="org.eclipse.ui.editors">
<editor
class="MyEditor"
contributorClass="MyActionBarContributor"
default="false"
extensions="%supportedFileExtensions"
id="my.com.editor"
name="My Editor">
</editor>
I also saw class ExtensionParameterValues which is something I can possibly use, but for developer who might want to reuse this little mechanism, it might not be obvious enough what's going, especially when extensions attribute would be empty when using ExtensionParameterValues class. Of course I can put this in documentation, but who reads that, right? :)
Perhaps I am overlooking something simple and there is an easier way to do what I am trying to accomplish?
Rather than using extensions directly you can Eclipse Content Types to do this. First define a base content type:
<extension
point="org.eclipse.core.contenttype.contentTypes">
<content-type
id="contenttype.base"
name="Base content type"/>
Then your different plugins can define a content type for their file extensions using a a content type derived from the base type:
<content-type
base-type="contenttype.base"
file-extensions="p1"
id="contenttype.p1"
name="P1 content type"/>
<content-type
base-type="contenttype.base"
file-extensions="p2"
id="contenttype.p2"
name="P2 content type"/>
For your editor don't specify any extensions, instead use the contentTypeBinding for the base type:
<extension
point="org.eclipse.ui.editors">
<editor
class="MyEditor"
contributorClass="MyActionBarContributor"
default="false"
id="my.com.editor"
name="My Editor">
<contentTypeBinding
contentTypeId="contenttype.base"/>
</editor>
The editor will now be used for all content types based on the base content type.

How to provide a custom component in the existing Web page Editor Palette

I want to add a new custom component in the Web page Editor Palete named "myHTMLComponent".
So, as soon as user opens any html page with WPE, myHTMLComponentM should be present there.
How can I do the needful, moreover this component will as well need to generate the code changes accordingly. How to achieve the desired result.
Is there any input I can get for this.
I already created standardmetadata tag, but what next!
Finally, I found the solution of the problem.
For adding new categories in the palette, we need to use pagedesignerextension in plugin.xml as following -
<extension
point="org.eclipse.jst.pagedesigner.pageDesignerExtension">
<paletteFactory
class="com.comp.myeditor.palette.CustomEditorPaletteFactory">
</paletteFactory>
</extension>
Where CustomEditorPaletteFactory will be extending AbstractPaletteFactory. Here in createPaletteRoot(), we can add our category.
public PaletteRoot createPaletteRoot(IEditorInput editorInput){
PaletteRoot paletteRoot = new PaletteRoot();
paletteRoot.add(createStandardComponents());
return paletteRoot;
//return null;
}
private static PaletteContainer createStandardComponents() {
PaletteDrawer componentsDrawer = new PaletteDrawer("CustomHTMLComponent");
TagToolPaletteEntry paletteEntry = new TagToolPaletteEntry(
new FormPaletteComponent(".....);
componentsDrawer.add(paletteEntry);
return componentsDrawer;
}
This will create the component category in the palette and we can add as many components as needed using the componentsdrawer.
For adding a new category in the existing one -
Add this in the constructor -
super();
this._paletteContext = PaletteItemManager.createPaletteContext(file);
this._manager = PaletteItemManager.getInstance(_paletteContext);
Then use Palette Grouping like this -
PaletteGroup controls = new PaletteGroup("CUST HTML");
super.add(controls);
ToolEntry tool = new SelectionToolEntry("CUST Cursor",
"Cursor DESCRIPTION");
controls.add(tool);
setDefaultEntry(tool);
//Custom Marquee
controls.add(new MarqueeToolEntry("Marquee", "Marquee Desc"));
controls.add(new PaletteSeparator());
//This class maintins or load all categories features
controls.add(new CustomComponentToolEntry("Custom Component", "Custom Component Descrition",
This really is a good start but I can't find any tutorial or book that get deeper in this matter. For instance, I don't want to replace the default palette but this code does with "new PaletteRoot()" and I lost my HTML tags. Also I want that my new custom components behave as HTML Tags using Drag and Drop, but I don't know how?????????
More Info:
I discovered this code, that was very helpful, whereas file come from ((FileEditorInput)editorInput).getFile()
PaletteRoot paletteRoot = DesignerPaletteRootFactory.createPaletteRoot(file);
This is very interesting topic and I think we are pioneer documenting this feature of eclipse. Here other good point, I want to personalize the tag... e.g. something similiar what I want to achieve is add a tag like "MY TRUEFALSE TAG" and then when is selected and place it in the HTML Designer, I want to become something like <select><option>YES</option><option>NO</option></select> and I guess that I can achieve it by doing something with the tagTransformOperation extension... if you know how to implement it, please let me know. also there is others extensions(tagConverterFactory, elValueResolver). I am guessing here! please I would like your help.
<extension point="org.eclipse.jst.pagedesigner.pageDesignerExtension">
<paletteFactory ...>
<tagTransformOperation id="plugin.tagTransformOperation1XXXXXX">...
SOLUTION?? (Chinese) -solved with tagConverterFactory
http://www.blogjava.net/reloadcn/archive/2007/11/08/webeditor1.html