Bug 3193 - Previewer: Refresh and Focus interaction issue
authorEugene Ostroukhov <eugeneo@symbian.org>
Fri, 09 Jul 2010 16:09:45 -0700
changeset 437 b9e175c43f07
parent 436 64b202d910ac
child 438 99afbaa8a4bc
Bug 3193 - Previewer: Refresh and Focus interaction issue
org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/AbstractPreviewPage.java
--- a/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/AbstractPreviewPage.java	Fri Jul 09 15:56:18 2010 -0700
+++ b/org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/preview/AbstractPreviewPage.java	Fri Jul 09 16:09:45 2010 -0700
@@ -14,10 +14,8 @@
 import org.eclipse.jface.viewers.ISelectionChangedListener;
 import org.eclipse.jface.viewers.ISelectionProvider;
 import org.eclipse.jface.viewers.StructuredSelection;
-import org.eclipse.swt.SWT;
 import org.eclipse.swt.browser.Browser;
 import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.ui.actions.ActionFactory;
@@ -25,189 +23,208 @@
 import org.eclipse.ui.part.Page;
 import org.symbian.tools.wrttools.previewer.Images;
 import org.symbian.tools.wrttools.previewer.PreviewerPlugin;
+import org.symbian.tools.wrttools.util.CoreUtil;
 
