Adding extra image/icon to label using JFace Tree and Eclipse RCP - java

I'm using a JFace TreeViewer object in my Eclipse RCP ViewPart, and I'd like to add some additional information to the label on some of my nodes by way of an image.
Essentially the image should sit to the right of the label text and will represent a rating (I'm thinking 1 - 5 stars)
If there's a way to do this I can't find it, does anybody know of one?
If not then does anyone know how the eclipse package explorer shows the different coloured extra info when using other plugins like Subclipse? I guess if I'm forced to I could use that and the "*" character? ( I have tried to look at the source but it's very abstracted and a little beyond me at the moment, so I'm just asking if anyone knows off hand, I'm not asking anyone to dig through the source for me)
Regards,
Glen
x

You can custom-draw tree items by adding SWT.MeasureItem and SWT.PaintItemlisteners to the tree. Check out example 5 in this tutorial.
In order to get the selection highlight painted over the extended area, add also SWT.EraseItem listener and update event.width.

Are you using the PackageExplorer or your own TreeViewer ?
1.PackageExplorer: You need to extend the ui decorator.
<extension point="org.eclipse.ui.decorators">
<decorator
adaptable="true"
class="org.example.com.PackageExplorerDecorator"
id="org.example.filedecorator"
label="File Decorator"
lightweight="true"
state="true">
<enablement>
<or>
<objectClass
name="org.eclipse.jdt.core.IMethod">
</objectClass>
<objectClass
name="org.eclipse.core.resources.IResource">
</objectClass>
</or>
</enablement>
The class should look like this:
public class PackageExplorerDecorator extends LabelProvider implements ILightweightLabelDecorator {
#Override
public void decorate(final Object resource, final IDecoration decoration) {
decoration.addSuffix(..)
decoration.addPrefix(..)
}
}
2. TreeViewer: You can try to create custom Widget, or just create TreeViewer with multiple columns ( First one for the tree and the second one for the stars).
This and this might be for you useful.

Related

setTooltipText for TreeItem not defined

Greetings fellow Stackoverflownians!
I am building an Eclipse RCP application, and have come across an issue:
I want to set a tooltip text on a TreeItem, but this class does not inherit Control, which is the class that has the setTooltipText
EDIT: It seems that jface is supposed to take care of this seamlessly, through a LabelProvider.
I am using a ColumnLabelProvider with getToolTipText method on each column of a complex TreeViewer, but it isn't working. I wonder why...
The probleme here is that you use the SWT-Tree.
You should use a TreeViewer (JFace) which wraps the tree and gives you more sophisticated options.
Inside the label provider of the TreeViewer, you can define your tooltips.
Learn more about viewers here and here
An code example (tool tip) is here
I strongly recommend you to use the viewers!
With TreeViewer use
ColumnViewerToolTipSupport.enableFor(viewer);
Use a label provider derived for CellLabelProvider or one of it subclasses and override getToolTipText (there are also several other methods to control the font, time out and the like).

How to set custom text on buttons in JFace Wizard (Java)

I am using JFace Wizard and I want to set my own text on buttons Next, Back, Finish and Cancel. I found only very old advices which are completely useless today. I also found some solution with external jar files, but I really don't want to add whole library to project only for setting text on 4 buttons...
Is there any reasonable solution?
Thanks in advance
After massive finding and trying, I have to say there is no such way. There are some brutal solutions, but compared with them, adding one jar file to project is much easier and nicer.
I will cite best working solution for me:
You need to download a language pack from here:
http://archive.eclipse.org/eclipse/downloads/drops/L-3.2.1_Language_Packs-200609210945/index.php
NLpack2-eclipse-SDK-3.2.1-gtk.zip works for me while I'm using Eclipse
3.7.2.
Extract org.eclipse.jface.nl2_3.2.1.v200609270227.jar (or other nl for
your language) from the archive and add it to your project. It will be
used automatically.
This do not let you to set texts on buttons, but at least gives you texts translated into your language.
Saw this post. Seems to be the answer to your question.
It basically says to create a dialog using WizardDialog class. Create a class that inherits from Wizard with the implementation of your choice then do below:
WizardDialog wizardDialog = new CustomWizardDialog(shell, new YourWizard());
and then in your CustomWizardDialog do the following:
public class CustomWizardDialog {
#Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
Button finishButton = getButton(IDialogConstants.FINISH_ID);
finishButton.setText("FinishButtonText");
Button cancelButton = getButton(IDialogConstants.CANCEL_ID);
cancelButton.setText("CancelButtonText");
}
}
All that is left is to perform wizardDialog.open() to open dialog.

