• 为org.eclipse.swt.widgets.Text 添加可点击的超链接


    可以为text, 也可以为label 添加可以点击的超链接。 代码如下:

    package us63740.parts;
    
    import java.awt.Desktop;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.util.Arrays;
    import java.util.List;
    import javax.annotation.PostConstruct;
    import javax.inject.Inject;
    import org.eclipse.e4.ui.di.Focus;
    import org.eclipse.e4.ui.di.Persist;
    import org.eclipse.e4.ui.model.application.ui.MDirtyable;
    import org.eclipse.jface.viewers.ArrayContentProvider;
    import org.eclipse.jface.viewers.TableViewer;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.MouseAdapter;
    import org.eclipse.swt.events.MouseEvent;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Text;
    
    public class SamplePart {
    
    	private Text txtInput;
    	private Label descLabel;
    	private TableViewer tableViewer;
    	private String urlString = "http://www.baidu.com/";
    
    	@Inject
    	private MDirtyable dirty;
    	
        @PostConstruct
    	public void createComposite(Composite parent) {
    		parent.setLayout(new GridLayout(1, false));
    
    		txtInput = new Text(parent, SWT.WRAP | SWT.READ_ONLY | SWT.BORDER | SWT.V_SCROLL);
    		
    		txtInput.addMouseListener(new MouseProcesser()); // add listener
    		GridData gd = new GridData();
    		gd.widthHint = 450;
    		gd.heightHint =100;
    		txtInput.setLayoutData(gd);
            txtInput.setText(urlString);
            
            createLable(parent);
    		
    		tableViewer = new TableViewer(parent);
    
    		tableViewer.setContentProvider(ArrayContentProvider.getInstance());;
    		tableViewer.setInput(createInitialDataModel());
    		tableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
    	}
    
    	private void createLable(Composite parent) {
    	    descLabel = new Label(parent, SWT.WRAP | SWT.READ_ONLY | SWT.BORDER | SWT.V_SCROLL);
    	    descLabel.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_BLUE));
    	    descLabel.setCursor(Display.getCurrent().getSystemCursor(SWT.CURSOR_HAND));
    	    
    	    GridData gd = new GridData();
            gd.widthHint = 450;
            gd.heightHint =100;
            descLabel.setLayoutData(gd);
            
            descLabel.setText(urlString);
            
            descLabel.addMouseListener(new MouseProcesser()); // add listener
    	    
    	    
    	    
    	}
    	
    	private class MouseProcesser extends MouseAdapter {
    	    
    	    @Override
    	    public void mouseDown(MouseEvent e) {
    	        if (e.button == 1) { // 1 = left button clicked, 2 = middle button clicked, 3 = right button clicked.
    	            try {
    	                Desktop.getDesktop().browse(new URI(urlString));
    	            } catch (IOException  | URISyntaxException e1) {
    	                e1.printStackTrace();
    	            }
    	        }
    	    }
    	}
    
        @Focus
    	public void setFocus() {
    		tableViewer.getTable().setFocus();
    	}
    
    	@Persist
    	public void save() {
    		dirty.setDirty(false);
    	}
    	
    	private List<String> createInitialDataModel() {
    		return Arrays.asList("Sample item 1", "Sample item 2", "Sample item 3", "Sample item 4", "Sample item 5");
    	}
    }
    
  • 相关阅读:
    SocketAsyncEventArgs的释放问题
    SharePoint 2013部署自定义HttpModule访问SPContext.Current的一个问题
    ASP.NET MVC View使用Conditional compilation symbols
    XPath注入
    Java基础(十二)之包和权限访问
    SSI注入漏洞
    java基础(十一)之抽象类和抽象函数
    邮件头注入
    java基础(十)之向上转型/向下转型
    java基础(八)之函数的复写/重写(override)
  • 原文地址:https://www.cnblogs.com/wenchunl/p/9957402.html
Copyright © 2020-2023  润新知