[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.platform.swt] GridData problem

Hi,

I have a problem with GridData / GridLayout configuration.
I use in my application (which runs on Linux) a general tool bar which contains :
- a command line,


- a progress bar,

- and a set of buttons which is sometimes visible, sometimes not.


My problem occurs when hiding the parent composite of the buttons : all others widgets are moved to the top of the parent composite, whereas they were correctly vertical aligned before.


Here a snippet to show the problem : click on show/hide button, and you will see what I mean.

Thanks,
Helene

// ----------------------------
// THE MAIN CLASS
// ----------------------------
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class GridDataProblem {
private static Display display;
private static Composite mainComposite;
private static SampleComposite sampleComposite;
private static boolean show = false;


public static void main(String[] args) {
display = new Display();
Shell shell = new Shell(display);

shell.setLayout(new GridLayout());

mainComposite = new Composite(shell, SWT.BORDER);
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = false;
gridData.horizontalAlignment = GridData.FILL;
gridData.verticalAlignment = GridData.FILL;
mainComposite.setLayoutData(gridData);
mainComposite.setLayout(new GridLayout());

sampleComposite = new SampleComposite(mainComposite, SWT.NONE);

Button hideShow = new Button(mainComposite, SWT.PUSH);
hideShow.setText("Hide/Show");

hideShow.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
sampleComposite.refresh(show);
show = !show;
}
});

shell.setSize(800,200);
shell.open();


		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
   }
}

// ----------------------------
// A SPECIFIC COMPOSITE
// ----------------------------
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Text;

public final class SampleComposite extends Composite {
private Text commandLine;
private ProgressBar progressBar;
private Composite buttonsComposite;
private Button pauseButton;
private Button startStopButton;
public SampleComposite(Composite parent, int style) {
super(parent, style | SWT.NO_REDRAW_RESIZE);
this.setLayout(getGridLayout(4));
createContent();
setGridData();
this.setBackground(getDisplay().getSystemColor(SWT.COLOR_GREEN));
}


public void refresh(boolean show) {
if (show) {
showButtons();
} else {
hideButtons();
}
this.layout();
}


private void showButtons() {
buttonsComposite.setVisible(true);
GridData gridData = (GridData)progressBar.getLayoutData();
gridData.horizontalSpan = 1;
}
private void hideButtons() {
buttonsComposite.setVisible(false);
GridData gridData = (GridData)progressBar.getLayoutData();
gridData.horizontalSpan = 2;
}


private void createContent() {
createCommandLine();
createStatusAreaProgressBar();
createButtons();
}
private void createCommandLine() {
Label commandLineLabel = new Label(this, SWT.NONE);
commandLineLabel.setText("Command :");
commandLineLabel.setLayoutData(getNewGridData(false));
this.commandLine = new Text(this, SWT.BORDER);
this.commandLine.setLayoutData(getNewGridData(true));
}
private void createStatusAreaProgressBar() {
this.progressBar = new ProgressBar(this, SWT.BORDER|SWT.SMOOTH);
this.progressBar.setMaximum(100);
this.progressBar.setMinimum(0);
this.progressBar.setLayoutData(getNewGridData(true));
}
private void createButtons() {
buttonsComposite = new Composite(this, SWT.NONE);
buttonsComposite.setLayout(getGridLayout(2));
buttonsComposite.setLayoutData(getNewGridData(false));
this.pauseButton = new Button(buttonsComposite, SWT.PUSH);
this.pauseButton.setText("Pause");
this.pauseButton.setLayoutData(getNewGridData(false));
this.startStopButton = new Button(buttonsComposite, SWT.PUSH);
this.startStopButton.setText("Start");
this.startStopButton.setLayoutData(getNewGridData(false));
}
private void setGridData() {
GridData statusGridData = new GridData();
statusGridData.grabExcessHorizontalSpace = true;
statusGridData.grabExcessVerticalSpace = false;
statusGridData.horizontalAlignment = GridData.FILL;
statusGridData.verticalAlignment = GridData.FILL;
this.setLayoutData(statusGridData);
}
private GridData getNewGridData(boolean grabExcessHorizontalSpace) {
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = grabExcessHorizontalSpace;
gridData.grabExcessVerticalSpace = false;
gridData.horizontalAlignment = GridData.FILL;
gridData.verticalAlignment = GridData.CENTER;
return gridData;
}
public GridLayout getGridLayout(int nbColumns) {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = nbColumns;
gridLayout.horizontalSpacing = 8;
gridLayout.verticalSpacing = 8;
return gridLayout;
}
}