SWT: prevent Tree from expanding by doubleclick?

I have a problem with SWT Tree.
My situation is like this: I have a SWT Tree, which contains many TreeItems (Log entries), which contain TreeItems too. Those log entries have really long messages, which could not be shown in the TreeColumns at all. So my idea was: adding a Listener to the tree, which opens a new Dialog by DoubleClick, which shows the entries' details. So far so good.
If I do a double click on a item, it works. BUT: If I do a double click on a parent Item, it will expand (and thats good), but my double click Listener is active then as well and the Dialog will open.
That's not, what I want.
So, there are two solutions to the problem:
1) prevent the Tree from expanding/collapsing by double click automatically and implement the method by myself or
2) recognize, that the item was expanded and the event has to be aborted.
I do not really know how to do 1 or 2. Do u guys know that?
Thanks in advance.
Other answers did not work for me. This worked:
treeViewer.getControl().addListener(SWT.MeasureItem, new Listener(){
#Override
public void handleEvent(Event event) {
}});
I found this in a discussion in the Eclipse Community Forums: Disabling Treeviewer doubleclick expand/collapse.
When you look at this code you might be tempted to believe that it pretends to the tree control that your tree items have a size of zero, and as a result the tree control fails to detect that the double-click happened within the item, so it does not perform the double-click action. However, this is not what is actually happening. Instead, what this snippet does is that it leverages some weird code in the implementation of the tree control, which checks whether a listener has been added for SWT.MeasureItem, and if so, it deliberately avoids handling a double-click. This piece of code is even prefixed with a lengthy comment which a) fails to make sense and b) does not agree with what the code does. (Whatever.) So, bottom line is that by simply adding a handler for SWT.MeasureItem, and regardless of what the handler does, we are preventing the tree control from handling double-clicks. This is a prime example of Programming by Coincidence1.
1 The term "Programming by coincidence" was coined in the book The Pragmatic Programmer by Andy Hunt and Dave Thomas. It refers to relying on luck and accidental successes rather than programming deliberately.
If you are using TreeViewer, you could make use of IOpenListener
treeViewer.addOpenListener(new IOpenListener() {
#Override
public void open(OpenEvent event) {
}
}
There is another solution which works much better.
The problem with the solution from 'sambi reddy' was, that the tree was prevented from expanding by doubleclick, but it was prevented from expanding by clickling on the left handside cross as well.
My solution (that works well), was easy: I added a TreeListener, which listens to expanding/collapsing the tree and removed the expanding/collpasing implementation from the MouseDoubleClick-Listener.
No JFace TreeViewer - it works fine.

Java DefaultMutableTreeNodes: Interactive displays given by my cell renderer?

In the program I'm writing, I have a JTree storing some objects of my own design. I made my own extension of DefaultTreeCellRenderer and overrode getTreeCellRendererComponent to return a JPanel with some buttons and things. What I found was that the buttons I added didn't act like buttons, which makes think that interaction with the components is being "stolen" by the tree cell. (If you click on the button, the container surrounding the button is also being clicked on, and the tree has its own response to being clicked.)
So my question is this:
If what I want is the basic functionality of a tree, plus some buttons, what approach should I use?
Continue on the same route; add a mouse listener of some kind to manually add the functionality to the button.
Continue on the same route; remove the existing mouse listener and add your own to achieve the right behavior.
Extend or implement a slightly different class or interface than you did - maybe not DefaultMutableTreeNodes, maybe not DefaultTreeCellRenderer, etc. - use the existing XXXX to do what you're trying to do.
Avoid using JTree; make your own, it's not that hard.
I'm leaning toward the last option - there's a decent chance I don't actually want the folding behavior of a tree anyway, so I may just make my own structure. However, even if I choose that option, I'd like to know what I should have done.
You'll also need a TreeCellEditor, illustrated here.
Avoid using JTree; make your own, it's not that hard
I wish you good luck wit that ;-)
What is happening is that the components returned by the renderer are only used as a 'stamp'. So the JTree does not really contain the returned components, they are only painted. Hence no interaction with your button. It only looks like a button. It seems the JTree tutorial does not contain a real section on this, but it is basically the same concept as for tables, which is explained in the 'Renderers and editors' part of the tutorial.
That also explains why a typical renderer class extends JLabel and can simply use return this after it made customizations to itself, without affecting the other nodes in the tree. For example the source code of the DefaultTreeCellRenderer, which extends JLabel, contains
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel,
boolean expanded,
boolean leaf, int row,
boolean hasFocus) {
//...
setText(stringValue);
//...
return this;
}
How to fix this: create an editor as well, as suggested by #trashgod

