1 package org.simantics.modeling.ui.actions;
3 import java.util.ArrayList;
4 import java.util.Iterator;
6 import org.eclipse.jface.dialogs.Dialog;
7 import org.eclipse.jface.dialogs.IDialogConstants;
8 import org.eclipse.jface.dialogs.IDialogSettings;
9 import org.eclipse.jface.layout.GridDataFactory;
10 import org.eclipse.jface.viewers.CheckStateChangedEvent;
11 import org.eclipse.jface.viewers.CheckboxTableViewer;
12 import org.eclipse.jface.viewers.ICheckStateListener;
13 import org.eclipse.jface.viewers.ICheckStateProvider;
14 import org.eclipse.jface.viewers.ILabelProvider;
15 import org.eclipse.jface.viewers.IStructuredContentProvider;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.KeyAdapter;
18 import org.eclipse.swt.events.KeyEvent;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.SelectionListener;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.dialogs.SelectionDialog;
31 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
32 import org.eclipse.ui.internal.WorkbenchMessages;
33 import org.simantics.ui.internal.Activator;
35 @SuppressWarnings("restriction")
36 public abstract class ConfigureConnectionTypesDialog extends SelectionDialog {
38 private static final String DIALOG = "ConfigureConnectionTypesDialog"; //$NON-NLS-1$
40 static String SELECT_ALL_TITLE = WorkbenchMessages.SelectionDialog_selectLabel;
42 static String DESELECT_ALL_TITLE = WorkbenchMessages.SelectionDialog_deselectLabel;
44 // the root element to populate the viewer with
45 protected Object inputElement;
47 // providers for populating this dialog
48 private ILabelProvider labelProvider;
50 private IStructuredContentProvider contentProvider;
52 private ICheckStateProvider checkStateProvider;
54 // the visual selection widget group
55 protected CheckboxTableViewer listViewer;
57 private IDialogSettings dialogBoundsSettings;
60 private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250;
62 private final static int SIZING_SELECTION_WIDGET_WIDTH = 300;
65 * Creates a list selection dialog.
67 * @param parentShell the parent shell
68 * @param input the root element to populate this dialog with
69 * @param contentProvider the content provider for navigating the model
70 * @param labelProvider the label provider for displaying model elements
71 * @param message the message to be displayed at the top of this dialog, or
72 * <code>null</code> to display a default message
74 public ConfigureConnectionTypesDialog(Shell parentShell, Object input,
75 IStructuredContentProvider contentProvider,
76 ILabelProvider labelProvider,
77 ICheckStateProvider checkStateProvider,
80 setTitle(WorkbenchMessages.ListSelection_title);
82 this.contentProvider = contentProvider;
83 this.labelProvider = labelProvider;
84 this.checkStateProvider = checkStateProvider;
85 if (message != null) {
88 setMessage(WorkbenchMessages.ListSelection_message);
91 IDialogSettings settings = Activator.getDefault().getDialogSettings();
92 dialogBoundsSettings = settings.getSection(DIALOG);
93 if (dialogBoundsSettings == null)
94 dialogBoundsSettings = settings.addNewSection(DIALOG);
98 protected IDialogSettings getDialogBoundsSettings() {
99 return dialogBoundsSettings;
103 * Add the selection and deselection buttons to the dialog.
104 * @param composite org.eclipse.swt.widgets.Composite
106 private void addSelectionButtons(Composite composite) {
107 Composite buttonComposite = new Composite(composite, SWT.NONE);
108 GridLayout layout = new GridLayout();
109 layout.marginWidth = 0;
110 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
111 layout.verticalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
112 buttonComposite.setLayout(layout);
113 buttonComposite.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, true));
115 Button selectButton = createButton(buttonComposite,
116 IDialogConstants.SELECT_ALL_ID, SELECT_ALL_TITLE, false);
118 SelectionListener listener = new SelectionAdapter() {
119 public void widgetSelected(SelectionEvent e) {
120 listViewer.setAllChecked(true);
121 checkStateChanged(contentProvider.getElements(inputElement), true);
124 selectButton.addSelectionListener(listener);
126 Button deselectButton = createButton(buttonComposite,
127 IDialogConstants.DESELECT_ALL_ID, DESELECT_ALL_TITLE, false);
129 listener = new SelectionAdapter() {
130 public void widgetSelected(SelectionEvent e) {
131 listViewer.setAllChecked(false);
132 checkStateChanged(contentProvider.getElements(inputElement), false);
135 deselectButton.addSelectionListener(listener);
137 layout.numColumns = 1;
140 protected abstract void checkStateChanged(Object[] elements, boolean checked);
143 * Visually checks the previously-specified elements in this dialog's list
146 private void checkInitialSelections() {
147 Iterator<?> itemsToCheck = getInitialElementSelections().iterator();
149 while (itemsToCheck.hasNext()) {
150 listViewer.setChecked(itemsToCheck.next(), true);
156 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
158 protected void configureShell(Shell shell) {
159 super.configureShell(shell);
160 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
161 IWorkbenchHelpContextIds.LIST_SELECTION_DIALOG);
165 * Method declared on Dialog.
167 protected Control createDialogArea(Composite parent) {
169 Composite composite = (Composite) super.createDialogArea(parent);
171 initializeDialogUnits(composite);
173 Composite cc = new Composite(composite, SWT.NONE);
175 GridLayout layout = new GridLayout();
176 layout.marginHeight = 0;
177 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
178 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
179 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
180 layout.numColumns = 2;
181 cc.setLayout(layout);
182 cc.setLayoutData(new GridData(GridData.FILL_BOTH));
185 Label label = createMessageArea(cc);
186 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(label);
188 listViewer = CheckboxTableViewer.newCheckList(cc, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
189 GridData data = new GridData(GridData.FILL_BOTH);
190 data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
191 data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
192 listViewer.getTable().setLayoutData(data);
194 listViewer.setLabelProvider(labelProvider);
195 listViewer.setContentProvider(contentProvider);
196 listViewer.setCheckStateProvider(checkStateProvider);
197 listViewer.getTable().addKeyListener(new KeyAdapter() {
199 public void keyPressed(KeyEvent e) {
202 listViewer.addCheckStateListener(new ICheckStateListener() {
204 public void checkStateChanged(CheckStateChangedEvent event) {
205 ConfigureConnectionTypesDialog.this.checkStateChanged(new Object[] { event.getElement() }, event.getChecked());
209 addSelectionButtons(cc);
214 if (!getInitialElementSelections().isEmpty()) {
215 checkInitialSelections();
218 Dialog.applyDialogFont(composite);
224 * Returns the viewer used to show the list.
226 * @return the viewer, or <code>null</code> if not yet created
228 protected CheckboxTableViewer getViewer() {
233 * Initializes this dialog's viewer after it has been laid out.
235 private void initializeViewer() {
236 listViewer.setInput(inputElement);
240 * The <code>ListSelectionDialog</code> implementation of this
241 * <code>Dialog</code> method builds a list of the selected elements for later
242 * retrieval by the client and closes this dialog.
244 protected void okPressed() {
246 // Get the input children.
247 Object[] children = contentProvider.getElements(inputElement);
249 // Build a list of selected children.
250 if (children != null) {
251 ArrayList<Object> list = new ArrayList<Object>();
252 for (int i = 0; i < children.length; ++i) {
253 Object element = children[i];
254 if (listViewer.getChecked(element)) {