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 static String SELECT_ALL_TITLE = ""; //$NON-NLS-1$
43 static String DESELECT_ALL_TITLE = ""; //$NON-NLS-1$
45 // the root element to populate the viewer with
46 protected Object inputElement;
48 // providers for populating this dialog
49 private ILabelProvider labelProvider;
51 private IStructuredContentProvider contentProvider;
53 private ICheckStateProvider checkStateProvider;
55 // the visual selection widget group
56 protected CheckboxTableViewer listViewer;
58 private IDialogSettings dialogBoundsSettings;
61 private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250;
63 private final static int SIZING_SELECTION_WIDGET_WIDTH = 300;
66 * Creates a list selection dialog.
68 * @param parentShell the parent shell
69 * @param input the root element to populate this dialog with
70 * @param contentProvider the content provider for navigating the model
71 * @param labelProvider the label provider for displaying model elements
72 * @param message the message to be displayed at the top of this dialog, or
73 * <code>null</code> to display a default message
75 public AssignSymbolGroupsDialog(Shell parentShell, Object input,
76 IStructuredContentProvider contentProvider,
77 ILabelProvider labelProvider,
78 ICheckStateProvider checkStateProvider,
81 setTitle(""); //$NON-NLS-1$
83 this.contentProvider = contentProvider;
84 this.labelProvider = labelProvider;
85 this.checkStateProvider = checkStateProvider;
86 if (message != null) {
89 setMessage(""); //$NON-NLS-1$
92 IDialogSettings settings = Activator.getDefault().getDialogSettings();
93 dialogBoundsSettings = settings.getSection(DIALOG);
94 if (dialogBoundsSettings == null)
95 dialogBoundsSettings = settings.addNewSection(DIALOG);
99 protected IDialogSettings getDialogBoundsSettings() {
100 return dialogBoundsSettings;
104 * Add the selection and deselection buttons to the dialog.
105 * @param composite org.eclipse.swt.widgets.Composite
107 private void addSelectionButtons(Composite composite) {
108 Composite buttonComposite = new Composite(composite, SWT.NONE);
109 GridLayout layout = new GridLayout();
110 layout.marginWidth = 0;
111 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
112 layout.verticalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
113 buttonComposite.setLayout(layout);
114 buttonComposite.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, true));
116 Button selectButton = createButton(buttonComposite,
117 IDialogConstants.SELECT_ALL_ID, SELECT_ALL_TITLE, false);
119 SelectionListener listener = new SelectionAdapter() {
120 public void widgetSelected(SelectionEvent e) {
121 listViewer.setAllChecked(true);
122 checkStateChanged(contentProvider.getElements(inputElement), true);
125 selectButton.addSelectionListener(listener);
127 Button deselectButton = createButton(buttonComposite,
128 IDialogConstants.DESELECT_ALL_ID, DESELECT_ALL_TITLE, false);
130 listener = new SelectionAdapter() {
131 public void widgetSelected(SelectionEvent e) {
132 listViewer.setAllChecked(false);
133 checkStateChanged(contentProvider.getElements(inputElement), false);
136 deselectButton.addSelectionListener(listener);
138 @SuppressWarnings("unused")
139 Label label = new Label(buttonComposite, SWT.NONE);
141 Button newButton = createButton(buttonComposite,
142 IDialogConstants.INTERNAL_ID-1, org.simantics.modeling.ui.actions.WorkbenchMessages.AssignSymbolGroupsDialog_NewDots, false);
144 listener = new SelectionAdapter() {
145 public void widgetSelected(SelectionEvent e) {
149 newButton.addSelectionListener(listener);
151 Button removeButton = createButton(buttonComposite,
152 IDialogConstants.INTERNAL_ID-2, org.simantics.modeling.ui.actions.WorkbenchMessages.AssignSymbolGroupsDialog_RemoveAnd, false);
154 listener = new SelectionAdapter() {
155 public void widgetSelected(SelectionEvent e) {
157 ((IStructuredSelection)listViewer.getSelection())
161 removeButton.addSelectionListener(listener);
163 layout.numColumns = 1;
168 protected abstract void newAction();
170 protected abstract void deleteAction(Object[] array);
172 protected abstract void checkStateChanged(Object[] elements, boolean checked);
175 * Visually checks the previously-specified elements in this dialog's list
178 private void checkInitialSelections() {
179 Iterator<?> itemsToCheck = getInitialElementSelections().iterator();
181 while (itemsToCheck.hasNext()) {
182 listViewer.setChecked(itemsToCheck.next(), true);
188 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
190 protected void configureShell(Shell shell) {
191 super.configureShell(shell);
192 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
193 IWorkbenchHelpContextIds.LIST_SELECTION_DIALOG);
197 * Method declared on Dialog.
199 protected Control createDialogArea(Composite parent) {
201 Composite composite = (Composite) super.createDialogArea(parent);
203 initializeDialogUnits(composite);
205 Composite cc = new Composite(composite, SWT.NONE);
207 GridLayout layout = new GridLayout();
208 layout.marginHeight = 0;
209 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
210 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
211 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
212 layout.numColumns = 2;
213 cc.setLayout(layout);
214 cc.setLayoutData(new GridData(GridData.FILL_BOTH));
217 Label label = createMessageArea(cc);
218 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(label);
220 listViewer = CheckboxTableViewer.newCheckList(cc, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
221 GridData data = new GridData(GridData.FILL_BOTH);
222 data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
223 data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
224 listViewer.getTable().setLayoutData(data);
226 listViewer.setLabelProvider(labelProvider);
227 listViewer.setContentProvider(contentProvider);
228 listViewer.setCheckStateProvider(checkStateProvider);
229 listViewer.getTable().addKeyListener(new KeyAdapter() {
231 public void keyPressed(KeyEvent e) {
232 if(e.keyCode == SWT.DEL) {
234 ((IStructuredSelection)listViewer.getSelection())
239 listViewer.addCheckStateListener(new ICheckStateListener() {
241 public void checkStateChanged(CheckStateChangedEvent event) {
242 AssignSymbolGroupsDialog.this.checkStateChanged(new Object[] { event.getElement() }, event.getChecked());
246 addSelectionButtons(cc);
251 if (!getInitialElementSelections().isEmpty()) {
252 checkInitialSelections();
255 Dialog.applyDialogFont(composite);
261 * Returns the viewer used to show the list.
263 * @return the viewer, or <code>null</code> if not yet created
265 protected CheckboxTableViewer getViewer() {
270 * Initializes this dialog's viewer after it has been laid out.
272 private void initializeViewer() {
273 listViewer.setInput(inputElement);
277 * The <code>ListSelectionDialog</code> implementation of this
278 * <code>Dialog</code> method builds a list of the selected elements for later
279 * retrieval by the client and closes this dialog.
281 protected void okPressed() {
283 // Get the input children.
284 Object[] children = contentProvider.getElements(inputElement);
286 // Build a list of selected children.
287 if (children != null) {
288 ArrayList<Object> list = new ArrayList<Object>();
289 for (int i = 0; i < children.length; ++i) {
290 Object element = children[i];
291 if (listViewer.getChecked(element)) {