?? tutorialdemo.java
字號:
/*
* This source code is part of TWaver 1.3.1
*
* SERVA Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
* Copyright 2000-2005 SERVA Software, Inc. All rights reserved.
*/
package demo.tutorial;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import demo.DemoPane;
import twaver.*;
import twaver.network.*;
import twaver.network.ui.*;
import twaver.tree.*;
public class TutorialDemo extends DemoPane {
private TDataBox box = new TDataBox("Tutorial Demo");
private TNetwork network;
private TTree tree;
private JPanel networkPane = new JPanel(new BorderLayout());
private JPanel treePane = new JPanel(new BorderLayout());
public TutorialDemo() {
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treePane, networkPane);
split.setDividerLocation(150);
this.add(split, BorderLayout.CENTER);
step1();
step2();
step3();
step4();
step5();
step6();
step7();
step8();
step9();
}
private void step1() {
//1.create the network component.
network = new TNetwork(box);
//2.add the network into specified container
networkPane.add(network, BorderLayout.CENTER);
//3.add some elements(business objects) into the data box
Node nodeA = new Node("A");
nodeA.setName("I'm node A!");
nodeA.setLocation(50, 50);
box.addElement(nodeA); //add into box.
Node nodeB = new Node("B");
nodeB.setName("I'm node B!");
nodeB.setLocation(200, 200);
box.addElement(nodeB); //add into box.
Link link = new Link("link", nodeA, nodeB);
link.setName("Telephone Line");
box.addElement(link); //add into box.
}
private void step2() {
//1. add some dummy elements to group the data with easy understanding hierarchy
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);
//2. create the tree component.
tree = new TTree(box);
//3. add the tree to frame.
JScrollPane scroll = new JScrollPane(tree);
treePane.add(scroll, BorderLayout.CENTER);
}
private void step3() {
//1.add chassis to node A.
Node node = (Node) box.getElementByID("A");
Chassis chassis = new Chassis("Chassis A");
chassis.setName("Chassis");
node.addChild(chassis);
box.addElement(chassis);
//2.add rack to 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);
//3.add ports to rack.
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 pop-up menu factory to use throughout the application.
PopupMenuFactory popupMenuFactory = new PopupMenuFactory() {
/**
* 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 getPopupMenu(DataBoxSelectionModel selectionModel,Point p) {
//Create an empty pop-up menu.
JPopupMenu popMenu = new JPopupMenu();
//If the selectedObjects collection is empty, no objects are selected.
if (selectionModel.isEmpty()) {
popMenu.add("Nothing selected");
} else {
//Access the selected objects from the selection model.
Iterator it = selectionModel.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 factory for network components
network.setPopupMenuFactory(popupMenuFactory);
}
private void step5() {
network.addElementDoubleClickedActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Element element = (Element)e.getSource();
JOptionPane.showMessageDialog(network, "You clicked '" + element.getName() + "'");
}
});
network.addBackgroundDoubleClickedActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(network, "You clicked nothing.");
}
});
}
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);
}
}
public void currentSubNetworkChanged(DataBoxSelectionEvent e) {}
};
box.getSelectionModel().addBoxSelectionListener(listener);
}
private void step7() {
//new ant set a summing propagator to the data source,
//this will make the box propagate alarms to it's 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 an 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 alarm with critical minor.
alarmState.increaseNewAlarm(AlarmSeverity.MINOR, 10);
}
private void step8() {
//put a "document" icon on element B.
Element element = box.getElementByID("A");
String iconName = "document";
element.putClientProperty("StateIcon:" + iconName, Boolean.TRUE);
IconAttachmentHolder.addAttachment("myState", "/demo/resource/tutorial/myIcon.png");
element.putClientProperty(TWaverConst.ELEMENT_STATE_ICON_PREFIX + "myState", Boolean.TRUE);
}
private void step9() {
//get the link element.
Element element = box.getElementByID("link");
//make the link animating flowing
element.putClientProperty("link.flowing", Boolean.TRUE);
//set the link flowing color
element.putClientProperty("link.flowing.color", Color.black);
//set the link outline color
element.putClientProperty("link.outline.color", Color.black);
//set the link body color.
element.putClientProperty("link.color", Color.white);
//set the link lable font and color
element.putClientProperty("label.font", new Font("Impact", 1, 20));
element.putClientProperty("label.color", Color.MAGENTA);
}
public String getTitle() {
return "Tutorial Demo";
}
public String getHelp() {
return "This demo teachs you to create a simple application step by step.";
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -