Project

General

Profile

Download (4.02 KB) Statistics
| Branch: | Tag: | Revision:
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
     * @param parentShell
60
     * @param dialogTitle
61
     * @param message
62
     * @param status
63
     */
64
    public CdmErrorDialog(Shell parentShell,
65
            String dialogTitle,
66
            String message,
67
            IStatus status) {
68
        this(parentShell, dialogTitle, message, status, "");
69
    }
70

    
71
	/* (non-Javadoc)
72
	 * @see org.eclipse.jface.dialogs.ErrorDialog#buttonPressed(int)
73
	 */
74
	@Override
75
	protected void buttonPressed(int id) {
76
		super.buttonPressed(id);
77
		if (id == IDialogConstants.DETAILS_ID) {
78
			Point oldSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
79
			// set height to max allowed
80
			if(getShell().getSize().y > DIALOG_MAX_HEIGHT) {
81
				getShell().setSize(getShell().getSize().x, 500);
82
			} else {
83
				getShell().setSize(getShell().getSize().x, oldSize.y);
84
			}
85
		}
86

    
87
	}
88

    
89
	/* (non-Javadoc)
90
	 * @see org.eclipse.jface.dialogs.ErrorDialog#createDropDownList(org.eclipse.swt.widgets.Composite)
91
	 */
92
	@Override
93
	protected List createDropDownList(Composite parent) {
94
	    List list = super.createDropDownList(parent);
95
	    list.getMenu().getItem(0).setText(JFaceResources.getString("copy all"));
96
	    return list;
97
	}
98

    
99
	/* (non-Javadoc)
100
	 * @see org.eclipse.jface.dialogs.ErrorDialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
101
	 */
102
	@Override
103
	protected void createButtonsForButtonBar(Composite parent) {
104
	    copyButton = createButton(parent, 2000,"Copy Error", false);
105
	    copyButton.addSelectionListener(new SelectionListener() {
106
	        /*
107
	         * @see SelectionListener.widgetSelected (SelectionEvent)
108
	         */
109
	        @Override
110
            public void widgetSelected(SelectionEvent e) {
111
	            copyStackTraceToClipboard();
112
	        }
113
	        /*
114
	         * @see SelectionListener.widgetDefaultSelected(SelectionEvent)
115
	         */
116
	        @Override
117
            public void widgetDefaultSelected(SelectionEvent e) {
118
	            copyStackTraceToClipboard();
119
	        }
120
	    });
121
	    super.createButtonsForButtonBar(parent);
122
	}
123

    
124

    
125
	/**
126
	 * Copies the stack trace to the clipboard
127
	 *
128
	 */
129
	private void copyStackTraceToClipboard() {
130
	    if(stackTrace != null && !stackTrace.isEmpty()) {
131
	        if (clipboard != null) {
132
	            clipboard.dispose();
133
	        }
134
	        clipboard = new Clipboard(copyButton.getDisplay());
135
	        clipboard.setContents(new Object[] { stackTrace },
136
	                new Transfer[] { TextTransfer.getInstance() });
137
	    }
138
	}
139
}
(4-4/35)