1034
|
1 |
/**
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.carbide.remoteconnections.internal.ui;
|
|
19 |
|
|
20 |
import java.text.MessageFormat;
|
|
21 |
|
|
22 |
import org.eclipse.core.runtime.IStatus;
|
|
23 |
import org.eclipse.jface.dialogs.Dialog;
|
|
24 |
import org.eclipse.jface.layout.GridLayoutFactory;
|
|
25 |
import org.eclipse.jface.resource.JFaceResources;
|
|
26 |
import org.eclipse.mylyn.internal.provisional.commons.ui.AbstractNotificationPopup;
|
|
27 |
import org.eclipse.swt.SWT;
|
|
28 |
import org.eclipse.swt.events.DisposeEvent;
|
|
29 |
import org.eclipse.swt.events.DisposeListener;
|
|
30 |
import org.eclipse.swt.events.SelectionAdapter;
|
|
31 |
import org.eclipse.swt.events.SelectionEvent;
|
|
32 |
import org.eclipse.swt.graphics.Image;
|
|
33 |
import org.eclipse.swt.layout.GridData;
|
|
34 |
import org.eclipse.swt.widgets.Composite;
|
|
35 |
import org.eclipse.swt.widgets.Display;
|
|
36 |
import org.eclipse.swt.widgets.Label;
|
|
37 |
import org.eclipse.swt.widgets.Link;
|
1047
|
38 |
import org.eclipse.swt.widgets.MessageBox;
|
|
39 |
import org.eclipse.swt.widgets.Shell;
|
1034
|
40 |
|
|
41 |
import com.nokia.carbide.remoteconnections.internal.api.IStatusDisplay;
|
|
42 |
import com.nokia.cpp.internal.api.utils.core.Check;
|
|
43 |
|
|
44 |
@SuppressWarnings("restriction")
|
|
45 |
public class StatusDisplay implements IStatusDisplay {
|
|
46 |
|
|
47 |
private final class NotificationPopup extends AbstractNotificationPopup {
|
|
48 |
private final IStatus status;
|
|
49 |
private final String prompt;
|
|
50 |
|
|
51 |
private NotificationPopup(Display display, IStatus status, String prompt) {
|
|
52 |
super(display);
|
|
53 |
this.status = status;
|
|
54 |
this.prompt = prompt;
|
|
55 |
setDelayClose(30 * 1000);
|
|
56 |
}
|
|
57 |
|
|
58 |
protected void createContentArea(Composite composite) {
|
|
59 |
GridLayoutFactory.fillDefaults().margins(5, 5).applyTo(composite);
|
|
60 |
Label label = new Label(composite, SWT.WRAP);
|
|
61 |
label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
|
|
62 |
label.setText(status.getMessage());
|
|
63 |
label.setBackground(composite.getBackground());
|
|
64 |
if (prompt != null) {
|
|
65 |
Link link = new Link(composite, SWT.WRAP);
|
|
66 |
link.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
|
|
67 |
link.setText(MessageFormat.format("<a href=\"{0}\">{0}</a>", prompt));
|
|
68 |
link.setBackground(composite.getBackground());
|
|
69 |
link.addSelectionListener(new SelectionAdapter() {
|
|
70 |
public void widgetSelected(SelectionEvent e) {
|
|
71 |
clicked = true;
|
|
72 |
NotificationPopup.this.close();
|
|
73 |
}
|
|
74 |
});
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
@Override
|
|
79 |
protected String getPopupShellTitle() {
|
1047
|
80 |
return getTitleString(status);
|
1034
|
81 |
}
|
1047
|
82 |
|
1034
|
83 |
@Override
|
|
84 |
protected Image getPopupShellImage(int maximumHeight) {
|
|
85 |
switch (status.getSeverity()) {
|
|
86 |
case IStatus.INFO:
|
|
87 |
return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO);
|
|
88 |
case IStatus.WARNING:
|
|
89 |
return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING);
|
|
90 |
case IStatus.ERROR:
|
|
91 |
return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
|
|
92 |
};
|
|
93 |
Check.checkState(false);
|
|
94 |
return null;
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
1047
|
98 |
private static final int MODAL_MASK = SWT.APPLICATION_MODAL | SWT.PRIMARY_MODAL | SWT.SYSTEM_MODAL;
|
|
99 |
|
1034
|
100 |
private boolean clicked;
|
|
101 |
private boolean closed;
|
|
102 |
|
|
103 |
public StatusDisplay() {
|
|
104 |
}
|
|
105 |
|
|
106 |
public void displayStatus(final IStatus status) {
|
|
107 |
final Display display = Display.getDefault();
|
|
108 |
display.syncExec(new Runnable() {
|
|
109 |
public void run() {
|
|
110 |
doDisplayStatus(display, null, status);
|
|
111 |
}
|
|
112 |
});
|
|
113 |
}
|
|
114 |
|
|
115 |
public void displayStatusWithAction(final IStatus status, final String prompt, Runnable action) {
|
|
116 |
final Display display = Display.getDefault();
|
|
117 |
display.asyncExec(new Runnable() {
|
|
118 |
public void run() {
|
|
119 |
doDisplayStatus(display, prompt, status);
|
|
120 |
}
|
|
121 |
});
|
|
122 |
while (!closed) {
|
|
123 |
try {
|
|
124 |
Thread.sleep(200);
|
|
125 |
} catch (InterruptedException e1) {
|
|
126 |
}
|
|
127 |
}
|
|
128 |
if (clicked)
|
|
129 |
action.run();
|
|
130 |
}
|
|
131 |
|
1047
|
132 |
private Shell getModalShell(Display display) {
|
|
133 |
Shell[] shells = display.getShells();
|
|
134 |
for (int i = shells.length - 1; i >= 0; i--) {
|
|
135 |
Shell shell = shells[i];
|
|
136 |
if ((shell.getStyle() & MODAL_MASK) != 0) {
|
|
137 |
return shell;
|
1034
|
138 |
}
|
1047
|
139 |
}
|
|
140 |
return null;
|
1034
|
141 |
}
|
|
142 |
|
1047
|
143 |
protected void doDisplayStatus(Display display, String prompt, IStatus status) {
|
|
144 |
Shell modalShell = getModalShell(display);
|
|
145 |
if (modalShell == null) {
|
|
146 |
NotificationPopup popup = new NotificationPopup(display, status, prompt);
|
|
147 |
popup.open();
|
|
148 |
popup.getShell().addDisposeListener(new DisposeListener() {
|
|
149 |
public void widgetDisposed(DisposeEvent e) {
|
|
150 |
StatusDisplay.this.closed = true;
|
|
151 |
}
|
|
152 |
});
|
|
153 |
}
|
|
154 |
else {
|
|
155 |
int style = SWT.YES | SWT.NO;
|
|
156 |
switch (status.getSeverity()) {
|
|
157 |
case IStatus.INFO:
|
|
158 |
style |= SWT.ICON_INFORMATION;
|
|
159 |
break;
|
|
160 |
case IStatus.WARNING:
|
|
161 |
style |= SWT.ICON_WARNING;
|
|
162 |
break;
|
|
163 |
case IStatus.ERROR:
|
|
164 |
style |= SWT.ICON_ERROR;
|
|
165 |
break;
|
|
166 |
default:
|
|
167 |
Check.checkState(false);
|
|
168 |
};
|
|
169 |
|
|
170 |
MessageBox messageBox = new MessageBox(modalShell, style);
|
|
171 |
messageBox.setText(getTitleString(status));
|
|
172 |
StringBuilder sb = new StringBuilder();
|
|
173 |
sb.append(status.getMessage());
|
|
174 |
sb.append("\n");
|
|
175 |
sb.append(prompt);
|
|
176 |
messageBox.setMessage(sb.toString());
|
|
177 |
int open = messageBox.open();
|
|
178 |
closed = true;
|
|
179 |
clicked = open == SWT.YES;
|
|
180 |
}
|
|
181 |
}
|
|
182 |
|
|
183 |
private String getTitleString(IStatus status) {
|
|
184 |
switch (status.getSeverity()) {
|
|
185 |
case IStatus.INFO:
|
|
186 |
return "Information";
|
|
187 |
case IStatus.WARNING:
|
|
188 |
return "Warning";
|
|
189 |
case IStatus.ERROR:
|
|
190 |
return "Error";
|
|
191 |
};
|
|
192 |
Check.checkState(false);
|
|
193 |
return null;
|
|
194 |
}
|
|
195 |
|
1034
|
196 |
}
|