blob: 72ac29763334e62f480a9a3a547945e90761bc80 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2011 Laurent CARON
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Laurent CARON (laurent.caron at gmail dot com) - Initial implementation and API
*******************************************************************************/
package org.mihalis.opal.preferenceWindow;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.mihalis.opal.preferenceWindow.widgets.PWButton;
import org.mihalis.opal.preferenceWindow.widgets.PWLabel;
import org.mihalis.opal.preferenceWindow.widgets.PWWidget;
/**
* Instances of this class are rows.
*/
public class PWRow extends PWRowGroup {
/** The widgets. */
protected final List<PWWidget> widgets;
/**
* Constructor.
*/
public PWRow() {
this.widgets = new ArrayList<PWWidget>();
}
/**
* Adds the.
*
* @param widget the widget
* @return the PW container
* @see org.mihalis.opal.preferenceWindow.PWContainer#add(org.mihalis.opal.preferenceWindow.widgets.PWWidget)
*/
@Override
public PWContainer add(final PWWidget widget) {
this.widgets.add(widget);
addColumn(widget.getNumberOfColumns());
return this;
}
/**
* Adds the.
*
* @param element the element
* @return the PW container
* @see org.mihalis.opal.preferenceWindow.PWContainer#add(org.mihalis.opal.preferenceWindow.PWContainer)
*/
@Override
public PWContainer add(final PWContainer element) {
if (element instanceof PWRow || element instanceof PWGroup) {
return this.parent.add(element);
} else {
throw new UnsupportedOperationException("Can only add a PWGroup or a PWRow.");
}
}
/**
* Builds the.
*
* @param parent the parent
* @see org.mihalis.opal.preferenceWindow.PWContainer#build(org.eclipse.swt.widgets.Composite)
*/
@Override
public void build(final Composite parent) {
final int size = this.widgets.size();
int columIndex = 0;
for (int i = 0; i < size; i++) {
final PWWidget widget = this.widgets.get(i);
final Control control = widget.checkAndBuild(parent);
if (control != null && control.getLayoutData() == null) {
final int colSpan;
final boolean grabExcessSpace;
final int alignment;
if (size == 1) {
if (widget.isSingleWidget()) {
colSpan = this.parentNumberOfColums;
} else {
colSpan = this.parentNumberOfColums - widget.getNumberOfColumns() + 1;
}
grabExcessSpace = true;
} else {
if (i == size - 1) {
colSpan = this.parentNumberOfColums - columIndex;
grabExcessSpace = widget.isGrabExcessSpace();
} else {
colSpan = 1;
grabExcessSpace = widget instanceof PWButton && i == 0 ? true : widget.isGrabExcessSpace();
}
}
columIndex += widget.getNumberOfColumns();
if (i == 0 && grabExcessSpace && size > 1) {
if (widget instanceof PWLabel || widget instanceof PWButton) {
alignment = GridData.END;
} else {
alignment = GridData.BEGINNING;
}
} else {
alignment = widget.getAlignment();
}
final GridData gd = new GridData(alignment, GridData.BEGINNING, grabExcessSpace, false, colSpan, 1);
gd.horizontalIndent = widget.getIndent();
gd.widthHint = widget.getWidth();
if (widget.getHeight() != -1) {
gd.heightHint = widget.getHeight();
}
control.setLayoutData(gd);
}
}
}
/**
* Check parent.
*
* @param parent the parent
* @see org.mihalis.opal.preferenceWindow.PWRowGroup#checkParent(org.mihalis.opal.preferenceWindow.PWContainer)
*/
@Override
protected void checkParent(final PWContainer parent) {
if (parent instanceof PWTab || parent instanceof PWGroup) {
return;
}
throw new UnsupportedOperationException("Bad parent, should be only PWTab or PWGroup");
}
/**
* Enable or disable.
*
* @see org.mihalis.opal.preferenceWindow.PWRowGroup#enableOrDisable()
*/
@Override
public void enableOrDisable() {
if (this.enabler == null) {
return;
}
final boolean enabled = this.enabler.isEnabled();
for (final PWWidget widget : this.widgets) {
final boolean widgetEnable = widget.enableOrDisable();
for (final Control c : widget.getControls()) {
if (!c.isDisposed()) {
c.setEnabled(enabled && widgetEnable);
}
}
}
}
}