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.jface.viewers.IStructuredSelection;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.KeyAdapter;
19 import org.eclipse.swt.events.KeyEvent;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.events.SelectionListener;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.dialogs.SelectionDialog;
32 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
33 import org.eclipse.ui.internal.WorkbenchMessages;
34 import org.simantics.ui.internal.Activator;
36 @SuppressWarnings("restriction")
37 public abstract class AssignSymbolGroupsDialog extends SelectionDialog {
39 private static final String DIALOG = "AssignSymbolGroupsDialog"; //$NON-NLS-1$
41 // the root element to populate the viewer with
42 protected Object inputElement;
44 // providers for populating this dialog
45 private ILabelProvider labelProvider;
47 private IStructuredContentProvider contentProvider;
49 private ICheckStateProvider checkStateProvider;
51 // the visual selection widget group
52 protected CheckboxTableViewer listViewer;
54 private IDialogSettings dialogBoundsSettings;
57 private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250;
59 private final static int SIZING_SELECTION_WIDGET_WIDTH = 300;
62 * Creates a list selection dialog.
64 * @param parentShell the parent shell
65 * @param input the root element to populate this dialog with
66 * @param contentProvider the content provider for navigating the model
67 * @param labelProvider the label provider for displaying model elements
68 * @param message the message to be displayed at the top of this dialog, or
69 * <code>null</code> to display a default message
71 public AssignSymbolGroupsDialog(Shell parentShell, Object input,
72 IStructuredContentProvider contentProvider,
73 ILabelProvider labelProvider,
74 ICheckStateProvider checkStateProvider,
77 setTitle(""); //$NON-NLS-1$
79 this.contentProvider = contentProvider;
80 this.labelProvider = labelProvider;
81 this.checkStateProvider = checkStateProvider;
82 if (message != null) {
85 setMessage(WorkbenchMessages.ListSelection_message);
88 IDialogSettings settings = Activator.getDefault().getDialogSettings();
89 dialogBoundsSettings = settings.getSection(DIALOG);
90 if (dialogBoundsSettings == null)
91 dialogBoundsSettings = settings.addNewSection(DIALOG);
95 protected IDialogSettings getDialogBoundsSettings() {
96 return dialogBoundsSettings;
100 * Add the selection and deselection buttons to the dialog.
101 * @param composite org.eclipse.swt.widgets.Composite
103 private void addSelectionButtons(Composite composite) {
104 Composite buttonComposite = new Composite(composite, SWT.NONE);
105 GridLayout layout = new GridLayout();
106 layout.marginWidth = 0;
107 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
108 layout.verticalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
109 buttonComposite.setLayout(layout);
110 buttonComposite.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, true));
112 Button selectButton = createButton(buttonComposite,
113 IDialogConstants.SELECT_ALL_ID, WorkbenchMessages.SelectionDialog_selectLabel, false);
115 SelectionListener listener = new SelectionAdapter() {
116 public void widgetSelected(SelectionEvent e) {
117 listViewer.setAllChecked(true);
118 checkStateChanged(contentProvider.getElements(inputElement), true);
121 selectButton.addSelectionListener(listener);
123 Button deselectButton = createButton(buttonComposite,
124 IDialogConstants.DESELECT_ALL_ID, WorkbenchMessages.SelectionDialog_deselectLabel, false);
126 listener = new SelectionAdapter() {
127 public void widgetSelected(SelectionEvent e) {
128 listViewer.setAllChecked(false);
129 checkStateChanged(contentProvider.getElements(inputElement), false);
132 deselectButton.addSelectionListener(listener);
134 @SuppressWarnings("unused")
135 Label label = new Label(buttonComposite, SWT.NONE);
137 Button newButton = createButton(buttonComposite,
138 IDialogConstants.INTERNAL_ID-1, org.simantics.modeling.ui.actions.Messages.AssignSymbolGroupsDialog_New, false);
140 listener = new SelectionAdapter() {
141 public void widgetSelected(SelectionEvent e) {
145 newButton.addSelectionListener(listener);
147 Button removeButton = createButton(buttonComposite,
148 IDialogConstants.INTERNAL_ID-2, org.simantics.modeling.ui.actions.Messages.AssignSymbolGroupsDialog_Remove, false);
150 listener = new SelectionAdapter() {
151 public void widgetSelected(SelectionEvent e) {
153 ((IStructuredSelection)listViewer.getSelection())
157 removeButton.addSelectionListener(listener);
159 layout.numColumns = 1;
164 protected abstract void newAction();
166 protected abstract void deleteAction(Object[] array);
168 protected abstract void checkStateChanged(Object[] elements, boolean checked);
171 * Visually checks the previously-specified elements in this dialog's list
174 private void checkInitialSelections() {
175 Iterator<?> itemsToCheck = getInitialElementSelections().iterator();
177 while (itemsToCheck.hasNext()) {
178 listViewer.setChecked(itemsToCheck.next(), true);
184 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
186 protected void configureShell(Shell shell) {
187 super.configureShell(shell);
188 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
189 IWorkbenchHelpContextIds.LIST_SELECTION_DIALOG);
193 * Method declared on Dialog.
195 protected Control createDialogArea(Composite parent) {
197 Composite composite = (Composite) super.createDialogArea(parent);
199 initializeDialogUnits(composite);
201 Composite cc = new Composite(composite, SWT.NONE);
203 GridLayout layout = new GridLayout();
204 layout.marginHeight = 0;
205 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
206 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
207 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
208 layout.numColumns = 2;
209 cc.setLayout(layout);
210 cc.setLayoutData(new GridData(GridData.FILL_BOTH));
213 Label label = createMessageArea(cc);
214 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(label);
216 listViewer = CheckboxTableViewer.newCheckList(cc, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
217 GridData data = new GridData(GridData.FILL_BOTH);
218 data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
219 data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
220 listViewer.getTable().setLayoutData(data);
222 listViewer.setLabelProvider(labelProvider);
223 listViewer.setContentProvider(contentProvider);
224 listViewer.setCheckStateProvider(checkStateProvider);
225 listViewer.getTable().addKeyListener(new KeyAdapter() {
227 public void keyPressed(KeyEvent e) {
228 if(e.keyCode == SWT.DEL) {
230 ((IStructuredSelection)listViewer.getSelection())
235 listViewer.addCheckStateListener(new ICheckStateListener() {
237 public void checkStateChanged(CheckStateChangedEvent event) {
238 AssignSymbolGroupsDialog.this.checkStateChanged(new Object[] { event.getElement() }, event.getChecked());
242 addSelectionButtons(cc);
247 if (!getInitialElementSelections().isEmpty()) {
248 checkInitialSelections();
251 Dialog.applyDialogFont(composite);
257 * Returns the viewer used to show the list.
259 * @return the viewer, or <code>null</code> if not yet created
261 protected CheckboxTableViewer getViewer() {
266 * Initializes this dialog's viewer after it has been laid out.
268 private void initializeViewer() {
269 listViewer.setInput(inputElement);
273 * The <code>ListSelectionDialog</code> implementation of this
274 * <code>Dialog</code> method builds a list of the selected elements for later
275 * retrieval by the client and closes this dialog.
277 protected void okPressed() {
279 // Get the input children.
280 Object[] children = contentProvider.getElements(inputElement);
282 // Build a list of selected children.
283 if (children != null) {
284 ArrayList<Object> list = new ArrayList<Object>();
285 for (int i = 0; i < children.length; ++i) {
286 Object element = children[i];
287 if (listViewer.getChecked(element)) {