Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
MatrixTreeModelBus.java
1 
16 package net.squiz.matrix.matrixtree;
17 
18 import net.squiz.matrix.core.*;
19 import net.squiz.matrix.debug.*;
20 import java.util.*;
21 import net.squiz.matrix.inspector.*;
22 import javax.swing.tree.DefaultTreeModel;
23 
31 public class MatrixTreeModelBus {
32 
33  private static List models = new ArrayList();
34  private static DefaultTreeModel[] components;
35 
36  // cannot instantiate
37  private MatrixTreeModelBus() {}
38 
43  public static void addToBus(DefaultTreeModel model) {
44  synchronized(models) {
45  models.add(model);
46  components = null;
47  }
48  }
49 
50  public static void removeFromBus(DefaultTreeModel model) {
51  synchronized(models) {
52  models.remove(model);
53  components = null;
54  }
55  }
56 
57  public static DefaultTreeModel[] getBusComponents() {
58  synchronized(models) {
59  if (components == null) {
60  components = (DefaultTreeModel[]) models.toArray(
61  new DefaultTreeModel[models.size()]);
62  }
63  return components;
64  }
65  }
66 
67  public static void setRoot(MatrixTreeNode root) {
68  for (int i = 0; i < components.length; i++) {
69  ((DefaultTreeModel) components[i]).setRoot(root);
70  }
71  }
72 
73  public static void moveNode(
74  MatrixTreeNode child,
75  MatrixTreeNode newParent,
76  int index) {
77  // if the node hasn't moved, just return
78  if ((child.getParent() == newParent) && newParent.getIndex(child) == index)
79  return;
80 
81  removeNodeFromParent(child);
82  insertNodeInto(child, newParent, index);
83  }
84 
85  public static void insertNodeInto(
86  MatrixTreeNode newChild,
87  MatrixTreeNode parent,
88  int index) {
89 
90  try {
91  // we have to do this ourselves, as the node tree
92  // is indepenant from the tree models
93 
94  // we could have a previous node, make sure we insert the new node after it
95  if (parent.hasPreviousNode() && !(newChild instanceof ExpandingNextNode)) {
96  index++;
97  }
98  // if we are inserting this new node to the end, make sure we insert it before the ExpandingNextNode
99  // ref: BUG1666-2
100  if (parent.hasNextNode() && !(newChild instanceof ExpandingPreviousNode) && (index == parent.getChildCount()) && index != 0) {
101  index--;
102  }
103 
104  parent.insert(newChild, index);
105  int[] newIndexs = new int[1];
106  newIndexs[0] = index;
107 
108  DefaultTreeModel[] components = getBusComponents();
109 
110  for (int i = 0; i < components.length; i++) {
111  DefaultTreeModel model = components[i];
112  // only fire the event if the node is below the models
113  // current root
114  if (((MatrixTreeNode) model.getRoot()).isNodeDescendant(parent)) {
115  model.nodesWereInserted(parent, newIndexs);
116  }
117  }
118  } catch (Throwable t) {
119  Log.log("Could Not insert node", MatrixTreeModelBus.class, t);
120  }
121  }
122 
123  public static void removeNodeFromParent(MatrixTreeNode child) {
124  MatrixTreeNode parent = (MatrixTreeNode) child.getParent();
125  if (parent == null)
126  throw new IllegalArgumentException("node does not have a parent");
127  try {
129  MatrixTreeNode mParent = parent;
130  MatrixTreeNode mChild = child;
131 
132  //parent = getMirrorNode(parent);
133  // TODO: this is a temp hack to get this working
134  //if (parent.getIndex(child) == -1)
135  // child = getMirrorNode(child);
136 
137  // END TESTING
138  // we have to do this ourselves, as the node tree
139  // is indepenant from the tree models
140  int[] childIndex = new int[1];
141  Object[] removedArray = new Object[1];
142  childIndex[0] = parent.getIndex(child);
143  parent.remove(child);
144  removedArray[0] = child;
145 
146  DefaultTreeModel[] components = getBusComponents();
147 
148  for (int i = 0; i < components.length; i++) {
149  DefaultTreeModel model = components[i];
150  // only fire the event if the node is below the models
151  // current root
152  if (((MatrixTreeNode) model.getRoot()).isNodeDescendant(parent))
153  model.nodesWereRemoved(parent, childIndex, removedArray);
154  }
155  Log.log("removing from " + parent + "(" + child + ")", MatrixTreeModelBus.class);
156  child = null;
157  } catch (Throwable t) {
158  Log.log("Could not remove node" + t.getMessage(), MatrixTreeModelBus.class, t);
159  }
160  }
161 
162  private static MatrixTreeNode getMirrorNode(MatrixTreeNode parent) {
163  Asset parentAsset = AssetManager.getAsset(parent.getAsset().getId());
164  Iterator iterator = parentAsset.getTreeNodes();
165  MatrixTreeNode mirrorParent = null;
166 
167  while (iterator.hasNext()) {
168  MatrixTreeNode nextNode = (MatrixTreeNode) iterator.next();
169  if (nextNode == parent)
170  return parent;
171  if (nextNode.getLinkid().equals(parent.getLinkid())) {
172  mirrorParent = nextNode;
173  break;
174  }
175  }
176  return mirrorParent;
177  }
178 
179  public static void nodeChanged(MatrixTreeNode node) {
180 
181  DefaultTreeModel[] components = getBusComponents();
182  for (int i = 0; i < components.length; i++) {
183  DefaultTreeModel model = components[i];
184  // only fire the event if the node is below the models
185  // current root
186  if (((MatrixTreeNode) model.getRoot()).isNodeDescendant(node))
187  model.nodeChanged(node);
188  }
189  }
190 
191 }
192