- Timestamp:
- 07/08/13 23:04:56 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/gui/TreeAtFrame.java
r97 r98 4 4 5 5 import com.framsticks.core.Tree; 6 import com.framsticks.core.TreeListener;7 import com.framsticks.core.ListChange;8 6 import com.framsticks.core.Node; 9 7 import com.framsticks.core.Path; 8 import com.framsticks.core.TreeOperations; 9 import com.framsticks.gui.controls.ValueControl; 10 10 import com.framsticks.params.CompositeParam; 11 11 import com.framsticks.params.FramsClass; … … 14 14 15 15 import javax.swing.tree.TreePath; 16 import com.framsticks.util.dispatching.RunAt; 16 17 18 import com.framsticks.util.dispatching.FutureHandler; 19 import com.framsticks.util.lang.Casting; 17 20 18 21 /** 19 22 * @author Piotr Sniegowski 20 23 */ 21 public class TreeAtFrame implements TreeListener{24 public class TreeAtFrame { 22 25 23 26 private static final Logger log = Logger.getLogger(TreeAtFrame.class); … … 26 29 protected final Tree tree; 27 30 protected final Map<String, Panel> knownPanels = new HashMap<String, Panel>(); 28 protected TreeNode rootTreeNode; 31 protected Node rootNode; 32 33 protected Map<TreeNode, NodeAtFrame> nodesStorage = new WeakHashMap<>(); 29 34 30 35 public TreeAtFrame(Tree tree, Frame frame) { … … 90 95 } 91 96 92 @Override 93 public void onListChange(Path path, ListChange change) { 97 public boolean hasLocalChanges(TreePath treePath) { 98 NodeAtFrame nodeAtFrame = nodesStorage.get(treePath.getLastPathComponent()); 99 if (nodeAtFrame == null) { 100 return false; 101 } 102 return !nodeAtFrame.localChanges.isEmpty(); 103 } 104 105 public NodeAtFrame assureLocalInfo(TreePath treePath) { 106 assert frame.isActive(); 107 NodeAtFrame nodeAtFrame = nodesStorage.get(treePath.getLastPathComponent()); 108 109 if (nodeAtFrame == null) { 110 nodeAtFrame = new NodeAtFrame(); 111 nodesStorage.put(Casting.throwCast(TreeNode.class, treePath.getLastPathComponent()), nodeAtFrame); 112 } 113 return nodeAtFrame; 114 } 115 116 public NodeAtFrame getLocalInfo(TreePath treePath) { 117 return nodesStorage.get(treePath.getLastPathComponent()); 118 } 119 120 public boolean changeValue(TreePath treePath, ValueControl component, Object newValue) { 121 log.debug("changing value of " + component + " to '" + newValue + "'"); 122 123 assureLocalInfo(treePath).localChanges.put(component, newValue); 124 125 return true; 126 } 127 128 public void pushLocalChanges(TreePath treePath) { 129 assert frame.isActive(); 130 131 NodeAtFrame nodeAtFrame = nodesStorage.get(treePath.getLastPathComponent()); 132 if (nodeAtFrame == null) { 133 return; 134 } 135 Path path = frame.treeModel.convertToPath(treePath); 136 137 for (Map.Entry<ValueControl, Object> e : nodeAtFrame.localChanges.entrySet()) { 138 TreeOperations.set(path, e.getKey().getParam(), e.getValue(), new FutureHandler<Integer>(frame) { 139 @Override 140 protected void result(Integer flag) { 141 } 142 }); 143 } 144 } 145 146 public void fillPanelWithValues(TreePath treePath) { 147 NodeAtFrame nodeAtFrame = assureLocalInfo(treePath); 148 if (nodeAtFrame == null) { 149 return; 150 } 151 152 if (nodeAtFrame.panel == null) { 153 return; 154 } 155 Node node = TreeNode.tryGetNode(treePath); 156 if (node == null) { 157 return; 158 } 159 nodeAtFrame.panel.setCurrentTreePath(treePath); 160 nodeAtFrame.panel.pullValuesFromLocalToUser(TreeOperations.bindAccess(node)); 161 162 frame.showPanel(nodeAtFrame.panel); 94 163 95 164 } 96 165 97 public TreePath getTreePath(Path path, boolean create) { 98 assert frame.isActive(); 99 TreeNode t = rootTreeNode; 100 TreePath result = new TreePath(frame.rootNode).pathByAddingChild(rootTreeNode); 101 List<Node> nodes = path.getNodes(); 102 Iterator<Node> i = nodes.iterator(); 103 i.next(); 104 // Node first = i.next(); 166 public void useOrCreatePanel(TreePath treePath) { 167 // node.assureResolved(); 168 Node node = TreeNode.tryGetNode(treePath); 105 169 106 // if (!t.path.isResolved()) { 107 // t.path = new Path(path.getInstance(), nodes, first); 108 // } 109 while (i.hasNext()) { 110 Node n = i.next(); 111 TreeNode r = null; 112 for (TreeNode c : t.childrenIterable()) { 113 if (c.param.getId().equals(n.getParam().getId())) { 114 r = c; 115 break; 116 } 170 NodeAtFrame nodeAtFrame = assureLocalInfo(treePath); 171 172 if (nodeAtFrame.panel == null) { 173 CompositeParam param = node.getParam(); 174 nodeAtFrame.panel = findPanel(param.computeAccessId()); 175 if (nodeAtFrame.panel == null) { 176 FramsClass framsClass = node.getTree().getInfoFromCache(param.getContainedTypeName()); 177 nodeAtFrame.panel = preparePanel(param, framsClass); 117 178 } 118 if (r == null) {119 log.debug("missing " + n.getParam().getId() + " in " + t);120 if (!create) {121 return result;122 }123 Path p = Path.build().tree(path.getTree()).buildUpTo(nodes, n).finish();124 125 126 log.debug("forced resolution: creating treenode for " + p);127 TreeNode childNode = new TreeNode(TreeAtFrame.this, p);128 129 frame.addNode(childNode, t);130 // frame.treeModel.reload();131 // t.add(childNode);132 // frame.treeModel.nodeStructureChanged(t);133 134 r = childNode;135 } else {136 // if (!r.path.isResolved()) {137 // r.path = new Path(path.getInstance(), nodes, n);138 // }139 }140 result = result.pathByAddingChild(r);141 t = r;142 179 } 143 return result; 144 } 145 146 @Override 147 public void onFetch(final Path path) { 148 assert tree.isActive(); 149 log.trace("fetched " + path); 150 151 frame.dispatch(new RunAt<Frame>(frame) { 152 @Override 153 protected void runAt() { 154 155 TreePath treePath = getTreePath(path, true); 156 assert treePath.getPathCount() == path.size() + 1; 157 158 final TreeNode result = (TreeNode) treePath.getLastPathComponent(); 159 // log.trace("found " + result + " == " + path); 160 tree.dispatch(new RunAt<Tree>(frame) { 161 @Override 162 protected void runAt() { 163 result.reactForFetchResult(path, null); 164 } 165 }); 166 } 167 }); 168 } 169 170 @Override 171 public void onRun(Exception e) { 172 173 } 174 175 @Override 176 public void onStop(Exception e) { 177 180 fillPanelWithValues(treePath); 178 181 } 179 182 }
Note: See TracChangeset
for help on using the changeset viewer.