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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*******************************************************************************
* Copyright (c) 2000, 2015 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font should be
* activated and used by other components.
* Markus Schorn <markus.schorn@windriver.com> - Fix for bug 136591 -
* [Dialogs] TypeFilteringDialog appends unnecessary comma
*******************************************************************************/
package org.eclipse.ui.dialogs;
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IFileEditorMapping;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
import org.eclipse.ui.internal.WorkbenchMessages;
import org.eclipse.ui.internal.registry.EditorRegistry;
/**
* The TypeFilteringDialog is a SelectionDialog that allows the user to select a
* file editor.
*/
public class TypeFilteringDialog extends SelectionDialog {
Button addTypesButton;
Collection initialSelections;
// the visual selection widget group
CheckboxTableViewer listViewer;
// sizing constants
private static final int SIZING_SELECTION_WIDGET_HEIGHT = 250;
private static final int SIZING_SELECTION_WIDGET_WIDTH = 300;
private static final String TYPE_DELIMITER = WorkbenchMessages.TypesFiltering_typeDelimiter;
//Define a title for the filter entry field.
private String filterTitle = WorkbenchMessages.TypesFiltering_otherExtensions;
Text userDefinedText;
IFileEditorMapping[] currentInput;
/**
* Creates a type filtering dialog using the supplied entries. Set the
* initial selections to those whose extensions match the preselections.
* @param parentShell The shell to parent the dialog from.
* @param preselections
* of String - a Collection of String to define the preselected
* types
*/
public TypeFilteringDialog(Shell parentShell, Collection preselections) {
super(parentShell);
setTitle(WorkbenchMessages.TypesFiltering_title);
this.initialSelections = preselections;
setMessage(WorkbenchMessages.TypesFiltering_message);
setShellStyle(getShellStyle() | SWT.SHEET);
}
/**
* Creates a type filtering dialog using the supplied entries. Set the
* initial selections to those whose extensions match the preselections.
*
* @param parentShell The shell to parent the dialog from.
* @param preselections
* of String - a Collection of String to define the preselected
* types
* @param filterText -
* the title of the text entry field for other extensions.
*/
public TypeFilteringDialog(Shell parentShell, Collection preselections,
String filterText) {
this(parentShell, preselections);
this.filterTitle = filterText;
}
/**
* Add the selection and deselection buttons to the dialog.
*
* @param composite
* org.eclipse.swt.widgets.Composite
*/
private void addSelectionButtons(Composite composite) {
Composite buttonComposite = new Composite(composite, SWT.RIGHT);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
buttonComposite.setLayout(layout);
GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END
| GridData.GRAB_HORIZONTAL);
data.grabExcessHorizontalSpace = true;
composite.setData(data);
Button selectButton = createButton(buttonComposite,
IDialogConstants.SELECT_ALL_ID, WorkbenchMessages.WizardTransferPage_selectAll, false);
SelectionListener listener = widgetSelectedAdapter(e -> listViewer.setAllChecked(true));
selectButton.addSelectionListener(listener);
Button deselectButton = createButton(buttonComposite,
IDialogConstants.DESELECT_ALL_ID, WorkbenchMessages.WizardTransferPage_deselectAll, false);
listener = widgetSelectedAdapter(e -> listViewer.setAllChecked(false));
deselectButton.addSelectionListener(listener);
}
/**
* Add the currently-specified extensions to result.
* @param result
*/
private void addUserDefinedEntries(List result) {
StringTokenizer tokenizer = new StringTokenizer(userDefinedText
.getText(), TYPE_DELIMITER);
//Allow the *. and . prefix and strip out the extension
while (tokenizer.hasMoreTokens()) {
String currentExtension = tokenizer.nextToken().trim();
if (!currentExtension.equals("")) { //$NON-NLS-1$
if (currentExtension.startsWith("*.")) { //$NON-NLS-1$
result.add(currentExtension.substring(2));
} else {
if (currentExtension.startsWith(".")) { //$NON-NLS-1$
result.add(currentExtension.substring(1));
} else {
result.add(currentExtension);
}
}
}
}
}
/**
* Visually checks the previously-specified elements in this dialog's list
* viewer.
*/
private void checkInitialSelections() {
IFileEditorMapping editorMappings[] = ((EditorRegistry) PlatformUI
.getWorkbench().getEditorRegistry()).getUnifiedMappings();
ArrayList selectedMappings = new ArrayList();
for (IFileEditorMapping mapping : editorMappings) {
//Check for both extension and label matches
if (this.initialSelections.contains(mapping.getExtension())) {
listViewer.setChecked(mapping, true);
selectedMappings.add(mapping.getExtension());
} else {
if (this.initialSelections.contains(mapping.getLabel())) {
listViewer.setChecked(mapping, true);
selectedMappings.add(mapping.getLabel());
}
}
}
//Now add in the ones not selected to the user defined list
Iterator initialIterator = this.initialSelections.iterator();
StringBuilder entries = new StringBuilder();
while (initialIterator.hasNext()) {
String nextExtension = (String) initialIterator.next();
if (!selectedMappings.contains(nextExtension)) {
if (entries.length() != 0) {
entries.append(',');
}
entries.append(nextExtension);
}
}
this.userDefinedText.setText(entries.toString());
}
@Override
protected void configureShell(Shell shell) {
super.configureShell(shell);
PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
IWorkbenchHelpContextIds.TYPE_FILTERING_DIALOG);
}
@Override
protected Control createDialogArea(Composite parent) {
// page group
Composite composite = (Composite) super.createDialogArea(parent);
createMessageArea(composite);
listViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);
GridData data = new GridData(GridData.FILL_BOTH);
data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
listViewer.getTable().setLayoutData(data);
listViewer.getTable().setFont(parent.getFont());
listViewer.setLabelProvider(FileEditorMappingLabelProvider.INSTANCE);
listViewer.setContentProvider(ArrayContentProvider.getInstance());
listViewer.setComparator(new ViewerComparator());
addSelectionButtons(composite);
createUserEntryGroup(composite);
initializeViewer();
// initialize page
if (this.initialSelections != null && !this.initialSelections.isEmpty()) {
checkInitialSelections();
}
return composite;
}
/**
* Create the group that shows the user defined entries for the dialog.
*
* @param parent
* the parent this is being created in.
*/
private void createUserEntryGroup(Composite parent) {
Font font = parent.getFont();
// destination specification group
Composite userDefinedGroup = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
userDefinedGroup.setLayout(layout);
userDefinedGroup.setLayoutData(new GridData(
GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
Label fTitle = new Label(userDefinedGroup, SWT.NONE);
fTitle.setFont(font);
fTitle.setText(filterTitle);
// user defined entry field
userDefinedText = new Text(userDefinedGroup, SWT.SINGLE | SWT.BORDER);
userDefinedText.setFont(font);
GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL
| GridData.GRAB_HORIZONTAL);
userDefinedText.setLayoutData(data);
}
/**
* Return the input to the dialog.
* @return IFileEditorMapping[]
*/
private IFileEditorMapping[] getInput() {
//Filter the mappings to be just those with a wildcard extension
if (currentInput == null) {
List wildcardEditors = new ArrayList();
IFileEditorMapping[] allMappings = ((EditorRegistry)PlatformUI.getWorkbench()
.getEditorRegistry()).getUnifiedMappings();
for (IFileEditorMapping allMapping : allMappings) {
if (allMapping.getName().equals("*")) { //$NON-NLS-1$
wildcardEditors.add(allMapping);
}
}
currentInput = new IFileEditorMapping[wildcardEditors.size()];
wildcardEditors.toArray(currentInput);
}
return currentInput;
}
/**
* Initializes this dialog's viewer after it has been laid out.
*/
private void initializeViewer() {
listViewer.setInput(getInput());
}
/**
* The <code>TypeFilteringDialog</code> implementation of this
* <code>Dialog</code> method builds a list of the selected elements for
* later retrieval by the client and closes this dialog.
*/
@Override
protected void okPressed() {
// Get the input children.
IFileEditorMapping[] children = getInput();
List list = new ArrayList();
// Build a list of selected children.
for (IFileEditorMapping element : children) {
if (listViewer.getChecked(element)) {
list.add(element.getExtension());
}
}
addUserDefinedEntries(list);
setResult(list);
super.okPressed();
}
}
|