How to create a custom SWT Tooltip using JFACE for an RCP MDirectToolItem? - eclipse

I would like to create a custom tooltip for an RCP MDirectToolItem or MHandledToolItem. JFace provides the org.eclipse.jface.window.ToolTip class, which I can extend and override the createToolTipContentArea() method. However, to instantiate a JFace ToolTip, I have to give it the SWT Control that will use the ToolTip. I cannot figure out a way to get the underlying SWT Control from the MDirectToolItem.
I have been able to get the MToolBar and the MDirectToolItem (which I defined in the Application.e4xmi) using the EModelService.find() method. I tried getting the underlying SWT Control from the MDirectToolItem, but it does not appear there is a way to do that.
I also tried creating an SWT ToolItem and adding it to the MToolBar, but the children of the MToolBar are only MToolBarElement's.

Tool items don't have a separate control, they are part of the parent ToolBar control. The SWT ToolItem class represents the tool item, this is just derived from Widget rather than Control.
So you will have to set the tool tip on the tool bar control and work out which tool item is active when the tool tip is shown.
The application model classes which represent UI objects all extend the MUIElement interface. This provides a getWidget method to get the UI object.
So for MToolBar you can do:
ToolBar toolbar = (ToolBar)mtoolbar.getWidget();
and for MToolItem (either handled or direct) you can do:
ToolItem toolitem = (ToolItem)mtoolitem.getWidget();
If you create the ToolTip with the NO_RECREATE style it will call the
getToolTipArea method to determine if the tool tip need to be changed. You can use something like the following to have a different area for each tool item:
#Override
protected Object getToolTipArea(final Event event)
{
// TODO save the ToolBar in the class as 'toolBar'
ToolItem item = toolBar.getItem(new Point(event.x, event.y));
if (item != null)
return item;
return super.getToolTipArea(event);
}

Related

Java StyledText control with IDocument support

My intention is to code a NASTRAN text editor (plain text editor, eclipse pure E4 RCP application).
NASTRAN is an engineering structural analysis application.
Simplifying, NASTRAN uses text cards of 8-characters-width per field and up to 10 fields per card (line).
See figure with the work done so far
The main feature of this Editor is to show plain text (fixed pitch font) with colored columns background, so it can be easy to distinguish different fields in each row.
I have used a StyledText control which provides methods to change background:
styledText.setBackgroundImage(backgroundImage);
How can I use IDocument interface with StyledText so It can provide me support for:
text manipulation
positions
partitions
line information
etc...
Other text controls (TextViewer, SourceViewer) provide setDocument(IDocument) method to load and manipulate text data
--org.eclipse.jface.text.TextViewer
|
--org.eclipse.jface.text.source.SourceViewer
But StyledText extends SWT Canvas and does not provide methods to set the imput documents
--org.eclipse.swt.custom.StyledText
Alternative approach could be how can I change background in a SourceViewer control so I can have columns of different colors.
Thanks in advance
TextViewer and SourceViewer are wrappers for StyledText and provide the code to deal with the IDocument interface so you should use one of those.
You can call the getTextWidget() method of the viewer to get access to the StyledText control they are using.
Thanks greg-449 for your answer, problem solved.
I had not a clear understanding of the concept of a class wrapping another class. So I tried first to create a StyledText object.
Now it is clear
I have attached below how I proceeded: creating a SourceViewer control and then, obtaining the StyledText wrapped.
So I could set the background image for the control
public class NastranEditor {
public StyledText st = null;
public SourceViewer sv = null;
private Image backgroundImage;//The image to appear at the backgroud
//....
#PostConstruct
public void postConstruct(Composite parent){
IVerticalRuler ruler = new VerticalRuler(20);
sv = new SourceViewer(parent, ruler, SWT.MULTI | SWT.V_SCROLL);
st = sv.getTextWidget();
st.setBackgroundImage(backgroundImage);
//....
}
//....
}

View size not match with layoutPanel container in GWT MVP and SmartGWT

