core/com.nokia.carbide.discovery.ui/src/com/nokia/carbide/internal/discovery/ui/extension/AbstractBrowserPortalPage.java
changeset 1721 18638adda547
parent 1718 c3599e02bf7f
child 1722 950f9b0d90b0
equal deleted inserted replaced
1718:c3599e02bf7f 1721:18638adda547
     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 package com.nokia.carbide.internal.discovery.ui.extension;
       
    18 
       
    19 import java.net.MalformedURLException;
       
    20 import java.net.URL;
       
    21 import java.util.ArrayList;
       
    22 import java.util.List;
       
    23 
       
    24 import org.eclipse.jface.action.Action;
       
    25 import org.eclipse.jface.action.IAction;
       
    26 import org.eclipse.swt.SWT;
       
    27 import org.eclipse.swt.SWTError;
       
    28 import org.eclipse.swt.browser.Browser;
       
    29 import org.eclipse.swt.browser.LocationAdapter;
       
    30 import org.eclipse.swt.browser.LocationEvent;
       
    31 import org.eclipse.swt.browser.ProgressAdapter;
       
    32 import org.eclipse.swt.browser.ProgressEvent;
       
    33 import org.eclipse.swt.layout.FillLayout;
       
    34 import org.eclipse.swt.widgets.Composite;
       
    35 import org.eclipse.swt.widgets.Control;
       
    36 import org.eclipse.ui.IEditorPart;
       
    37 
       
    38 import com.nokia.carbide.internal.discovery.ui.extension.IPortalPageLayer;
       
    39 import com.nokia.carbide.discovery.ui.Activator;
       
    40 import com.nokia.carbide.discovery.ui.Messages;
       
    41 
       
    42 public abstract class AbstractBrowserPortalPage implements IPortalPageLayer {
       
    43 
       
    44 	private final class NavigationActionBar implements IActionBar {
       
    45 		private List<IAction> actions;
       
    46 		private IActionUIUpdater updater;
       
    47 		private boolean loading;
       
    48 
       
    49 		private NavigationActionBar(IActionUIUpdater updater) {
       
    50 			this.updater = updater;
       
    51 			makeActions();
       
    52 		}
       
    53 
       
    54 		public void hookBrowser() {
       
    55 			browser.addLocationListener(new LocationAdapter() {
       
    56 				@Override
       
    57 				public void changed(LocationEvent event) {
       
    58 					NavigationActionBar.this.updater.updateAll();
       
    59 				}
       
    60 			});
       
    61 			browser.addProgressListener(new ProgressAdapter() {
       
    62 				@Override
       
    63 				public void changed(ProgressEvent event) {
       
    64 	                if (event.total == 0)
       
    65 	                    return;
       
    66 	                setLoading(event.current != event.total);
       
    67 					NavigationActionBar.this.updater.updateAll();
       
    68 				}
       
    69 				@Override
       
    70 				public void completed(ProgressEvent event) {
       
    71 					setLoading(false);
       
    72 					NavigationActionBar.this.updater.updateAll();
       
    73 				}
       
    74 			});
       
    75 		}
       
    76 
       
    77 		@Override
       
    78 		public String getTitle() {
       
    79 			return Messages.AbstractBrowserPortalPage_NavigationTitle;
       
    80 		}
       
    81 
       
    82 		@Override
       
    83 		public IAction[] getActions() {
       
    84 			return (IAction[]) actions.toArray(new IAction[actions.size()]);
       
    85 		}
       
    86 
       
    87 		private void makeActions() {
       
    88 			actions = new ArrayList<IAction>();
       
    89 			IAction a = new Action(Messages.AbstractBrowserPortalPage_BackLabel) {
       
    90 				@Override
       
    91 				public void run() {
       
    92 					if (browser != null) {
       
    93 						browser.back();
       
    94 					}
       
    95 				}
       
    96 				
       
    97 				@Override
       
    98 				public boolean isEnabled() {
       
    99 					return browser != null ? browser.isBackEnabled() : false;
       
   100 				}
       
   101 			};
       
   102 			actions.add(a);
       
   103 			
       
   104 			a = new Action(Messages.AbstractBrowserPortalPage_ForwardLabel) {
       
   105 				@Override
       
   106 				public void run() {
       
   107 					if (browser != null) {
       
   108 						browser.forward();
       
   109 					}
       
   110 				}
       
   111 				
       
   112 				@Override
       
   113 				public boolean isEnabled() {
       
   114 					return browser != null ? browser.isForwardEnabled() : false;
       
   115 				}
       
   116 			};
       
   117 			actions.add(a);
       
   118 			
       
   119 			a = new Action(Messages.AbstractBrowserPortalPage_RefreshLabel) {
       
   120 				@Override
       
   121 				public void run() {
       
   122 					if (browser != null) {
       
   123 						browser.refresh();
       
   124 						update();
       
   125 					}
       
   126 				}
       
   127 
       
   128 				@Override
       
   129 				public boolean isEnabled() {
       
   130 					return browser != null ? !loading : false;
       
   131 				}
       
   132 			};
       
   133 			actions.add(a);
       
   134 
       
   135 			a = new Action(Messages.AbstractBrowserPortalPage_StopLabel) {
       
   136 				@Override
       
   137 				public void run() {
       
   138 					if (browser != null) {
       
   139 						browser.stop();
       
   140 						update();
       
   141 					}
       
   142 				}
       
   143 
       
   144 				@Override
       
   145 				public boolean isEnabled() {
       
   146 					return browser != null ? loading : false;
       
   147 				}
       
   148 			};
       
   149 			actions.add(a);
       
   150 		}
       
   151 
       
   152 		public void update() {
       
   153 			updater.updateAll();
       
   154 		}
       
   155 
       
   156 		private void setLoading(boolean loading) {
       
   157 			browser.setCursor(loading ? browser.getDisplay().getSystemCursor(SWT.CURSOR_WAIT) : null);
       
   158 			this.loading = loading;
       
   159 		}
       
   160 
       
   161 		@Override
       
   162 		public String[] getHighlightedActionIds() {
       
   163 			return null;
       
   164 		}
       
   165 	}
       
   166 
       
   167 	private Browser browser;
       
   168 	private NavigationActionBar actionBar;
       
   169 
       
   170 	@Override
       
   171 	public Control createControl(Composite parent, IEditorPart part) {
       
   172 		Composite composite = new Composite(parent, SWT.NONE);
       
   173 		composite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
       
   174 		composite.setLayout(new FillLayout());
       
   175 		try {
       
   176 			browser = new Browser(composite, SWT.MOZILLA);
       
   177 		} catch (SWTError e) {
       
   178 			e.printStackTrace();
       
   179 		}
       
   180 		
       
   181 		return composite;
       
   182 	}
       
   183 
       
   184 	protected String getURL() {
       
   185 		try {
       
   186 			URL url = new URL(Activator.getFromServerProperties(getClass().getName()));
       
   187 			return url.toString();
       
   188 		} catch (MalformedURLException e) {
       
   189 			Activator.logError(Messages.AbstractBrowserPortalPage_BadURLError, e);
       
   190 		}
       
   191 		
       
   192 		return null;
       
   193 	}
       
   194 
       
   195 	@Override
       
   196 	public void init() {
       
   197 		if (browser != null) {
       
   198 			browser.setUrl(getURL());
       
   199 			actionBar.hookBrowser();
       
   200 		}
       
   201 		actionBar.update();
       
   202 	}
       
   203 	
       
   204 	@Override
       
   205 	public IActionBar[] createCommandBars(IEditorPart part, IActionUIUpdater updater) {
       
   206 		actionBar = new NavigationActionBar(updater);
       
   207 		return new IActionBar[] {
       
   208 			actionBar
       
   209 		};
       
   210 	}
       
   211 
       
   212 	@Override
       
   213 	public void dispose() {
       
   214 	}
       
   215 	
       
   216 }