Skip to content

Package: AbstractCheckboxSelectionDialog$1

AbstractCheckboxSelectionDialog$1

nameinstructionbranchcomplexitylinemethod
widgetSelected(SelectionEvent)
M: 18 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: * Copyright (c) 2006, 2008 IBM Corporation and others.
3: * This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v2.0
5: * which accompanies this distribution, and is available at
6: * https://www.eclipse.org/legal/epl-v2.0
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * IBM Corporation - initial API and implementation
12: */
13: package org.eclipse.egf.core.ui.dialogs;
14:
15: import java.util.Arrays;
16: import java.util.List;
17:
18: import org.eclipse.egf.core.ui.internal.SWTFactory;
19: import org.eclipse.egf.core.ui.l10n.CoreUIMessages;
20: import org.eclipse.jface.dialogs.IDialogConstants;
21: import org.eclipse.jface.viewers.CheckStateChangedEvent;
22: import org.eclipse.jface.viewers.CheckboxTableViewer;
23: import org.eclipse.jface.viewers.ICheckStateListener;
24: import org.eclipse.jface.viewers.StructuredSelection;
25: import org.eclipse.jface.viewers.StructuredViewer;
26: import org.eclipse.swt.SWT;
27: import org.eclipse.swt.events.SelectionAdapter;
28: import org.eclipse.swt.events.SelectionEvent;
29: import org.eclipse.swt.layout.GridData;
30: import org.eclipse.swt.widgets.Button;
31: import org.eclipse.swt.widgets.Composite;
32: import org.eclipse.swt.widgets.Shell;
33: import org.eclipse.swt.widgets.Table;
34:
35: /**
36: * This class provides selection dialog using a check box table viewer.
37: *
38: * @since 3.4
39: */
40: public abstract class AbstractCheckboxSelectionDialog extends AbstractSelectionDialog {
41:
42: /**
43: * Whether to add Select All / De-select All buttons to the custom footer controls.
44: */
45: private boolean fShowSelectButtons = false;
46:
47: /**
48: * Constructor
49: *
50: * @param parentShell
51: */
52: public AbstractCheckboxSelectionDialog(Shell parentShell) {
53: super(parentShell);
54: setShellStyle(getShellStyle() | SWT.RESIZE);
55: }
56:
57: /**
58: * Returns the viewer cast to the correct instance. Possibly <code>null</code> if
59: * the viewer has not been created yet.
60: *
61: * @return the viewer cast to CheckboxTableViewer
62: */
63: protected CheckboxTableViewer getCheckBoxTableViewer() {
64: return (CheckboxTableViewer) fViewer;
65: }
66:
67: /**
68: * (non-Javadoc)
69: *
70: * @see org.eclipse.egf.core.ui.dialogs.AbstractSelectionDialog#initializeControls()
71: */
72: @Override
73: protected void initializeControls() {
74: List<?> selectedElements = getInitialElementSelections();
75: if (selectedElements != null && selectedElements.isEmpty() == false) {
76: fViewer.setInput(getViewerInput());
77: getCheckBoxTableViewer().setCheckedElements(selectedElements.toArray());
78: getCheckBoxTableViewer().setSelection(StructuredSelection.EMPTY);
79: }
80: if (getButtonBar() != null) {
81: super.initializeControls();
82: }
83: }
84:
85: /**
86: * (non-Javadoc)
87: *
88: * @see org.eclipse.egf.core.ui.dialogs.AbstractSelectionDialog#createViewer (org.eclipse.swt.widgets.Composite)
89: */
90: @Override
91: protected StructuredViewer createViewer(Composite parent) {
92: // by default return a checkbox table viewer
93: Table table = new Table(parent, SWT.BORDER | SWT.SINGLE | SWT.CHECK);
94: table.setLayoutData(new GridData(GridData.FILL_BOTH));
95: return new CheckboxTableViewer(table);
96: }
97:
98: /**
99: * (non-Javadoc)
100: *
101: * @see org.eclipse.egf.core.ui.dialogs.AbstractSelectionDialog#addViewerListeners (org.eclipse.jface.viewers.StructuredViewer)
102: */
103: @Override
104: protected void addViewerListeners(StructuredViewer viewer) {
105: getCheckBoxTableViewer().addCheckStateListener(new DefaultCheckboxListener());
106: }
107:
108: /**
109: * A checkbox state listener that ensures that exactly one element is checked
110: * and enables the OK button when this is the case.
111: *
112: */
113: private class DefaultCheckboxListener implements ICheckStateListener {
114:
115: public void checkStateChanged(CheckStateChangedEvent event) {
116: if (getButtonBar() != null) {
117: getButton(IDialogConstants.OK_ID).setEnabled(isValid());
118: }
119: }
120:
121: }
122:
123: /**
124: * (non-Javadoc)
125: *
126: * @see org.eclipse.egf.core.ui.dialogs.AbstractSelectionDialog#isValid()
127: */
128: @Override
129: protected boolean isValid() {
130: return getCheckBoxTableViewer().getCheckedElements().length > 0;
131: }
132:
133: /**
134: * (non-Javadoc)
135: *
136: * @see org.eclipse.egf.core.ui.dialogs.AbstractSelectionDialog# addCustomFooterControls(org.eclipse.swt.widgets.Composite)
137: */
138: @Override
139: protected void addCustomFooterControls(Composite parent) {
140: if (fShowSelectButtons) {
141: Composite comp = SWTFactory.createComposite(parent, 2, 1, GridData.FILL_HORIZONTAL);
142: GridData gd = (GridData) comp.getLayoutData();
143: gd.horizontalAlignment = SWT.END;
144: Button button = SWTFactory.createPushButton(comp, CoreUIMessages.AbstractCheckboxSelectionDialog_Select, null);
145: button.addSelectionListener(new SelectionAdapter() {
146:
147: @Override
148: public void widgetSelected(SelectionEvent e) {
149: getCheckBoxTableViewer().setAllChecked(true);
150:• if (getButtonBar() != null) {
151: getButton(IDialogConstants.OK_ID).setEnabled(isValid());
152: }
153: }
154:
155: });
156: button = SWTFactory.createPushButton(comp, CoreUIMessages.AbstractCheckboxSelectionDialog_Deselect, null);
157: button.addSelectionListener(new SelectionAdapter() {
158:
159: @Override
160: public void widgetSelected(SelectionEvent e) {
161: getCheckBoxTableViewer().setAllChecked(false);
162: if (getButtonBar() != null) {
163: getButton(IDialogConstants.OK_ID).setEnabled(isValid());
164: }
165: }
166:
167: });
168: }
169: }
170:
171: /**
172: * If this setting is set to true before the dialog is opened, a Select All and
173: * a De-select All button will be added to the custom footer controls. The default
174: * setting is false.
175: *
176: * @param setting
177: * whether to show the select all and de-select all buttons
178: */
179: protected void setShowSelectAllButtons(boolean setting) {
180: fShowSelectButtons = setting;
181: }
182:
183: /**
184: * (non-Javadoc)
185: *
186: * @see org.eclipse.jface.dialogs.Dialog#okPressed()
187: */
188: @Override
189: protected void okPressed() {
190: Object[] elements = getCheckBoxTableViewer().getCheckedElements();
191: setResult(Arrays.asList(elements));
192: super.okPressed();
193: }
194:
195: /**
196: * Returns the list of selections made by the user, or <code>null</code>
197: * if the selection was canceled.
198: *
199: * @return the array of selected elements, or <code>null</code> if Cancel
200: * was pressed
201: */
202: @Override
203: public Object[] getResult() {
204: if (getCheckBoxTableViewer().getControl().isDisposed()) {
205: return super.getResult();
206: }
207: return getCheckBoxTableViewer().getCheckedElements();
208: }
209:
210: }