Merge branch 'develop' of ssh://dev.e-taxonomy.eu/var/git/taxeditor into
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / CdmErrorDialog.java
1 package eu.etaxonomy.taxeditor.model;
2
3 import org.eclipse.core.runtime.IStatus;
4 import org.eclipse.jface.dialogs.ErrorDialog;
5 import org.eclipse.jface.dialogs.IDialogConstants;
6 import org.eclipse.jface.resource.JFaceResources;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.dnd.Clipboard;
9 import org.eclipse.swt.dnd.TextTransfer;
10 import org.eclipse.swt.dnd.Transfer;
11 import org.eclipse.swt.events.SelectionEvent;
12 import org.eclipse.swt.events.SelectionListener;
13 import org.eclipse.swt.graphics.Point;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.List;
17 import org.eclipse.swt.widgets.Shell;
18
19 /**
20 * Error dialog class specifically implemented for the editor.
21 * The main difference with {@link org.eclipse.jface.dialogs.ErrorDialog} is that
22 * this dialog has a fixed max height.
23 *
24 * @author cmathew
25 *
26 */
27 public class CdmErrorDialog extends ErrorDialog {
28
29 private static final int DIALOG_MAX_HEIGHT = 500;
30
31 private Button copyButton;
32 private final String stackTrace;
33
34 /**
35 * The current clipboard. To be disposed when closing the dialog.
36 */
37 private Clipboard clipboard;
38
39 /**
40 * @param parentShell
41 * @param dialogTitle
42 * @param message
43 * @param status
44 * @param stackTrace
45 */
46 public CdmErrorDialog(Shell parentShell,
47 String dialogTitle,
48 String message,
49 IStatus status,
50 String stackTrace,
51 boolean showStatusMessage) {
52 super(parentShell,
53 dialogTitle,
54 message, status,
55 IStatus.OK| IStatus.INFO | IStatus.WARNING | IStatus.ERROR);
56 this.stackTrace = stackTrace;
57 String statusMessage = "";
58 if(showStatusMessage) {
59 statusMessage = status.getMessage();
60 }
61 this.message = message == null ? statusMessage : message + "\n " + statusMessage;
62 }
63
64
65 /**
66 * @param parentShell
67 * @param dialogTitle
68 * @param message
69 * @param status
70 */
71 public CdmErrorDialog(Shell parentShell,
72 String dialogTitle,
73 String message,
74 IStatus status) {
75 this(parentShell, dialogTitle, message, status, "", true);
76 }
77
78 /* (non-Javadoc)
79 * @see org.eclipse.jface.dialogs.ErrorDialog#buttonPressed(int)
80 */
81 @Override
82 protected void buttonPressed(int id) {
83 super.buttonPressed(id);
84 if (id == IDialogConstants.DETAILS_ID) {
85 Point oldSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
86 // set height to max allowed
87 if(getShell().getSize().y > DIALOG_MAX_HEIGHT) {
88 getShell().setSize(getShell().getSize().x, 500);
89 } else {
90 getShell().setSize(getShell().getSize().x, oldSize.y);
91 }
92 }
93
94 }
95
96 /* (non-Javadoc)
97 * @see org.eclipse.jface.dialogs.ErrorDialog#createDropDownList(org.eclipse.swt.widgets.Composite)
98 */
99 @Override
100 protected List createDropDownList(Composite parent) {
101 List list = super.createDropDownList(parent);
102 list.getMenu().getItem(0).setText(JFaceResources.getString("copy all"));
103 return list;
104 }
105
106 /* (non-Javadoc)
107 * @see org.eclipse.jface.dialogs.ErrorDialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
108 */
109 @Override
110 protected void createButtonsForButtonBar(Composite parent) {
111 copyButton = createButton(parent, 2000,"Copy Error", false);
112 copyButton.addSelectionListener(new SelectionListener() {
113 /*
114 * @see SelectionListener.widgetSelected (SelectionEvent)
115 */
116 @Override
117 public void widgetSelected(SelectionEvent e) {
118 copyStackTraceToClipboard();
119 }
120 /*
121 * @see SelectionListener.widgetDefaultSelected(SelectionEvent)
122 */
123 @Override
124 public void widgetDefaultSelected(SelectionEvent e) {
125 copyStackTraceToClipboard();
126 }
127 });
128 super.createButtonsForButtonBar(parent);
129 }
130
131
132 /**
133 * Copies the stack trace to the clipboard
134 *
135 */
136 private void copyStackTraceToClipboard() {
137 if(stackTrace != null && !stackTrace.isEmpty()) {
138 if (clipboard != null) {
139 clipboard.dispose();
140 }
141 clipboard = new Clipboard(copyButton.getDisplay());
142 clipboard.setContents(new Object[] { stackTrace },
143 new Transfer[] { TextTransfer.getInstance() });
144 }
145 }
146
147
148 }