20 package net.squiz.matrix.core;
22 import net.squiz.matrix.matrixtree.*;
23 import net.squiz.matrix.debug.*;
24 import javax.swing.tree.*;
28 import java.awt.Color;
39 protected final String id;
40 protected String initName =
"";
43 private boolean accessible;
44 private String url =
"";
45 private String webPath =
"";
46 private boolean childrenLoaded =
false;
47 private int numKids = 0;
48 private int totalKidsLoaded = 0;
49 protected Map nodes = null;
50 private int sort_order = 0;
54 public Asset(String
id) {
57 nodes =
new HashMap();
71 String linkid = processAssetXML(assetElement,
true);
72 createNode(linkid, getLinkType(assetElement), parent, index);
74 setLinkType(assetElement, linkid);
77 public void setTotalKidsLoaded(
int kidsLoaded) {
78 this.totalKidsLoaded = kidsLoaded;
81 public int getTotalKidsLoaded() {
82 return totalKidsLoaded;
85 public String toString() {
86 return "[" +
id +
"]";
106 return node.getName();
109 public boolean setName(String name, String linkid) {
110 if (linkid.equals(
"")) {
111 Iterator nodeIterator = getTreeNodes();
112 while (nodeIterator.hasNext()) {
121 MatrixTreeNode node = getNode(linkid);
123 String nodeName = node.getName();
124 if (nodeName != null) {
125 if (nodeName.equals(name)) {
136 public void addNode(MatrixTreeNode node, String linkid) {
137 nodes.put(linkid, node);
140 public MatrixTreeNode getNode(String linkid) {
141 if (nodes.containsKey(linkid)) {
142 return (MatrixTreeNode)nodes.get(linkid);
152 String[] linkIds =
new String[nodes.size()];
153 Iterator nodeIterator = getTreeNodes();
155 while (nodeIterator.hasNext()) {
165 public Iterator getTreeNodes() {
166 return nodes.values().iterator();
217 public void update(Element assetElement) {
218 processAssetXML(assetElement,
false);
221 public String processAssetXML(Element assetElement) {
222 String linkid = processAssetXML(assetElement,
false);
232 Element assetElement,
235 String linkid = processAssetXML(assetElement,
false);
237 return createNode(linkid, getLinkType(assetElement), parent, index);
238 return parent.getChildWithLinkid(linkid);
242 public boolean childrenLoaded() {
243 return childrenLoaded;
246 public void setChildrenLoaded(
boolean loaded) {
247 childrenLoaded = loaded;
268 Iterator nodes = getTreeNodes();
269 List wantedNodes = null;
270 while (nodes.hasNext()) {
274 if (wantedNodes == null)
275 wantedNodes =
new ArrayList();
276 wantedNodes.add(node);
279 if (wantedNodes == null) {
287 public void propagateNode(
Asset childAsset, String linkid,
int linkType,
int index) {
288 Iterator nodeIterator = getTreeNodes();
290 DefaultTreeModel[] components = MatrixTreeModelBus.getBusComponents();
292 while (nodeIterator.hasNext()) {
298 Enumeration children = node.children();
299 boolean childExists =
false;
300 while (children.hasMoreElements()) {
302 if (fellowChild.getName() == childAsset.
getName(linkid))
306 MatrixTreeNode child = node.getChildWithLinkid(linkid);
308 if (child == null || (AssetManager.isShadowAsset(childAsset) && !childExists)) {
310 }
else if (!AssetManager.isShadowAsset(childAsset)) {
314 if (node.getIndex(child) != index) {
315 MatrixTreeModelBus.moveNode(child, node, index);
321 public void propagateChildren(MatrixTreeNode node) {
322 Iterator nodes = getTreeNodes();
323 while (nodes.hasNext()) {
324 MatrixTreeNode nextNode = (MatrixTreeNode) nodes.next();
328 if (nextNode.getChildCount() > 0) {
329 Enumeration children = nextNode.children();
331 while (children.hasMoreElements()) {
332 MatrixTreeNode childNode
333 = (MatrixTreeNode) children.nextElement();
334 MatrixTreeNode newChild =
335 childNode.getAsset().createNode(
336 childNode.getLinkid(),
337 childNode.getLinkType(),
343 MatrixTree tree = MatrixTreeBus.getLastExpandedTree();
344 ((DefaultTreeModel) tree.getModel()).insertNodeInto(newChild, node, index);
387 private boolean setNodeSortOrder(String linkid,
int sort_order) {
389 boolean changed =
false;
390 for (
int i = 0; i < nodes.length; i++) {
391 if (nodes[i].getSortOrder() != sort_order) {
392 nodes[i].setSortOrder(sort_order);
402 protected String processAssetXML(Element assetElement,
boolean create) {
404 String linkid =
"", name =
"", typeCode =
"";
405 boolean accessible =
false;
406 int linkType = 0, status = 0, numKids = 0;
407 boolean hasLinkType =
false, hasStatus =
false, hasName =
false,
408 hasAccessible =
false, hasUrl =
false, hasWebPath =
false, hasSortOrder =
false,
409 hasNumKids =
false, hasTypeCode =
false;
410 AssetType type = this.type;
412 if (assetElement.hasAttribute(
"linkid"))
413 linkid = MatrixToolkit.rawUrlDecode(assetElement.getAttribute(
"linkid"));
419 if (assetElement.hasAttribute(
"type_code")) {
420 typeCode = assetElement.getAttribute(
"type_code");
421 type = AssetManager.getAssetType(typeCode);
424 if (assetElement.hasAttribute(
"name")) {
425 name = MatrixToolkit.rawUrlDecode(assetElement.getAttribute(
"name"));
429 if (assetElement.hasAttribute(
"link_type")) {
430 linkType = Integer.parseInt(assetElement.getAttribute(
"link_type"));
433 if (assetElement.hasAttribute(
"status")) {
434 status = Integer.parseInt(assetElement.getAttribute(
"status"));
437 if (assetElement.hasAttribute(
"num_kids")) {
438 numKids = Integer.parseInt(assetElement.getAttribute(
"num_kids"));
441 if (assetElement.hasAttribute(
"accessible")) {
442 accessible = assetElement.getAttribute(
"accessible").equals(
"1");
443 hasAccessible =
true;
445 if (assetElement.hasAttribute(
"url")) {
446 String url = assetElement.getAttribute(
"url");
449 if (assetElement.hasAttribute(
"web_path")) {
450 String webPath = assetElement.getAttribute(
"web_path");
453 if (assetElement.hasAttribute(
"sort_order")) {
454 this.sort_order = Integer.parseInt(assetElement.getAttribute(
"sort_order"));
458 }
catch (NumberFormatException exp) {
459 System.out.println(exp.getMessage());
463 this.initName = name;
464 this.accessible = accessible;
465 this.status = status;
467 this.webPath = webPath;
468 this.numKids = numKids;
469 this.type = AssetManager.getAssetType(typeCode);
471 this.initName = name;
472 boolean refresh =
false;
475 boolean linkTypeChanged = setLinkType(linkType, linkid);
478 Log.log(
"Link Type changed for asset " +
id,
Asset.class);
483 refresh |= hasName && setName(name, linkid);
484 refresh |= hasStatus && setStatus(status);
485 refresh |= hasLinkType && linkTypeChanged;
486 refresh |= hasAccessible && setAccessible(accessible);
487 refresh |= hasNumKids && setNumKids(numKids);
488 refresh |= hasTypeCode && setTypeCode(type);
489 refresh |= hasSortOrder && setNodeSortOrder(linkid, this.sort_order);
493 if (hasUrl && setUrl(url))
495 if (hasWebPath && setWebPath(webPath))
501 protected MatrixTreeNode createNode(
504 MatrixTreeNode parent,
506 MatrixTreeNode node = createNode(linkid, linkType, parent);
507 if (parent != null) {
508 MatrixTreeModelBus.insertNodeInto(node, parent, index);
531 addNode(node, linkid);
548 void removeDiffChildNodes(String[] linkids) {
549 Iterator nodeIterator = getTreeNodes();
550 List staleNodes = null;
551 boolean found =
false;
554 while (nodeIterator.hasNext()) {
556 Enumeration children = node.children();
560 while (children.hasMoreElements()) {
563 if (childNode instanceof ExpandingNextNode && (node.getChildCount() >=
AssetManager.getLimit())) {
565 }
else if (childNode instanceof ExpandingPreviousNode && (getTotalKidsLoaded() > 0 )) {
571 for (
int i = 0; i < linkids.length; i++) {
572 if (linkids[i].equals(linkid)) {
579 if (staleNodes == null)
580 staleNodes =
new ArrayList();
581 staleNodes.add(childNode);
587 if (staleNodes != null) {
588 Iterator staleIterator = staleNodes.iterator();
589 DefaultTreeModel[] components = MatrixTreeModelBus.getBusComponents();
590 while (staleIterator.hasNext()) {
591 MatrixTreeNode node = (MatrixTreeNode) staleIterator.next();
593 if (node instanceof ExpandingPreviousNode) {
595 MatrixTreeNode parent = (MatrixTreeNode)node.getParent();
596 parent.insert(node,0);
597 for (
int i = 0; i < components.length; i++) {
598 DefaultTreeModel model = components[i];
599 model.nodeStructureChanged(parent);
602 Log.log(
"removing " + node +
" in Asset",
Asset.class);
603 MatrixTreeModelBus.removeNodeFromParent(node);
609 void updateChildren() {}
614 private void nodesChanged() {
615 Iterator nodeIterator = getTreeNodes();
616 while (nodeIterator.hasNext()) {
617 MatrixTreeNode node = (MatrixTreeNode) nodeIterator.next();
620 boolean removedNext =
false;
621 boolean removedPrev =
false;
623 if (!node.hasNextNode() && node.getChildCount() >= AssetManager.getLimit() &&
getNumKids() > AssetManager.getLimit() && node.getParent() != null) {
626 MatrixTreeNode nextNode = (MatrixTreeNode)
new ExpandingNextNode(
getNumKids(), node.getChildCount(), getTotalKidsLoaded());
627 MatrixTreeModelBus.insertNodeInto((MatrixTreeNode) nextNode, node, node.getChildCount());
628 }
else if (node.hasNextNode() && (
getNumKids() <= AssetManager.getLimit())) {
632 MatrixTreeNode nextNode = (MatrixTreeNode) node.getChildAt(node.getChildCount()-1);
633 MatrixTreeModelBus.removeNodeFromParent(nextNode);
637 if (node.hasPreviousNode() && (
getNumKids() == 0)) {
638 MatrixTreeNode prevNode = (MatrixTreeNode) node.getChildAt(0);
639 MatrixTreeModelBus.removeNodeFromParent(prevNode);
644 if (node.hasPreviousNode() && !removedPrev) {
645 ExpandingNode prevNode = (ExpandingNode) node.getChildAt(0);
647 prevNode.setName(prevNode.getName());
648 MatrixTreeModelBus.nodeChanged(prevNode);
650 if (node.hasNextNode() && !removedNext) {
651 ExpandingNode nextNode = (ExpandingNode) node.getChildAt(node.getChildCount()-1);
653 nextNode.setName(nextNode.getName());
654 MatrixTreeModelBus.nodeChanged(nextNode);
657 MatrixTreeModelBus.nodeChanged(node);
661 private void updateUrls() {
662 Iterator nodeIterator = getTreeNodes();
663 while (nodeIterator.hasNext()) {
664 MatrixTreeNode node = (MatrixTreeNode) nodeIterator.next();
665 node.propagateUrl(url);
669 private void updateWebPaths() {
670 Iterator nodeIterator = getTreeNodes();
671 while (nodeIterator.hasNext()) {
672 MatrixTreeNode node = (MatrixTreeNode) nodeIterator.next();
673 node.propagateWebPath(webPath);
681 private String getNodeURL(MatrixTreeNode parent) {
685 if (url != null && (!url.equals(
"")))
687 else if (parent != null)
688 nodeUrl = parent.getURL();
692 private boolean setNumKids(
int count) {
693 if (numKids == count)
699 setTotalKidsLoaded(0);
705 private boolean setStatus(
int newStatus) {
706 if (status == newStatus)
712 private boolean setTypeCode(AssetType newTypeCode) {
713 if (type.equals(newTypeCode))
719 protected int getLinkType(Element assetElement) {
720 return Integer.parseInt(assetElement.getAttribute(
"link_type"));
723 private boolean setLinkType(Element assetElement, String linkid) {
724 return setLinkType(getLinkType(assetElement), linkid);
727 private boolean setLinkType(
int newLinkType, String linkid) {
729 boolean changed =
false;
730 for (
int i = 0; i < nodes.length; i++) {
731 if (nodes[i].getLinkType() != newLinkType) {
732 nodes[i].setLinkType(newLinkType);
740 if (accessible == isAccessible)
746 private boolean setUrl(String newUrl) {
747 if (url.equals(newUrl))
753 private boolean setWebPath(String newWebPath) {
754 if (webPath.equals(newWebPath))
756 webPath = newWebPath;