📄 nglink.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.DataDecoder;
import com.novocode.naf.data.ModelBinding;
import com.novocode.naf.data.XMLProperty;
import com.novocode.naf.gui.event.*;
import com.novocode.naf.resource.*;
/**
* An SWT Link.
*
* @author Stefan Zeiger (szeiger@novocode.com)
* @since Mar 19, 2005
*/
public final class NGLink extends NGWidget
{
private String text;
private LinkedList<String> childTokens;
@XMLProperty public void setText(String s) { this.text = s; this.text = s; }
public String getText() { return text; }
protected void init() throws NAFException
{
super.init();
if(childTokens != null && text == 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()))
{
String loc = n.getLocalName();
if(loc.equals("br"))
{
if(childTokens == null) childTokens = new LinkedList<String>();
if(childTokens.size() != 0 && " ".equals(childTokens.getLast())) childTokens.removeLast();
childTokens.add("\n");
}
else if(loc.equals("a"))
{
if(childTokens == null) childTokens = new LinkedList<String>();
Node hrefN = n.getAttributes().getNamedItem("href");
if(hrefN != null) childTokens.add("<a href=\""+hrefN.getTextContent()+"\">"+n.getTextContent()+"</a>");
else childTokens.add("<a>"+n.getTextContent()+"</a>");
}
else super.parseChild(n);
}
else super.parseChild(n);
}
public Control createControl(Composite parent, NGComponent parentComp, final ShellWindowInstance wi, WidgetData pwd) throws NAFException
{
Link link = new Link(parent, SWT.NONE);
if(text != null) link.setText(DataDecoder.decodeAccessKey(text));
final IActionListener actionModel = getModel("action", wi.models);
if(actionModel != null)
{
link.addListener(SWT.Selection, new Listener()
{
public void handleEvent(Event event)
{
actionModel.performAction(new ActionEvent(NGLink.this, event.text, wi));
}
});
}
else
{
link.addListener(SWT.Selection, new Listener()
{
public void handleEvent(Event event)
{
Program.launch(event.text);
}
});
}
return link;
}
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 + -