privatevoidstep4() {
//Create a popup menu generator
PopupMenuGenerator popupMenuGenerator = new PopupMenuGenerator() {
/**
* Add the identifier of each of the selected objects to the menu.
* In this example, the items added to the menu do nothing.
* In a real application, you would probably associate an
* implementation of the Swing Action interface with each menu item.
*/public JPopupMenu generate(TView tview, MouseEvent mouseEvent){
//Create an empty pop-up menu.
JPopupMenu popMenu = new JPopupMenu();
JMenuItem item;
//If the selectedObjects collection is empty, no objects are selected.if (tview.getDataBox().getSelectionModel().isEmpty()) {
popMenu.add("Nothing selected");
} else {
//Access the selected objects from the selection model.
Iterator it = tview.getDataBox().getSelectionModel().selection();
while (it.hasNext()) {
Element element = (Element) it.next();
popMenu.add(element.getName());
}
}
//If menu is empty, return null.if (popMenu.getComponentCount() == 0) {
returnnull;
} else {
return popMenu;
}
}
};
//Set the pop-up menu generator for network components
network.setPopupMenuGenerator(popupMenuGenerator);
}
privatevoidstep6() {
//create a selection listener.
DataBoxSelectionListener listener = new DataBoxSelectionListener() {
publicvoidselectionChanged(DataBoxSelectionEvent e) {
//get the last selected element and make it visible.
Element element = e.getBoxSelectionModel().lastElement();
if (element != null) {
network.ensureVisible(element);
}
}
};
box.getSelectionModel().addDataBoxSelectionListener (listener);
}
privatevoidstep7() {
}
//还没看明白
private void step7() {
//create andset a summing propagator to the data source,
//here will make the box propagate alarms to its parent.
box.setAlarmPropagator(new SummingAlarmPropagator());
//get a port in the equipment rack.
Port nodeA = (Port) box.getElementByID("0:0");
AlarmState alarmState = nodeA.getAlarmState();
//add an acknowledged alarm with critical severity.
alarmState.acknowledgeAlarm(AlarmSeverity.CRITICAL);
//addand new alarm with major severity.
alarmState.addNewAlarm(AlarmSeverity.MAJOR);
//get another port.
Port nodeB = (Port) box.getElementByID("3:3");
alarmState = nodeB.getAlarmState();
//add10 new alarms with critical minor.
alarmState.increaseNewAlarm(AlarmSeverity.MINOR, 10);
}
//define a new LayoutedIconAttachment.//It must define as public static class.publicstaticclassMyIconAttachmentextendsIconAttachment{publicMyIconAttachment(String name, ElementUI ui) {
super(name, ui, TWaverUtil.getImageIcon("myIcon.png"));
}
}
privatevoidstep8() {
String iconName="document";
TUIManager.registerAttachment(iconName, MyIconAttachment.class);
//put a "document" icon on element B.
Element element = box.getElementByID("A");
element.addAttachment(iconName);
}
private void step9() {
//get the link element.
Link element = (Link)box.getElementByID("link");
//make the link animating flowing
element.putLinkFlowing(true);
//set the link flowing color
element.putLinkFlowingColor(Color.black);
//set the link outline color
element.putLinkOutlineColor(Color.black);
//set the link body color.
element.putLinkColor(Color.white);
//set the link lable font
element.putLabelFont(new Font("Impact", 1, 20));
//set the link lable color
element.putLabelColor(Color.MAGENTA);
}
总的代码
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import twaver.*;
import twaver.network.*;
import twaver.network.ui.*;
import twaver.table.*;
import twaver.tree.*;
publicclassTutorialextendsJFrame {private TDataBox box = new TDataBox("Simple Data Box");
private TNetwork network;
private TTree tree;
private JPanel networkPane = new JPanel(new BorderLayout());
private JPanel treePane = new JPanel(new BorderLayout());
private JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treePane, networkPane);
publicTutorial() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(split, BorderLayout.CENTER);
split.setDividerLocation(200); // 不加的话,左边的框显示不出来
doSample();
}
publicstaticvoidmain(String[] args) {
Tutorial frame = new Tutorial();
frame.setSize(500, 300);
frame.setTitle("TWaver Tutorial");
TWaverUtil.centerWindow(frame);
frame.setVisible(true);
}
privatevoiddoSample() {
try {
step1();
// step2();// step3();// step4();// step5();// step6();// step7();// step8();// step9();
} catch (Exception ex) {
ex.printStackTrace();
}
}
privatevoidstep1() {
network = new TNetwork(box);
// network.setToolbarByName(""); //这句话可以把工具栏去掉
networkPane.add(network, BorderLayout.CENTER);
Node nodeA = new Node("A");
nodeA.setName("I'm node A!");
nodeA.setLocation(50, 50);
box.addElement(nodeA);
Node nodeB = new Node("B");
nodeB.setName("I'm node B!");
nodeB.setLocation(200, 200);
box.addElement(nodeB);
Link link = new Link("link", nodeA, nodeB);
link.setName("Telephone Line");
box.addElement(link);
}
privatevoidstep2() {
Dummy nodeDummy = new Dummy("node dummy");
nodeDummy.setName("All Nodes");
nodeDummy.addChild(box.getElementByID("A"));
nodeDummy.addChild(box.getElementByID("B"));
box.addElement(nodeDummy);
Dummy linkDummy = new Dummy("link dummy");
linkDummy.setName("All Links");
linkDummy.addChild(box.getElementByID("link"));
box.addElement(linkDummy); // 只是用来分类,不加这两个Dummy会把Node和Link放到一块
tree = new TTree(box);
JScrollPane scroll = new JScrollPane(tree);
treePane.add(scroll, BorderLayout.CENTER); // 这里要套一个scrollpane,nodepane没套是因为TNetwork已经有了
}
privatevoidstep3() {
// add chassis to node A.
Node node = (Node) box.getElementByID("A");
Chassis chassis = new Chassis("Chassis A");
chassis.setDataSource("/equipment1_template.xml");
node.addChild(chassis);
box.addElement(chassis);
}
privatevoidstep4() {
//Create a popup menu generator
PopupMenuGenerator popupMenuGenerator = new PopupMenuGenerator() {
/**
* Add the identifier of each of the selected objects to the menu.
* In this example, the items added to the menu do nothing.
* In a real application, you would probably associate an
* implementation of the Swing Action interface with each menu item.
*/public JPopupMenu generate(TView tview, MouseEvent mouseEvent){
//Create an empty pop-up menu.
JPopupMenu popMenu = new JPopupMenu();
JMenuItem item;
//If the selectedObjects collection is empty, no objects are selected.if (tview.getDataBox().getSelectionModel().isEmpty()) {
popMenu.add("Nothing selected");
} else {
//Access the selected objects from the selection model.
Iterator it = tview.getDataBox().getSelectionModel().selection();
while (it.hasNext()) {
Element element = (Element) it.next();
popMenu.add(element.getName());
}
}
//If menu is empty, return null.if (popMenu.getComponentCount() == 0) {
returnnull;
} else {
return popMenu;
}
}
};
//Set the pop-up menu generator for network components
network.setPopupMenuGenerator(popupMenuGenerator);
}
privatevoidstep5() {
network.getCanvas().addMouseListener(new MouseAdapter() {
publicvoidmouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
//获取双击的网元对象
Element element = network.getElementPhysicalAt(e.getPoint());
String message;
if (element == null) {
message = "You clicked nothing.";
} else {
message = "You clicked '" + element.getName() + "'";
}
JOptionPane.showMessageDialog(network, message);
}
}
});
}
privatevoidstep6() {
//create a selection listener.
DataBoxSelectionListener listener = new DataBoxSelectionListener() {
publicvoidselectionChanged(DataBoxSelectionEvent e) {
//get the last selected element and make it visible.
Element element = e.getBoxSelectionModel().lastElement();
if (element != null) {
network.ensureVisible(element);
}
}
};
box.getSelectionModel().addDataBoxSelectionListener (listener);
}
privatevoidstep7() {
//create and set a summing propagator to the data source,//here will make the box propagate alarms to its parent.
box.setAlarmPropagator(new SummingAlarmPropagator());
//get a port in the equipment rack.
Port nodeA = (Port) box.getElementByID("0:0");
AlarmState alarmState = nodeA.getAlarmState();
//add an acknowledged alarm with critical severity.
alarmState.acknowledgeAlarm(AlarmSeverity.CRITICAL);
//add and new alarm with major severity.
alarmState.addNewAlarm(AlarmSeverity.MAJOR);
//get another port.
Port nodeB = (Port) box.getElementByID("3:3");
alarmState = nodeB.getAlarmState();
//add 10 new alarms with critical minor.
alarmState.increaseNewAlarm(AlarmSeverity.MINOR, 10);
}
//define a new LayoutedIconAttachment.//It must define as public static class.publicstaticclassMyIconAttachmentextendsIconAttachment{publicMyIconAttachment(String name, ElementUI ui) {
super(name, ui, TWaverUtil.getImageIcon("myIcon.png"));
}
}
privatevoidstep8() {
String iconName="document";
TUIManager.registerAttachment(iconName, MyIconAttachment.class);
//put a "document" icon on element B.
Element element = box.getElementByID("A");
element.addAttachment(iconName);
}
privatevoidstep9() {
//get the link element.
Link element = (Link)box.getElementByID("link");
//make the link animating flowing
element.putLinkFlowing(true);
//set the link flowing color
element.putLinkFlowingColor(Color.black);
//set the link outline color
element.putLinkOutlineColor(Color.black);
//set the link body color.
element.putLinkColor(Color.white);
//set the link lable font
element.putLabelFont(new Font("Impact", 1, 20));
//set the link lable color
element.putLabelColor(Color.MAGENTA);
}
}