📄 xpathexample.java
字号:
package com.novocode.naf.example.xpath;
import java.util.*;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import com.novocode.naf.app.NAFApplication;
import com.novocode.naf.gui.WindowInstance;
import com.novocode.naf.gui.event.*;
import com.novocode.naf.model.DefaultBooleanModel;
import com.novocode.naf.model.DefaultObjectModel;
import com.novocode.naf.model.DefaultStringModel;
import com.novocode.naf.model.ModelMap;
import com.novocode.naf.resource.*;
import com.novocode.naf.xml.NAFNavigator;
import com.novocode.naf.xml.NAFXPath;
/**
* An XPath explorer which allows you to evaluate XPath expressions
* on NAF widget trees.
*
* @author Stefan Zeiger (szeiger@novocode.com)
* @since Nov 18, 2004
*/
public class XPathExample
{
public static void main(String[] args)
{
final NAFApplication app = new NAFApplication(XPathExample.class);
NGComponent win = app.getResource("gui.naf");
final Object base = win; // You can use any NGComponent here
ModelMap models = new ModelMap();
models.put("exit", new IActionListener()
{
public void performAction(ActionEvent e) { e.windowInstance.application.dispose(); }
});
final DefaultStringModel expression = new DefaultStringModel(".");
models.put("expression", expression);
final DefaultBooleanModel autoExpand = new DefaultBooleanModel(true);
models.put("auto-expand", autoExpand);
final NAFNavigator nav = NAFNavigator.getInstance();
final DefaultObjectModel<Object> treeContent = new DefaultObjectModel<Object>(new Object[] { base });
models.put("treeContent", treeContent);
models.put("treeContentProvider", new ITreeContentProvider()
{
public Object[] getChildren(Object element)
{
if(element instanceof Object[]) return (Object[])element;
if(element instanceof String) return null;
try
{
Iterator<?> it = nav.getChildAxisIterator(element);
if(it == null) return null;
List<Object> l = new ArrayList<Object>();
while(it.hasNext()) l.add(it.next());
return l.toArray();
}
catch(Exception ex) { return null; }
}
public boolean hasChildren(Object element)
{
if(element instanceof String) return false;
if(element instanceof Object[]) return true;
Object[] children = getChildren(element);
return children != null && children.length > 0;
}
public Object getParent(Object element)
{
if(element instanceof String) return treeContent.getValue();
if(element instanceof Object[]) return null;
try { return nav.getParentNode(element); } catch(Exception ex) { return null; }
}
public Object[] getElements(Object element) { return getChildren(element); }
public void dispose() {}
public void inputChanged(Viewer viewer, Object old_input, Object new_input) {}
});
models.put("treeLabelProvider", new LabelProvider()
{
Image fileImage = app.getImageManager().getImage("file.gif").acquire();
Image nodeImage = app.getImageManager().getImage("bundle_obj.gif").acquire();
Image errImage = app.getImageManager().getImage("showerr_tsk.gif").acquire();
public String getText(Object element)
{
if(element instanceof Throwable) return ((Throwable)element).toString();
if(element instanceof Object[]) return "";
if(element instanceof NAFNavigator.Attr) return element.toString();
if(element instanceof NAFNavigator.Document) return "[Document Root]";
if(element instanceof NGComponent)
{
StringBuilder b = new StringBuilder().append(nav.getElementName(element));
try
{
for(Iterator<?> it = nav.getAttributeAxisIterator(element); it != null && it.hasNext();)
{
Object a = it.next();
b.append(" @").append(nav.getAttributeName(a));
b.append("=\"").append(nav.getAttributeStringValue(a)).append("\"");
}
}
catch(Exception ignored) {}
return b.toString();
}
return element.toString();
}
public Image getImage(Object element)
{
if(element instanceof Throwable) return errImage;
if(element instanceof NAFNavigator.Document) return fileImage;
if(element instanceof NGComponent) return nodeImage;
return null;
}
});
final DefaultActionBroadcaster expandAll = new DefaultActionBroadcaster();
models.put("expand-all", expandAll);
models.put("evaluate", new IActionListener()
{
public void performAction(ActionEvent e)
{
try
{
NAFXPath xp = new NAFXPath(expression.getValue());
List nodes = xp.selectNodes(base);
treeContent.setValue(nodes.toArray());
if(autoExpand.getBoolean()) expandAll.performAction(new ActionEvent(e.source, null, e.windowInstance ));
}
catch(Exception ex) { treeContent.setValue(new Object[] { ex }); }
}
});
WindowInstance wi = app.createInstance(win, models);
if(autoExpand.getBoolean()) expandAll.performAction(new ActionEvent(win, null, wi));
wi.open();
app.runApp();
app.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -