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

📄 nghyperlink.java

📁 Novocode的 SWT 控件框架 丰富了MDI功能
💻 JAVA
字号:
/*******************************************************************************
 * Copyright (c) 2004 Stefan Zeiger and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.novocode.com/legal/epl-v10.html
 * 
 * Contributors:
 *     Stefan Zeiger (szeiger@novocode.com) - initial API and implementation
 *******************************************************************************/

package com.novocode.naf.gui;

import java.util.*;
import java.util.LinkedList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.*;
import org.w3c.dom.*;

import com.novocode.naf.app.*;
import com.novocode.naf.data.Alignment;
import com.novocode.naf.data.DataDecoder;
import com.novocode.naf.data.ModelBinding;
import com.novocode.naf.data.XMLProperty;
import com.novocode.naf.gui.event.*;
import com.novocode.naf.resource.*;
import com.novocode.naf.swt.custom.Hyperlink;


/**
 * A hyperlink label.
 * 
 * @author Stefan Zeiger (szeiger@novocode.com)
 * @since Mar 1, 2004
 */

public final class NGHyperlink extends NGWidget
{
  private DisplayStyle displayStyle = DisplayStyle.DEFAULT;
  private Alignment alignment = Alignment.LEFT;
  private String text, attrText, href;
  private LinkedList<String> childTokens;
  private boolean acceptFocus;

  public enum DisplayStyle { DEFAULT, MOUSEOVER, QUIET };

  
  @XMLProperty public void setText(String s) { this.text = s; this.attrText = s; }
  public String getText() { return text; }


  @XMLProperty("href") public void setHRef(String s) { this.href = s; }
  public String getHRef() { return href; }

  
  @XMLProperty public void setAcceptFocus(boolean b) { this.acceptFocus = b; }
  public boolean getAcceptFocus() { return acceptFocus; }


  @XMLProperty("align") public void setAlignment(Alignment a) { this.alignment = a; }
  public Alignment getAlignment() { return alignment; }

  
  @XMLProperty public void setDisplayStyle(DisplayStyle d) { this.displayStyle = d; }
  public DisplayStyle getDisplayStyle() { return displayStyle; }

  
  protected void init() throws NAFException
  {
    super.init();
    if(childTokens != null && attrText == null)
    {
      StringBuffer b = new StringBuffer();
      for(String s : childTokens) b.append(s);
      text = b.toString().trim().replace((char)160,' ');
      if(text.length() == 0) text = null;
    }
  }


  protected void parseChild(Node n) throws NAFException // [TODO] Don't decode backslash sequences
  {
    if(n.getNodeType() == Node.TEXT_NODE)
    {
      if(childTokens == null) childTokens = new LinkedList<String>();
      String text = ((org.w3c.dom.Text)n).getData();
      text = text.replace('\r', ' ').replace('\n', ' ').replace('\r', ' ');
      if(text.startsWith(" ") && childTokens.size() != 0 && (!" ".equals(childTokens.getLast())) && (!"\n".equals(childTokens.getLast())))
        childTokens.add(" ");
      boolean first = true;
      for(StringTokenizer tok = new StringTokenizer(text); tok.hasMoreTokens();)
      {
        String s = tok.nextToken();
        if(s.length() == 0) continue;
        if(!first) childTokens.add(" ");
        childTokens.add(s);
        first = false;
      }
      if(text.endsWith(" ")) childTokens.add(" ");
    }
    else if(n.getNodeType() == Node.ELEMENT_NODE
            && GUIResourceManager.NAF_NAMESPACE_URI.equals(n.getNamespaceURI())
            && n.getLocalName().equals("br"))
    {
      if(childTokens == null) childTokens = new LinkedList<String>();
      if(childTokens.size() != 0 && " ".equals(childTokens.getLast())) childTokens.removeLast();
      childTokens.add("\n");
    }
    else super.parseChild(n);
  }


  public Control createControl(Composite parent, NGComponent parentComp, ShellWindowInstance wi, WidgetData pwd) throws NAFException
  {
    int swtstyle = alignment.style;
    if(!acceptFocus) swtstyle |= SWT.NO_FOCUS;
    Hyperlink hl = new Hyperlink(parent, swtstyle);

    if(text != null) hl.setText(DataDecoder.decodeAccessKey(DataDecoder.decodeBackslashEscapes(text)));
    final String usedHRef = (href == null) ? text : href;
    
    final IActionListener actionModel = getModel("action", wi.models);
    if(actionModel != null)
    {
      String cmd = getModelBinding("action").getAttribute("command");
      if(cmd == null) cmd = usedHRef;
      final ActionEvent ae = new ActionEvent(this, cmd, wi);

      hl.addListener(SWT.Selection, new Listener()
      {
        public void handleEvent(Event event)
        {
          actionModel.performAction(ae);
        }
      });
    }
    else
    {
      hl.addListener(SWT.Selection, new Listener()
      {
        public void handleEvent(Event event)
        {
          Program.launch(usedHRef);
        }
      });
    }

    switch(displayStyle)
    {
      case QUIET:
        hl.setHoverUnderline(null);
        hl.setHoverForeground(null);
        hl.setActiveForeground(null);
        hl.setActiveUnderline(null);
        break;
        
      case DEFAULT:
        hl.setUnderline(hl.getForeground());
        hl.setHoverUnderline(hl.getForeground());
        hl.setHoverForeground(hl.getForeground());
        break;
        
      case MOUSEOVER:
        // do nothing
        break;
    }

    return hl;
  }

    
  protected Object createDefaultModel(ModelBinding mb)
  {
    if("action".equals(mb.type)) return new DefaultActionBroadcaster(mb.id);
    else return super.createDefaultModel(mb);
  }
}

⌨️ 快捷键说明

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