]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/RemoveInstancesDialog.java
WIP: Some old-ish adapter registry initialization improvements
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / RemoveInstancesDialog.java
1 package org.simantics.modeling.adapters;
2
3 import org.eclipse.jface.dialogs.IDialogConstants;
4 import org.eclipse.jface.dialogs.MessageDialog;
5 import org.eclipse.jface.layout.GridDataFactory;
6 import org.eclipse.jface.layout.GridLayoutFactory;
7 import org.eclipse.jface.layout.TableColumnLayout;
8 import org.eclipse.jface.resource.ImageDescriptor;
9 import org.eclipse.jface.resource.JFaceResources;
10 import org.eclipse.jface.resource.LocalResourceManager;
11 import org.eclipse.jface.viewers.ColumnLabelProvider;
12 import org.eclipse.jface.viewers.ColumnWeightData;
13 import org.eclipse.jface.viewers.ISelectionChangedListener;
14 import org.eclipse.jface.viewers.IStructuredContentProvider;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.jface.viewers.SelectionChangedEvent;
17 import org.eclipse.jface.viewers.TableViewer;
18 import org.eclipse.jface.viewers.TableViewerColumn;
19 import org.eclipse.jface.viewers.Viewer;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.dnd.Clipboard;
22 import org.eclipse.swt.dnd.TextTransfer;
23 import org.eclipse.swt.dnd.Transfer;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.graphics.Color;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Label;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.swt.widgets.Text;
35 import org.simantics.utils.strings.EString;
36
37 /**
38  * @author Tuukka Lehtonen
39  */
40 public class RemoveInstancesDialog extends MessageDialog {
41
42         public static class Content {
43                 public String label;
44                 public ImageDescriptor image;
45                 public String details;
46
47                 public Content(String label) {
48                         assert label != null;
49                         this.label = label;
50                 }
51
52                 @Override
53                 public String toString() {
54                         if (details == null)
55                                 return label;
56                         return label + "\nDetails: " + details;
57                 }
58         }
59
60         protected String message;
61         protected boolean hasDetails; 
62
63         protected Content[] content;
64
65         protected LocalResourceManager resourceManager;
66         protected Composite tableAndButtons;
67         protected Text messageText;
68         protected Text detailsText;
69         private GridData detailsGridData;
70
71         public RemoveInstancesDialog(Shell parentShell, String dialogTitle, String dialogMessage, Content[] content) {
72                 this(parentShell,
73                                 dialogTitle,
74                                 dialogMessage,
75                                 MessageDialog.QUESTION,
76                                 new String[] {
77                                         IDialogConstants.PROCEED_LABEL,
78                                         IDialogConstants.CANCEL_LABEL
79                                 },
80                                 1,
81                                 content);
82         }
83
84         public RemoveInstancesDialog(Shell parentShell, String dialogTitle, String dialogMessage, int dialogType, String[] buttons, int defaultIndex, Content[] content) {
85                 super(parentShell,
86                                 dialogTitle,
87                                 null,
88                                 null,
89                                 dialogType,
90                                 buttons,
91                                 defaultIndex);
92
93                 this.message = dialogMessage;
94                 this.content = content;
95                 this.hasDetails = contentHasDetails(content);
96         }
97
98         private boolean contentHasDetails(Content[] content) {
99                 for (Content c : content) {
100                         if (c.details != null && !c.details.trim().isEmpty())
101                                 return true;
102                 }
103                 return false;
104         }
105
106         @Override
107         protected int getShellStyle() {
108                 return super.getShellStyle() | SWT.RESIZE;
109         }
110
111         @Override
112         protected Control createDialogArea(Composite parent) {
113                 resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);
114                 Composite composite = (Composite) super.createDialogArea(parent);
115                 return composite;
116         }
117
118         @Override
119         protected Control createMessageArea(Composite composite) {
120                 super.createMessageArea(composite);
121                 // create message
122                 if (message != null) {
123                         messageText = new Text(composite, getMessageLabelStyle());
124                         messageText.setText(message);
125                         messageText.setEditable(false);
126                         GridDataFactory
127                                         .fillDefaults()
128                                         .align(SWT.FILL, SWT.BEGINNING)
129                                         .grab(true, false)
130                                         .hint(
131                                                         convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
132                                                         SWT.DEFAULT).applyTo(messageText);
133                 }
134                 return composite;
135         }
136
137         @Override
138         protected Control createCustomArea(Composite parent) {
139                 tableAndButtons = new Composite(parent, 0);
140                 GridDataFactory.fillDefaults().grab(true, true).applyTo(tableAndButtons);
141                 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(tableAndButtons);
142
143                 Label uses = new Label(tableAndButtons, 0);
144                 uses.setText("Instances:");
145                 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(uses);
146
147                 TableColumnLayout ad = new TableColumnLayout();
148                 Composite tableComposite = new Composite(tableAndButtons, SWT.NONE);
149                 GridDataFactory.fillDefaults().grab(true, true).minSize(300, 300).applyTo(tableComposite);
150                 tableComposite.setLayout(ad);
151                 TableViewer viewer = new TableViewer(tableComposite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
152                 viewer.setContentProvider(new CP());
153                 TableViewerColumn c1 = new TableViewerColumn(viewer, SWT.LEFT);
154                 c1.setLabelProvider(new LP());
155                 ad.setColumnData(c1.getColumn(), new ColumnWeightData(100, 100, true));
156
157                 Composite buttons = new Composite(tableAndButtons, 0);
158                 GridDataFactory.fillDefaults().grab(false, true).applyTo(buttons);
159                 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(buttons);
160
161                 Button copyButton = new Button(buttons, SWT.PUSH);
162                 copyButton.setText("&Copy to Clipboard");
163                 copyButton.setToolTipText("Copy List Contents to Clipboard");
164                 copyButton.addSelectionListener(new SelectionAdapter() {
165                         @Override
166                         public void widgetSelected(SelectionEvent e) {
167                                 String text = EString.implode(content, String.format("%n"));
168                                 Clipboard cp = new Clipboard(e.display);
169                                 cp.setContents(new Object[] { text }, new Transfer[] { TextTransfer.getInstance() });
170                                 cp.dispose();
171                         }
172                 });
173
174 //              Button navigateButton = new Button(buttons, SWT.PUSH);
175 //              navigateButton.setText("Go To");
176 //              navigateButton.setToolTipText("Go To Selected Instances");
177
178                 detailsText = new Text(tableAndButtons, SWT.BORDER | SWT.WRAP | SWT.MULTI | SWT.V_SCROLL);
179                 Color bg = detailsText.getBackground();
180                 detailsText.setEditable(false);
181                 detailsText.setBackground(bg);
182                 detailsGridData = GridDataFactory.fillDefaults().grab(true, false).hint(SWT.DEFAULT, 100).span(2, 1).create();
183                 detailsText.setLayoutData(detailsGridData);
184
185                 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
186                         @Override
187                         public void selectionChanged(SelectionChangedEvent event) {
188                                 IStructuredSelection sel = (IStructuredSelection) event.getSelection();
189                                 Content content = (Content)(sel.getFirstElement());
190                                 showDetails(content);
191                         }
192                 });
193                 viewer.setInput(content);
194
195                 detailsText.getDisplay().asyncExec(new Runnable() {
196                         @Override
197                         public void run() {
198                                 showDetails(null);
199                         }
200                 });
201                 return tableAndButtons;
202         }
203
204         protected void showDetails(Content content) {
205                 if (detailsText == null || detailsText.isDisposed())
206                         return;
207                 if (content != null && content.details != null) {
208                         detailsText.setText(content.details);
209                         if (detailsGridData.exclude) {
210                                 detailsGridData.exclude = false;
211                                 detailsGridData.heightHint = 100;
212                                 tableAndButtons.layout(true, true);
213                         }
214                 } else {
215                         detailsText.setText("");
216                         if (!detailsGridData.exclude) {
217                                 detailsGridData.exclude = true;
218                                 detailsGridData.heightHint = 0;
219                                 detailsText.setSize(0, 0);
220                                 tableAndButtons.layout(true, true);
221                         }
222                 }
223         }
224
225         protected class CP implements IStructuredContentProvider {
226
227                 @Override
228                 public void dispose() {
229                 }
230
231                 @Override
232                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
233                 }
234
235                 @Override
236                 public Object[] getElements(Object inputElement) {
237                         return content;
238                 }
239
240         }
241
242         protected class LP extends ColumnLabelProvider {
243                 @Override
244                 public Image getImage(Object element) {
245                         Content content = (Content) element;
246                         return content.image != null ? (Image) resourceManager.get(content.image) : null;
247                 }
248                 @Override
249                 public String getText(Object element) {
250                         Content content = (Content) element;
251                         return content.label;
252                 }
253         }
254
255 }