Swing JTable custom rendering

I have this kind of progamming task without JavaFx, instead it's Java Swing. I realized my knowledge is still limited.
I have one single JTable.
But, within this JTable I need a custome Cell Renderer.
The goal is to make this kind of JTable: Example image
My current solutions are: Example Image
Create a Single JTable:
get each Column and set its CellRenderer with a custom Renderer (below).
Create a new Class implements TableCellRenderer:
return different JPanel inside getTableCellRendererComponent
method using switch case (as column counted).
After hours, and hours, I think my current solutions is quite daunting tasks. Thus, My question is:
What are the simplest method of creating this Custom JTable to achieve the main goal as mentioned above?
you have two options
1) JPanel nested another JComponents and solve that by using standard LayoutManagers note scrolling isn't natural nor nice
2) JTable with JPanel can solve that, notice about scrolling inner JScrollPane inside another JScrollPane
I've been facing this problem for a while, and I decided to do it myself. Extending the existing implementation of a table, adding some concepts for what I expect from a table, and writting some editors/listeners for that. All the same, but with a treetable.
I'm working on this project called SUMI.
It contains a java package (ar.com.tellapic.sumi.treetable) that is an extension of a JXTreeTable from SwingLabs.
The project is being developed and I didn't provide any documentation yet. You can do what you want by creating a renderer and if needed, an editor, for lastly attaching actions to each object.
If you decide to use it and you need help, email me, I'll help you without any problem.
Or, you could read the source by your own.
Regards,
EDITED (again):
To clear a little bit this answer, I've just created a wiki page in the project wiki and put the relevant code there. If someone feels that the code should be inserted here, please let me know.
Basically, I try to explain how to find a straight solution to the renderer/editor problems you may find using JTable with your specifics needs by using part of my project, in order to get something like this:
Note that the screenshot was taken after clicking on the respective tick-button.
Once you create a nested panel for one row, as suggested by #mKorbel, you can add any number of them to a GridLayout(0, 1) in a JScrollPane. If rendering many rows becomes an issue, you can adopt the same approach used by JTable, illustrated here.
Even though, JTable can be customized to whatever you desire through cell renderer and cell editors, it is never preferred because you have to do a lot of messy codings for that. Instead, for your problem, I suggest to use JScrollPane and add your component (view panel as your sample jTable ) to its viewPort.
For this implementation, represent each rows with your custom class that extends JPanel. And add the required row components (that may be any components like jlabel, jtextfields or even jpanel too) in it. For the simplicity, you can use null layout for the row panel and add the components at any location you want.
I hope this will help you workout with your problem. If you got any problem in this implementation, feel free you ask again.

Categories