1 package jspecview.tree;
2 
3 import javajs.util.Lst;
4 
5 import jspecview.api.JSVPanel;
6 import jspecview.api.JSVTree;
7 import jspecview.api.JSVTreeNode;
8 import jspecview.api.JSVTreePath;
9 import jspecview.api.ScriptInterface;
10 import jspecview.common.JSVFileManager;
11 import jspecview.common.PanelNode;
12 import jspecview.common.JSViewer;
13 import jspecview.source.JDXSource;
14 
15 public class SimpleTree implements JSVTree {
16 
17 	protected ScriptInterface si;
18   private JSVTreeNode rootNode;
19   private SimpleTreeModel spectraTreeModel;
20 	protected JSViewer vwr;
21 	private SimpleTreePath selectedPath;
22 
23 	@Override
getRootNode()24 	public JSVTreeNode getRootNode() {
25 		return rootNode;
26 	}
27 
SimpleTree(JSViewer viewer)28 	public SimpleTree(JSViewer viewer) {
29 		this.vwr = viewer;
30     rootNode = new SimpleTreeNode("Spectra", null);
31     spectraTreeModel = new SimpleTreeModel(rootNode);
32 	}
33 
valueChanged()34   public void valueChanged() {
35   	vwr.selectedTreeNode(getLastSelectedPathComponent());
36   }
37 
getLastSelectedPathComponent()38   private JSVTreeNode getLastSelectedPathComponent() {
39   	return (JSVTreeNode) (selectedPath == null ? null : selectedPath.getLastPathComponent());
40 	}
41 
42 	@Override
setSelectedPanel(ScriptInterface si, JSVPanel jsvp)43 	public void setSelectedPanel(ScriptInterface si, JSVPanel jsvp) {
44 		if (jsvp != null) {
45 			JSVTreeNode treeNode = PanelNode.findNode(jsvp, vwr.panelNodes).treeNode;
46 			setSelectionPath(vwr.spectraTree.newTreePath(treeNode.getPath()));
47 		}
48 	}
49 
setSelectionPath(JSVTreePath newTreePath)50 	private void setSelectionPath(JSVTreePath newTreePath) {
51 		selectedPath = (SimpleTreePath) newTreePath;
52 		valueChanged();
53 	}
54 
55 	@Override
createTree(int fileCount, JDXSource source, JSVPanel[] panels)56 	public JSVTreeNode createTree(int fileCount, JDXSource source, JSVPanel[] panels) {
57 		SimpleTree tree = (SimpleTree) vwr.spectraTree;
58 		JSVTreeNode rootNode = tree.getRootNode();
59 		Lst<PanelNode> panelNodes = vwr.panelNodes;
60 
61 		String fileName = JSVFileManager.getTagName(source.getFilePath());
62 		PanelNode panelNode = new PanelNode(null, fileName, source, null);
63 		JSVTreeNode fileNode = new SimpleTreeNode(fileName, panelNode);
64 		panelNode.setTreeNode(fileNode);
65 		tree.spectraTreeModel.insertNodeInto(fileNode, rootNode,
66 				rootNode.getChildCount());
67 		// tree.scrollPathToVisible(new JsTreePath(fileNode.getPath()));
68 
69 		for (int i = 0; i < panels.length; i++) {
70 			JSVPanel jsvp = panels[i];
71 			String id = fileCount + "." + (i + 1);
72       panelNode = new PanelNode(id, fileName, source, jsvp);
73 			JSVTreeNode treeNode = new SimpleTreeNode(panelNode.toString(), panelNode);
74 			panelNode.setTreeNode(treeNode);
75 			panelNodes.addLast(panelNode);
76 			tree.spectraTreeModel.insertNodeInto(treeNode, fileNode,
77 					fileNode.getChildCount());
78 			// tree.scrollPathToVisible(new TreePath(treeNode.getPath()));
79 		}
80 		vwr.selectFrameNode(panels[0]);
81 		return fileNode;
82 	}
83 
84 	@Override
setPath(JSVTreePath path)85 	public void setPath(JSVTreePath path) {
86 		setSelectionPath(path);
87 	}
88 
89 	@Override
newTreePath(Object[] path)90 	public JSVTreePath newTreePath(Object[] path) {
91 		return new SimpleTreePath(path);
92 	}
93 
94 	@Override
deleteNodes(Lst<JSVTreeNode> toDelete)95 	public void deleteNodes(Lst<JSVTreeNode> toDelete) {
96 	  for (int i = 0; i < toDelete.size(); i++) {
97 	  	spectraTreeModel.removeNodeFromParent(toDelete.get(i));
98 	  }
99 
100 	}
101 
102 }
103