1 5 package SOFA.SOFAnode.InOut.Impl; 6 7 import SOFA.SOFAnode.TR.ComponentInfo; 8 import SOFA.Util.VMProperties; 9 10 import javax.swing.*; 11 import javax.swing.tree.*; 12 import java.awt.*; 13 import java.awt.event.ActionEvent; 14 import java.awt.event.ActionListener; 15 import java.io.FileInputStream; 16 import java.io.IOException; 17 import java.util.*; 18 19 23 public class Browser extends JFrame implements ActionListener { 24 25 28 private Properties TRList; 29 30 33 private String messageText; 34 35 38 private DefaultTreeModel model; 39 40 44 private JTree leftTR, rightTR; 45 46 49 private JButton obtainButton; 50 51 54 private JButton distributeButton; 55 56 59 private JButton obtainOfferButton; 60 61 64 private JButton distributeOfferButton; 65 66 69 private JButton refreshButton; 70 71 74 private static JTextArea messages; 75 76 80 public Browser (String confPath) { 81 TRList = new Properties(); 82 messageText = ""; 83 try { 84 TRList.load(new FileInputStream(confPath)); 85 } catch (IOException e) { 86 messageText = "Configuration file not found (" + confPath + ")!\n"; 87 System.out.print(messageText); 88 } 89 90 obtainButton = new JButton("<<<"); 91 obtainButton.setToolTipText("Obtain selected components"); 92 obtainButton.addActionListener(this); 93 distributeButton = new JButton(">>>"); 94 distributeButton.setToolTipText("Distribute selected components"); 95 distributeButton.addActionListener(this); 96 97 obtainOfferButton = new JButton("<"); 98 obtainOfferButton.setToolTipText("Obtain description of selected components"); 99 obtainOfferButton.addActionListener(this); 100 distributeOfferButton = new JButton(">"); 101 distributeOfferButton.setToolTipText("Distribute description of selected components"); 102 distributeOfferButton.addActionListener(this); 103 104 refreshButton = new JButton("<>"); 105 refreshButton.setToolTipText("Refresh nodes"); 106 refreshButton.addActionListener(this); 107 108 messages = new JTextArea(messageText); 109 110 model = getTreeStructure(TRList); 111 leftTR = new JTree(model); 112 rightTR = new JTree(model); 113 114 JSplitPane componentPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, 115 new JScrollPane(leftTR), new JScrollPane(rightTR)); 116 componentPanel.setResizeWeight(0.5); 117 118 GridBagLayout gridBag = new GridBagLayout(); 119 JPanel panel = new JPanel(gridBag); 120 121 GridBagConstraints c = new GridBagConstraints(); 122 123 c.fill = GridBagConstraints.BOTH; 124 c.weightx = 1.0; 125 c.weighty = 1.0; 126 c.gridwidth = GridBagConstraints.REMAINDER; 127 gridBag.setConstraints(componentPanel, c); 128 panel.add(componentPanel); 129 130 c.fill = GridBagConstraints.NONE; 131 c.gridwidth = 1; 132 c.weighty = 0.0; 133 c.anchor = GridBagConstraints.CENTER; 134 gridBag.setConstraints(distributeOfferButton, c); 135 panel.add(distributeOfferButton); 136 137 gridBag.setConstraints(distributeButton, c); 138 panel.add(distributeButton); 139 140 gridBag.setConstraints(refreshButton, c); 141 panel.add(refreshButton); 142 143 gridBag.setConstraints(obtainButton, c); 144 panel.add(obtainButton); 145 146 c.gridwidth = GridBagConstraints.REMAINDER; 147 gridBag.setConstraints(obtainOfferButton, c); 148 panel.add(obtainOfferButton); 149 150 JScrollPane messagePanel = new JScrollPane(messages); 151 152 getContentPane().add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, panel, messagePanel)); 153 setDefaultCloseOperation(DISPOSE_ON_CLOSE); 154 String sofaNodeName = System.getProperty(VMProperties.NODE_NAME); 155 setTitle("SOFA Net Manager" + ((sofaNodeName == null) ? "" : " on " + sofaNodeName)); 156 pack(); 157 componentPanel.setDividerLocation(0.5); 158 setVisible(true); 159 160 } 161 162 170 private DefaultTreeModel getTreeStructure (Properties TRList) { 171 DefaultMutableTreeNode root = new DefaultMutableTreeNode("Known SOFA nodes"); 172 173 Enumeration enum = TRList.keys(); 174 while (enum.hasMoreElements()) { String name = (String) enum.nextElement(); 176 String ref = (String) TRList.get(name); 177 SOFANode node = new SOFANode(name, ref, messages); 178 179 DefaultMutableTreeNode tNode = node.connect(root); 180 if (node.isConnected()) 181 node.list(tNode); 182 } 183 184 return new DefaultTreeModel(root); 185 } 186 187 194 public static void main (String[] argv) { 195 if (argv.length > 1) { 196 System.out.println( 197 "Browser [config file name]\n" + 198 " - a config file of the name 'tr.cfg' in the working directory is used as default."); 199 System.exit(-1); 200 } 201 String confPath = (argv.length == 0) ? "tr.cfg" : argv[0]; 202 203 new Browser(confPath); 204 } 205 206 210 public void actionPerformed (ActionEvent e) { 211 if (e.getSource() == distributeButton || e.getSource() == obtainButton || 212 e.getSource() == distributeOfferButton || e.getSource() == obtainOfferButton) { 213 JTree sourceTR, destTR; 214 if (e.getSource() == distributeButton || e.getSource() == distributeOfferButton) { 215 sourceTR = leftTR; 216 destTR = rightTR; 217 } else { 218 sourceTR = rightTR; 219 destTR = leftTR; 220 } 221 222 HashMap from = new HashMap(); 223 TreePath[] selected = sourceTR.getSelectionPaths(); 224 if (selected != null) { 225 for (int i = 0; i < selected.length; i++) { 226 TreePath path = selected[i]; 227 DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) path.getLastPathComponent(); 228 Object o = treeNode.getUserObject(); 229 if (o instanceof SOFANode) { 230 SOFANode node = (SOFANode) o; 231 HashSet components = new HashSet(); 232 ComponentInfo[] infos = node.getComponents(); 233 for (int j = 0; j < infos.length; j++) { 234 ComponentInfo info = infos[j]; 235 components.add(info); 236 } 237 from.put(node, components); 238 } 239 if (o instanceof ComponentInfo) { 240 ComponentInfo info = (ComponentInfo) o; 241 DefaultMutableTreeNode treeNode2 = (DefaultMutableTreeNode) path.getParentPath().getLastPathComponent(); 242 SOFANode node = (SOFANode) treeNode2.getUserObject(); 243 HashSet components = (HashSet) from.get(node); 244 if (components == null) { 245 components = new HashSet(); 246 from.put(node, components); 247 } 248 components.add(info); } 250 } 251 } 252 HashSet to = new HashSet(); 253 selected = destTR.getSelectionPaths(); 254 if (selected != null) { 255 for (int i = 0; i < selected.length; i++) { 256 TreePath path = selected[i]; 257 DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) path.getLastPathComponent(); 258 Object o = treeNode.getUserObject(); 259 if (o instanceof SOFANode) { 260 SOFANode node = (SOFANode) o; 261 to.add(node); 262 } 263 } 264 } 265 if (e.getSource() == distributeButton) 266 new DistributeDialog(from, to, true, false); 267 else if (e.getSource() == distributeOfferButton) 268 new DistributeDialog(from, to, true, true); 269 else if (e.getSource() == obtainButton) 270 new DistributeDialog(from, to, false, false); 271 else if (e.getSource() == obtainOfferButton) 272 new DistributeDialog(from, to, false, true); 273 else 274 { 275 assert(true); 276 } 277 } 278 if (e.getSource() == refreshButton) { 279 DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot(); 280 int size = root.getChildCount(); 281 for (int i = 0; i < size; i++) { 282 DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) root.getChildAt(i); 283 SOFANode sofaNode = (SOFANode) treeNode.getUserObject(); 284 if (!sofaNode.isConnected()) { 285 sofaNode.reconnect(treeNode); 286 model.nodeChanged(treeNode); 287 } 288 if (sofaNode.isConnected()) { 289 sofaNode.list(treeNode); 290 model.nodeStructureChanged(treeNode); 291 } 292 } 293 } 294 295 } 296 297 } 298 | Popular Tags |