087fa83c40241b425b7a74348d0e6ec32dccabeb
[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 super(parentShell,
52 dialogTitle,
53 message, status,
54 IStatus.OK| IStatus.INFO | IStatus.WARNING | IStatus.ERROR);
55 this.stackTrace = stackTrace;
56
57
58 }
59
60 /**
61 * @param parentShell
62 * @param dialogTitle
63 * @param message
64 * @param status
65 * @param stackTrace
66 */
67 public CdmErrorDialog(Shell parentShell,
68 String dialogTitle,
69 String message,
70 IStatus status,
71 String stackTrace,
72 Object[] updatedObjects) {
73 this(parentShell, dialogTitle, message, status, stackTrace);
74 this.message = message == null ? status.getMessage()
75 : message + "\n " + status.getMessage(); //$NON-NLS-1$
76 }
77
78 /**
79 * @param parentShell
80 * @param dialogTitle
81 * @param message
82 * @param status
83 */
84 public CdmErrorDialog(Shell parentShell,
85 String dialogTitle,
86 String message,
87 IStatus status) {
88 this(parentShell, dialogTitle, message, status, "");
89 }
90
91 /* (non-Javadoc)
92 * @see org.eclipse.jface.dialogs.ErrorDialog#buttonPressed(int)
93 */
94 @Override
95 protected void buttonPressed(int id) {
96 super.buttonPressed(id);
97 if (id == IDialogConstants.DETAILS_ID) {
98 Point oldSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
99 // set height to max allowed
100 if(getShell().getSize().y > DIALOG_MAX_HEIGHT) {
101 getShell().setSize(getShell().getSize().x, 500);
102 } else {
103 getShell().setSize(getShell().getSize().x, oldSize.y);
104 }
105 }
106
107 }
108
109 /* (non-Javadoc)
110 * @see org.eclipse.jface.dialogs.ErrorDialog#createDropDownList(org.eclipse.swt.widgets.Composite)
111 */
112 @Override
113 protected List createDropDownList(Composite parent) {
114 List list = super.createDropDownList(parent);
115 list.getMenu().getItem(0).setText(JFaceResources.getString("copy all"));
116 return list;
117 }
118
119 /* (non-Javadoc)
120 * @see org.eclipse.jface.dialogs.ErrorDialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
121 */
122 @Override
123 protected void createButtonsForButtonBar(Composite parent) {
124 copyButton = createButton(parent, 2000,"Copy Error", false);
125 copyButton.addSelectionListener(new SelectionListener() {
126 /*
127 * @see SelectionListener.widgetSelected (SelectionEvent)
128 */
129 @Override
130 public void widgetSelected(SelectionEvent e) {
131 copyStackTraceToClipboard();
132 }
133 /*
134 * @see SelectionListener.widgetDefaultSelected(SelectionEvent)
135 */
136 @Override
137 public void widgetDefaultSelected(SelectionEvent e) {
138 copyStackTraceToClipboard();
139 }
140 });
141 super.createButtonsForButtonBar(parent);
142 }
143
144
145 /**
146 * Copies the stack trace to the clipboard
147 *
148 */
149 private void copyStackTraceToClipboard() {
150 if(stackTrace != null && !stackTrace.isEmpty()) {
151 if (clipboard != null) {
152 clipboard.dispose();
153 }
154 clipboard = new Clipboard(copyButton.getDisplay());
155 clipboard.setContents(new Object[] { stackTrace },
156 new Transfer[] { TextTransfer.getInstance() });
157 }
158 }
159
160
161 }