Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 723b01ab409ad18375c015dde409324db6841721 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*******************************************************************************
 * Copyright (c) 2008, 2011 Wind River Systems, Inc. and others.
 * 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:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.ui.preferences;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

/**
 * An integer field editor with an enablement check box.
 */
public class IntegerWithBooleanFieldEditor extends DecoratingIntegerFieldEditor {

    private final String fEnableKey;

    private Button fCheckbox;

    private boolean fWasSelected;

    public IntegerWithBooleanFieldEditor(String enableKey, String nameKey,
            String labelText, Composite parent) {
        super(nameKey, labelText, parent);
        fEnableKey = enableKey;
    }

    public IntegerWithBooleanFieldEditor(String enableKey, String nameKey,
            String labelText, Composite parent, int textLimit) {
        super(nameKey, labelText, parent, textLimit);
        fEnableKey = enableKey;
    }

    @Override
    protected void doFillIntoGrid(Composite parent, int numColumns) {
        getCheckboxControl(parent);
        super.doFillIntoGrid(parent, numColumns);
    }

    private Button getCheckboxControl(Composite parent) {
        if (fCheckbox == null) {
            Composite inner = new Composite(parent, SWT.NULL);
            final GridLayout layout = new GridLayout(2, false);
            layout.marginWidth = 0;
            inner.setLayout(layout);
            fCheckbox = new Button(inner, SWT.CHECK);
            fCheckbox.setFont(parent.getFont());
            fCheckbox.setText(getLabelText());
            // create and hide label from base class
            Label label = getLabelControl(inner);
            label.setText(""); //$NON-NLS-1$
            label.setVisible(false);
            fCheckbox.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    boolean isSelected = fCheckbox.getSelection();
                    valueChanged(fWasSelected, isSelected);
                    fWasSelected = isSelected;
                }
            });
        }
        else {
            checkParent(fCheckbox.getParent(), parent);
        }
        return fCheckbox;
    }

    @Override
    public Label getLabelControl(Composite parent) {
        final Label label = getLabelControl();
        if (label == null) {
            return super.getLabelControl(parent);
        }
        else {
            checkParent(label.getParent(), parent);
        }
        return label;
    }

    protected void valueChanged(boolean oldValue, boolean newValue) {
        if (oldValue != newValue) {
            valueChanged();
            fireStateChanged(VALUE, oldValue, newValue);
            getTextControl().setEnabled(newValue);
            getLabelControl().setEnabled(newValue);
        }
    }

    @Override
    protected boolean checkState() {
        if (fCheckbox != null && !fCheckbox.getSelection()) {
            clearErrorMessage();
            return true;
        }
        return super.checkState();
    }

    @Override
    protected void doLoad() {
        super.doLoad();
        if (fCheckbox != null) {
            boolean value = getPreferenceStore().getBoolean(fEnableKey);
            fCheckbox.setSelection(value);
            fWasSelected = value;
            getTextControl().setEnabled(value);
            getLabelControl().setEnabled(value);
        }
    }

    @Override
    protected void doLoadDefault() {
        super.doLoadDefault();
        if (fCheckbox != null) {
            boolean value = getPreferenceStore().getDefaultBoolean(fEnableKey);
            fCheckbox.setSelection(value);
            fWasSelected = value;
            getTextControl().setEnabled(value);
            getLabelControl().setEnabled(value);
        }
    }

    @Override
    protected void doStore() {
        super.doStore();
        getPreferenceStore().setValue(fEnableKey, fCheckbox.getSelection());
    }

    /**
     * Returns this field editor's current boolean value.
     *
     * @return the value
     */
    public boolean getBooleanValue() {
        return fCheckbox.getSelection();
    }
}

Back to the top