-public abstract class AbstractPreviewPage extends Page implements IPreviewPage, ISelectionProvider {
-    private final IAction refreshAction = new Action("Refresh") {
-        public void run() {
-            refresh(true);
-        };
-    };
-    private final IAction toggleRefresh = new Action("Toggle Refresh", IAction.AS_RADIO_BUTTON) {
-        public void run() {
-            toggleRefresh();
-        };
-    };
+public abstract class AbstractPreviewPage extends Page implements IPreviewPage,
+		ISelectionProvider {
+	private final IAction refreshAction = new Action("Refresh") {
+		public void run() {
+			refresh(true);
+		};
+	};
+	private final IAction toggleRefresh = new Action("Toggle Refresh",
+			IAction.AS_RADIO_BUTTON) {
+		public void run() {
+			toggleRefresh();
+		};
+	};
 
-    protected IProject project;
-    private Browser browser;
-    private boolean toggleState = true;
-    private final PreviewView previewView;
-    private boolean needsRefresh = false;
+	protected IProject project;
+	private Browser browser;
+	private boolean toggleState = true;
+	private final PreviewView previewView;
+	private boolean needsRefresh = false;
 
-    public AbstractPreviewPage(IProject project, PreviewView previewView) {
-        this.project = project;
-        this.previewView = previewView;
-    }
+	public AbstractPreviewPage(IProject project, PreviewView previewView) {
+		this.project = project;
+		this.previewView = previewView;
+	}
 
-    protected void toggleRefresh() {
-        toggleState = !toggleState;
-        toggleRefresh.setChecked(toggleState);
-        previewView.setProjectAutorefresh(project, toggleState);
-        toggleRefresh.setToolTipText(getToggleActionTooltip());
-        if (toggleState && needsRefresh) {
-            refresh(true);
-        }
-    }
+	protected void toggleRefresh() {
+		toggleState = !toggleState;
+		toggleRefresh.setChecked(toggleState);
+		previewView.setProjectAutorefresh(project, toggleState);
+		toggleRefresh.setToolTipText(getToggleActionTooltip());
+		if (toggleState && needsRefresh) {
+			refresh(true);
+		}
+	}
 
-    @Override
-    public void createControl(Composite parent) {
-        browser = createBrowser(parent);
-        browser.setLayoutData(new GridData(GridData.FILL_BOTH));
-        browser.setUrl(getURI().toASCIIString());
-    }
+	@Override
+	public void createControl(Composite parent) {
+		browser = createBrowser(parent);
+		browser.setLayoutData(new GridData(GridData.FILL_BOTH));
+		browser.setUrl(getURI().toASCIIString());
+	}
 
-    protected abstract Browser createBrowser(Composite parent);
+	protected abstract Browser createBrowser(Composite parent);
 
-    private URI getURI() {
-        return PreviewerPlugin.getDefault().getHttpPreviewer().previewProject(project, null);
-    }
-
-    @Override
-    public Control getControl() {
-        return browser;
-    }
+	private URI getURI() {
+		return PreviewerPlugin.getDefault().getHttpPreviewer().previewProject(
+				project, null);
+	}
 
-    @Override
-    public void setFocus() {
-        if (browser != null && !browser.isDisposed()) {
-            browser.setFocus();
-        }
-    }
+	@Override
+	public Control getControl() {
+		return browser;
+	}
 
-    private boolean refreshScheduled = false;
+	@Override
+	public void setFocus() {
+		if (browser != null && !browser.isDisposed()) {
+			browser.setFocus();
+		}
+	}
+
+	private boolean refreshScheduled = false;
 
-    public synchronized void process(Collection<IFile> files) {
-        if (!isDisposed() && !refreshScheduled && needsRefresh(files)) {
-            asyncExec(new Runnable() {
-                public void run() {
-                    refreshBrowser();
-                }
-            });
-            refreshScheduled = true;
-        }
-    }
+	public synchronized void process(Collection<IFile> files) {
+		if (!isDisposed() && !refreshScheduled && needsRefresh(files)) {
+			asyncExec(new Runnable() {
+				public void run() {
+					refreshBrowser();
+				}
+			});
+			refreshScheduled = true;
+		}
+	}
 
-    protected void promptIfNeeded() {
-        if (toggleState) {
-            toggleState = previewView.promptUserToToggle(project, toggleState);
-            toggleRefresh.setChecked(toggleState);
-        }
-    }
+	protected void promptIfNeeded() {
+		if (toggleState) {
+			toggleState = previewView.promptUserToToggle(project, toggleState);
+			toggleRefresh.setChecked(toggleState);
+		}
+	}
 
-    private void asyncExec(Runnable runnable) {
-        getControl().getDisplay().asyncExec(runnable);
-    }
+	private void asyncExec(Runnable runnable) {
+		getControl().getDisplay().asyncExec(runnable);
+	}
 
-    private boolean needsRefresh(Collection<IFile> files) {
-        for (IFile iFile : files) {
-            if (iFile.getProject().equals(project)) {
-                return true;
-            }
-        }
-        return false;
-    }
+	private boolean needsRefresh(Collection<IFile> files) {
+		for (IFile iFile : files) {
+			if (iFile.getProject().equals(project)) {
+				return true;
+			}
+		}
+		return false;
+	}
 
-    protected synchronized void refresh(boolean manual) {
-        try {
-            if (!isDisposed()) {
-                final Control focusControl = browser.getDisplay().getFocusControl();
-                browser.setUrl(getURI().toASCIIString());
-                refreshAction.setImageDescriptor(PreviewerPlugin.getImageDescriptor(Images.GREEN_SYNC));
-                if (!manual && focusControl != null) {
-                    asyncExec(new Runnable() {
-                        public void run() {
-                            focusControl.setFocus();
-                        }
-                    });
-                }
-                refreshAction.setToolTipText("Refresh the preview browser");
-            }
-            needsRefresh = false;
-        } finally {
-            refreshScheduled = false;
-        }
-    }
+	protected synchronized void refresh(final boolean manual) {
+		try {
+			if (!isDisposed()) {
+				final Control focusControl = browser.getDisplay()
+						.getFocusControl();
+				if (manual && CoreUtil.isMac()) {
+					browser.getParent().forceFocus();
+				}
+				browser.setUrl(getURI().toASCIIString());
+				refreshAction.setImageDescriptor(PreviewerPlugin
+						.getImageDescriptor(Images.GREEN_SYNC));
+				asyncExec(new Runnable() {
+					public void run() {
+						if (!manual && focusControl != null) {
+							focusControl.setFocus();
+						} else if (manual) {
+							browser.forceFocus();
+						}
+					}
+				});
+				refreshAction.setToolTipText("Refresh the preview browser");
+			}
+			needsRefresh = false;
+		} finally {
+			refreshScheduled = false;
+		}
+	}
 
-    @Override
-    public void init(IPageSite pageSite) {
-        super.init(pageSite);
-        IToolBarManager toolBar = pageSite.getActionBars().getToolBarManager();
+	@Override
+	public void init(IPageSite pageSite) {
+		super.init(pageSite);
+		IToolBarManager toolBar = pageSite.getActionBars().getToolBarManager();
 
-        contributeToToolbar(toolBar);
+		contributeToToolbar(toolBar);
 
-        refreshAction.setImageDescriptor(PreviewerPlugin.getImageDescriptor(Images.GREEN_SYNC));
-        refreshAction.setToolTipText("Refresh the preview browser");
-        toolBar.add(refreshAction);
-
-        toggleState = previewView.getProjectAutorefresh(project);
+		refreshAction.setImageDescriptor(PreviewerPlugin
+				.getImageDescriptor(Images.GREEN_SYNC));
+		refreshAction.setToolTipText("Refresh the preview browser");
+		toolBar.add(refreshAction);
 
-        toggleRefresh.setImageDescriptor(PreviewerPlugin.getImageDescriptor(Images.YELLOW_SYNC));
-        toggleRefresh.setToolTipText(getToggleActionTooltip());
-        toggleRefresh.setChecked(toggleState);
-        toolBar.add(toggleRefresh);
+		toggleState = previewView.getProjectAutorefresh(project);
 
-        pageSite.getActionBars().setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction);
-        getSite().setSelectionProvider(this);
-    }
+		toggleRefresh.setImageDescriptor(PreviewerPlugin
+				.getImageDescriptor(Images.YELLOW_SYNC));
+		toggleRefresh.setToolTipText(getToggleActionTooltip());
+		toggleRefresh.setChecked(toggleState);
+		toolBar.add(toggleRefresh);
 
-    protected void contributeToToolbar(IToolBarManager toolBar) {
-        // Do nothing here
-    }
+		pageSite.getActionBars().setGlobalActionHandler(
+				ActionFactory.REFRESH.getId(), refreshAction);
+		getSite().setSelectionProvider(this);
+	}
 
-    private String getToggleActionTooltip() {
-        return toggleState ? "Disable preview autorefresh" : "Enable preview autorefresh";
-    }
+	protected void contributeToToolbar(IToolBarManager toolBar) {
+		// Do nothing here
+	}
 
-    public void addSelectionChangedListener(ISelectionChangedListener listener) {
-        // Do nothing
-    }
+	private String getToggleActionTooltip() {
+		return toggleState ? "Disable preview autorefresh"
+				: "Enable preview autorefresh";
+	}
 
-    public ISelection getSelection() {
-        return new StructuredSelection(project);
-    }
+	public void addSelectionChangedListener(ISelectionChangedListener listener) {
+		// Do nothing
+	}
 
-    public void removeSelectionChangedListener(ISelectionChangedListener listener) {
-        // Do nothing
-    }
+	public ISelection getSelection() {
+		return new StructuredSelection(project);
+	}
 
-    public void setSelection(ISelection selection) {
-        // Do nothing
-    }
+	public void removeSelectionChangedListener(
+			ISelectionChangedListener listener) {
+		// Do nothing
+	}
 
-    public boolean isDisposed() {
-        return browser != null && browser.isDisposed();
-    }
+	public void setSelection(ISelection selection) {
+		// Do nothing
+	}
 
-    private synchronized void refreshBrowser() {
-        if (toggleState) {
-            promptIfNeeded();
-        }
-        if (toggleState) {
-            refresh(false);
-        } else {
-            needsRefresh = true;
-            refreshAction.setImageDescriptor(PreviewerPlugin.getImageDescriptor(Images.RED_SYNC));
-            refreshAction.setToolTipText("Refresh the preview browser (there are updated files)");
-        }
-    }
+	public boolean isDisposed() {
+		return browser != null && browser.isDisposed();
+	}
 
-    public IProject getProject() {
-        return project;
-    }
+	private synchronized void refreshBrowser() {
+		if (toggleState) {
+			promptIfNeeded();
+		}
+		if (toggleState) {
+			refresh(false);
+		} else {
+			needsRefresh = true;
+			refreshAction.setImageDescriptor(PreviewerPlugin
+					.getImageDescriptor(Images.RED_SYNC));
+			refreshAction
+					.setToolTipText("Refresh the preview browser (there are updated files)");
+		}
+	}
 
-    public synchronized void projectRenamed(IPath newPath) {
-        if (!isDisposed()) {
-            project = ResourcesPlugin.getWorkspace().getRoot().getProject(newPath.lastSegment());
-        }
-    }
+	public IProject getProject() {
+		return project;
+	}
+
+	public synchronized void projectRenamed(IPath newPath) {
+		if (!isDisposed()) {
+			project = ResourcesPlugin.getWorkspace().getRoot().getProject(
+					newPath.lastSegment());
+		}
+	}
 }