connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/ui/StatusDisplay.java
branchRCL_2_4
changeset 1034 42eaaa076776
child 1047 cc3916f16460
equal deleted inserted replaced
1032:083e0a53ef29 1034:42eaaa076776
       
     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;
       
    38 
       
    39 import com.nokia.carbide.remoteconnections.internal.api.IStatusDisplay;
       
    40 import com.nokia.cpp.internal.api.utils.core.Check;
       
    41 
       
    42 @SuppressWarnings("restriction")
       
    43 public class StatusDisplay implements IStatusDisplay {
       
    44 		
       
    45 	private final class NotificationPopup extends AbstractNotificationPopup {
       
    46 		private final IStatus status;
       
    47 		private final String prompt;
       
    48 
       
    49 		private NotificationPopup(Display display, IStatus status, String prompt) {
       
    50 			super(display);
       
    51 			this.status = status;
       
    52 			this.prompt = prompt;
       
    53 			setDelayClose(30 * 1000);
       
    54 		}
       
    55 		
       
    56 		protected void createContentArea(Composite composite) {
       
    57 			GridLayoutFactory.fillDefaults().margins(5, 5).applyTo(composite);
       
    58 			Label label = new Label(composite, SWT.WRAP);
       
    59 			label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
    60 			label.setText(status.getMessage());
       
    61 			label.setBackground(composite.getBackground());
       
    62 			if (prompt != null) {
       
    63 				Link link = new Link(composite, SWT.WRAP);
       
    64 				link.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
    65 				link.setText(MessageFormat.format("<a href=\"{0}\">{0}</a>", prompt));
       
    66 				link.setBackground(composite.getBackground());
       
    67 				link.addSelectionListener(new SelectionAdapter() {
       
    68 					public void widgetSelected(SelectionEvent e) {
       
    69 						clicked = true;
       
    70 						NotificationPopup.this.close();
       
    71 					}
       
    72 				});
       
    73 			}
       
    74 		}
       
    75 		
       
    76 		@Override
       
    77 		protected String getPopupShellTitle() {
       
    78 			switch (status.getSeverity()) {
       
    79 			case IStatus.INFO:
       
    80 				return "Information";
       
    81 			case IStatus.WARNING:
       
    82 				return "Warning";
       
    83 			case IStatus.ERROR:
       
    84 				return "Error";
       
    85 			};
       
    86 			Check.checkState(false);
       
    87 			return null;
       
    88 		}
       
    89 	 
       
    90 		@Override
       
    91 		protected Image getPopupShellImage(int maximumHeight) {
       
    92 			switch (status.getSeverity()) {
       
    93 			case IStatus.INFO:
       
    94 				return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO);
       
    95 			case IStatus.WARNING:
       
    96 				return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING);
       
    97 			case IStatus.ERROR:
       
    98 				return JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_ERROR);
       
    99 			};
       
   100 			Check.checkState(false);
       
   101 			return null;
       
   102 		}
       
   103 	}
       
   104 
       
   105 	private boolean clicked;
       
   106 	private boolean closed;
       
   107 
       
   108 	public StatusDisplay() {
       
   109 	}
       
   110 
       
   111 	public void displayStatus(final IStatus status) {
       
   112 		final Display display = Display.getDefault();
       
   113 		display.syncExec(new Runnable() {
       
   114 			public void run() {
       
   115 				doDisplayStatus(display, null, status);
       
   116 			}
       
   117 		});
       
   118 	}
       
   119 
       
   120 	public void displayStatusWithAction(final IStatus status, final String prompt, Runnable action) {
       
   121 		final Display display = Display.getDefault();
       
   122 		display.asyncExec(new Runnable() {
       
   123 			public void run() {
       
   124 				doDisplayStatus(display, prompt, status);
       
   125 			}
       
   126 		});
       
   127 		while (!closed) {
       
   128 			try {
       
   129 				Thread.sleep(200);
       
   130 			} catch (InterruptedException e1) {
       
   131 			}
       
   132 		}
       
   133 		if (clicked)
       
   134 			action.run();
       
   135 	}
       
   136 
       
   137 	protected void doDisplayStatus(Display display, String prompt, IStatus status) {
       
   138 		NotificationPopup popup = new NotificationPopup(display, status, prompt);
       
   139 		popup.open();
       
   140 		popup.getShell().addDisposeListener(new DisposeListener() {
       
   141 			public void widgetDisposed(DisposeEvent e) {
       
   142 				StatusDisplay.this.closed = true;
       
   143 			}
       
   144 		});
       
   145 	}
       
   146 
       
   147 }