Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3105880d84a38d180655c4d2dce008cd2e30b771 (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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*****************************************************************************
 * Copyright (c) 2011 CEA LIST.
 *
 *    
 * 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:
 *   Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *   Gregoire Dupe (Mia-Software) - Bug 341238 - We need to be able to specify which column have to be hidden/visible using the customization mechanism
 *   Vincent Lorenzo (CEA-LIST) - Bug 341238 - We need to be able to specify which column have to be hidden/visible using the customization mechanism
 *   Nicolas Bros (Mia-Software) - Bug 344247 - illegal API use in org.eclipse.emf.facet.widgets.nattable.internal.dialogs.ColumnsToHideDialog
 *   Gregoire Dupe (Mia-Software) - Bug 366804 - [Restructuring] Table widget upgrade
 *****************************************************************************/
package org.eclipse.papyrus.infra.table.efacet.menu.handler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.ETypedElement;
import org.eclipse.emf.facet.widgets.table.metamodel.v0_2_0.table.Column;

//import org.eclipse.emf.facet.widgets.table.ui.internal.comparator.ColumnComparator;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.SelectionDialog;

public class ColumnsToHideDialog extends SelectionDialog {

	//	private final Object inputElement;
	private final ILabelProvider labelProvider;

	private final SortedColumnContentProvider contentProvider;

	private CheckboxTreeViewer listViewer;

	//
	//	private static final int DIALOG_WIDTH = 300;
	//	private static final int DIALOG_HEIGHT = 300;
	//
	//	/** indicates if the local customization files should be set at the top */
	//	private boolean setAtTheTop;
	//
	//	/** the initial selection */
	//	private List<Column> initialSelection;
	//
	//	private final boolean askToPutOnTheTopTheLocalCustomization;Z
	//
	//	private boolean mustAskTheUserNextTime;
	//
	//	private PutOnTheTopQuestionDialog putOnTheTopQuestionDialog;

	private final Collection<EObject> input;

	private final Collection<ETypedElement> initialSelection;

	public ColumnsToHideDialog(final Shell parentShell, final Collection<EObject> classifiers, final Collection<ETypedElement> initialSelection, final ILabelProvider labelProvider) {
		super(parentShell);
		setTitle("Select the columns to show");
		this.input = classifiers;
		this.labelProvider = labelProvider;
		this.contentProvider = new SortedColumnContentProvider();
		this.initialSelection = initialSelection;
	}

	//	/**
	//	 * Visually checks the previously-specified elements in this dialog's list
	//	 * viewer.
	//	 */
	//	private void checkInitialSelections() {
	//		Iterator<?> itemsToCheck = getInitialElementSelections().iterator();
	//
	//		while(itemsToCheck.hasNext()) {
	//			this.listViewer.setChecked(itemsToCheck.next(), true);
	//		}
	//	}

	@Override
	protected void configureShell(final Shell shell) {
		super.configureShell(shell);
		//		shell.setImage(ImageProvider.getInstance().getSelectColumnsToHide());
	}

	@Override
	protected Control createDialogArea(final Composite parent) {
		// page group
		Composite composite = (Composite)super.createDialogArea(parent);

		initializeDialogUnits(composite);

		createMessageArea(composite);

		this.listViewer = new CheckboxTreeViewer(composite, 0);
		GridData data = new GridData(GridData.FILL_BOTH);
		data.heightHint = 300;
		//		data.widthHint = ColumnsToHideDialog.DIALOG_WIDTH;
		this.listViewer.getTree().setLayoutData(data);

		this.listViewer.setLabelProvider(this.labelProvider);
		this.listViewer.setContentProvider(this.contentProvider);

		//		addSelectionButtons(composite);

		this.listViewer.setInput(input);
		this.listViewer.setCheckedElements((this.initialSelection.toArray()));
		this.listViewer.expandAll();
		// initialize page
		//		if(!getInitialElementSelections().isEmpty()) {
		//			//			checkInitialSelections();
		//		}

		// this checkbox allows sorting the columns by name
		//		final Button cbSort = new Button(composite, SWT.CHECK);
		//		cbSort.setText("Sort Columns By Name"); //$NON-NLS-1$ // TODO should be externalized
		//		cbSort.pack();
		//		cbSort.addSelectionListener(new SelectionAdapter() {
		//
		//			@Override
		//			public void widgetSelected(final SelectionEvent e) {
		//				ColumnsToHideDialog.this.contentProvider.setIsSorted(cbSort.getSelection());
		//				getViewer().refresh();
		//			}
		//		});
		Dialog.applyDialogFont(composite);
		return composite;

	}

	/**
	 * Returns the viewer used to show the list.
	 * 
	 * @return the viewer, or <code>null</code> if not yet created
	 */
	protected CheckboxTreeViewer getViewer() {
		return this.listViewer;
	}

	//
	//	/** Add the selection and deselection buttons to the dialog. */
	//	private void addSelectionButtons(final Composite composite) {
	//		Composite buttonComposite = new Composite(composite, SWT.NONE);
	//		GridLayout layout = new GridLayout();
	//		layout.numColumns = 0;
	//		layout.marginWidth = 0;
	//		layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
	//		buttonComposite.setLayout(layout);
	//		buttonComposite.setLayoutData(new GridData(SWT.END, SWT.TOP, true, false));
	//
	//		Button selectButton = createButton(buttonComposite, IDialogConstants.SELECT_ALL_ID, "&Select All", false); //$NON-NLS-1$ // TODO should be externalized
	//
	//		SelectionListener listener = new SelectionAdapter() {
	//
	//			@Override
	//			public void widgetSelected(final SelectionEvent e) {
	//				selectAll();
	//			}
	//		};
	//		selectButton.addSelectionListener(listener);
	//
	//		Button deselectButton = createButton(buttonComposite, IDialogConstants.DESELECT_ALL_ID, "&Deselect All", false); //$NON-NLS-1$ // TODO should be externalized
	//
	//		listener = new SelectionAdapter() {
	//
	//			@Override
	//			public void widgetSelected(final SelectionEvent e) {
	//				deselectAll();
	//			}
	//		};
	//		deselectButton.addSelectionListener(listener);
	//	}

	/**
	 * 
	 * @see org.eclipse.jface.window.Window#open()
	 * 
	 * @return
	 */
	@Override
	public int open() {
		//		if(this.askToPutOnTheTopTheLocalCustomization) {
		//			this.putOnTheTopQuestionDialog = openPutOnTheTopQuestionDialog();
		//		}
		//		this.initialSelection = getVisibleColumns(this.setAtTheTop);
		//		setInitialElementSelections(this.initialSelection);
		return super.open();
	}

	//	/**
	//	 * 
	//	 * @return <ul>
	//	 *         <li>IDialogConstants.NO_ID</li>
	//	 *         <li>IDialogConstants.YES_ID</li>
	//	 *         <li><code>-1<code> in other cases</li>
	//	 *         </ul>
	//	 */
	//	private PutOnTheTopQuestionDialog openPutOnTheTopQuestionDialog() {
	//		String message = TableWidgetPreferencePage.DISPLAYED_MESSAGE_1 + " " + TableWidgetPreferencePage.DISPLAYED_MESSAGE_2 + "\n\n" + TableWidgetPreferencePage.DISPLAYED_QUESTION; //$NON-NLS-1$//$NON-NLS-2$
	//		String toggleMessage = TableWidgetPreferencePage.DONT_DISPLAY_NEXT_TIME;
	//		final PutOnTheTopQuestionDialog dialog = new PutOnTheTopQuestionDialog(getShell(), Messages.NatTableWidget_selectColumnToShowHide, ImageProvider.getInstance().getSelectColumnsToHide(), message, MessageDialog.WARNING, new String[]{ IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 0, toggleMessage, false) {
	//
	//			@Override
	//			public boolean close() {
	//				ColumnsToHideDialog.this.setAtTheTop = isPutOnTheTop();
	//				ColumnsToHideDialog.this.mustAskTheUserNextTime = this.isAskTheUserNextTime();
	//				return super.close();
	//			}
	//		};
	//		Display.getDefault().asyncExec(new Runnable() {
	//
	//			public void run() {
	//				dialog.open();
	//			}
	//		});
	//		return dialog;
	//	}

	//	public boolean isPutOnTheTop() {
	//		return this.setAtTheTop;
	//	}

	//	/**
	//	 * 
	//	 * @see org.eclipse.emf.facet.widgets.nattable.internal.IColumnsToHideDialog#getInitialSelection()
	//	 * 
	//	 * @return the initial selection
	//	 */
	//	public List<Column> getInitialSelection() {
	//		return this.initialSelection;
	//	}
	//
	//	public IPutOnTheTopQuestionDialog isPutOnTheTopQuestionDialog() {
	//		return this.putOnTheTopQuestionDialog;
	//	}

	public void pressOk() {
		okPressed();
	}

	//	public abstract List<Column> getVisibleColumns(boolean putOnTheTop);

	public List<Column> getSelectedColumns() {
		List<Column> resultList = new ArrayList<Column>();
		for(Object result : getResult()) {
			if(result instanceof Column) {
				Column column = (Column)result;
				resultList.add(column);
			}
		}
		return resultList;
	}

	//	public boolean isMustAskTheUserNextTime() {
	//		return this.mustAskTheUserNextTime;
	//	}

	public void selectAll() {
		getViewer().setAllChecked(true);
	}

	public void deselectAll() {
		getViewer().setAllChecked(false);
	}

	/**
	 * The <code>ListSelectionDialog</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() {
		setResult(Arrays.asList(this.listViewer.getCheckedElements()));
		//		// Get the input children.
		//		Object[] children = this.contentProvider.getElements(this.inputElement);
		//
		//		// Build a list of selected children.
		//		if(children != null) {
		//			ArrayList<Object> list = new ArrayList<Object>();
		//			for(int i = 0; i < children.length; ++i) {
		//				Object element = children[i];
		//				if(this.listViewer.getChecked(element)) {
		//					list.add(element);
		//				}
		//			}
		//			setResult(list);
		//		}

		super.okPressed();
	}

	//	public IPutOnTheTopQuestionDialog getPutOnTheTopQuestionDialog() {
	//		return this.putOnTheTopQuestionDialog;
	//	}
}

Back to the top