Drawing into an Eclipse editor - eclipse

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 :)

Related

Codename One LeftBarCommand Text Size not Changing

In my application I am using a command added to the top left bar and I am trying to change the font size.
What is generally a simple task is causing quite a bit of labor and I still couldn't figure it out.
In the theme builder, I have tried to change the font size in the default settings (changes the color, but not the size) and accessing several component UIIDs, including Command, SideCommand, ToolBar and others, all to no avail.
Command also does not give give direct access to the styles.
How can I change the font size of the command? Is there a special UIID?
Command left = new Command("Done") {
#Override
public void actionPerformed(ActionEvent evt) {
confirmationDialog();
}
};
tb.addCommandToLeftBar(left);
You can customize the command UIID by:
left.putClientProperty("uiid", "MyUIID");
In the case of title commands they have the UIID TitleCommand.

Change graphic context in SWT application

I have a simple application written with SWT. I'm geting GC from SWT.Paint event. In my main canvas I've added a MouseMovelistener so some text will be displayed over specified area if mouse is in area.
I would like to remove text from GC after the mouse is not anymore over area. I didnt find any mention about removing drawn objects from GC or replacing GC with new one.
Could you tell me how can I can achive such result? There is no other access to GC object than through SWT.Paint event.
Elements like text of lines that are drawn on a GC cannot be removed. Even though, a GC has methods to draw text and lines, etc, these methods merely transform the shapes into pixels and that is all a GC knows of.
Therefore your application should maintain a model that allows to position text and remove once placed text elements. Whenever the model changes, the canvas should be refreshed with canvas.redraw() which sends an SWT.Paint event. Your paint listener can then examine the model and paint text accordingly.
The article Graphics Context - Quick on the draw has further details on SWTs graphics context.
Add MouseTrackListener and control mouse enter and leave canvas
MouseTrackListener mtl = new MouseTrackAdapter() {
#Override
public void mouseEnter(MouseEvent e) {
// add your PaintListener here
}
#Override
public void mouseExit(MouseEvent e) {
// remove PaintListener here
}
};
canvas.addMouseTrackListener(mtl)

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

Eclipse ui: retrieving the first visible line of an editor

In the Eclipse UI, I'd like to set the visible area in an editor. In other words, if the number of lines of my file is larger than the number of lines my editor can show then I want to specify the first shown line. My first approach was to calculate the first visible line via the selection value of its vertical scroll bar. The following link points to my initial question. Its answer explains how to set the first visible line in an editor.
eclipse ui: setting scrollbar but editor does not follow
The problem now is that my initial way of retrieving the first visible line in an editor fails in some cases: Although I verify that the active page is indeed an editor, the focus might be assigned to another page. In such a case, the following code yields the ScrollBar of a different page:
public static void update(final IWorkbenchWindow w)
final Scrollable scrollable =
(Scrollable) w.getWorkbench().getDisplay().getFocusControl();
final ScrollBar vScrollBar = scrollable.getVerticalBar();
So, my question: If editor is the reference of an active editor (ITextEditor and IReusableEditor), how to I get its first visible line?
If you can access the editor ITextViewer or its extension ISourceViewer (usually implemented by the SourceViewer or TextViewer class) you can call the ITextViewer.getTopIndex() method to get the top line index.
If your editor is derived from AbstractTextEditor (or one of its subclasses such as TextEditor) there is a protected method getSourceViewer() that returns this. You may have to add a public method if you want to access this from outside of the editor.

How to put image on JPanel?

I know this has probably been ask one billion times, but I am still finding it difficult in getting a straight forward answer.
Where do you put the code under? Can you just add it through the GUI builder-if so how? Or do you have to 'manually' add it in the code? If so do you put it under public class or just class? How to you write it?
Though I would personally prefer if there was a way to add the photo through the GUI builder.
Also, if I added an imagine to a JLabel, would it be possible for me to set it as a background so all the other JLabels or Buttons etc in the GUI is overlapping the picture?
Netbeans version 6.9.1
In Netbeans it is a little bit difficult to do this but still it could be done (not as easy as VS). You just have to follow the following steps:
Create the new JPanel object using the wizard
Go to the Source mode and paste the following text
-
public NewJPanel() { //this is the contsructor , so change the name apropriately
try {
image = ImageIO.read(new File("c:\\1.png")); //path to your image
} catch (IOException ex) {
}
initComponents();
}
private BufferedImage image;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null); //image drawing properties
}
Import all apropriate libs
Save the NewJPanel file.
Now go to your JFrame and drag and drop a Panel Object from the Swing Container list
Right click on the new jPanel object and select Customize Code from the menu
In the Code Customizer box select Custom creation and enter the following code. see image below
jPanel1 = new NewJPanel();
By doing this you replace the standard JPanel object with the one you created in the first steps
Click ok and then run your JFrame. You should see the image inside the JPanel now
PS: My Netbeans version is 7.2.1