Redraw Eclipse RCP Editor after adding a widget - java

In my Eclipse plugin editor there is a button called "Add Graph" which adds a composite with a graph on another already created composite with "Grid Layout", but the created composite is not appearing until resizing the Editor manually or switch to another editor and go back
i have also used
parentComposite.pack();
parentComposite.layout();
but the added composite is shown up with a small size, and it`s adjusted also after resizing the Editor manually or switch to another editor and go back.
So my Question is how to call the same handler which runs while resizing the Editor manually or switch to another editor and go back. to adjust my view after adding the new composite.
Adding the Example:
public class MyEditor extends EditorPart {
private Composite compGraphesArea;
#Override
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout(2, false));
Button btnAddGraph = new Button(container, SWT.NONE);
btnAddGraph.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
createGraph(compGraphesArea);
}
});
btnAddGraph.setText("Add Graph");
compGraphesArea = new Composite(container, SWT.NONE);
compGraphesArea.setLayout(new GridLayout(1, true));
compGraphesArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
createGraph(compGraphesArea);
}
private void createGraph(Composite compGraphArea) {
JFreeChart chart = chartHandler.getChart();
compGraphPlot = new Composite(compGraphArea, SWT.BORDER | SWT.EMBEDDED);
compGraphPlot.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Frame chartFrame = SWT_AWT.new_Frame(compGraphPlot);
ChartPanel label = new ChartPanel(chart);
chartFrame.add(label);
chartFrame.pack();
chartFrame.setVisible(true);
XYPlot plot = chart.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setFixedAutoRange(1000000 + slider.getMaximum() / 2);
}
And this what i got after pressing "Add Graph":
and this what i am expecting and what i got after making any change on the Editor manually:

Related

Horizontal Alignment Off for Dynamic Composite within a ScrolledComposite

I have a parent ScrolledComposite with a dynamic child Composite. When I delete a row, the alignment appears off when using the ScrolledComposite. I have tried this using just Composite instead of ScrolledComposite and it appears to work, so I am wondering if the scrolled layout is just not updating correctly.
Attached are some images showing 1 entry versus 3 entries. The area of interest is the Movable Rings section. The alignment is clearly off when a single entry, but near normal with 3 entries. Code below...
/**
* Create display area for movable rings.
*
* #param topComposite
*/
private void createMovableRingsComposite(Composite topComposite) {
new Label(topComposite, SWT.NONE).setText("Movable Rings");
Composite borderComposite = new Composite(topComposite, SWT.BORDER);
borderComposite.setLayout(new GridLayout(1, false));
borderComposite.setLayoutData(new GridData(
GridData.HORIZONTAL_ALIGN_FILL));
scroll = new ScrolledComposite(borderComposite,SWT.V_SCROLL|SWT.BORDER);
GridLayout gridL = new GridLayout(1, false);
GridData gridD = new GridData(SWT.FILL,SWT.FILL,true,true);
gridD.heightHint = 200;
scroll.setLayout(gridL);
scroll.setLayoutData(gridD);
scroll.setAlwaysShowScrollBars(true);
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
movableRingsComposite = new Composite(scroll, SWT.NONE);
movableRingsComposite.setLayoutData(new GridData(SWT.CENTER,
SWT.CENTER, true, true, 1, 1));
movableRingsComposite.setLayout(new GridLayout(6, false));
// Make all the labels
new Label(movableRingsComposite, SWT.NONE).setText("Show");
new Label(movableRingsComposite, SWT.NONE).setText("ID");
new Label(movableRingsComposite, SWT.NONE).setText("Lat");
new Label(movableRingsComposite, SWT.NONE).setText("Lon");
new Label(movableRingsComposite, SWT.NONE).setText("Radius");
new Label(movableRingsComposite, SWT.NONE).setText("Labels");
scroll.setContent(movableRingsComposite);
Composite createComposite = new Composite(borderComposite, SWT.NONE);
createComposite.setLayout(new GridLayout(3, false));
createComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
true, true, 1, 1));
new Label(createComposite, SWT.NONE).setText("New at: ");
pointsMenuButton = new MenuButton(createComposite);
populatePointsMenuButton();
pointsDataManager.addPointsChangedListener(this);
pointsMenuButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
MenuButton menuButton = (MenuButton) e.widget;
MenuItem mi = menuButton.getSelectedItem();
addMovableRing(mi.getText());
menuButton.setSelectedItem("Select One");
}
});
Button delete = new Button(createComposite, SWT.PUSH);
delete.setText("Delete");
delete.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event event) {
deleteMovableRing();
}
});
}
/**
* Remove the ring of the selected movable ring row.
*/
private void deleteMovableRing() {
for (MovableRingRow row : movableRings) {
if (row.isSelected()) {
row.dispose();
movableRings.remove(row);
movableRingsComposite.layout(true);
getShell().pack();
if (!movableRings.isEmpty()) {
movableRings.iterator().next().selectRow(true);
}
break;
}
}
}

Unable to redraw SWT Composite in a Wizard Page

I have a composite, which has 3 UI controls - SWT Group, SWT TabFolder, and a child composite that has a label and a link. In some use-cases the SWT Group is hidden, and I would like that the blank space is not seen instead the composite has to be redrawn and show only the SWT TabFolder and the child composite, without any space.
I'm using composite.layout(true), but still there is blank space of the SWT Group when it is made hidden.
Could someone help me solve this issue
TestComposite extends Composite{
private Label childlabel;
private Group group;
private TabFolder tabFolder;
private Button button;
TestComposite(Compossite parent){
super(parent, SWT.NONE);
setLayout(new GridLayout(1, true));
setFont(parent.getFont());
setBackground(parent.getBackground());
GridDataFactory.fillDefaults().grab(true, true).applyTo(this);
createControl();
}
public void createControl() {
this.group = new Group(this, SWT.SHADOW_IN);
this.group.setSize(this.getDisplay().getActiveShell().getSize());
this.button = new Button(this.group, SWT.PUSH);
GridDataFactory.swtDefaults().applyTo(this.button);
this.tabFolder = new TabFolder(this, SWT.TOP);
GridDataFactory.swtDefaults().align(SWT.FILL, SWT.FILL).grab(true,
true).applyTo(this.tabFolder);
Composite childComposite = new Composite(this, SWT.NONE);
childComposite .setLayout(new GridLayout(2, false));
GridDataFactory.swtDefaults().grab(true, false).align(SWT.LEFT,
SWT.TOP).applyTo(childComposite );
this.childLabel = new Label(childComposite, SWT.NONE);
GridDataFactory.swtDefaults().grab(true, false).applyTo(childlabel);
setInput(abc);
enableVisibilityOfGroup(false);
}
public void enableVisibilityOfGroup(Boolean visible){
this.group.setVisible(visible);
// this shoudl redraw the UI and shown only tabFolder and label in the
// whole wizard page, but there is blank space in place of group
this.layout(true);
}
}
Set GridData on the Group
GridDataFactory.swtDefaults().applyTo(this.group);
Use GridData.exclude to exclude/include the group in the layout:
public void enableVisibilityOfGroup(boolean visible) {
this.group.setVisible(visible);
GridData gridData = (GridData)this.group.getLayoutData();
gridData.exclude = !visible;
this.layout(true);
}

Eclipse SWT ScrolledComposite refuses to scroll

I'm developing a plugin for eclipse and I'm struggling to use ScrolledComposite as well as making it take up the remaining space.
The parent layout is a 2-column Grid:
#Override
public void createPartControl(Composite parent) {
parent.setLayout(new GridLayout(2, false));
// other views etc
}
The ScrolledComposite is created like so (slightly abbreviated):
private void fillScroll() {
if (scroll != null) {
scroll.dispose();
}
scroll = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
GridData gd = new GridData();
gd.horizontalSpan = 2;
scroll.setLayoutData(gd);
Composite innerContainer = new Composite(scroll, 0);
innerContainer.setLayout(new GridLayout(1, false));
// for loop that adds widgets to innerContainer
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
scroll.setContent(innerContainer);
scroll.setSize(innerContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT));
parent.layout(true);
}
The list of questions are the widgets added in the for loop and are contained by innerContainer.
Expectation: Since innerContainer is too large to fit in the parent, there should be scroll bars.
What actually happens: There are no scroll bars.
How do I fix this problem?
The GridData on the ScrolledComposite needs to have grabExcessVerticalSpace set to true. I suggest you to use the extended constructor in order to clearly define how it should be displayed, for example:
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1);
Option A: change scroll.setSize to scroll.setMinSize:
scroll.setMinSize(innerContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT));
or, Option B: remove scroll.setExpandHorizontal and scroll.setExpandVertical and change scroll.setSize to innerContainer.setSize:
innerContainer.setSize(innerContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT));

TextBox in Toolbar Eclipse RCP Application

I am trying to add a search field in the toolbar after my buttons of my Eclipse RCP Application from the Application.e4xmi , but it doesn't work. I created a ToolControl with a handler :
#Execute
public void execute(Shell shell)
{
shell.setLayout(new GridLayout());
final Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new GridLayout());
Text text = new Text(comp, SWT.BORDER);
text.setMessage("Search");
text.setToolTipText("search");
System.out.println("i am in SearchToolItem ");
GridData lGridData = new GridData(GridData.FILL, GridData.FILL, true, true);
lGridData.widthHint = 200;
text.setLayoutData(lGridData);
}
How should I do this?
I assume you are specifying this class as a ToolControl in the e4xmi.
ToolControls don't use #Execute and they aren't given a Shell.
Instead use #PostConstruct and specify a Composite:
#PostConstruct
public void postConstruct(Composite parent)
{
Composite comp = new Composite(parent, SWT.NONE);
comp.setLayout(new GridLayout());
....
}
Note: Do not change the layout for the parent composite.

Eclipse canvas paintListener never called

I'm creating an editor extension to eclipse, and a for a part of the editor I need to draw simple graphical parts. From what I understand I should be able to use the eclipse widget Canvas for this. But the paintControl method of the PaintListener added to the canvas is never called. I'm assuming I'm missing some crucial simple part but I can't find it. Here is the code that creates, adds and calls redraw on the canvas.
void createGraphicPage() {
Composite composite = new Composite(getContainer(), SWT.NONE);
drawWidget = new Canvas(composite, SWT.NONE);
FormLayout layout = new FormLayout();
drawWidget.setLayout(layout);
drawWidget.addPaintListener(new PaintListener() {
#Override
public void paintControl(PaintEvent e) {
System.out.println("inDrawWidget");
Rectangle r = drawWidget.getClientArea();
e.gc.drawOval(0, 0, r.width, r.height);
}
});
drawWidget.redraw();
int index = addPage(composite);
setPageText(index, "Graphics Editor");
}
You need to set a layout on each Composite which contains controls, and usually set layout data on the controls. So:
Composite composite = new Composite(getContainer(), SWT.NONE);
composite.setLayout(new GridLayout());
Canvas drawWidget = new Canvas(composite, SWT.NONE);
drawWidget.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
drawWidget.addPaintListener(new PaintListener() {
#Override
public void paintControl(final PaintEvent e) {
System.out.println("inDrawWidget");
final Rectangle r = drawWidget.getClientArea();
e.gc.drawOval(0, 0, r.width, r.height);
}
});
I have added a GridLayout to the Composite and GridData layout data to the Canvas. The GridData makes the Canvas use all available space.

Categories