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