new look
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / MsaTreeEditor.java
1 package org.tianocore.migration;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.tree.*;
7
8 public class MsaTreeEditor extends JPanel {
9 /**
10 * Define class Serial Version UID
11 */
12 private static final long serialVersionUID = 3169905938472150649L;
13 /*
14 MsaTreeEditor(ModuleInfo m, UI u, ModuleSurfaceAreaDocument md) {
15 mi = m;
16 ui = u;
17 msadoc = md;
18
19 //rootNode = msadoc.getDomNode();
20 rootNode = new DefaultMutableTreeNode("Root Node");
21 treeModel = new DefaultTreeModel(rootNode);
22
23 tree = new JTree(treeModel);
24 tree.setEditable(true);
25 tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
26 tree.setShowsRootHandles(false);
27 tree.addMouseListener(mouseadapter);
28
29 JScrollPane scrollPane = new JScrollPane(tree);
30 add(scrollPane);
31
32 popupmenu = new JPopupMenu();
33 menuitemadd = new JMenuItem("addNode");
34 menuitemdel = new JMenuItem("deleteNode");
35 popupmenu.add(menuitemadd);
36 popupmenu.add(menuitemdel);
37 menuitemadd.addActionListener(actionListener);
38 menuitemdel.addActionListener(actionListener);
39
40 addNode(rootNode, "1st");
41 addNode(rootNode, "2nd");
42 }
43 */
44 MsaTreeEditor(ModuleInfo m, UI u) {
45 mi = m;
46 ui = u;
47
48 rootNode = new DefaultMutableTreeNode("Root Node");
49 treeModel = new DefaultTreeModel(rootNode);
50
51 tree = new JTree(treeModel);
52 tree.setEditable(true);
53 tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
54 tree.setShowsRootHandles(false);
55 tree.addMouseListener(mouseadapter);
56
57 JScrollPane scrollPane = new JScrollPane(tree);
58 add(scrollPane);
59
60 popupmenu = new JPopupMenu();
61 menuitemadd = new JMenuItem("addNode");
62 menuitemdel = new JMenuItem("deleteNode");
63 popupmenu.add(menuitemadd);
64 popupmenu.add(menuitemdel);
65 menuitemadd.addActionListener(actionListener);
66 menuitemdel.addActionListener(actionListener);
67
68 addNode(rootNode, "1st");
69 addNode(rootNode, "2nd");
70 }
71
72 private ModuleInfo mi;
73 private UI ui;
74 //private ModuleSurfaceAreaDocument msadoc;
75
76 private JTree tree;
77 private DefaultMutableTreeNode rootNode;
78 private DefaultTreeModel treeModel;
79 private JMenuItem menuitemadd, menuitemdel;
80
81 private JPopupMenu popupmenu;
82 private MouseAdapter mouseadapter = new MouseAdapter() {
83 public void mouseReleased(MouseEvent me) {
84 if (me.getClickCount() == 1 && SwingUtilities.isRightMouseButton(me)) {
85 tree.setSelectionPath(tree.getPathForLocation(me.getX(), me.getY()));
86 popupmenu.show(tree, me.getX(), me.getY());
87 }
88 }
89 };
90 private ActionListener actionListener = new ActionListener() {
91 public void actionPerformed(ActionEvent ae) {
92 if (ae.getSource() == menuitemadd) {
93 addNode();
94 } else if (ae.getSource() == menuitemdel) {
95 delNode();
96 }
97 }
98 };
99
100 private void delNode() {
101 treeModel.removeNodeFromParent((DefaultMutableTreeNode)(tree.getSelectionPath().getLastPathComponent()));
102 }
103
104 private void addNode() {
105 addNode((DefaultMutableTreeNode)(tree.getSelectionPath().getLastPathComponent()), MigrationTool.ui.getInput("Input Node Name"));
106 }
107
108 private void addNode(DefaultMutableTreeNode parentNode, Object child) {
109 DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
110 treeModel.insertNodeInto(childNode, parentNode, parentNode.getChildCount());
111 tree.scrollPathToVisible(new TreePath(childNode.getPath()));
112 }
113 /*
114 public static void init(ModuleInfo mi, UI ui, ModuleSurfaceAreaDocument msadoc) throws Exception {
115 init(mi, ui);
116 }
117 */
118 public static void init(ModuleInfo mi, UI ui) throws Exception {
119 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
120
121 JFrame frame = new JFrame("MsaTreeEditor");
122 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
123
124 MsaTreeEditor mte = new MsaTreeEditor(mi, ui);
125 mte.setLayout(new GridBagLayout());
126 mte.setOpaque(true);
127 frame.setContentPane(mte);
128
129 frame.pack();
130 frame.setVisible(true);
131 }
132 }