at.spardat.xma.page.effects.DefaultPageEffects Maven / Gradle / Ivy
The newest version!
package at.spardat.xma.page.effects;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Control;
import at.spardat.xma.page.PageClient;
public class DefaultPageEffects implements IPageEffects {
final private static String UNCOLLAPSED_FORMDATA_KEY = "uncollapsedFormData";
final PageClient pageClient;
final IControlColoring controlColoring;
boolean layoutRequired;
public DefaultPageEffects(PageClient pageClient) {
this.pageClient = pageClient;
controlColoring = createControlColoring(this);
}
public void initializePageEffects() {
layoutRequired = false;
}
public void applyPageEffects() {
if (layoutRequired) {
pageClient.getComposite().layout(true,true);
}
}
protected IControlColoring createControlColoring(IPageEffects pageEffects) {
return new DefaultControlColoring(pageEffects);
}
/**
* This method is called every time the error state is changed.
*/
public void errorStateChanged(Control control, boolean editable, boolean inError) {
controlColoring.setBackgroundForErrorState(control,editable,inError);
}
/**
* This method is called every time the mandatory flag has changed.
*/
public void mandatoryChanged(Object uiControl, boolean mandatory) {
}
public void setEditableBackground(Control control, Color color) {
controlColoring.setEditableBackground(control,color);
}
public PageClient getPage () {
return pageClient;
}
protected IControlColoring getControlColoring() {
return controlColoring;
}
public void setCollapsed(Control control, boolean condition) {
FormData data = (FormData)control.getLayoutData();
FormData uncollapsedFormData = getUncollapsedFormData(control,data);
if (data.top != null || data.bottom != null) {
if (!condition) {
if (data.top != null) {
data.top = cloneFormAttachment(uncollapsedFormData.top);
}
if (data.bottom != null) {
data.bottom = cloneFormAttachment(uncollapsedFormData.bottom);
}
data.height = uncollapsedFormData.height;
control.setVisible(true);
} else {
if (uncollapsedFormData.top != null) {
data.top = cloneFormAttachment(uncollapsedFormData.top);
data.top.offset = 0;
} else {
data.top=null;
}
if (data.bottom != null) {
data.bottom = cloneFormAttachment(uncollapsedFormData.bottom);
data.bottom.offset = 0;
} else {
data.bottom = null;
}
data.height = 0;
control.setVisible(false);
}
} else if (data.left != null || data.right != null) {
if (!condition) {
data.width = -1;
control.setVisible(true);
} else {
data.width = 0;
control.setVisible(false);
}
} else {
throw new RuntimeException("Can not determine collapse direction for control "+control);
}
layoutRequired=true;
}
protected FormData getUncollapsedFormData(Control control, FormData data) {
FormData uncollapsedFormData = (FormData)control.getData(UNCOLLAPSED_FORMDATA_KEY);
if (uncollapsedFormData == null) {
uncollapsedFormData=cloneFormData(data);
control.setData(UNCOLLAPSED_FORMDATA_KEY,uncollapsedFormData);
}
return uncollapsedFormData;
}
private FormData cloneFormData ( FormData data ) {
FormData clone = new FormData();
clone.height = data.height;
clone.width = data.width;
clone.top = cloneFormAttachment(data.top);
clone.bottom = cloneFormAttachment(data.bottom);
clone.left = cloneFormAttachment(data.left);
clone.right = cloneFormAttachment(data.right);
return clone;
}
private FormAttachment cloneFormAttachment ( FormAttachment formAttachment ) {
if (formAttachment != null) {
FormAttachment clone = new FormAttachment();
clone.control = formAttachment.control;
clone.alignment = formAttachment.alignment;
clone.offset = formAttachment.offset;
clone.numerator = formAttachment.numerator;
clone.denominator = formAttachment.denominator;
return clone;
}
return null;
}
}