⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 openurlaction.java

📁 基于Eclipse RCP开发的管理工具
💻 JAVA
字号:
 /*  
  * Copyright 2006 Marcel Schoffelmeer 
  * 
  * Licensed under the Apache License, Version 2.0 (the "License"); 
  * you may not use this file except in compliance with the License. 
  * You may obtain a copy of the License at 
  * 
  *    http://www.apache.org/licenses/LICENSE-2.0 
  *    
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

package com.s10r.manager.actions;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.s10r.manager.Application;
import com.s10r.manager.model.PasswordEntry;

/**
 * Opens a browser with the url of the password entry. Note: this method
 * does not work on Suse 10.1
 * 
 * @author schoffem
 */
public class OpenUrlAction extends PasswordEntryAction implements
		IWorkbenchAction, IObjectActionDelegate
{
	public final static String ID = "manager_rcp.OpenUrlAction";

	public OpenUrlAction()
	{
	}

	public OpenUrlAction(IWorkbenchWindow window)
	{
		super(window);

		setId(ID);
		setText("Open URL");
		setToolTipText("Open URL in browser");

		setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(
				Application.PLUGIN_ID, "icons/link_go.png"));
	}

	@Override
	public void run()
	{
		PasswordEntry entry = getSelectedPasswordEntry();
		if (entry != null)
		{
			if (entry.getUrl() != null && entry.getUrl().length() > 0)
			{
				String url = entry.getUrl();
				// picky, picky...
				if (!url.startsWith("http://"))
				{
					url = "http://" + url;
				}
				openLink(url);
			}
			else
			{
			}
		}
		else
		{
		}
	}

	// popup run
	public void run(IAction action)
	{
		run();
	}
	
	 protected void openLink(String href) {
		 
		 // code from: Eclipse's ProductInfoDIalog.
		 
	        // format the href for an html file (file:///<filename.html>
	        // required for Mac only.
	        if (href.startsWith("file:")) { //$NON-NLS-1$
	            href = href.substring(5);
	            while (href.startsWith("/")) { //$NON-NLS-1$
	                href = href.substring(1);
	            }
	            href = "file:///" + href; //$NON-NLS-1$
	        }
			IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
			try {
				IWebBrowser browser = support.getExternalBrowser();
				System.out.println("openLink href = " + href);
				System.out.println("  browser class = " + browser.getClass().getName());
				browser.openURL(new URL(href));
			}
			catch (MalformedURLException e) {
				// openWebBrowserError(href, e);
				e.printStackTrace();
			}
			catch (PartInitException e) {
				// openWebBrowserError(href, e);
				e.printStackTrace();
			}
	    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -