]>
Commit | Line | Data |
---|---|---|
90503bad | 1 | package org.tianocore.migration;\r |
2 | \r | |
3 | import java.awt.*;\r | |
4 | import java.awt.event.*;\r | |
5 | import javax.swing.*;\r | |
6 | import javax.swing.tree.*;\r | |
7 | \r | |
8 | public class MsaTreeEditor extends JPanel {\r | |
609384c5 | 9 | /**\r |
10 | * Define class Serial Version UID\r | |
11 | */\r | |
12 | private static final long serialVersionUID = 3169905938472150649L;\r | |
13 | \r | |
90503bad | 14 | MsaTreeEditor(ModuleInfo m, UI u) {\r |
15 | mi = m;\r | |
16 | ui = u;\r | |
17 | \r | |
18 | rootNode = new DefaultMutableTreeNode("Root Node");\r | |
19 | treeModel = new DefaultTreeModel(rootNode);\r | |
20 | \r | |
21 | tree = new JTree(treeModel);\r | |
22 | tree.setEditable(true);\r | |
23 | tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);\r | |
24 | tree.setShowsRootHandles(false);\r | |
25 | tree.addMouseListener(mouseadapter);\r | |
26 | \r | |
27 | JScrollPane scrollPane = new JScrollPane(tree);\r | |
28 | add(scrollPane);\r | |
29 | \r | |
30 | popupmenu = new JPopupMenu();\r | |
31 | JMenuItem menuitemadd = new JMenuItem("addNode");\r | |
32 | JMenuItem menuitemdel = new JMenuItem("deleteNode");\r | |
33 | popupmenu.add(menuitemadd);\r | |
34 | popupmenu.add(menuitemdel);\r | |
35 | menuitemadd.addActionListener(actionListener);\r | |
36 | menuitemdel.addActionListener(actionListener);\r | |
37 | \r | |
38 | addNode(rootNode, "1st");\r | |
39 | addNode(rootNode, "2nd");\r | |
40 | }\r | |
41 | \r | |
42 | private ModuleInfo mi;\r | |
43 | private UI ui;\r | |
44 | \r | |
45 | private JTree tree;\r | |
46 | private DefaultMutableTreeNode rootNode;\r | |
47 | private DefaultTreeModel treeModel;\r | |
48 | \r | |
49 | private JPopupMenu popupmenu;\r | |
50 | private MouseAdapter mouseadapter = new MouseAdapter() {\r | |
51 | public void mouseReleased(MouseEvent me) {\r | |
52 | if (me.getClickCount() == 1 && SwingUtilities.isRightMouseButton(me)) {\r | |
53 | tree.setSelectionPath(tree.getPathForLocation(me.getX(), me.getY()));\r | |
54 | popupmenu.show(tree, me.getX(), me.getY());\r | |
55 | }\r | |
56 | }\r | |
57 | };\r | |
58 | private ActionListener actionListener = new ActionListener() {\r | |
59 | public void actionPerformed(ActionEvent ae) {\r | |
60 | addNode();\r | |
61 | }\r | |
62 | };\r | |
63 | \r | |
64 | public void addNode() {\r | |
65 | addNode((DefaultMutableTreeNode)(tree.getSelectionPath().getLastPathComponent()), ui.getInput("Input Node Name"));\r | |
66 | System.out.println();\r | |
67 | }\r | |
68 | \r | |
69 | public void addNode(DefaultMutableTreeNode parentNode, Object child) {\r | |
70 | DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);\r | |
71 | treeModel.insertNodeInto(childNode, parentNode, parentNode.getChildCount());\r | |
72 | tree.scrollPathToVisible(new TreePath(childNode.getPath()));\r | |
73 | }\r | |
74 | \r | |
75 | public static void init(ModuleInfo mi, UI ui) throws Exception {\r | |
76 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());\r | |
77 | \r | |
78 | JFrame frame = new JFrame("MsaTreeEditor");\r | |
79 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\r | |
80 | \r | |
81 | MsaTreeEditor mte = new MsaTreeEditor(mi, ui);\r | |
82 | mte.setLayout(new GridBagLayout());\r | |
83 | mte.setOpaque(true);\r | |
84 | frame.setContentPane(mte);\r | |
85 | \r | |
86 | frame.pack();\r | |
87 | frame.setVisible(true);\r | |
88 | }\r | |
89 | } |