I'm having a few troubles with the dialogs in the eclipse rcp. I wish to have a dialog which shows me a MasterDetailBlock to manage an arbitrary amount of entities shown in a table on the master part with their corresponding DetailPages shown in the detail part. As of now, this is done using a View, but a non-modal dialog seems more fitting for this.
At first, I tried the naive way of just taking the code from the view und put it into the dialog, with a few modifications due to the difference between view and dialog creation. However, most controls were missing. A search on Google, the eclipse forums and here on Stackoverflow did not bring a solution for this. After checking these sites for the solution, I tried to understand what's happening by stepping through the code with the debugger, but that didn't help me either.
The following Code should show a dialog containing section in which a button should be displayed. However, it doesn't:
protected Control createDialogArea(Composite parent) {
parent.setLayout(new GridLayout(1, true));
Section section = toolkit.createSection(parent, ExpandableComposite.EXPANDED | ExpandableComposite.TITLE_BAR);
section.setLayout(new GridLayout());
section.setText("Section");
section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button test = toolkit.createButton(section, "test", SWT.PUSH);
test.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
test.setVisible(true);
section.computeSize(SWT.DEFAULT, SWT.DEFAULT);
return parent;
}
The result of this is:
However, as a MasterDetailBlock needs a form, I'll provide this Code as well:
protected Control createDialogArea(Composite parent) {
parent.setLayout(new GridLayout(1, true));
form = new ScrolledForm(parent);
form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
form.getBody().setLayout(new FillLayout());
Composite formComposite = toolkit.createComposite(form.getBody());
formComposite.setLayout(new GridLayout(1,true));
Section section = toolkit.createSection(formComposite, ExpandableComposite.EXPANDED | ExpandableComposite.TITLE_BAR);
section.setLayout(new GridLayout());
section.setText("Section");
section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button test = toolkit.createButton(section, "test", SWT.PUSH);
test.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
test.setVisible(true);
section.computeSize(SWT.DEFAULT, SWT.DEFAULT);
return parent;
}
Just a slight modification by adding a form to the dialog and everything goes on the form. However, the result looks like this:
I'm afraid I'm missing something obvious here. As I said, searching didn't bring any enlightment and stepping through the code didn't help either. My last resort "trying stuff to see what happens and try to understand that" didn't help much, as the results didn't change from the ones already posted.
So, do I miss something? (Which I think it is)
If you can provide me a link to show me what's wrong (or anything from your experience as well), I would appriciate that.
Thank you for your help.
There is a
section.setClient(test);
missing. Moreover, you should have this when using a Form and section is expandible:
protected final IExpansionListener expansionListener= new ExpansionAdapter()
{
/**
* {#inheritDoc}
*/
#Override
public void expansionStateChanged(ExpansionEvent e)
{
ScrolledForm form = ...; //your scrolled form goes here
form.layout(new Control[] {(ExpandableComposite) e.getSource()}, SWT.ALL | SWT.CHANGED);
form.reflow(true);
}
};
...
section.addExpansionListener(expansionListener);
Instead of the button test you should add a Composite which contains the button. Each section can have only one client.
Related
I am creating a preference page for an eclipse plugin we developed. in the preference page there is an Expandable Composite. it is by default collapsed but when it is expanded the window will not resize and even no scrolls appear! so you have to drag the window open to see the options. I use GridLayout for my container and also the Expandable Composite but still have this problem. I even tries to manually setSize of the container and still no luck. does anyone else had the same problem and solved it?
Thanks in advance.
final Composite container = new Composite(parent, SWT.FILL);
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
container.setLayout(new GridLayout(1, true));
final ExpandableComposite collap = new ExpandableComposite(container, SWT.Collapse);
collap.setClient(advancedOptions);
collap.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
advancedOptions.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
advancedOptions.setLayout(new RowLayout(SWT.VERTICAL));
I read a lot about this problem here in Stack Overflow and applied all the proposed solutions (getShell pack, layout, getparent layout etc...) and none of them worked.
I have a label with a text value. Also I have a button and when click on that button I'm changing the content of the label with a longer text. The problem is that the width of the label doesn't change and only a part of the new longer text is visible.
My text code:
Composite composite_2 = new Composite(this, SWT.NONE);
composite_2.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1));
composite_2.setLayout(new GridLayout(2, false));
Label lblNumbers = new Label(composite_2, SWT.NONE);
lblNumbers.setText("NĂºmeros:");
Label lblNumbersValue = new Label(composite_2, SWT.NONE);
lblNumbersValue.setText("...");
.
.
.
btnElegirNmeros.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
lblNumbersValue.setText("asddadadasdasdasd");
lblNumbersValue.getParent().layout();
lblNumbersValue.pack();
getShell().pack();
getShell().layout();
}
});
Since SWT 4.6, the most reliable method to trigger a layout update is Control.requestLayout().
It should solve these kind of problems.
So in your case try lblNumbersValue.requestLayout();.
I have a ScrolledComposite widget that I'm initializing inside a selection listener (in turn attached to another composite). This ScrolledComposite contains in turn another Composite, which in turn holds a Button and a Label. While the internal composite appears, none of its children widgets do. I've used ScrolledComposite plenty of times before, and everything looks right to my eye. Can any of you see anything wrong? Note, the ScrolledComposite is a class variable. Also note that this problem is occurring regardless of if I ever dispose of the composite and its contents in the else condition.
final Button showConsole = new Button(topLeft, SWT.CHECK);
showConsole.setText("Show Debug Console");
showConsole.setFont(new Font(domains.getDisplay(), "Segoe UI", 9, SWT.ITALIC));
showConsole.setSelection(false);
showConsole.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
//The total widget group is only supposed to appear when the button is selected
if (showConsole.getSelection()) {
scrolledConsoleComp = new ScrolledComposite(leftComposite,
SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
Composite consoleComposite = new ScrolledComposite(scrolledConsoleComp, SWT.NONE | SWT.BORDER);
consoleComposite.setLayout(new GridLayout());
consoleComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
consoleComposite.setVisible(true);
scrolledConsoleComp.setContent(consoleComposite);
scrolledConsoleComp.setExpandHorizontal(true);
scrolledConsoleComp.setExpandVertical(true);
scrolledConsoleComp.setLayout(new GridLayout());
scrolledConsoleComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button clear = new Button(consoleComposite, SWT.PUSH);
clear.setText("Clear Console");
final Label consoleText = new Label(consoleComposite, SWT.WRAP);
consoleText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
consoleText.setText("Messages: \n" + consoleData);
clear.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
consoleData = "";
consoleText.setText("Messages: \n" + consoleData);
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
scrolledConsoleComp.setMinSize(leftComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
leftComposite.layout(true);
} else {
scrolledConsoleComp.setVisible(false);
scrolledConsoleComp.dispose();
leftComposite.layout(true);
}
}
I appreciate any insight. Let me know if anything in this question is unclear. Thank you!
You're creating your Button and your Label inside a ScrolledComposite. They won't be shown because setContent() is not called.
Usually a ScrolledComposite contains a Composite that holds all the other Widgets.
Your consoleComposite should become a Composite.
No need for a call to setVisible()
In eclipse i have a layout with table , buttons text fields. I have created them in the:
public class NewForm extends FormPage{
#Override
protected void createFormContent(final IManagedForm managedForm) {
FormToolkit ftk= managedForm.getToolkit();
ScrolledForm scrldfrm = managedForm.getForm();
scrldfrm.getBody().setLayout(null);
scrldfrm.setText("Hello there!");
Section section = managedForm.getToolkit().createSection(
managedForm.getForm().getBody(),
Section.TWISTIE | Section.TITLE_BAR);
section.setBounds(522, 10, 374, 21);
managedForm.getToolkit().paintBordersFor(section);
section.setText("Selected API");
section.setExpanded(true);
textName = new Text(managedForm.getForm().getBody(), SWT.BORDER);
textName.setBounds(610, 67, 275, 21);
managedForm.getToolkit().adapt(textName, true, true);
textName.setEnabled(false);
//similarly table is added.
}
}
Everything works fine until the user maximizes the window. After maximizing the window, the form contents remain at same position, well I have used absolute values in the form. How to make it increase size when window re-sizes? If i should not give absolute values in form, the how to avoid absolute values and give values relative to window size?
Use a Layout rather than setBounds calls. GridLayout is one possibility or FormLayout.
Something like:
ScrolledForm scrldfrm = managedForm.getForm();
scrldfrm.getBody().setLayout(new GridLayout());
scrldfrm.setText("Hello there!");
Section section = managedForm.getToolkit().createSection(
managedForm.getForm().getBody(),
Section.TWISTIE | Section.TITLE_BAR);
section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
managedForm.getToolkit().paintBordersFor(section);
section.setText("Selected API");
section.setExpanded(true);
textName = new Text(managedForm.getForm().getBody(), SWT.BORDER);
textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false);
managedForm.getToolkit().adapt(textName, true, true);
textName.setEnabled(false);
I'm working on an application using SWT in Java, and I've encountered some wierd behavior from multiple StyledText widgets. It's fairly consistent: if there are more than one StyledText widget displayed in the window/view/editor at once, you can select whatever you want from each one individually, at the same time. In the screenshot, there are 4 separate widgets with 4 separate selections.
My expectation is that if I start selecting from one widget, whatever other widget that might already have a selection should then lose it, similarly to the behavior you would expect from a web browser; only one selection at a time.
I'd like to fix this problem, but I hope to avoid needing to make some manager that listens to every widget and turns off selections when a new one is made. Is there a better way to do this? (Also why is this happening in the first place?)
Why is it happening?
To best of my knowledge the behavior of a widget depends on its implementation i.e. whether it is a
native widget (like org.eclipse.swt.widgets.Text) or
custom widget (like org.eclipse.swt.custom.StyledText)
The difference lies in the handling of the mouse down or mouse up events.
For example,
In case of org.eclipse.swt.widgets.Text the left mouse down eventually translates to OS.SendMessage (hwnd, OS.WM_LBUTTONUP, wParam, lParam);
Whereas
org.eclipse.swt.custom.StyledText uses a mouse event handler and extra processing in the handleMouseDown(Event event) method. Most of the functionality or UI is done using custom draw/redraw/validate/invalidate/update methods.
To put it in a very crude win32 sdk way:
There are some controls provided by windows/win32 GDI
And, some are custom user drawn controls
See the below SWT code which uses text, styledtext, browser etc for testing. Also Note the browser control is not exactly a win32 control it is a wrapper control around Internet Explorer activex or mozilla's gecko engine, therefore its behavior is same as that of styled text.
Any possible solution ?
Well I can only think of borrowing SWT's styledtext code and then making a version which suits me. Or
As you have already mentioned, use some listener to reset all the other unfocused widgets (which even in my opinion is not a very clean solution).
Test Code & Output
Code:
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class StyledTextTest {
private static Display display;
public static void main(String[] args)
{
display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(2,true));
shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
createStyledText(shell);
createStyledText(shell);
createText(shell);
createText(shell);
createCombo(shell);
createCombo(shell);
createCustomCombo(shell);
createCustomCombo(shell);
createBrowser(shell);
createBrowser(shell);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void createCustomCombo(Composite parent)
{
new Label(parent, SWT.NONE).setText("Custom Combo");
CCombo c = new CCombo(parent, SWT.DROP_DOWN);
c.setItems(new String[] {"test best", "best rest", "rest test"});
c.select(0);
c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
}
private static void createCombo(Composite parent)
{
new Label(parent, SWT.NONE).setText("Combo");
Combo c = new Combo(parent, SWT.DROP_DOWN);
c.setItems(new String[] {"test best", "best rest", "rest test"});
c.select(0);
c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
}
static void createBrowser(Composite parent)
{
new Label(parent, SWT.NONE).setText("Browser");
Browser browser = new Browser(parent, SWT.NONE);
browser.setText("<div>This is a test !!</div>");
browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
}
static void createText(Composite parent) {
new Label(parent, SWT.NONE).setText("Text");
final Text text = new Text(parent, SWT.BORDER);
text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
}
static void createStyledText(Composite parent)
{
new Label(parent, SWT.NONE).setText("Styled Text");
StyledText text = new StyledText (parent, SWT.BORDER|SWT.SINGLE);
text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
// make 0123456789 appear bold
StyleRange style1 = new StyleRange();
style1.start = 0;
style1.length = 10;
style1.fontStyle = SWT.BOLD;
text.setStyleRange(style1);
// make ABCDEFGHIJKLM have a red font
StyleRange style2 = new StyleRange();
style2.start = 11;
style2.length = 13;
style2.foreground = display.getSystemColor(SWT.COLOR_RED);
text.setStyleRange(style2);
// make NOPQRSTUVWXYZ have a blue background
StyleRange style3 = new StyleRange();
style3.start = 25;
style3.length = 13;
style3.fontStyle = SWT.BOLD | SWT.ITALIC;
text.setStyleRange(style3);
}
}