1 package org.simantics.modeling.adapters;
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;
38 * @author Tuukka Lehtonen
40 public class RemoveInstancesDialog extends MessageDialog {
42 public static class Content {
44 public ImageDescriptor image;
45 public String details;
47 public Content(String label) {
53 public String toString() {
56 return label + "\nDetails: " + details;
60 protected String message;
61 protected boolean hasDetails;
63 protected Content[] content;
65 protected LocalResourceManager resourceManager;
66 protected Composite tableAndButtons;
67 protected Text messageText;
68 protected Text detailsText;
69 private GridData detailsGridData;
71 public RemoveInstancesDialog(Shell parentShell, String dialogTitle, String dialogMessage, Content[] content) {
75 MessageDialog.QUESTION,
77 IDialogConstants.PROCEED_LABEL,
78 IDialogConstants.CANCEL_LABEL
84 public RemoveInstancesDialog(Shell parentShell, String dialogTitle, String dialogMessage, int dialogType, String[] buttons, int defaultIndex, Content[] content) {
93 this.message = dialogMessage;
94 this.content = content;
95 this.hasDetails = contentHasDetails(content);
98 private boolean contentHasDetails(Content[] content) {
99 for (Content c : content) {
100 if (c.details != null && !c.details.trim().isEmpty())
107 protected int getShellStyle() {
108 return super.getShellStyle() | SWT.RESIZE;
112 protected Control createDialogArea(Composite parent) {
113 resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);
114 Composite composite = (Composite) super.createDialogArea(parent);
119 protected Control createMessageArea(Composite composite) {
120 super.createMessageArea(composite);
122 if (message != null) {
123 messageText = new Text(composite, getMessageLabelStyle());
124 messageText.setText(message);
125 messageText.setEditable(false);
128 .align(SWT.FILL, SWT.BEGINNING)
131 convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
132 SWT.DEFAULT).applyTo(messageText);
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);
143 Label uses = new Label(tableAndButtons, 0);
144 uses.setText("Instances:");
145 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(uses);
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));
157 Composite buttons = new Composite(tableAndButtons, 0);
158 GridDataFactory.fillDefaults().grab(false, true).applyTo(buttons);
159 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(buttons);
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() {
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() });
174 // Button navigateButton = new Button(buttons, SWT.PUSH);
175 // navigateButton.setText("Go To");
176 // navigateButton.setToolTipText("Go To Selected Instances");
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);
185 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
187 public void selectionChanged(SelectionChangedEvent event) {
188 IStructuredSelection sel = (IStructuredSelection) event.getSelection();
189 Content content = (Content)(sel.getFirstElement());
190 showDetails(content);
193 viewer.setInput(content);
195 detailsText.getDisplay().asyncExec(new Runnable() {
201 return tableAndButtons;
204 protected void showDetails(Content content) {
205 if (detailsText == null || detailsText.isDisposed())
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);
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);
225 protected class CP implements IStructuredContentProvider {
228 public void dispose() {
232 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
236 public Object[] getElements(Object inputElement) {
242 protected class LP extends ColumnLabelProvider {
244 public Image getImage(Object element) {
245 Content content = (Content) element;
246 return content.image != null ? (Image) resourceManager.get(content.image) : null;
249 public String getText(Object element) {
250 Content content = (Content) element;
251 return content.label;