Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
MatrixTreeBus.java
1 
16 package net.squiz.matrix.matrixtree;
17 
18 import java.util.*;
19 import java.awt.Color;
20 import javax.swing.tree.*;
21 import javax.swing.event.*;
22 import net.squiz.matrix.core.*;
23 
31 public class MatrixTreeBus {
32 
33  private static List trees = new ArrayList();
34  private static MatrixTree lastExpandedTree;
35  private static MatrixTree lastCollapsedTree;
36  private static MatrixTree activeTree;
37  private static FinderTree finderTree;
38  private static TreeExpansionHandler teHandler = new TreeExpansionHandler();
39  private static MatrixTreeCellRenderer cellRenderer = new MatrixTreeCellRenderer();
40  private static MatrixTreeComm comm = new MatrixTreeComm();
41  private static String[] restrictedTypes = new String[0];
42 
43  // cannot instantiate
44  private MatrixTreeBus() {}
45 
46  public static MatrixTree getLastExpandedTree() {
47  return lastExpandedTree;
48  }
49 
50  public static MatrixTree getLastCollapsedTree() {
51  return lastCollapsedTree;
52  }
53 
54  public static MatrixTreeCellRenderer getCellRenderer() {
55  return cellRenderer;
56  }
57 
58  private static class TreeExpansionHandler implements TreeWillExpandListener {
59  public void treeWillExpand(TreeExpansionEvent evt) {
60  lastExpandedTree = (MatrixTree) evt.getSource();
61  }
62 
63  public void treeWillCollapse(TreeExpansionEvent evt) {
64  lastCollapsedTree = (MatrixTree) evt.getSource();
65  }
66  }
67 
68  public static void setActiveTree(MatrixTree tree) {
69  activeTree = tree;
70  }
71 
72  public static MatrixTree getActiveTree() {
73  if (activeTree == null) {
74  if (trees.size() > 0) {
75  activeTree = (MatrixTree)trees.get(0);
76  } else {
77  return null;
78  }
79  }
80  return activeTree;
81  }
82 
83  public static void startAssetFinderMode(String[] assetTypes) {
84  restrictedTypes = assetTypes;
85  Iterator iterator = trees.iterator();
86  while (iterator.hasNext()) {
87  MatrixTree tree = (MatrixTree) iterator.next();
88  tree.startAssetFinderMode();
89  tree.repaint();
90  }
91  }
92 
93  public static void stopAssetFinderMode() {
94  restrictedTypes = new String[0];
95  Iterator iterator = trees.iterator();
96  while (iterator.hasNext()) {
97  MatrixTree tree = (MatrixTree) iterator.next();
98  tree.stopAssetFinderMode();
99  tree.repaint();
100  }
101  }
102 
103  public static void startAssetLocator(String[] assetIds, String[] sort_orders) {
104  MatrixTree tree = getActiveTree();
105  if (tree == null) {
106  if (trees.size() <= 0) {
107  return;
108  }
109  tree = (MatrixTree)trees.get(0);
110  }
111  tree.loadChildAssets(assetIds, sort_orders, true, false);
112  }
113 
114  public static void startSimpleAssetLocator(String[] assetIds, String[] sort_orders) {
115  // when we are in limbo/wysiwyg (simple asset map)
116  MatrixTree tree = (MatrixTree) finderTree;
117  if (tree == null) {
118  // if finder tree is not set, use the old behaviour
119  startAssetLocator(assetIds, sort_orders);
120  }
121  tree.loadChildAssets(assetIds, sort_orders, true, false);
122  }
123 
127  public static MatrixTree createTree(MatrixTreeNode root) {
128  DefaultTreeModel model = new DefaultTreeModel(root);
129  MatrixTree tree = new MatrixTree(model);
130  trees.add(tree);
131 
133  tree.setCellRenderer(new MatrixTreeCellRenderer());
134  tree.addTreeWillExpandListener(teHandler);
135  tree.addNewLinkListener(comm);
136  tree.addNewAssetListener(comm);
137  tree.setRootVisible(false);
138  tree.setShowsRootHandles(true);
139  // MM: need to add a listener for the tree to fire events to
140  // there only really needs to be one place listening to events, but
141  // we could always add another manually to the tree
142 
143  return tree;
144  }
145 
150  DefaultTreeModel model = new DefaultTreeModel(root);
151  FinderTree tree = new FinderTree(model);
152  trees.add(tree);
153 
155 
156  tree.setCellRenderer(new MatrixTreeCellRenderer());
157  tree.setRootVisible(false);
158  tree.setShowsRootHandles(true);
159 
160  //disable move, right click menu and delete key
161  tree.setMoveEnabled(false);
162  tree.removeKeyStroke("DELETE");
163 
164  // remember this finder tree for simple asset map
165  finderTree = tree;
166 
167  return tree;
168  }
169 
170  public static boolean typeIsRestricted(AssetType type) {
171  if (type == null) {
172  return false;
173  }
174  if (restrictedTypes.length == 0)
175  return true;
176  for (int i = 0; i < restrictedTypes.length; i++) {
177  if (type.isAncestor(AssetManager.getAssetType(restrictedTypes[i])))
178  return true;
179  }
180  return false;
181  }
182 
183  public static void destroyTree(MatrixTree tree) {
184  MatrixTreeModelBus.removeFromBus((DefaultTreeModel) tree.getModel());
185  trees.remove(tree);
186  tree = null;
187  }
188 
189  public static Iterator getTrees() {
190  return trees.iterator();
191  }
192 }