001: /**
002: *
003: */package newprocess.diagram.cust.annotations.utils;
004:
005: import java.net.MalformedURLException;
006: import java.net.URL;
007:
008: import org.eclipse.core.resources.IFile;
009: import org.eclipse.core.runtime.IStatus;
010: import org.eclipse.jface.dialogs.ErrorDialog;
011: import org.eclipse.jface.dialogs.MessageDialog;
012: import org.eclipse.osgi.util.NLS;
013: import org.eclipse.swt.widgets.Shell;
014: import org.eclipse.ui.IWorkbenchPage;
015: import org.eclipse.ui.IWorkbenchWindow;
016: import org.eclipse.ui.PartInitException;
017: import org.eclipse.ui.PlatformUI;
018: import org.eclipse.ui.browser.IWebBrowser;
019: import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
020: import org.eclipse.ui.ide.IDE;
021: import org.eclipse.ui.internal.WorkbenchMessages;
022: import org.eclipse.ui.internal.WorkbenchPlugin;
023:
024: /**
025: * Utility Class for open the Annotations
026: * @author sh
027: */
028:
029: public class OpenAnnotationUtil {
030:
031: /*
032: * Constructor
033: */
034: public OpenAnnotationUtil() {
035: }
036:
037: /**
038: * opens the given url in the internal browser, if possible.
039: */
040: public void openWebsite(String href) {
041: // open the Website in the browser
042: IWorkbenchBrowserSupport support = PlatformUI.getWorkbench()
043: .getBrowserSupport();
044: try {
045: IWebBrowser browser = null;
046: if (support.isInternalWebBrowserAvailable())
047: browser = support.createBrowser(null);
048: else
049: browser = support.getExternalBrowser();
050: browser.openURL(new URL(urlEncodeForSpaces(href
051: .toCharArray())));
052: } catch (MalformedURLException e) {
053: openWebBrowserError(href, e);
054: } catch (PartInitException e) {
055: openWebBrowserError(href, e);
056: }
057: }
058:
059: private String urlEncodeForSpaces(char[] input) {
060: StringBuffer retu = new StringBuffer(input.length);
061: for (int i = 0; i < input.length; i++) {
062: if (input[i] == ' ')
063: retu.append("%20");
064: else
065: retu.append(input[i]);
066: }
067: return retu.toString();
068: }
069:
070: /**
071: * display an error message
072: */
073: private void openWebBrowserError(final String href,
074: final Throwable t) {
075: final Shell shell = PlatformUI.getWorkbench()
076: .getActiveWorkbenchWindow().getShell();
077: shell.getDisplay().asyncExec(new Runnable() {
078: public void run() {
079: String title = WorkbenchMessages.ProductInfoDialog_errorTitle;
080: String msg = NLS
081: .bind(
082: WorkbenchMessages.ProductInfoDialog_unableToOpenWebBrowser,
083: href);
084: IStatus status = WorkbenchPlugin.getStatus(t);
085: ErrorDialog.openError(shell, title, msg, status);
086: }
087: });
088: }
089:
090: /**
091: * Opens the the selected Annotation from the
092: * TableViewer in its program
093: * @param path
094: * @param projectName
095: */
096: public void openAnnotation(String path, String projectName) {
097: // if it is a Website, open in the
098: // Annotation in the browser
099: if ((path.startsWith("http://"))
100: || (path.startsWith("https://"))) {
101: openWebsite(path);
102: return;
103: }
104:
105: // if it is a file from the workspace, open it in
106: // its editor
107: openFile(path, projectName);
108: }
109:
110: /**
111: * Opens the the selected Annotation from the
112: * TableViewer in its program
113: * @param path
114: * @param projectName
115: */
116: private void openFile(String path, String projectName) {
117: // get the file for the path
118: IFile file = new URIConvertUtil().getFileforPath(path,
119: projectName);
120:
121: // check if the file has been found
122: if ((file == null) || (!file.isAccessible())
123: || (!file.exists()))
124: return;
125:
126: // open the favourite Annotation in its Editor
127: IWorkbenchWindow dw = PlatformUI.getWorkbench()
128: .getActiveWorkbenchWindow();
129: try {
130: if (dw != null) {
131: IWorkbenchPage page = dw.getActivePage();
132: if (page != null) {
133: IDE.openEditor(page, file, true);
134: }
135: }
136: } catch (PartInitException e) {
137: MessageDialog.openError(dw.getShell(), "Error",
138: "file could not be opened");
139: }
140: }
141: }
|