Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
Added EnablementButtonTextField2
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed Feb 24, 2016
1 parent 9ce01c0 commit 2e97dec
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,23 @@
import melnorme.lang.tooling.ops.util.PathValidator;
import melnorme.util.swt.SWTFactoryUtil;
import melnorme.util.swt.components.AbstractCompositeWidget;
import melnorme.util.swt.components.FieldComponent;
import melnorme.util.swt.components.AbstractDisableableWidget;
import melnorme.util.swt.components.fields.ButtonTextField;
import melnorme.util.swt.components.fields.DirectoryTextField;

public abstract class LangSDKConfigBlock extends AbstractCompositePreferencesBlock {

public final LanguageSDKLocationGroup sdkLocationGroup;

public LangSDKConfigBlock(PreferencesPageContext prefContext) {
super(prefContext);

this.sdkLocationGroup = init_createSDKLocationGroup();
AbstractDisableableWidget sdkLocationGroup = init_createSDKLocationGroup();
addSubComponent(sdkLocationGroup);
}

protected LanguageSDKLocationGroup init_createSDKLocationGroup() {
protected AbstractDisableableWidget init_createSDKLocationGroup() {
return new LanguageSDKLocationGroup();
}

public FieldComponent<String> getLocationField() {
return sdkLocationGroup.sdkLocationField;
}

protected abstract PathValidator getSDKValidator();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*******************************************************************************/
package melnorme.util.swt.components;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;

import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;

Expand All @@ -21,26 +23,32 @@

public abstract class AbstractCompositeWidget extends AbstractDisableableWidget {

private final ArrayList2<IDisableableWidget> subComponents = new ArrayList2<>();
private final ArrayList2<AbstractDisableableWidget> subComponents = new ArrayList2<>();
protected final CompositeValidatableField validation = new CompositeValidatableField();

protected boolean createInlined = true;

public AbstractCompositeWidget() {
}

public AbstractCompositeWidget(boolean createInlined) {
this.createInlined = createInlined;
}

@Override
public IFieldView<IStatusMessage> getStatusField() {
return validation;
}

protected void addSubComponent(AbstractDisableableWidget subComponent) {
assertNotNull(subComponent);
validation.addStatusField(true, subComponent.getStatusField());
subComponent.setParent(this);
subComponents.add(subComponent);
}

protected final Indexable<IDisableableWidget> getSubWidgets() {
return subComponents;
return subComponents.<IDisableableWidget>upcastTypeParameter();
}

@Override
Expand All @@ -65,21 +73,16 @@ protected GridData createSubComponentDefaultGridData() {
return gdFillDefaults().grab(true, false).create();
}

@Override
protected final void doSetEnabled(boolean enabled) {
getSubWidgets().forEach(subComponent -> {
subComponent.setEnabled(enabled);
});
}

@Override
protected void _verifyContract_IDisableableComponent() {
// No need to check, only possible children are AbstractComponentExt
}

@Override
protected void updateControlEnablement() {
super.updateControlEnablement();
protected final void doSetEnabled(boolean enabled) {
subComponents.forEach(subComponent -> {
subComponent.updateControlEnablement();
});

updateValidationStatusForEnablement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*******************************************************************************/
package melnorme.util.swt.components;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;
import static melnorme.utilbox.core.Assert.AssertNamespace.assertTrue;

import melnorme.lang.ide.ui.preferences.common.AbstractWidgetExt;

/**
Expand All @@ -18,21 +21,27 @@
public abstract class AbstractDisableableWidget extends AbstractWidgetExt
implements IDisableableWidget {

protected AbstractDisableableWidget parent;
protected boolean enabled = true;

public AbstractDisableableWidget() {
}

protected boolean enabled = true;
public void setParent(AbstractDisableableWidget parent) {
assertTrue(this.parent == null);
this.parent = assertNotNull(parent);
}

public boolean isEnabled() {
return enabled && (parent == null || parent.isEnabled());
}

@Override
public final void setEnabled(boolean enabled) {
this.enabled = enabled;
updateControlEnablement();
}

public boolean isEnabled() {
return enabled;
}

protected void updateControlEnablement() {
doSetEnabled(isEnabled());
}
Expand All @@ -41,12 +50,12 @@ protected void updateControlEnablement() {


@Override
protected final void updateComponentFromInput() {
doUpdateComponentFromInput();
public void updateComponentFromInput() {
updateControlEnablement();
doUpdateComponentFromInput2();
}

protected void doUpdateComponentFromInput() {
protected void doUpdateComponentFromInput2() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void removeListener(IFieldValueListener listener) {
/* ----------------- ----------------- */

@Override
public void doUpdateComponentFromInput() {
public void doUpdateComponentFromInput2() {
if(isCreated()) {
doUpdateComponentFromValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ protected void createContents_Other(Composite topControl) {
}

protected void createContents_Label(Composite parent) {
label = SWTFactory.createLabel(parent, SWT.NONE, labelText);
if(labelText != null) {
label = SWTFactory.createLabel(parent, SWT.NONE, labelText);
}
}

protected abstract void createContents_layout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public abstract class ButtonTextField extends TextFieldComponent {
protected String buttonLabel;
protected Button button;

public ButtonTextField(String labelText, int textStyle, String buttonlabel) {
super(labelText, textStyle);
buttonLabel = buttonlabel;
public ButtonTextField(String label, int textStyle, String buttonLabel) {
super(label, textStyle);
this.buttonLabel = buttonLabel;
}

public ButtonTextField(String labelText, String buttonlabel) {
super(labelText);
buttonLabel = buttonlabel;
public ButtonTextField(String label, String buttonLabel) {
super(label);
this.buttonLabel = buttonLabel;
}

protected String getButtonLabel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*******************************************************************************
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* 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:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.util.swt.components.fields;


import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;

import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;

import melnorme.util.swt.SWTFactoryUtil;
import melnorme.util.swt.components.AbstractCompositeWidget;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.fields.IProperty;

/**
* Second version of EnablementButtonTextField2,
* using AbstractCompositeWidget as base instead
*/
public abstract class EnablementButtonTextField2 extends AbstractCompositeWidget {

public static final String LABEL_UseDefault = "Use default:";

protected final String label;
protected final String useDefaultCheckboxLabel;

protected final CheckBoxField useDefaultField;
protected final ButtonTextField buttonTextField;

public EnablementButtonTextField2(String label) {
this(label, LABEL_UseDefault);
}

public EnablementButtonTextField2(String label, String useDefaultCheckboxLabel) {
super(true);

this.label = assertNotNull(label);
this.useDefaultCheckboxLabel = assertNotNull(useDefaultCheckboxLabel);

this.useDefaultField = createUseDefaultField(useDefaultCheckboxLabel);
useDefaultField.setFieldValue(true);
useDefaultField.addListener(this::updateComponentFromInput);
addSubComponent(useDefaultField);

this.buttonTextField = init_createButtonTextField();
addSubComponent(buttonTextField);
}

protected CheckBoxField createUseDefaultField(String enablementCheckBoxLabel) {
return new CheckBoxField(enablementCheckBoxLabel);
}

protected abstract ButtonTextField init_createButtonTextField();

public ButtonTextField getButtonTextField() {
return buttonTextField;
}

public CheckBoxField getUseDefaultField() {
return useDefaultField;
}

public boolean isUseDefault() {
return useDefaultField.getBooleanFieldValue();
}

public String getEffectiveFieldValue() {
return isUseDefault() ? null : buttonTextField.getFieldValue();
}

public void setEffectiveFieldValue(String effectiveFieldValue) {
getUseDefaultField().setFieldValue(effectiveFieldValue == null);

if(effectiveFieldValue != null) {
buttonTextField.setFieldValue(effectiveFieldValue);
}
}

@Override
protected void doUpdateComponentFromInput2() {
if(isUseDefault()) {
String defaultFieldValue;
try {
defaultFieldValue = getDefaultFieldValue();
} catch(CommonException e) {
defaultFieldValue = "";
}
buttonTextField.setFieldValue(defaultFieldValue);
}
buttonTextField.setEnabled(!isUseDefault());
}

protected final IProperty<String> effectiveValueProperty = new IProperty<String>() {

@Override
public String getValue() {
return getEffectiveFieldValue();
}

@Override
public void setValue(String value) {
setEffectiveFieldValue(value);
}
};

public IProperty<String> asEffectiveValueProperty2() {
return effectiveValueProperty;
}

protected abstract String getDefaultFieldValue() throws CommonException;

/* ----------------- ----------------- */

@Override
protected Composite doCreateTopLevelControl(Composite parent) {
return SWTFactoryUtil.createGroup(parent, label, SWT.NONE);
}

@Override
protected GridLayoutFactory createTopLevelLayout() {
return glSwtDefaults().numColumns(getPreferredLayoutColumns());
}

@Override
public int getPreferredLayoutColumns() {
return 3;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public class TextFieldComponent extends LabelledFieldComponent<String> {
protected int defaultTextStyle = SWT.SINGLE | SWT.BORDER;
protected Text text;

public TextFieldComponent(String labelText) {
this(labelText, SWT.SINGLE | SWT.BORDER);
public TextFieldComponent(String label) {
this(label, SWT.SINGLE | SWT.BORDER);
}

public TextFieldComponent(String labelText, int textStyle) {
super(labelText, Option_AllowNull.NO, "");
public TextFieldComponent(String label, int textStyle) {
super(label, Option_AllowNull.NO, "");
this.defaultTextStyle = textStyle;
}

Expand Down

0 comments on commit 2e97dec

Please sign in to comment.