core/com.nokia.carbide.discovery.ui/src/com/nokia/carbide/internal/discovery/ui/extension/AbstractBrowserPortalPage.java
branchC3_BUILDER_WORK
changeset 1724 b99f029f4d47
parent 1720 6433764204b0
parent 1723 b386036cfe5d
child 1730 e0e324a4ef67
equal deleted inserted replaced
1720:6433764204b0 1724:b99f029f4d47
     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.discovery.ui.Activator;
       
    39 
       
    40 public abstract class AbstractBrowserPortalPage implements IPortalPage {
       
    41 
       
    42 	private final class NavigationActionBar implements IActionBar {
       
    43 		private List<IAction> actions;
       
    44 		private IActionUIUpdater updater;
       
    45 		private boolean loading;
       
    46 
       
    47 		private NavigationActionBar(IActionUIUpdater updater) {
       
    48 			this.updater = updater;
       
    49 			makeActions();
       
    50 		}
       
    51 
       
    52 		public void hookBrowser() {
       
    53 			browser.addLocationListener(new LocationAdapter() {
       
    54 				@Override
       
    55 				public void changed(LocationEvent event) {
       
    56 					NavigationActionBar.this.updater.updateAll();
       
    57 				}
       
    58 			});
       
    59 			browser.addProgressListener(new ProgressAdapter() {
       
    60 				@Override
       
    61 				public void changed(ProgressEvent event) {
       
    62 	                if (event.total == 0)
       
    63 	                    return;
       
    64 	                setLoading(event.current != event.total);
       
    65 					NavigationActionBar.this.updater.updateAll();
       
    66 				}
       
    67 				@Override
       
    68 				public void completed(ProgressEvent event) {
       
    69 					setLoading(false);
       
    70 					NavigationActionBar.this.updater.updateAll();
       
    71 				}
       
    72 			});
       
    73 		}
       
    74 
       
    75 		@Override
       
    76 		public String getTitle() {
       
    77 			return "Navigation";
       
    78 		}
       
    79 
       
    80 		@Override
       
    81 		public IAction[] getActions() {
       
    82 			return (IAction[]) actions.toArray(new IAction[actions.size()]);
       
    83 		}
       
    84 
       
    85 		private void makeActions() {
       
    86 			actions = new ArrayList<IAction>();
       
    87 			IAction a = new Action("Back") {
       
    88 				@Override
       
    89 				public void run() {
       
    90 					if (browser != null) {
       
    91 						browser.back();
       
    92 					}
       
    93 				}
       
    94 				
       
    95 				@Override
       
    96 				public boolean isEnabled() {
       
    97 					return browser != null ? browser.isBackEnabled() : false;
       
    98 				}
       
    99 			};
       
   100 			actions.add(a);
       
   101 			
       
   102 			a = new Action("Forward") {
       
   103 				@Override
       
   104 				public void run() {
       
   105 					if (browser != null) {
       
   106 						browser.forward();
       
   107 					}
       
   108 				}
       
   109 				
       
   110 				@Override
       
   111 				public boolean isEnabled() {
       
   112 					return browser != null ? browser.isForwardEnabled() : false;
       
   113 				}
       
   114 			};
       
   115 			actions.add(a);
       
   116 			
       
   117 			a = new Action("Refresh Page") {
       
   118 				@Override
       
   119 				public void run() {
       
   120 					if (browser != null) {
       
   121 						browser.refresh();
       
   122 						update();
       
   123 					}
       
   124 				}
       
   125 
       
   126 				@Override
       
   127 				public boolean isEnabled() {
       
   128 					return browser != null ? !loading : false;
       
   129 				}
       
   130 			};
       
   131 			actions.add(a);
       
   132 
       
   133 			a = new Action("Stop Loading") {
       
   134 				@Override
       
   135 				public void run() {
       
   136 					if (browser != null) {
       
   137 						browser.stop();
       
   138 						update();
       
   139 					}
       
   140 				}
       
   141 
       
   142 				@Override
       
   143 				public boolean isEnabled() {
       
   144 					return browser != null ? loading : false;
       
   145 				}
       
   146 			};
       
   147 			actions.add(a);
       
   148 		}
       
   149 
       
   150 		public void update() {
       
   151 			updater.updateAll();
       
   152 		}
       
   153 
       
   154 		private void setLoading(boolean loading) {
       
   155 			browser.setCursor(loading ? browser.getDisplay().getSystemCursor(SWT.CURSOR_WAIT) : null);
       
   156 			this.loading = loading;
       
   157 		}
       
   158 	}
       
   159 
       
   160 	private Browser browser;
       
   161 	private NavigationActionBar actionBar;
       
   162 
       
   163 	@Override
       
   164 	public Control createControl(Composite parent, IEditorPart part) {
       
   165 		Composite composite = new Composite(parent, SWT.NONE);
       
   166 		composite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
       
   167 		composite.setLayout(new FillLayout());
       
   168 		try {
       
   169 			browser = new Browser(composite, SWT.MOZILLA);
       
   170 		} catch (SWTError e) {
       
   171 			e.printStackTrace();
       
   172 		}
       
   173 		
       
   174 		return composite;
       
   175 	}
       
   176 
       
   177 	protected String getURL() {
       
   178 		try {
       
   179 			URL url = new URL(Activator.getFromServerProperties(getClass().getName()));
       
   180 			return url.toString();
       
   181 		} catch (MalformedURLException e) {
       
   182 			Activator.logError("Could not load page due to bad URL", e);
       
   183 		}
       
   184 		
       
   185 		return null;
       
   186 	}
       
   187 
       
   188 	@Override
       
   189 	public void init() {
       
   190 		if (browser != null) {
       
   191 			browser.setUrl(getURL());
       
   192 			actionBar.hookBrowser();
       
   193 		}
       
   194 		actionBar.update();
       
   195 	}
       
   196 	
       
   197 	@Override
       
   198 	public IActionBar[] createCommandBars(IEditorPart part, IActionUIUpdater updater) {
       
   199 		actionBar = new NavigationActionBar(updater);
       
   200 		return new IActionBar[] {
       
   201 			actionBar
       
   202 		};
       
   203 	}
       
   204 
       
   205 	@Override
       
   206 	public void dispose() {
       
   207 	}
       
   208 	
       
   209 }