📄 nggenericswtcontrol.java
字号:
/*******************************************************************************
* Copyright (c) 2005 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.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;
import com.novocode.naf.app.*;
import com.novocode.naf.data.XMLProperty;
import com.novocode.naf.resource.*;
/**
* A generic wrapper class for SWT Controls.
*
* @author Stefan Zeiger (szeiger@novocode.com)
* @since May 2, 2005
*/
public final class NGGenericSWTControl extends NGWidget
{
public static final String NAF_REFLECT_NAMESPACE_URI = "http://www.novocode.com/namespaces/naf-reflect";
private static final Class[] CONS_PARAM = new Class[] { Composite.class, Integer.TYPE };
private SWTStyle[] swtStyle = new SWTStyle[] { SWTStyle.CLOSE, SWTStyle.TITLE, SWTStyle.MIN };
private HashMap<String, String> reflectProperties = new HashMap<String, String>();
public enum SWTStyle
{
BORDER (SWT.BORDER), CLOSE (SWT.CLOSE), MIN (SWT.MIN), BORDERLESS (SWT.NO_TRIM),
TITLE (SWT.TITLE), SHELL (SWT.CLOSE|SWT.TITLE|SWT.MIN), DIALOG (SWT.DIALOG_TRIM),
// HSCROLL (SWT.H_SCROLL), VSCROLL (SWT.V_SCROLL),
PUSH (SWT.PUSH),
TOP (SWT.ON_TOP), TOOL (SWT.TOOL);
public final int style;
SWTStyle(int style) { this.style = style; }
public static int style(SWTStyle[] a)
{
if(a == null) return 0;
int res = 0;
for(SWTStyle t : a) if(t != null) res |= t.style;
return res;
}
}
@XMLProperty("style")
public void setStyle(SWTStyle[] a) { swtStyle = a; }
public SWTStyle[] getStyle() { return swtStyle; }
@XMLProperty("class")
public void setControlClass(String s) throws ClassNotFoundException { setReferencedClass(Class.forName(s)); }
public String getControlClass() { Class c = getReferencedClass(); return c == null ? null : c.getName(); }
protected void parseChild(Node n) throws NAFException
{
if(NAF_REFLECT_NAMESPACE_URI.equals(n.getNamespaceURI()))
{
if(n.getNodeType() == Node.ATTRIBUTE_NODE)
{
String name = ((Attr)n).getLocalName();
String value = n.getTextContent();
//System.out.println("Reflection Property: "+name+" -> "+value);
reflectProperties.put(name, value);
}
/*else if(n.getNodeType() == Node.ELEMENT_NODE)
{
}*/
}
else super.parseChild(n);
}
protected Control createControl(Composite parent, NGComponent parentComp, ShellWindowInstance wi, WidgetData pwd) throws NAFException
{
try
{
Constructor<?> cons = getReferencedClass().getConstructor(CONS_PARAM);
int style = SWTStyle.style(swtStyle);
Control c = (Control)cons.newInstance(new Object[] { parent, new Integer(style) });
for(Map.Entry<String, String> entry : reflectProperties.entrySet())
{
String name = entry.getKey();
String value = entry.getValue();
}
return c;
}
catch(Exception ex)
{
throw new NAFException("Error instantiating SWT Control", ex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -