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