Thread: [Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/treeview CameraTreeView.java, 1.2, 1.3
Status: Pre-Alpha
Brought to you by:
henryml
From: Nordholt <nor...@us...> - 2006-06-16 10:00:16
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/treeview In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv3869/treeview Modified Files: CameraTreeView.java Log Message: added clipplanes as camera children. Made selection stay across updates. Added responsablity of changing camera, and implemented imuteable default cameras Index: CameraTreeView.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/treeview/CameraTreeView.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** CameraTreeView.java 23 Jan 2006 10:16:26 -0000 1.2 --- CameraTreeView.java 16 Jun 2006 09:51:06 -0000 1.3 *************** *** 10,16 **** --- 10,25 ---- import java.util.Collection; import java.util.Iterator; + import java.util.Enumeration; + import java.util.ArrayList; + + import javax.swing.event.TreeSelectionEvent; + import javax.swing.event.TreeSelectionListener; + import javax.swing.tree.TreePath; + import javax.swing.tree.DefaultMutableTreeNode; import net.sourceforge.bprocessor.model.Camera; import net.sourceforge.bprocessor.model.Project; + import net.sourceforge.bprocessor.model.Entity; + /** *************** *** 18,21 **** --- 27,32 ---- */ public class CameraTreeView extends GenericTreeView { + /** The selected camera */ + private Camera selected; /** *************** *** 24,27 **** --- 35,40 ---- public CameraTreeView() { super(); + this.addTreeSelectionListener(new SelectionListener()); + selected = null; } *************** *** 30,33 **** --- 43,49 ---- */ public void update() { + selected = findSelectedCamera(); + Enumeration e = getExpandedDescendants(new TreePath(root)); + Object[] paths = findPaths(e); root.removeAllChildren(); Collection cameras = Project.getInstance().getCameras(); *************** *** 35,41 **** while (iter.hasNext()) { Camera current = (Camera) iter.next(); ! root.add(new CameraNode(current)); } model.nodeStructureChanged(root); } } --- 51,188 ---- while (iter.hasNext()) { Camera current = (Camera) iter.next(); ! CameraNode cn = new CameraNode(current); ! Iterator clipIt = current.getClipplanes().iterator(); ! while (clipIt.hasNext()) { ! cn.add(new EntityNode((Entity)clipIt.next())); ! } ! root.add(cn); } model.nodeStructureChanged(root); + openPaths(paths); + selectCamera(selected); + } + + /** + * Selects a given camera in the tree + * @param cam the camera to select + */ + private void selectCamera(Camera cam) { + Enumeration en = root.children(); + while (en.hasMoreElements()) { + Object o = en.nextElement(); + if (o instanceof CameraNode) { + CameraNode cn = (CameraNode)o; + if (cn.getUserObject() == cam) { + selectionModel.clearSelection(); + selectionModel.addSelectionPath(new TreePath(new Object[] {root, cn})); + break; + } + } + } + } + + /** + * Finds the currently selected Camera + * @return the selected camera if any else null + */ + private Camera findSelectedCamera() { + Camera selected = null; + if (selectionModel.getSelectionPath() != null) { + Object o = selectionModel.getSelectionPath().getLastPathComponent(); + if (o instanceof DefaultMutableTreeNode) { + DefaultMutableTreeNode node = (DefaultMutableTreeNode)o; + if (node.getUserObject() instanceof Camera) { + selected = (Camera)node.getUserObject(); + } + } + } + return selected; + } + + /** + * Find the paths + * @param e and enumerator of open nodes + * @return And array of arrays of objects + */ + private Object[] findPaths(Enumeration e) { + if (e != null) { + ArrayList res = new ArrayList(); + while (e.hasMoreElements()) { + Object o = e.nextElement(); + if (o instanceof TreePath) { + TreePath tp = (TreePath)o; + Object[] os = tp.getPath(); + Object[] temp = new Object[os.length]; + for (int i = 0; i < os.length; i++) { + temp[i] = ((DefaultMutableTreeNode)os[i]).getUserObject(); + } + res.add(temp); + } + } + return res.toArray(); + } + return null; + } + + /** + * Open the paths of the given objects + * @param paths The open paths + */ + public void openPaths(Object[] paths) { + if (paths != null) { + for (int i = 0; i < paths.length; i++) { + Object[] route = (Object[])paths[i]; + if (route.length > 1) { + openPath(root, 1, route); + } + } + } + } + + /** + * Open one path in the tree + * @param depth The depth reached in this call (< route.length) + * @param node The node to check in + * @param route The path to open + */ + private void openPath(DefaultMutableTreeNode node, int depth, Object[] route) { + Enumeration e = node.children(); + while (e.hasMoreElements()) { + DefaultMutableTreeNode current = (DefaultMutableTreeNode)e.nextElement(); + if (current.getUserObject() == route[depth]) { + expandPath(new TreePath(current.getPath())); + if (depth + 1 < route.length) { + openPath(current, depth + 1, route); + } + } + } + } + + /** + * Selection Listener + */ + private class SelectionListener implements TreeSelectionListener { + /** + * ValueChanged + * @param event The Event + */ + public void valueChanged(TreeSelectionEvent event) { + if (event.isAddedPath()) { + Object object = event.getPath().getLastPathComponent(); + if (object instanceof DefaultMutableTreeNode) { + DefaultMutableTreeNode node = (DefaultMutableTreeNode) object; + Object target = node.getUserObject(); + if (target instanceof Camera && target != selected) { + selected = (Camera)target; + if (Project.getInstance().getDefaultCameras().contains(target)) { + Project.getInstance().setCurrentCamera(new Camera(selected, + "default copy")); + } else { + Project.getInstance().setCurrentCamera(selected); + } + } + } + } + } } } |