Java StyledText control with IDocument support - eclipse

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);
//....
}
//....
}

Related

Set StyledText in SWT ExpandItem Title/Text

everyone. In an ExpandItem in SWT, Java you're able to set the title or header text of the ExpandItem by using the built-in method ExpandItem.setText(String text). This only supports String as input. I want to be able to use StyledText as title/header text of the ExpandItem. Is there a way to implement something like:
ExpandItem.setStyledText(StyledText text)
If not, what are my options? My main goal is to be able to change font style of the title/header text of ExpandItem. That's why I need StyledText.

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

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);
}

How to gray selection highlight when NatTable not in focus

Some lists and tables gray out their selection when they lose keyboard focus.
In the presence of multiple lists/tables, this helps communicate to the user which selection is active.
Is there an easy way to do this with NatTable?
The best I've come up with so far is to flip between different attributes for DisplayMode.SELECT as focus comes and goes -- but I'm not sure I can do that after NatTable.configure() has been called.
Yes you can change configuration attributes dynamically after NatTable#configure() has been called. That is a common approach for dynamic changes. Another approach would be to configure a selection style for a special label and apply that label only in case the table is active. This approach can be seen in this example.
https://github.com/eclipse/nebula.widgets.nattable/blob/master/org.eclipse.nebula.widgets.nattable.examples/src/org/eclipse/nebula/widgets/nattable/examples/_500_Layers/_505_Selection/_5054_SelectionProviderExample.java
I have this working, after #DirkFauth's answer. This answer includes some specifics.
After the table has been configured with NatTable.configure(), you can modify the configuration not with NatTable.addConfiguration(IConfiguration), but instead by calling IConfiguration.configureRegistry(IConfigRegistry). For example:
myConfiguration.configureRegistry( myTable.getConfigRegistry() )
Within that implementation of configureRegistry(), you can set the style for selected and anchor cells:
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE,
selectedStyle, DisplayMode.SELECT, GridRegion.BODY);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE,
anchorStyle, DisplayMode.SELECT,
SelectionStyleLabels.SELECTION_ANCHOR_STYLE);
When the table is inactive, selectedStyle and anchorStyle can be modified clones of their usual setting. For example:
private static Color myInactiveColor = ...;
public static Style makeInactiveBodyCellStyleFrom(#Nonnull Style style) {
Style rv = style.clone();
rv.setAttributeValue( CellStyleAttributes.BACKGROUND_COLOR,
myInactiveColor );
return rv;
}
Similar work can be done for the styles of selected row and column headers.

In gwt read value from dynamic created text area

In gwt, I am creating a dynamic text area and now I want to read the value of the text area.
Can some help in me reading the value of dynamic created text area.
It all depends on which classes you use. The convenient way to read from com.google.gwt.user.client.ui.Label is to call getText(). But of cause the most generic way to get the interior of your "text area" is to call getElement().getInnerHTML() of your com.google.gwt.user.client.ui.UIObject object. For example:
FlowPanel myTextArea = new FlowPanel(); // FlowPanel extends Widget extends UIObject
myTextArea.add(someWidget);
GWT.log("Here is what I have inside myTextArea: " +
myTextArea.getElement().getInnerHTML());
A note of criticism. Normally, when you ask a question related to some custom UI, it should come along with at least few lines of code.

Drawing into an Eclipse editor

I am trying to draw some shapes (boxed ans arrows) into, i.e., "over" the text in an eclipse editor. To get started, I wrote the following code:
IWorkbenchPage activePage = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
final Shell shell2 = activePage.getActiveEditor().getSite().getShell();
shell2.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e){
Rectangle clientArea = shell2.getClientArea();
e.gc.drawLine(0,0,clientArea.width,clientArea.height);
}
});
The problem with this code is twofold: (1) The line is drawn not across the editor but across the entire workbench, i.e., Eclipse window, and (2) the line is drawn behind (!) all other controls like toolbars and editors. This causes the line to be almost invisible: it only shows at some pixels between other controls.
How can I draw a line across a control like a text editor in Eclipse?
The problem that you have is that you are getting the Shell, not the actual component for the editor. The Shell is the whole window where Eclipse is being shown.
I think the only solution is to create your own Editor implementation, and then in the createPartControl() method you can create a text area and then add the paint listener to it.
You can get started with:
http://www.realsolve.co.uk/site/tech/jface-text.php
And then, looking at the source code of AbstractTextEditor, you can find the "real" SWT component that you want to draw to. You would need to override the method that creates the UI components, copy the original code and add your custom painting.
I'm not sure if it works, but you need to extend the TextEditor:
public class MyEditor extends TextEditor {
protected StyledText createTextWidget(Composite parent, int styles) {
StyledText widget = super.createTextWidget( parent, styles );
widget.addPaintListener( <yourPaintlistener> );
return widget;
}
}
That should at least get you the basic text-drawing control of the editor. Still, it's a PITA to work with these classes, as it is very internal stuff from eclipse, and neither documented nor really extensible.
Good luck with that :)