I'm new in GWT, and have problem with view implementation... I use MVP, and SmartGWT. I'll expose this by defining how I settle my MVP and what its weird.
In my onModule, I define class builded with UIbinder. I've declared a LayoutPanel and set it like this in the constructor of the class.
layoutPanel = binder.createAndBindUi(this);
I have container in this class:
public void setBodyLayout() {
panel.setWidgetLeftWidth(menuPanel, xx, PCT, xxx, PCT);
panel.setWidgetRightWidth(bodyPanel, xx, PCT, xx, PCT);
}
menuPanel and bodyPanel are both simplePanel declared in the class above(UIfield use with UIbinder). There are in LayoutPanel. For the method display of my ActivityMapper I've got this method (In reality I have two ActivityMappers, two method that like below and two containers, for menu and body)
public AcceptsOneWidget getBodyContainer() {
return new AcceptsOneWidget() {
#Override
public void setWidget(IsWidget w) {
Widget widget = Widget.asWidgetOrNull(w);
bodyPanel.setWidget(widget);
}
};}
return in my onModule, I declared my ActivityMapper like this
BodyActivityMapper bodyContainerActivityMapper = new BodyActivityMapper(clientFactory);
ActivityManager bodyContainerActivityManager = new ActivityManager(bodyContainerActivityMapper, eventBus);
bodyContainerActivityManager.setDisplay(my_class_described_above.getBodyContainer());
the same work was done with MenuActivityMapper...
Finally
RootLayoutPanel.get().add(my_class_described_above.getLayoutPanel());
when getLayoutPanel() return my layoutPanel declared in the class that I have declared above.
So, each region have its own ActivityMapper.ActivityMapper for the menu have only one activity, and "ActivityMapperBody" have sevral activities triggered by menu.
Utility of container are to settle my layout for different "action". I defined zone with it, in order to receive view started with activity.
But this configuration work only with view builded with UIbinder... In each view, I declare a Layout and return it like this
public Widget asWidget() {
return my_layout_declared;
}
When I return my layout, nothing works. I really don't understand why, and I figure that its worse with smartgwt. All I want its just retrieve my layout and put it in my container... Work with smartgwt can save a lot of time...
I've more detailed my issue to make sure that anyone understand. And ask to you Chris Lercher if your post can help me.
Thank for reading
The Smart GWT FAQ mentions a solution:
If you absolutely must place a Smart GWT interface inside a GWT container and you want it to
fill the container, the best approach is to listen for a window-level resize event and run
your own layout calculations that ultimately call resizeTo() on your topmost Smart GWT
widget.
However, it is wrong by saying that
...GWT containers [do not] fire events when they are resized
Layout panels (the "new" panels since GWT 2.0, which was released on Dec 08, 2009) actually do (see the ProvidesResize and RequiresResize interfaces). So if you have such a panel as your MVP body container (and if its parents are also LayoutPanels up to a top RootLayoutPanel), then you can also override the body container's onResize() method instead of listening to the Window resize event.
So how can you solve your concrete problem?
You need to call resizeTo(width, height) on your topmost Smart GWT layout
When you put in a (new) Smart GWT layout, e.g. maybe you put in a new one when the Place changes.
When the window resizes, or when other things happen that change the size of the container (alternatively, if you're using Layout Panels, you can use its onResize())
For the resizeTo(width, height) call, you'll have to determine the width and height by asking the surrounding container, e.g. by using
container.getElement().getClientWidth()

Eclipse jface tableviewer - changing celleditor at runtime

I am using a Table Viewer with cell editors... one is a ComboxBox and the other one is a textcell editor... At runtime based on the selection in the combo box i want to change the other cell editor to either a texteditor or combobox... How can this be achieved??
When you make selection in one comobox editor, you update the underlying model element with the selection in combobox editor. You get the same element in below method getCellEditor(Object element). Depending on state of the element, you should either return TextCellEditor or ComboBoxCellEditor
org.eclipse.jface.viewers.EditingSupport
org.eclipse.jface.viewers.EditingSupport.getCellEditor(Object element)
you find lot of help online about how to use EditingSupport on TableViewer/TreeViewer

Gwt get Components

I have a Vertiacal panel object and This object contains many radiobuttons
So can i get those radioButton objects through Vertiacal panel object.
Maybe via iteration or ?
private void initCourse() {
coursePopupPanel.clear();
VerticalPanel verticalPanel = new VerticalPanel();
coursePopupPanel.setWidget(verticalPanel);
JsArray<JCourse> jCourseArray = JCourse.getList(stringMainData);
for (int i = 0; i < jCourseArray.length(); i++) {
final RadioButton courseRadioButton = new RadioButton("course");
courseRadioButton.setText(jCourseArray.get(i).getName());
courseRadioButton.getElement().setId(jCourseArray.get(i).getView());
verticalPanel.add(courseRadioButton);
//handler of course radio buttons
courseRadioButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
}
});
}
}
I have a reference to coursePopupPanel. but i have not reference to vertical panel, so can i get elements of vertical panel sonce holding reference to coursePopupPanel.
A GWT VerticalPanel is a subclass of ComplexPanel, an abstract class for Panels that contain more than one child widget. In ComplexPanel (and so inherited by VerticalPanel) are methods for getting the number of child widgets, getting references to them by index, and so on. You could build an iterator something like this:
Iterator<Widget> vPanelWidgets = myVerticalPanel.iterator();
while (vPanelWidgets.hasNext()){
Widget childWidget = vPanelWidgets.next();
if (childWidget instanceof RadioButton) {
...do stuff
}
}
I tend not to query a widget for its members. That ties me to the decisions I made about how to display the RadioButtons, following your example. What if you decide later to display your radio buttons in the cells of a FlexTable in order to control vertical and horizontal arrangement? To make that change means your widget iterator won't work. FlexTable is a Panel but not a ComplexPanel. The code I wrote above won't work if you decide to replace the VerticalPanel with a FlexTable.
If was to take something like this approach, I would keep my lists of related widgets (like a group of RadioButtons) in some sort of Java Collection. I pass that Collection to my presentation class, and inside there I write the code to do the layout. Usually that's a UiBinder class, with "#UiField(provided = true)" for these RadioButtons. The code in the presenter then associates the RadioButton elements of the Collection I passed in to the UiField placeholders for the RadioButtons in the UiBinder layout. So all my layout decisions are actually in the UiBinder xml file. If I decide to rip out my Vertical Panel and replace it with a FlexTable, I might not have to touch a single line of Java code, assuming I separated things out correctly.
[Actually, I would probably keep my decision to use RadioButtons inside the presentation layer, and inside the XML file in particular. That presentation class would fire a message on the EventBus to indicate the user had made a selection via a RadioButton ValueChangeHandler, and I wouldn't care if I used RadioButtons in a VerticalPanel or ToggleButtons in a FlexTable.]
You're not being to specific, add more details and maybe a code example.
I'm gonan try to guesstimate what you're trying to say here: You have a verticalPanel object. To it you add several radioButton objects. Later you want to retrive those radioButton objects (to maybe check if they're selected or not), right? There's several ways to do this. At any rate, why don't you check the code examples at the Gwt Showcase site here:
http://gwt.google.com/samples/Showcase/Showcase.html?locale=en_UM#!CwRadioButton
it has tons of visual examples, each with the attached code and css.
Since PopupPanel implements HasOneWidget interface you can coursePopupPanel.getWidget() to get a reference to your verticalPanel. And iterate widgets in it simply using
for (Widget w : verticalPanel){
//Do Stuff
}

GWT Tree Item:How to add image to tree item?

I am using gwt 2.3 version.I am using gwt tree in my application.
Here is my code:
public void onModuleLoad() {
// Create a tree with a few items in it.
TreeItem root = new TreeItem("root");
root.addItem("item0");
root.addItem("item1");
root.addItem("item2");
// Add a CheckBox to the tree
TreeItem item = new TreeItem(new CheckBox("item3"));
root.addItem(item);
Tree t = new Tree();
t.addItem(root);
// Add it to the root panel.
RootPanel.get().add(t);
}
There is a item with check box.I want add image to this tree item.But I am not able to do this as I already added one widget check box.Is there any other way add image to tree item with check box??
TreeItem has a TreeItem(Widget w) constructor.
You can put anything you want in there. So write a small widget that has an image and text next to each other in a div and the Tree will render it correctly.
You are already using it in your example code. So just write one more widget that combines the CheckBox with an image in a FlowPanel or HorizontalPanel. Whatever you want.
This is something that made me whack my head for a while also. Basically I found 2 options:
Use SmartGWT, it has nice customizable tree widgets that let you change the pictures of the nodes:
http://www.smartclient.com/smartgwt/showcase/#tree_databinding_local
Use GWT's tree-image:
http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/user/client/ui/TreeImages.html
If you ask me, SmartGWT is somewhat demanding and rigid, and you might not like the fact that it doesn't let you go to the low leves like GWT does but it does have a nice set of customizable tree widgets. TreeImage on the other hand lets you still work with pure GWT (which I think is better overall), but it doesn't let you customize the tree as much as Smart GWT does