Twaver Gui Demo

电信级拓扑及设备面板开发工具,功能强大。对于网元的抽象也做得很好,可以快速构建拓扑视图。

一个Develop Guide中的Example,

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.WindowConstants;

import twaver.AlarmSeverity;
import twaver.AlarmState;
import twaver.Chassis;
import twaver.DataBoxSelectionEvent;
import twaver.DataBoxSelectionListener;
import twaver.Dummy;
import twaver.Element;
import twaver.Link;
import twaver.Node;
import twaver.PopupMenuGenerator;
import twaver.Port;
import twaver.Rack;
import twaver.SummingAlarmPropagator;
import twaver.TDataBox;
import twaver.TUIManager;
import twaver.TView;
import twaver.TWaverUtil;
import twaver.network.TNetwork;
import twaver.network.ui.ElementUI;
import twaver.network.ui.IconAttachment;
import twaver.tree.TTree;

public class Tutorial extends JFrame {
	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);

	public Tutorial() {
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		getContentPane().add(split, BorderLayout.CENTER);
		split.setDividerLocation(100);

		doSample();
	}

	private void doSample() {
		try {
			step1();
			step2();
			step3();
			step4();
			step5();
			step6();
			step7();
			step8();
			step9();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void step1() {//创建两个节点,中间带连接线
		network = new TNetwork(box);
		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);
	}

	private void step2() {//在树上面将设备分类,并加载
		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);

		tree = new TTree(box);

		JScrollPane scroll = new JScrollPane(tree);
		treePane.add(scroll, BorderLayout.CENTER);
	}

	private void step3() {//创建机架,面板,及面板上添加端口
		Node node = (Node) box.getElementByID("A");
		Chassis chassis = new Chassis("Chassis A");
		node.addChild(chassis);
		box.addElement(chassis);

		// 1.在chassis上添加机架
		Rack rack = new Rack("Rack A");
		rack.setName("Rack");
		rack.setLocation(50, 50);
		rack.setImage("/demo/resource/tutorial/rack.png");
		chassis.addChild(rack);
		box.addElement(rack);

		// 2.在机架上添加端口
		String imgPort1 = "/demo/resource/tutorial/port1.png";
		String imgPort2 = "/demo/resource/tutorial/port2.png";
		for (int module = 0; module < 4; module++) {
			Dummy dummy = new Dummy("PortDummy" + module);
			
			dummy.setName("module" + module);
			rack.addChild(dummy);
			box.addElement(dummy);

			for (int index = 0; index < 4; index++) {
				Port port = new Port(module + ":" + index);
				int x, y;
				if (module % 2 == 0) {
					x = 210 + index * 24;
				} else {
					x = 319 + index * 24;
				}
				if (module < 2) {
					y = 16;
					port.setImage(imgPort1);
				} else {
					y = 37;
					port.setImage(imgPort2);
				}
				x += rack.getLocation().x;
				y += rack.getLocation().y;
				port.setLocation(new Point(x, y));
				dummy.addChild(port);
				box.addElement(port);
			}
		}
	}

	private void step4() {//给视图添加弹出菜单,
		// 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) {
					return null;
				} else {
					return popMenu;
				}
			}
		};
		// Set the pop-up menu generator for network components
		network.setPopupMenuGenerator(popupMenuGenerator);
	}

	private void step5() {//拓扑视图鼠标响应事件,有个可能不可接受的问题是为什么点出子视图后总是会回到主视图?
		network.getCanvas().addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					// get the element the mouse clicked.
					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);
				}
			}
		});
	}

	private void step6() {//树中的结点与拓扑中的网元动作关联
		// create a selection listener.
		DataBoxSelectionListener listener = new DataBoxSelectionListener() {
			public void selectionChanged(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);
	}

	private void step7() {//给端口添加告警状态。支持子网元告警在主网元上自动显示,且显示最高等级,属于自动告警收缩。
		// 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.addAcknowledgedAlarm(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);
	}

	//装饰图标
	private void step8() {
		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);
	}

	public static class MyIconAttachment extends IconAttachment {
		public MyIconAttachment(String name, ElementUI ui) {
			super(name, ui, TWaverUtil.getImageIcon("myIcon.png"));
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Tutorial frame = new Tutorial();
		frame.setSize(640, 480);
		frame.setTitle("TWaver Tutorial");
		TWaverUtil.centerWindow(frame);
		frame.setVisible(true);
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值