1 /*******************************************************************************
2  * Copyright (c) 2014 TwelveTone LLC and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors: Steven Spungin <steven@spungin.tv> - initial API and implementation, Bug 432555
12  *******************************************************************************/
13 package org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.empty;
14 
15 import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.Messages;
16 import org.eclipse.jface.viewers.ILabelProvider;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.RowLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Shell;
25 
26 /**
27  * A standard ElementListSelectionDialog with additional options for including,
28  * excluding, and selecting empty values.
29  *
30  * @author Steven Spungin
31  *
32  */
33 public class TitleAreaFilterDialogWithEmptyOptions extends TitleAreaFilterDialog {
34 	private Button btnExcludeEmptyValues;
35 	private Button btnOnlyEmptyValues;
36 	private Button btnIncludeEmptyValues;
37 	private EmptyFilterOption emptyFilterOption;
38 	private boolean bShowEmptyOptions = true;
39 	private Composite compOptions;
40 	private Composite parent;
41 
TitleAreaFilterDialogWithEmptyOptions(Shell parent, ILabelProvider renderer)42 	public TitleAreaFilterDialogWithEmptyOptions(Shell parent, ILabelProvider renderer) {
43 		super(parent, renderer);
44 	}
45 
46 	@Override
createDialogArea(Composite parent)47 	protected org.eclipse.swt.widgets.Control createDialogArea(Composite parent) {
48 		this.parent = parent;
49 		Composite comp = (Composite) super.createDialogArea(parent);
50 
51 		// Label labelEmptyInfo = new Label(comp, SWT.NONE);
52 		// labelEmptyInfo.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER,
53 		// false, false));
54 		// labelEmptyInfo.setText("An empty value is defined as a null object, an empty string, or an empty collection.");
55 
56 		compOptions = new Composite(comp, SWT.NONE);
57 		compOptions.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
58 		compOptions.setLayout(new RowLayout());
59 
60 		// labelEmptyInfo.moveAbove(getText());
61 		compOptions.moveAbove(getText());
62 
63 		btnExcludeEmptyValues = new Button(compOptions, SWT.RADIO);
64 		btnExcludeEmptyValues.setText(Messages.TitleAreaFilterDialogWithEmptyOptions_excludeEmptyValues);
65 		btnExcludeEmptyValues.addSelectionListener(new SelectionAdapter() {
66 			@Override
67 			public void widgetSelected(SelectionEvent e) {
68 				setEmptyFilterOption(EmptyFilterOption.EXCLUDE);
69 			}
70 		});
71 
72 		btnIncludeEmptyValues = new Button(compOptions, SWT.RADIO);
73 		btnIncludeEmptyValues.setText(Messages.TitleAreaFilterDialogWithEmptyOptions_includeEmptyValues);
74 		btnIncludeEmptyValues.addSelectionListener(new SelectionAdapter() {
75 			@Override
76 			public void widgetSelected(SelectionEvent e) {
77 				setEmptyFilterOption(EmptyFilterOption.INCLUDE);
78 			}
79 		});
80 
81 		btnOnlyEmptyValues = new Button(compOptions, SWT.RADIO);
82 		btnOnlyEmptyValues.setText(Messages.TitleAreaFilterDialogWithEmptyOptions_onlyEmptyValues);
83 		btnOnlyEmptyValues.addSelectionListener(new SelectionAdapter() {
84 			@Override
85 			public void widgetSelected(SelectionEvent e) {
86 				setEmptyFilterOption(EmptyFilterOption.ONLY);
87 			}
88 		});
89 
90 		String toolTip = Messages.TitleAreaFilterDialogWithEmptyOptions_emptyValueDescription;
91 		btnExcludeEmptyValues.setToolTipText(toolTip);
92 		btnIncludeEmptyValues.setToolTipText(toolTip);
93 		btnOnlyEmptyValues.setToolTipText(toolTip);
94 
95 		setEmptyFilterOption(EmptyFilterOption.EXCLUDE);
96 
97 		updateEmptyOptionState();
98 
99 		parent.pack();
100 		return comp;
101 	}
102 
103 	@Override
close()104 	public boolean close() {
105 		return super.close();
106 	}
107 
getEmptyFilterOption()108 	public EmptyFilterOption getEmptyFilterOption() {
109 		return emptyFilterOption;
110 	}
111 
setEmptyFilterOption(EmptyFilterOption emptyFilterOption)112 	public void setEmptyFilterOption(EmptyFilterOption emptyFilterOption) {
113 		this.emptyFilterOption = emptyFilterOption;
114 		updateUi();
115 	}
116 
updateUi()117 	private void updateUi() {
118 		if (btnExcludeEmptyValues == null) {
119 			return;
120 		}
121 		switch (emptyFilterOption) {
122 		case EXCLUDE:
123 			btnExcludeEmptyValues.setSelection(true);
124 			btnIncludeEmptyValues.setSelection(false);
125 			btnOnlyEmptyValues.setSelection(false);
126 			break;
127 		case INCLUDE:
128 			btnExcludeEmptyValues.setSelection(false);
129 			btnIncludeEmptyValues.setSelection(true);
130 			btnOnlyEmptyValues.setSelection(false);
131 			break;
132 		case ONLY:
133 			btnExcludeEmptyValues.setSelection(false);
134 			btnIncludeEmptyValues.setSelection(false);
135 			btnOnlyEmptyValues.setSelection(true);
136 			break;
137 		default:
138 			break;
139 		}
140 	}
141 
setShowEmptyOptions(boolean bShow)142 	public void setShowEmptyOptions(boolean bShow) {
143 		this.bShowEmptyOptions = bShow;
144 		updateEmptyOptionState();
145 	}
146 
updateEmptyOptionState()147 	private void updateEmptyOptionState() {
148 		if (compOptions == null) {
149 			return;
150 		}
151 		compOptions.setVisible(bShowEmptyOptions);
152 		((GridData) compOptions.getLayoutData()).exclude = bShowEmptyOptions == false;
153 		parent.layout();
154 	}
155 }