Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
MatrixTreeNode.java
1 
16 package net.squiz.matrix.matrixtree;
17 
18 import net.squiz.matrix.core.*;
19 import javax.swing.tree.*;
20 import java.awt.*;
21 import java.util.*;
22 import java.awt.datatransfer.*;
23 import java.io.*;
24 
32 public class MatrixTreeNode extends DefaultMutableTreeNode
33  implements Serializable {
34 
35 
36  /* The linkid that this node represents */
37  private final String linkid;
38 
43  private int linkType;
44 
45  private int sort_order;
46 
47  /* The URL including paths to this node */
48  private String url;
49  private String webPath;
50 
51  /* Name of node */
52  protected String name;
53 
61  public MatrixTreeNode(Asset asset, String linkid, int linkType, String url, String webPath, String name, int sort_order) {
62  setUserObject(asset);
63  this.linkid = linkid;
64  this.linkType = linkType;
65  this.url = url;
66  this.webPath = webPath;
67  this.name = name;
68  this.sort_order = sort_order;
69  }
70 
71  public String toString() {
72  return getName() + " Linkid : " + linkid;
73  }
74 
75  /* Each node can have different name, but belong to same asset*/
76  public void setName(String name) {
77  this.name = name;
78  }
79 
80  public String getName() {
81  return name;
82  }
83 
84  public void setSortOrder(int sort_order) {
85  this.sort_order = sort_order;
86  }
87 
88  public int getSortOrder() {
89  return this.sort_order;
90  }
91 
97  public Asset getAsset() {
98  return (Asset) getUserObject();
99  }
100 
101  public int getLinkType() {
102  return linkType;
103  }
104 
105  public void setLinkType(int linkType) {
106  this.linkType = linkType;
107  }
108 
114  public String getLinkid() {
115  return linkid;
116  }
117 
118  public boolean isShadowAsset() {
119  return (linkid.equals("0") && linkid.split(":").length > 1);
120  }
121 
127  public boolean isLeaf() {
128  // if the asset is not root and user has no access then make this asset a leaf node
129  if (!getAsset().getId().equals("1") && !getAsset().isAccessible()) {
130  return true;
131  }
132 
133  return (getAsset().getNumKids() == 0);
134  }
135 
141  public String getURL() {
142  if (url == null) {
143  return "";
144  } else if (webPath == null) {
145  return url;
146  }
147  return url + "/" + webPath;
148  }
149 
158  public boolean hasChildWithLinkid(String linkid) {
159  return (getChildWithLinkid(linkid) == null) ? false : true;
160  }
161 
162  public MatrixTreeNode getChildWithLinkid(String linkid) {
163  Enumeration children = children();
164  while (children.hasMoreElements()) {
165  MatrixTreeNode node = (MatrixTreeNode) children.nextElement();
166  if (node.getLinkid().equals(linkid))
167  return (MatrixTreeNode) node;
168  }
169  return null;
170  }
171 
172 
173  public void propagateUrl(String url) {
174  this.url = url;
175  Enumeration children = children();
176  while (children.hasMoreElements()) {
177  MatrixTreeNode node = (MatrixTreeNode) children.nextElement();
178  node.propagateUrl(getURL());
179  }
180  }
181 
182  public void propagateWebPath(String webPath) {
183  this.webPath = webPath;
184  Enumeration children = children();
185  while (children.hasMoreElements()) {
186  MatrixTreeNode node = (MatrixTreeNode) children.nextElement();
187  // from here on in, all the nodes under this particular node
188  // only have changed their urls as this node's url + "/" + webPath
189  // is its children's url
190  node.propagateUrl(getURL());
191  }
192  }
193 
201  public String getAssetPath() {
202  Object[] path = getPath();
203  StringBuffer assetPath = new StringBuffer();
204  for (int i = 0; i < path.length; i++) {
205  assetPath.append(",").append(((MatrixTreeNode) path[i]).getAsset().getId());
206  }
207  return assetPath.toString();
208  }
209 
217  public String getLinkPath() {
218  Object[] path = getPath();
219  StringBuffer linkPath = new StringBuffer();
220  for (int i = 0; i < path.length; i++) {
221  linkPath.append(",").append(((MatrixTreeNode) path[i]).getLinkid());
222  }
223  return linkPath.toString();
224  }
225 
229  public boolean hasPreviousNode() {
230  if (getChildCount() != 0) {
231  MatrixTreeNode node = (MatrixTreeNode)getChildAt(0);
232  if (node instanceof ExpandingPreviousNode) {
233  return true;
234  }
235  }
236  return false;
237  }
238 
242  public boolean hasNextNode() {
243  if (getChildCount() != 0) {
244  MatrixTreeNode node = (MatrixTreeNode)getChildAt(getChildCount()-1);
245  if (node instanceof ExpandingNextNode) {
246  return true;
247  }
248  }
249  return false;
250  }
251 
252 }
253