]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/AssignSymbolGroupsDialog.java
Sync git svn branch with SVN repository r33319.
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / AssignSymbolGroupsDialog.java
1 package org.simantics.modeling.ui.actions;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5
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;
35
36 @SuppressWarnings("restriction")
37 public abstract class AssignSymbolGroupsDialog extends SelectionDialog {
38
39     private static final String DIALOG = "AssignSymbolGroupsDialog"; //$NON-NLS-1$
40
41     static String SELECT_ALL_TITLE = WorkbenchMessages.SelectionDialog_selectLabel;
42
43     static String DESELECT_ALL_TITLE = WorkbenchMessages.SelectionDialog_deselectLabel;
44
45     // the root element to populate the viewer with
46     protected Object inputElement;
47
48     // providers for populating this dialog
49     private ILabelProvider labelProvider;
50
51     private IStructuredContentProvider contentProvider;
52
53     private ICheckStateProvider checkStateProvider;
54
55     // the visual selection widget group
56     protected CheckboxTableViewer listViewer;
57
58     private IDialogSettings     dialogBoundsSettings;
59
60     // sizing constants
61     private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250;
62
63     private final static int SIZING_SELECTION_WIDGET_WIDTH = 300;
64
65     /**
66      * Creates a list selection dialog.
67      *
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
74      */
75     public AssignSymbolGroupsDialog(Shell parentShell, Object input,
76             IStructuredContentProvider contentProvider,
77             ILabelProvider labelProvider,
78             ICheckStateProvider checkStateProvider,
79             String message) {
80         super(parentShell);
81         setTitle(WorkbenchMessages.ListSelection_title);
82         inputElement = input;
83         this.contentProvider = contentProvider;
84         this.labelProvider = labelProvider;
85         this.checkStateProvider = checkStateProvider;
86         if (message != null) {
87             setMessage(message);
88         } else {
89             setMessage(WorkbenchMessages.ListSelection_message);
90         }
91
92         IDialogSettings settings = Activator.getDefault().getDialogSettings();
93         dialogBoundsSettings = settings.getSection(DIALOG);
94         if (dialogBoundsSettings == null)
95             dialogBoundsSettings = settings.addNewSection(DIALOG);
96     }
97
98     @Override
99     protected IDialogSettings getDialogBoundsSettings() {
100         return dialogBoundsSettings;
101     }
102
103     /**
104      * Add the selection and deselection buttons to the dialog.
105      * @param composite org.eclipse.swt.widgets.Composite
106      */
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));
115
116         Button selectButton = createButton(buttonComposite,
117                 IDialogConstants.SELECT_ALL_ID, SELECT_ALL_TITLE, false);
118
119         SelectionListener listener = new SelectionAdapter() {
120             public void widgetSelected(SelectionEvent e) {
121                 listViewer.setAllChecked(true);
122                 checkStateChanged(contentProvider.getElements(inputElement), true);
123             }
124         };
125         selectButton.addSelectionListener(listener);
126
127         Button deselectButton = createButton(buttonComposite,
128                 IDialogConstants.DESELECT_ALL_ID, DESELECT_ALL_TITLE, false);
129
130         listener = new SelectionAdapter() {
131             public void widgetSelected(SelectionEvent e) {
132                 listViewer.setAllChecked(false);
133                 checkStateChanged(contentProvider.getElements(inputElement), false);
134             }
135         };
136         deselectButton.addSelectionListener(listener);
137
138         @SuppressWarnings("unused")
139         Label label = new Label(buttonComposite, SWT.NONE);
140
141         Button newButton = createButton(buttonComposite,
142                 IDialogConstants.INTERNAL_ID-1, "&New...", false);
143
144         listener = new SelectionAdapter() {
145             public void widgetSelected(SelectionEvent e) {
146                 newAction();
147             }
148         };
149         newButton.addSelectionListener(listener);
150
151         Button removeButton = createButton(buttonComposite,
152                 IDialogConstants.INTERNAL_ID-2, "&Remove", false);
153
154         listener = new SelectionAdapter() {
155             public void widgetSelected(SelectionEvent e) {
156                 deleteAction(
157                         ((IStructuredSelection)listViewer.getSelection())
158                         .toArray());
159             }
160         };
161         removeButton.addSelectionListener(listener);
162
163         layout.numColumns = 1;
164     }
165     
166
167
168     protected abstract void newAction();
169
170     protected abstract void deleteAction(Object[] array);
171
172     protected abstract void checkStateChanged(Object[] elements, boolean checked);
173
174     /**
175      * Visually checks the previously-specified elements in this dialog's list 
176      * viewer.
177      */
178     private void checkInitialSelections() {
179         Iterator<?> itemsToCheck = getInitialElementSelections().iterator();
180
181         while (itemsToCheck.hasNext()) {
182                         listViewer.setChecked(itemsToCheck.next(), true);
183                 }
184     }
185
186     /*
187      *  (non-Javadoc)
188      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
189      */
190     protected void configureShell(Shell shell) {
191         super.configureShell(shell);
192         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
193                                 IWorkbenchHelpContextIds.LIST_SELECTION_DIALOG);
194     }
195
196     /* (non-Javadoc)
197      * Method declared on Dialog.
198      */
199     protected Control createDialogArea(Composite parent) {
200         // page group
201         Composite composite = (Composite) super.createDialogArea(parent);
202         
203         initializeDialogUnits(composite);
204         
205         Composite cc = new Composite(composite, SWT.NONE);
206         {
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));
215         }
216         
217         Label label = createMessageArea(cc);
218         GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(label);
219         
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);
225
226         listViewer.setLabelProvider(labelProvider);
227         listViewer.setContentProvider(contentProvider);
228         listViewer.setCheckStateProvider(checkStateProvider);
229         listViewer.getTable().addKeyListener(new KeyAdapter() {
230             @Override
231             public void keyPressed(KeyEvent e) {
232                 if(e.keyCode == SWT.DEL) {
233                     deleteAction(
234                             ((IStructuredSelection)listViewer.getSelection())
235                             .toArray());
236                 }
237             }
238         });
239         listViewer.addCheckStateListener(new ICheckStateListener() {
240             @Override
241             public void checkStateChanged(CheckStateChangedEvent event) {
242                 AssignSymbolGroupsDialog.this.checkStateChanged(new Object[] { event.getElement() }, event.getChecked());
243             }
244         });
245
246         addSelectionButtons(cc);
247
248         initializeViewer();
249
250         // initialize page
251         if (!getInitialElementSelections().isEmpty()) {
252                         checkInitialSelections();
253                 }
254
255         Dialog.applyDialogFont(composite);
256         
257         return composite;
258     }
259
260     /**
261      * Returns the viewer used to show the list.
262      * 
263      * @return the viewer, or <code>null</code> if not yet created
264      */
265     protected CheckboxTableViewer getViewer() {
266         return listViewer;
267     }
268
269     /**
270      * Initializes this dialog's viewer after it has been laid out.
271      */
272     private void initializeViewer() {
273         listViewer.setInput(inputElement);
274     }
275
276     /**
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.
280      */
281     protected void okPressed() {
282
283         // Get the input children.
284         Object[] children = contentProvider.getElements(inputElement);
285
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)) {
292                                         list.add(element);
293                                 }
294             }
295             setResult(list);
296         }
297
298         super.okPressed();
299     }
300
301 }