I am working on Eclipse plugin. Here i created a separate view and now i want to format the color of tree node.
These are code present in createpartcontrol method.
ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL );
Composite composite1 = new Composite(sc, SWT.NONE);
Composite composite_1 = creatingcomposite(composite1);
Tree tree = new Tree(composite_1, SWT.FULL_SELECTION );
TreeItem item = new TreeItem(tree, SWT.NONE);
here i want to set some colour like blue.
item.setText("This is sparta");
Now here i want some different colour like yellow on subsubitem text.
TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
subsubItem.setText(new String[] { "Function Name: "+ errorPreTest11.description.get(j).function });
For doing this i tried to set SWT.COLOR_BLUE but it's not working.
Use
item.setForeground(tree.getDisplay().getSystemColor(SWT.COLOR_BLUE));
You can also create your own colors but if you do this you must dispose of them when you are done.
I suggest you using the TreeViewer. In this case you would have a functionality to set a LabelProvier on your viewer. Label provider has a subclass called StyledCellLabelProvider, which you can successfully extend to provide styling of your labels like this: (Please also see a TextStyle class for more formating options).
public class MyStyledLabelProvider extends StyledCellLabelProvider {
private Styler defaultStyler;
public MyStyledLabelProvider () {
defaultStyler = new Styler() {
#Override
public void applyStyles(TextStyle textStyle) {
textStyle.strikeout = true;
}
};
}
#Override
public void update(ViewerCell cell) {
Object element = cell.getElement();
StyledString styledString = getStyledString(element);
cell.setText(styledString.toString());
cell.setStyleRanges(styledString.getStyleRanges());
super.update(cell);
}
#SuppressWarnings("unchecked")
private StyledString getStyledString(Object element) {
return new StyledString("Cell string", defaultStyler);
}
}
Related
In order for the end-user to constrain a search to some columns of the main TableView, I needed a treeview with checkboxes.
I decided to embed this TreeView in a popup, showing on click on a custom button.
I have created the following class, inspired from the question:
Java FX8 TreeView in a table cell
public class CustomTreeMenuButton extends MenuButton {
private PopupControl popup = new PopupControl();
private TreeView<? extends Object> tree;
private CustomTreeMenuButton me = this;
public void setTree(TreeView<? extends Object> tree) {
this.tree = tree;
}
public CustomTreeMenuButton() {
super();
this.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (!popup.isShowing()) {
Bounds b = me.localToScreen(me.getBoundsInLocal());
double x = b.getMinX();
double y = b.getMaxY();
popup.setAutoHide(true);
// popup.setAutoFix(true);
popup.setAnchorX(x);
popup.setAnchorY(y);
popup.setSkin(new Skin<Skinnable>() {
#Override
public void dispose() {
}
#Override
public Node getNode() {
return tree;
}
#Override
public Skinnable getSkinnable() {
return null;
}
});
popup.show(me.getScene().getWindow());
}
}
});
}
}
The tree I am working with contains CheckBoxTreeItem objects, and while the popup is working, there is some weird blur on all checkboxes, whenever the focus is not on a checkbox. (See GIF below)
First, I was thinking it was maybe an antialiasing problem, but popup.getScene().getAntiAliasing().toString() returns DISABLED
Then, I saw that non integer anchor points could cause problems. However popup.setAutoFix(true) did nothing, nor did the following:
popup.setAnchorX(new Double(x).intValue());
popup.setAnchorY(new Double(y).intValue());
It might be worth noting that I am working with FXML.
How can I get sharp checkboxes regardless of their focus ?
I would suggest a built-in control, CustomMenuItem, rather than reinventing the wheel:
A MenuItem that allows for arbitrary nodes to be embedded within it,
by assigning a Node to the content property.
An example
// Create the tree
CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<String>("All stuff");
rootItem.setExpanded(true);
final TreeView<String> tree = new TreeView<String>(rootItem);
tree.setEditable(true);
tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
for (int i = 0; i < 8; i++) {
final CheckBoxTreeItem<String> checkBoxTreeItem =
new CheckBoxTreeItem<String>("Stuff" + (i+1));
rootItem.getChildren().add(checkBoxTreeItem);
}
tree.setRoot(rootItem);
tree.setShowRoot(true);
// Create a custom menu item
CustomMenuItem customMenuItem = new CustomMenuItem(tree);
customMenuItem.setHideOnClick(false);
// Create the menu button
MenuButton mb = new MenuButton("Stuffs");
mb.getItems().add(customMenuItem);
And the output
Note: It is important to set the hideOnClickProperty to true, to avoid closing when the user clicks in the tree, which can be even done in the contructor, so you can shorten the initialization to:
CustomMenuItem customMenuItem = new CustomMenuItem(tree, false);
If you want to remove the hover glow, you can add the following CSS class:
.menu-item {
-fx-padding: 0;
}
I have two comboviewer and the idea is that the second combo will display a subset of options depending of the first combo value selected. But after to set the new input in the second combo and refresh the combo width is too small. How can I set it for auto adjusting to options width?
public class ExpresionDialog extends Dialog {
private ComboViewer combo1;
private ComboViewer combo2;
#Override
protected Control createDialogArea(Composite composite) {
Composite parent = (Composite) super.createDialogArea(composite);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
combo1 = new ComboViewer(parent);
combo1.setLabelProvider(new LabelProvider());
combo1.setContentProvider(ArrayContentProvider.getInstance());
combo1.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent e) {
IStructuredSelection sel = (IStructuredSelection) e.getSelection();
AttributeOption option = (AttributeOption) sel.getFirstElement();
combo2.setInput(getValuesCombo2(option));
combo2.refresh(true);
}
});
combo1.setInput(getValuesCombo1());
combo2 = new ComboViewer(parent);
combo2.setLabelProvider(new LabelProvider());
combo2.setContentProvider(ArrayContentProvider.getInstance());
return parent;
}
// Omitted getValuesCombo1 and getValuesCombo2 methods ...
}
You need to call the layout method of the parent Composite to get it to redo the child layouts each time you change the contents.
I am creating a RCP application which will display a tree structure. I used the following code for this purpose. But, I need to make the nodes editable. How to do that?
Please find the below code which I have written.
public class TreeView extends ViewPart {
public static final String ID = "TreeProject.project";
public TreeView() {
}
public static ProjectTree mc = new ProjectTree("root");
public static TreeViewer treeViewer;
#Override
public void createPartControl(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
treeViewer = new TreeViewer(composite);
Tree tree = treeViewer.getTree();
tree.setLocation(0, 0);
tree.setSize(181, 469);
StyledText styledText = new StyledText(composite, SWT.BORDER);
styledText.setText("Welcome\"!");
styledText.setBounds(179, 0, 415, 469);
treeViewer.setContentProvider(new ProjectContentProvider());
treeViewer.setInput(getRootNode());
treeViewer.expandAll();
System.out.println(tree.getSelection());
}
private ProjectTree getRootNode() {
ProjectTree node0 = new ProjectTree("Node0");
ProjectTree node1 = new ProjectTree("Node1");
mc.addChild(node0, "");
node0.addChild(node1, "");
return mc;
}
#Override
public void setFocus() {
}
}
You need to use a selection listener on the treeViewer which would give you the node selected. You then have to remove the node object and its children,if any, from the model . Here I see that your model is the object mc. Then call treeViewer.refresh(). Similarly for add .
I'm trying to replicate a UI similar to this:
http://librixxxi.blogspot.com/2011/06/punch-clock-021-and-clickable-edit.html
and I have been following the authors instructions (without success) on how to create buttons that are in each column of my table. The difference between my project as his is I am trying to use a Tree rather than a Table, and I am doing it in the context of an eclipse TreeViewer plugin. In theory it seems the implementation should be straightforward, but I can't seem to get it to work.
Here is my code, it is easily replicate-able as it is just the sample Java PDT sampleview with a treeviewer, plus a dozen or so extra lines in the createPartControl. Everything you don't see here is just the same as the sample:
class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
public String getColumnText(Object obj, int i) {
if(i == 0){
return obj.toString();
}
return "";
}
public Image getColumnImage(Object obj, int i) {
if(i == 0){
String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
if (obj instanceof TreeParent)
imageKey = ISharedImages.IMG_OBJ_FOLDER;
return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
}
return null;
}
}
class NameSorter extends ViewerSorter {
}
/**
* The constructor.
*/
public ButtonView() {
}
/**
* This is a callback that will allow us
* to create the viewer and initialize it.
*/
public void createPartControl(Composite parent) {
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
Tree tree = viewer.getTree();
tree.setLinesVisible(true);
tree.setHeaderVisible(true);
TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
column1.setText("Name");
column1.setWidth(400);
TreeColumn column2 = new TreeColumn(tree, SWT.LEFT);
column2.setText("Some info");
column2.setWidth(300);
// Button experimentation
TreeItem[] items = tree.getItems();
for(int i = 0; i < items.length; i++){
TreeEditor editor = new TreeEditor(tree);
TreeItem item = items[i];
Button button = new Button(tree, SWT.PUSH);
button.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT));
button.setSize(16,16);
button.pack();
editor.horizontalAlignment = SWT.RIGHT;
editor.setEditor(button, item);
}
drillDownAdapter = new DrillDownAdapter(viewer);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setSorter(new NameSorter());
viewer.setInput(getViewSite());
// Create the help context id for the viewer's control
PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "ButtonTest.viewer");
makeActions();
hookContextMenu();
hookDoubleClickAction();
contributeToActionBars();
}
When you say that you can't seem to get it to work, do you mean that you can't see the buttons in your Tree?
The javadoc of the SWT TreeEditor class gives an example of a tree editor stating that
"The editor must have the same size as the cell and must not be any smaller than 50 pixels."
The following lines assure that these conditions are met in the example:
editor.grabHorizontal = true;
editor.minimumWidth = 50;
So if you add these lines to your editor the buttons should be visible.
[Edit: What I did to reproduce the behaviour]
I used the standard RCP Mail Example project and added your "Button experimentation" code to it. Inside, I used simple text buttons.
public void createPartControl(Composite parent) {
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
viewer.setInput(createDummyModel());
experiment();
}
private void experiment() {
// Button experimentation
Tree tree = viewer.getTree();
TreeItem[] items = tree.getItems();
for(int i = 0; i < items.length; i++){
TreeEditor editor = new TreeEditor(tree);
TreeItem item = items[i];
Button button = new Button(tree, SWT.PUSH);
button.setText("A");
button.setSize(16,16);
button.pack();
editor.horizontalAlignment = SWT.RIGHT;
editor.grabHorizontal = true;
editor.minimumWidth = 50;
editor.setEditor(button, item);
}
}
When I execute the code like this, the buttons show up. When I comment out the lines setting the editor's grabHorizontal and minimumWidth values, the normal tree cell renderer is shown and the buttons are not visible.
I have a composite element, that initially has a Label. Now I call dispose on the it (the label) and create another label in the same container (composite elm), but I don't see the new text. It brings me to question how do I enable redraw on the composite, so that the new label (or any other component I might create) will render in place of the old one.
Here is the code I have (separated into a unit test for redraw a composite)
private Label createLabel( Composite parent) {
Label label = new Label(parent, SWT.NONE);
label.setAlignment(SWT.CENTER);
label.setLayoutData( new GridData( SWT.CENTER, SWT.CENTER, true, true) );
return label;
}
private void changeText() {
assert testCell != null : "Please initialize test cell";
testCell.getChildren()[0].dispose();
Label l = createLabel(testCell);
l.setText("New TexT");
testCell.redraw();
}
private void draw() {
Display display = new Display();
shell = new Shell(display);
shell.setLayout(new GridLayout(2,false));
testCell = new Composite(shell,SWT.BORDER);
testCell.setLayout(new GridLayout());
Label l = createLabel(testCell);
l.setText("Old Text");
Composite btnCell = new Composite(shell,SWT.NONE);
btnCell.setLayout(new GridLayout());
Button b = new Button(btnCell, SWT.PUSH);
b.setText("Change");
b.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event e) {
changeText();
}
});
As you can see, I am calling redraw on the composite after I add a new element. Also, I have verified that after the call to dispose, testCell.getChildren().length returns 0, as expected, and when I create a new label, I get the same expression to return 1, verifying that the new element is indeed getting added to its parent composite container
Am I missing something here ?
In the changeText() function, the
testCell.redraw();
line should be replaced by
testCell.layout();
Or, if you want to resize it correctly you should use
shell.layout();.
I would say add a selectionListener on the label.
.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(final SelectionEvent e) {
//Change text by Label.setText();
}
}