1 13 package info.magnolia.cms.gui.control; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.beans.config.Template; 17 import info.magnolia.cms.core.Content; 18 import info.magnolia.cms.core.ContentHandler; 19 import info.magnolia.cms.core.HierarchyManager; 20 import info.magnolia.cms.core.ItemType; 21 import info.magnolia.cms.core.MetaData; 22 import info.magnolia.cms.core.NodeData; 23 import info.magnolia.cms.core.Path; 24 import info.magnolia.cms.exchange.simple.Syndicator; 25 import info.magnolia.cms.gui.misc.Spacer; 26 import info.magnolia.cms.security.Authenticator; 27 import info.magnolia.cms.security.SessionAccessControl; 28 import info.magnolia.cms.util.MetaDataUtil; 29 import info.magnolia.cms.util.NodeDataUtil; 30 31 import java.util.ArrayList ; 32 import java.util.Collection ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 import javax.jcr.PropertyType; 37 import javax.jcr.RepositoryException; 38 import javax.jcr.Value; 39 import javax.servlet.http.HttpServletRequest ; 40 41 import org.apache.commons.lang.StringUtils; 42 import org.apache.log4j.Logger; 43 44 45 49 public class Tree extends ControlSuper { 50 51 public static final String DOCROOT = "/admindocroot/controls/tree/"; 53 public static final String ICONDOCROOT = "/admindocroot/icons/16/"; 56 public static final int ACTION_MOVE = 0; 57 58 public static final int ACTION_COPY = 1; 59 60 public static final int ACTION_ACTIVATE = 2; 61 62 public static final int ACTION_DEACTIVATE = 3; 63 64 public static final int PASTETYPE_ABOVE = 0; 65 66 public static final int PASTETYPE_BELOW = 1; 67 68 public static final int PASTETYPE_SUB = 2; 69 70 public static final int PASTETYPE_LAST = 3; 71 72 75 private static Logger log = Logger.getLogger(Tree.class); 76 77 private String repository; 78 79 private String pathOpen; 80 81 private String pathCurrent; 82 83 private String pathSelected; 84 85 private int indentionWidth = 15; 86 87 private List itemTypes = new ArrayList (); 88 89 private int height = 400; 90 91 private String iconPage = ICONDOCROOT + "document_plain_earth.gif"; 93 private String iconContentNode = ICONDOCROOT + "cubes.gif"; 95 private String iconNodeData = ICONDOCROOT + "cube_green.gif"; 97 private String iconOndblclick; 98 99 private String shifterExpand = DOCROOT + "shifter_EXPAND.gif"; 101 private String shifterCollaspe = DOCROOT + "shifter_COLLAPSE.gif"; 103 private String shifterEmpty = DOCROOT + "shifter_EMPTY.gif"; 105 private boolean drawShifter = true; 106 107 private String javascriptTree = "mgnlTreeControl"; 109 private List columns = new ArrayList (); 110 111 private ContextMenu menu; 113 114 private boolean snippetMode = true; 115 116 private String columnResizer = DOCROOT + "columnResizer.gif"; 118 private boolean browseMode; 119 120 126 public Tree(String name, String repository, HttpServletRequest request) { 127 this.setName(name); 128 this.setRepository(repository); 129 this.setRequest(request); 130 this.setMenu(new ContextMenu(this.getJavascriptTree())); 131 } 132 133 139 public Tree(String repository, HttpServletRequest request) { 140 this(repository, repository, request); 141 } 142 143 144 145 public void setRepository(String s) { 146 this.repository = s; 147 } 148 149 public String getRepository() { 150 return this.repository; 151 } 152 153 public void setPathOpen(String s) { 154 this.pathOpen = s; 155 } 156 157 public String getPathOpen() { 158 return this.pathOpen; 159 } 160 161 165 public void setPathSelected(String s) { 166 if (StringUtils.isNotEmpty(s)) { 167 this.setPathOpen(StringUtils.substringBeforeLast(s, "/")); } 169 this.pathSelected = s; 170 } 171 172 public String getPathSelected() { 173 return this.pathSelected; 174 } 175 176 public String getPath() { 177 if (super.getPath() != null) { 178 return super.getPath(); 179 } 180 181 return ("/"); } 183 184 protected void setPathCurrent(String s) { 185 this.pathCurrent = s; 186 } 187 188 protected String getPathCurrent() { 189 return this.pathCurrent; 190 } 191 192 public void setIndentionWidth(int i) { 193 this.indentionWidth = i; 194 } 195 196 public int getIndentionWidth() { 197 return this.indentionWidth; 198 } 199 200 public List getItemTypes() { 201 return this.itemTypes; 202 } 203 204 208 public void addItemType(String s) { 209 this.itemTypes.add(s); 210 } 211 212 216 public void addItemType(ItemType s) { 217 this.itemTypes.add(s.getSystemName()); 218 } 219 220 224 public void setIconPage(String src) { 225 this.iconPage = src; 226 } 227 228 public String getIconPage() { 229 return this.iconPage; 230 } 231 232 236 public void setIconContentNode(String src) { 237 this.iconContentNode = src; 238 } 239 240 public String getIconContentNode() { 241 return this.iconContentNode; 242 } 243 244 248 public void setIconNodeData(String src) { 249 this.iconNodeData = src; 250 } 251 252 public String getIconNodeData() { 253 return this.iconNodeData; 254 } 255 256 260 public void setIconOndblclick(String s) { 261 this.iconOndblclick = s; 262 } 263 264 public String getIconOndblclick() { 265 return this.iconOndblclick; 266 } 267 268 273 public void setShifterExpand(String src) { 274 this.shifterExpand = src; 275 } 276 277 public String getShifterExpand() { 278 return this.shifterExpand; 279 } 280 281 286 public void setShifterCollapse(String src) { 287 this.shifterCollaspe = src; 288 } 289 290 public String getShifterCollapse() { 291 return this.shifterCollaspe; 292 } 293 294 299 public void setShifterEmpty(String src) { 300 this.shifterEmpty = src; 301 } 302 303 public String getShifterEmpty() { 304 return this.shifterEmpty; 305 } 306 307 public void setDrawShifter(boolean b) { 308 this.drawShifter = b; 309 } 310 311 public boolean getDrawShifter() { 312 return this.drawShifter; 313 } 314 315 public void setHeight(int i) { 316 this.height = i; 317 } 318 319 public int getHeight() { 320 return this.height; 321 } 322 323 327 public void setColums(List al) { 328 this.columns = al; 329 } 330 331 public List getColumns() { 332 return this.columns; 333 } 334 335 public TreeColumn getColumns(int col) { 336 return (TreeColumn) this.getColumns().get(col); 337 } 338 339 public void addColumn(TreeColumn tc) { 340 this.getColumns().add(tc); 341 } 342 343 347 public void setJavascriptTree(String variableName) { 348 this.javascriptTree = variableName; 349 this.menu.setName(variableName + "Menu"); } 351 352 public String getJavascriptTree() { 353 return this.javascriptTree; 354 } 355 356 361 public void setSnippetMode(boolean b) { 362 this.snippetMode = b; 363 } 364 365 public boolean getSnippetMode() { 366 return this.snippetMode; 367 } 368 369 public void setColumnResizer(String src) { 371 this.columnResizer = src; 372 } 373 374 public String getColumnResizer() { 375 return this.columnResizer; 376 } 377 378 public void deleteNode(String parentPath, String label) { 379 try { 380 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository() ); 381 Content parentNode = hm.getContent(parentPath); 382 String path; 383 if (!parentPath.equals("/")) { path = parentPath + "/" + label; } 386 else { 387 path = "/" + label; } 389 this.deActivateNode(path); 390 parentNode.delete(label); 391 parentNode.save(); 392 } 393 catch (Exception e) { 394 log.debug("Exception caught: " + e.getMessage(), e); } 396 } 397 398 public void deleteNode(String path) { 399 try { 400 String parentPath = StringUtils.substringBeforeLast(path, "/"); String label = StringUtils.substringAfterLast(path, "/"); deleteNode(parentPath, label); 403 } 404 catch (Exception e) { 405 log.debug("Exception caught: " + e.getMessage(), e); } 407 } 408 409 public void createNode(String itemType) { 410 this.createNode("untitled", itemType); } 412 413 public void createNode(String label, String itemType) { 414 try { 415 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository()); 416 Content parentNode = hm.getContent(this.getPath()); 417 String slash = "/"; boolean isRoot = false; 419 if (this.getPath().equals("/")) { isRoot = true; 421 slash = StringUtils.EMPTY; 422 } 423 if (hm.isExist(this.getPath() + slash + label)) { 424 if (!isRoot) { 426 label = Path.getUniqueLabel(hm, this.getPath(), label); 427 } 428 else { 429 label = Path.getUniqueLabel(hm, StringUtils.EMPTY, label); 430 } 431 } 432 if (itemType.equals(ItemType.NT_NODEDATA)) { 433 parentNode.createNodeData(label); 434 } 435 else { 436 Content newNode; 437 if (itemType.equals(ItemType.CONTENT.getSystemName())) { 438 newNode = parentNode.createContent(label); 439 } 440 else { 441 newNode = parentNode.createContent(label, ItemType.CONTENTNODE); 442 } 443 newNode.getMetaData().setAuthorId(Authenticator.getUserId(this.getRequest())); 444 newNode.getMetaData().setCreationDate(); 445 newNode.getMetaData().setModificationDate(); 446 newNode.getMetaData().setSequencePosition(); 447 if (this.getRepository().equals(ContentRepository.WEBSITE) 450 && itemType.equals(ItemType.CONTENT.getSystemName())) { 451 Iterator templates = Template.getAvailableTemplates(SessionAccessControl.getAccessManager(this 452 .getRequest(), ContentRepository.CONFIG)); 453 while (templates.hasNext()) { 454 Template template = (Template) templates.next(); 455 newNode.getMetaData().setTemplate(template.getName()); 456 break; 457 } 458 } 459 } 460 parentNode.save(); 461 } 462 catch (Exception e) { 463 log.error(e.getMessage(), e); 464 } 465 } 466 467 public String saveNodeData(String nodeDataName, String value, boolean isMeta) { 468 String returnValue = StringUtils.EMPTY; 469 try { 470 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository() ); 471 Content page = hm.getContent(this.getPath()); 472 if (!isMeta) { 473 NodeData node; 474 int type = PropertyType.STRING; 475 if (!page.getNodeData(nodeDataName).isExist()) { 476 node = page.createNodeData(nodeDataName); 477 } 478 else { 479 node = page.getNodeData(nodeDataName); 480 type = node.getType(); 481 } 482 switch (type) { 484 case PropertyType.STRING: 485 node.setValue(value); 486 break; 487 case PropertyType.BOOLEAN: 488 if (value.equals("true")) { node.setValue(true); 490 } 491 else { 492 node.setValue(false); 493 } 494 break; 495 case PropertyType.DOUBLE: 496 try { 497 node.setValue(Double.valueOf(value).doubleValue()); 498 } 499 catch (Exception e) { 500 node.setValue(0); 501 } 502 break; 503 case PropertyType.LONG: 504 try { 505 node.setValue(Long.valueOf(value).longValue()); 506 } 507 catch (Exception e) { 508 node.setValue(0); 509 } 510 break; 511 case PropertyType.DATE: 512 break; 514 } 515 page.updateMetaData(this.getRequest()); 516 page.save(); 517 returnValue = new NodeDataUtil(node).getValueString(); 518 } 519 else { 520 page.getMetaData().setProperty(nodeDataName, value); 521 page.updateMetaData(this.getRequest()); 522 page.save(); 523 returnValue = new MetaDataUtil(page).getPropertyValueString(nodeDataName); 524 } 525 } 526 catch (Exception e) { 527 log.debug("Exception caught: " + e.getMessage(), e); } 529 return returnValue; 530 } 531 532 public String saveNodeDataType(String nodeDataName, int type) { 533 try { 534 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository()); 535 Content page = hm.getContent(this.getPath()); 536 Value value = null; 537 if (page.getNodeData(nodeDataName).isExist()) { 538 value = page.getNodeData(nodeDataName).getValue(); 539 page.deleteNodeData(nodeDataName); 540 } 541 NodeData node = page.createNodeData(nodeDataName); 542 if (value != null) { 543 switch (type) { 544 case PropertyType.STRING: 545 node.setValue(value.getString()); 546 break; 547 case PropertyType.BOOLEAN: 548 if (value != null && value.getBoolean()) { 549 node.setValue(true); 550 } 551 else { 552 node.setValue(false); 553 } 554 break; 555 case PropertyType.DOUBLE: 556 try { 557 node.setValue(value.getDouble()); 558 } 559 catch (Exception e) { 560 node.setValue(0); 561 } 562 break; 563 case PropertyType.LONG: 564 try { 565 node.setValue(value.getLong()); 566 } 567 catch (Exception e) { 568 node.setValue(0); 569 } 570 break; 571 case PropertyType.DATE: 572 break; 574 } 575 } 576 page.updateMetaData(this.getRequest()); 577 page.save(); 578 return PropertyType.nameFromValue(page.getNodeData(nodeDataName).getType()); 579 } 581 catch (Exception e) { 582 log.debug("Exception caught: " + e.getMessage(), e); } 584 return StringUtils.EMPTY; 585 } 586 587 public String pasteNode(String pathOrigin, String pathSelected, int pasteType, int action) { 588 boolean move = false; 597 if (action == ACTION_MOVE) { 598 move = true; 599 } 600 String label = StringUtils.substringAfterLast(pathOrigin, "/"); String slash = "/"; if (pathSelected.equals("/")) { slash = StringUtils.EMPTY; 604 } 605 String destination = pathSelected + slash + label; 606 if (pasteType == PASTETYPE_SUB && action != ACTION_COPY && destination.equals(pathOrigin)) { 607 pasteType = PASTETYPE_LAST; 609 } 610 if (pasteType == PASTETYPE_SUB) { 611 destination = pathSelected + slash + label; 612 Content touchedContent = this.copyMoveNode(pathOrigin, destination, move); 613 if (touchedContent == null) { 614 return StringUtils.EMPTY; 615 } 616 return touchedContent.getHandle(); 617 618 } 619 else if (pasteType == PASTETYPE_LAST) { 620 try { 622 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository()); 623 Content touchedContent = hm.getContent(pathOrigin); 624 touchedContent.getMetaData().setSequencePosition(); 625 return touchedContent.getHandle(); 626 } 627 catch (RepositoryException re) { 628 return StringUtils.EMPTY; 629 } 630 } 631 else { 632 try { 633 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository()); 634 636 String pathSelectedParent = StringUtils.substringBeforeLast(pathSelected, "/"); String pathOriginParent = StringUtils.substringBeforeLast(pathOrigin, "/"); if (StringUtils.isEmpty(pathSelectedParent)) { 639 slash = StringUtils.EMPTY; 640 pathSelectedParent = "/"; } 642 if (StringUtils.isEmpty(pathOriginParent)) { 643 pathOriginParent = "/"; } 645 Content touchedContent = null; 646 if (action == ACTION_COPY || !pathSelectedParent.equals(pathOriginParent)) { 649 destination = pathSelectedParent + slash + label; 650 touchedContent = this.copyMoveNode(pathOrigin, destination, move); 651 } 652 else { 653 touchedContent = hm.getContent(pathOrigin); 655 } 656 Content parentContent = hm.getContent(pathSelectedParent); 657 Content selectedContent = hm.getContent(pathSelected); 658 659 664 String selectedType = ItemType.NT_NODEDATA; 665 String touchedType = ItemType.NT_NODEDATA; 666 Iterator it1 = parentContent.getChildren(ItemType.CONTENT, ContentHandler.SORT_BY_SEQUENCE).iterator(); 667 while (it1.hasNext()) { 668 Content c = (Content) it1.next(); 669 if (c.getHandle().equals(selectedContent.getHandle())) { 670 selectedType = ItemType.CONTENT.getSystemName(); 671 } 672 if (c.getHandle().equals(touchedContent.getHandle())) { 673 touchedType = ItemType.CONTENT.getSystemName(); 674 } 675 } 676 Iterator it2 = parentContent 677 .getChildren(ItemType.CONTENTNODE, ContentHandler.SORT_BY_SEQUENCE) 678 .iterator(); 679 while (it2.hasNext()) { 680 Content c = (Content) it2.next(); 681 if (c.getHandle().equals(selectedContent.getHandle())) { 682 selectedType = ItemType.CONTENTNODE.getSystemName(); 683 } 684 if (c.getHandle().equals(touchedContent.getHandle())) { 685 touchedType = ItemType.CONTENTNODE.getSystemName(); 686 } 687 } 688 if (touchedType.equals(ItemType.NT_NODEDATA)) { 689 return StringUtils.EMPTY; } 691 long posSelected = selectedContent.getMetaData().getSequencePosition(); 692 long posAbove = 0; 693 long posBelow = 0; 694 long posFirst = 0; 695 Iterator it = parentContent.getChildren(touchedType, ContentHandler.SORT_BY_SEQUENCE).iterator(); 696 boolean first = true; 697 while (it.hasNext()) { 698 Content c = (Content) it.next(); 699 if (first) { 700 posFirst = c.getMetaData().getSequencePosition(); 701 first = false; 702 } 703 if (c.getHandle().equals(selectedContent.getHandle())) { 704 if (it.hasNext()) { 705 Content nextC = (Content) it.next(); 706 posBelow = nextC.getMetaData().getSequencePosition(); 707 } 708 break; 709 } 710 711 posAbove = c.getMetaData().getSequencePosition(); 712 713 } 714 if (!touchedType.equals(selectedType)) { 715 if (touchedType.equals(ItemType.CONTENTNODE.getSystemName()) 716 && selectedType.equals(ItemType.CONTENT.getSystemName())) { 717 pasteType = PASTETYPE_ABOVE; 720 posAbove = posFirst; 721 } 722 else { 723 pasteType = PASTETYPE_BELOW; 727 posBelow = 0; 728 } 729 } 730 long posTouched; 731 if (pasteType == PASTETYPE_ABOVE) { 732 if (posAbove == 0) { 733 posTouched = posSelected - (MetaData.SEQUENCE_POS_COEFFICIENT * 1000); } 735 else { 739 posTouched = (posAbove + posSelected) / 2; 740 } 741 } 742 else { 743 if (posBelow == 0) { 744 posTouched = 0; } 746 else { 747 posTouched = (posBelow + posSelected) / 2; 748 } 749 } 750 touchedContent.getMetaData().setSequencePosition(posTouched); 751 touchedContent.updateMetaData(this.getRequest()); 752 touchedContent.save(); 753 return touchedContent.getHandle(); 754 } 755 catch (RepositoryException re) { 756 return StringUtils.EMPTY; 757 } 758 } 759 } 760 761 public Content copyMoveNode(String source, String destination, boolean move) { 762 try { 764 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository()); 765 if (hm.isExist(destination)) { 766 String parentPath = StringUtils.substringBeforeLast(destination, "/"); String label = StringUtils.substringAfterLast(destination, "/"); label = Path.getUniqueLabel(hm, parentPath, label); 769 destination = parentPath + "/" + label; } 771 if (move) { 772 if (destination.indexOf(source + "/") == 0) { return null; 776 } 777 this.deActivateNode(source); 778 try { 779 hm.moveTo(source, destination); 780 } 781 catch (Exception e) { 782 return null; 784 } 785 } 786 else { 787 hm.copyTo(source, destination); 789 } 790 SessionAccessControl.invalidateUser(this.getRequest()); 791 Content newContent = hm.getContent(destination); 792 try { 793 newContent.updateMetaData(this.getRequest()); 794 newContent.getMetaData().setSequencePosition(); 795 newContent.getMetaData(MetaData.ACTIVATION_INFO).setUnActivated(); 796 } 797 catch (Exception e) { 798 log.debug("Exception caught: " + e.getMessage(), e); } 800 newContent.save(); 801 return newContent; 802 } 803 catch (Exception e) { 804 log.debug("Exception caught: " + e.getMessage(), e); } 806 return null; 807 } 808 809 public void moveNode(String source, String destination) { 810 this.copyMoveNode(source, destination, true); 811 } 812 813 public void copyNode(String source, String destination) { 814 this.copyMoveNode(source, destination, false); 815 } 816 817 public String renameNode(String newLabel) { 818 String returnValue = StringUtils.EMPTY; 819 try { 820 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository()); 821 String parentPath = StringUtils.substringBeforeLast(this.getPath(), "/"); newLabel = Path.getValidatedLabel(newLabel); 823 String dest = parentPath + "/" + newLabel; if (hm.isExist(dest)) { 825 newLabel = Path.getUniqueLabel(hm, parentPath, newLabel); 826 dest = parentPath + "/" + newLabel; } 828 829 this.deActivateNode(this.getPath()); 830 831 if (log.isInfoEnabled()) { 832 log.info("Moving node from " + this.getPath() + " to " + dest); } 834 if (hm.isNodeData(this.getPath())) { 835 Content parentPage = hm.getContent(parentPath); 836 NodeData newNodeData = parentPage.createNodeData(newLabel); 837 NodeData existingNodeData = hm.getNodeData(this.getPath()); 838 newNodeData.setValue(existingNodeData.getString()); 839 existingNodeData.delete(); 840 dest = parentPath; 841 } 842 else { 843 Content current = hm.getContent(this.getPath()); 846 Content parent = current.getParent(); 847 String placedBefore = null; 848 for (Iterator iter = parent.getChildren(current.getNodeType().getName()).iterator(); iter.hasNext();) { 849 Content child = (Content) iter.next(); 850 if (child.getHandle().equals(this.getPath())) { 851 if (iter.hasNext()) { 852 child = (Content) iter.next(); 853 placedBefore = child.getName(); 854 } 855 } 856 } 857 858 hm.moveTo(this.getPath(), dest); 859 860 if (placedBefore != null) { 862 parent.orderBefore(newLabel, placedBefore); 863 } 864 } 865 SessionAccessControl.invalidateUser(this.getRequest()); 866 Content newPage = hm.getContent(dest); 867 returnValue = newLabel; 868 newPage.updateMetaData(this.getRequest()); 869 newPage.save(); 870 } 871 catch (Exception e) { 872 log.debug("Exception caught: " + e.getMessage(), e); } 874 return returnValue; 875 } 876 877 public void activateNode(String path, boolean recursive) { 878 try { 880 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository()); 881 Content c = null; 882 if (hm.isPage(path)) { 883 c = hm.getContent(path); 884 } 885 else { 886 c = hm.getContent(path); 887 } 888 Syndicator syndicator = new Syndicator(this.getRequest()); 889 if (recursive) { 890 deepActivate(syndicator, c, hm); 891 } 892 else { 893 syndicator.activate(this.getRepository(), StringUtils.EMPTY, path, recursive); 894 } 895 } 896 catch (Exception e) { 897 log.error(e.getMessage(), e); 898 } 899 } 900 901 private void deepActivate(Syndicator syndicator, Content content, HierarchyManager hm) { 902 try { 903 syndicator.activate(this.getRepository(), StringUtils.EMPTY, content.getHandle(), false); 904 Collection children = content.getChildren(); 905 if (children != null) { 906 Iterator it = children.iterator(); 907 while (it.hasNext()) { 908 deepActivate(syndicator, (Content) it.next(), hm); 909 } 910 } 911 } 912 catch (Exception e) { 913 log.error(e.getMessage(), e); 914 } 915 } 916 917 public void deActivateNode(String path) { 918 try { 919 Syndicator syndicator = new Syndicator(this.getRequest()); 920 syndicator.deActivate(this.getRepository(), path); 921 } 922 catch (Exception e) { 923 log.debug("Exception caught: " + e.getMessage(), e); } 925 } 926 927 public String getHtml() { 928 StringBuffer html = new StringBuffer (); 929 if (!this.getSnippetMode()) { 930 html.append(this.getHtmlHeader()); 931 } 932 this.setPathCurrent(this.getPath()); 933 html.append(this.getHtmlChildren()); 934 if (!this.getSnippetMode()) { 935 html.append(this.getHtmlFooter()); 936 } 937 return html.toString(); 938 } 939 940 public String getHtmlHeader() { 941 942 StringBuffer html = new StringBuffer (); 943 944 html.append("<style type=\"text/css\">"); int numberOfColumns = this.getColumns().size(); 948 if (numberOfColumns == 0) { 949 numberOfColumns = 1; 950 } 951 for (int i = 0; i < numberOfColumns; i++) { 952 html.append("." + this.getJavascriptTree() + "CssClassColumn" + i); html.append("\n{position:absolute;left:0px;clip:rect(0 0 100 0);cursor:default;}\n"); } 955 html.append("</style>\n\n"); 957 html.append("<div id=\"" + this.getJavascriptTree() + "_ColumnResizerDiv\" style=\"position:absolute;top:-50px;z-index:500;\">"); for (int i = 1; i < this.getColumns().size(); i++) { 961 html.append("<div onmousedown=\"" + this.getJavascriptTree() + ".dragColumnStart(this," + i + ");\" id=\"" + this.getJavascriptTree() + "ColumnResizer" + i + "\" style=\"position:relative;left:-1000px;background-image:url(" + this.getRequest().getContextPath() + this.getColumnResizer() + ");display:inline;\">"); html.append("<img SRC=\"" + this.getRequest().getContextPath() 972 + this.getColumnResizer() 973 + "\" alt=\"\" style=\"visibility:hidden;\" />"); html.append("</div>"); } 976 html.append("</div>"); html.append("<div id=\"" + this.getJavascriptTree() + "_ColumnHeader\" style=\"position:absolute;top:-50px;z-index:480;\">"); for (int i = 0; i < this.getColumns().size(); i++) { 981 TreeColumn tc = this.getColumns(i); 982 html.append("<span class=\"mgnlTreeColumn " + this.getJavascriptTree() + "CssClassColumn" + i + "\"><span class=\"mgnlTreeHeader\">" + tc.getTitle() + "<!-- ie --></span></span>"); } 987 html.append("</div>"); 989 html.append("<div id=\"" + this.getJavascriptTree() 991 + "_ColumnResizerLine\" style=\"position:absolute;top:0px;left:-100px;visibility:hidden;width:1px;height:" + this.getHeight() 993 + "px;background-color:#333333;z-index:490;\"></div>"); html.append("<div id=\"" + this.getJavascriptTree() + "_" + this.getPath() + "_DivMain\" onclick=\"" + this.getJavascriptTree() + ".mainDivReset();\" oncontextmenu=\"" + this.getJavascriptTree() + ".menuShow(event);return false;\" class=\"mgnlTreeDiv\" style=\"height:" + this.getHeight() + "px;\">"); html.append(Spacer.getHtml(8, 8)); 1001 html.append("<div id=\"" + this.getJavascriptTree() + "_" + this.getPath() + "_DivSub\">"); return html.toString(); 1004 } 1005 1006 public String getHtmlFooter() { 1007 StringBuffer html = new StringBuffer (); 1008 html.append("</div>"); boolean permissionWrite = true; 1010 try { 1011 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository()); 1012 Content root = hm.getContent(this.getPath()); 1013 permissionWrite = root.isGranted(info.magnolia.cms.security.Permission.WRITE); 1014 } 1015 catch (RepositoryException e) { 1016 log.debug("Exception caught: " + e.getMessage(), e); } 1018 1019 String lineId = this.getJavascriptTree() + "_" + this.getPath() + "_LineInter"; 1023 html.append("<div id=\"" + lineId + "\" class=\"mgnlTreeLineInter mgnlLineEnabled\" onmouseover=\"" + this.javascriptTree + ".moveNodeHighlightLine('" + lineId + "');\" onmouseout=\"" + this.javascriptTree + ".moveNodeResetLine('" + lineId + "');\" onmousedown=\"" + this.javascriptTree + ".pasteNode('" + this.getPath() + "'," + Tree.PASTETYPE_SUB + "," + Boolean.toString(permissionWrite) + ",'" + lineId + "');\" ></div>"); 1035 html.append(new Hidden(this.getJavascriptTree() + "_" + this.getPath() + "_PermissionWrite", Boolean .toString(permissionWrite), false).getHtml()); 1037 html.append("</div>"); String pathOpen = this.getPathOpen(); 1040 if (pathOpen == null) { 1041 pathOpen = StringUtils.EMPTY; 1042 } 1043 1044 html.append(Spacer.getHtml(3, 3)); 1045 html.append("\n\n\n\n\n\n\n\n<input id=\"" + this.getJavascriptTree() 1047 + "AddressBar\" type=\"text\" onkeydown=\"if (mgnlIsKeyEnter(event)) " + this.getJavascriptTree() 1049 + ".expandNode(this.value);\" class=\"mgnlDialogControlEdit\" style=\"width:100%;\" value=\"" + pathOpen 1051 + "\" />\n\n\n\n"); 1053 html.append("<div id=\""); html.append(this.getJavascriptTree()); 1056 html.append("_MoveShadow\" "); html.append("style=\"position:absolute;top:0px;left:0px;visibility:hidden;background-color:#fff;\"></div>"); 1059 html.append("<img SRC=\"" + this.getRequest().getContextPath() 1062 + Tree.DOCROOT 1063 + "move_denied.gif\" id=\"" + this.getJavascriptTree() 1065 + "_MoveDenied\" style=\"position:absolute;top:0px;left:0px;visibility:hidden;\" />"); html.append("<script type=\"text/javascript\">"); html.append("var " + this.getJavascriptTree() + "=new mgnlTree('" + this.getRepository() + "','" + this.getPath() + "','" + this.getJavascriptTree() + "'," + this.getHeight() + "," + "'" + this.getName() + "'," 1075 + this.isBrowseMode() + ");"); 1077 for (int i = 0; i < this.getColumns().size(); i++) { 1079 TreeColumn tc = this.getColumns(i); 1080 html.append(this.getJavascriptTree() + ".columns[" + i + "]=new mgnlTreeColumn(" + tc.getWidth() + ",'" + tc.getHtmlEdit() + "','" + tc.getName() + "'," + tc.getIsMeta() + "," + tc.getIsLabel() + "," + tc.getIsNodeDataValue() + "," + tc.getIsNodeDataType() + ");"); } 1090 html.append("mgnlTreeControls['" + this.getJavascriptTree() + "']=" + this.getJavascriptTree() + ";\n"); html.append(this.getJavascriptTree() + ".selectNode('" + this.getPathSelected() + "');"); html.append("</script>"); 1095 if (menu.getMenuItems().size() != 0) { 1097 html.append(menu.getHtml()); 1098 } 1099 1100 html.append("<script>" + this.getJavascriptTree() + ".menu = " + menu.getName() + "</script>"); 1103 return html.toString(); 1104 } 1105 1106 public String getHtmlBranch() { 1107 return StringUtils.EMPTY; 1108 } 1109 1110 public String getHtmlChildren() { 1111 StringBuffer html = new StringBuffer (); 1112 HierarchyManager hm = SessionAccessControl.getHierarchyManager(this.getRequest(), this.getRepository()); 1113 Content parentNode = null; 1114 try { 1115 parentNode = hm.getContent(this.getPathCurrent()); 1116 for (int i = 0; i < this.getItemTypes().size(); i++) { 1118 String type = (String ) this.getItemTypes().get(i); 1119 html.append(this.getHtmlChildrenOfOneType(parentNode, type)); 1120 } 1121 } 1122 catch (RepositoryException e) { 1123 log.debug("Exception caught: " + e.getMessage(), e); } 1125 return html.toString(); 1126 } 1127 1128 public String getHtmlChildrenOfOneType(Content parentNode, String itemType) { 1129 StringBuffer html = new StringBuffer (); 1130 try { 1131 int left = (parentNode.getLevel()) * this.getIndentionWidth(); 1133 Iterator it; 1134 if (itemType.equalsIgnoreCase(ItemType.NT_NODEDATA)) { 1135 it = parentNode.getNodeDataCollection().iterator(); 1136 } 1137 else { 1138 it = parentNode.getChildren(itemType, ContentHandler.SORT_BY_SEQUENCE).iterator(); 1139 } 1140 while (it.hasNext()) { 1141 Object o = it.next(); 1142 Content c = null; 1143 NodeData d = null; 1144 String handle = StringUtils.EMPTY; 1145 String name = StringUtils.EMPTY; 1146 boolean hasSub = false; 1147 boolean showSub = false; 1148 boolean isActivated = false; 1149 boolean permissionWrite = false; 1150 boolean permissionWriteParent = false; 1151 if (itemType.equals(ItemType.NT_NODEDATA)) { 1152 d = (NodeData) o; 1153 handle = d.getHandle(); 1154 name = d.getName(); 1155 1156 if (d.isGranted(info.magnolia.cms.security.Permission.WRITE)) { 1157 permissionWrite = true; 1158 } 1159 } 1160 else { 1161 c = (Content) o; 1162 1163 handle = c.getHandle(); 1164 if (this.getColumns().size() == 0) { 1165 name = c.getName(); 1166 } 1167 else { 1168 this.getColumns(0).setWebsiteNode(c); 1169 name = this.getColumns(0).getHtml(); 1170 } 1171 if (c.isGranted(info.magnolia.cms.security.Permission.WRITE)) { 1172 permissionWrite = true; 1173 } 1174 if (c.getAncestor(c.getLevel() - 1).isGranted(info.magnolia.cms.security.Permission.WRITE)) { 1175 permissionWriteParent = true; 1176 } 1177 isActivated = c.getMetaData(MetaData.ACTIVATION_INFO).getIsActivated(); 1178 for (int i = 0; i < this.getItemTypes().size(); i++) { 1179 String type = (String ) this.getItemTypes().get(i); 1180 int size = 0; 1181 if (type.equalsIgnoreCase(ItemType.NT_NODEDATA)) { 1182 size = c.getNodeDataCollection().size(); 1183 } 1184 else { 1185 size = c.getChildren(type).size(); 1186 } 1187 if (size > 0) { 1188 hasSub = true; 1189 if (this.getPathOpen() != null 1190 && (this.getPathOpen().indexOf(handle + "/") == 0 || this.getPathOpen().equals(handle))) { showSub = true; 1192 } 1193 break; 1194 } 1195 } 1196 } 1197 1198 if(!showNode(c, d, itemType)){ 1200 continue; 1201 } 1202 1203 String icon = getIcon(c, d, itemType); 1204 1205 String idPre = this.javascriptTree + "_" + handle; String jsHighlightNode = this.javascriptTree + ".nodeHighlight(this,'" + handle + "'," + Boolean.toString(permissionWrite) + ");"; String jsResetNode = this.javascriptTree + ".nodeReset(this,'" + handle + "');"; String jsSelectNode = this.javascriptTree + ".selectNode('" + handle + "'," + Boolean.toString(permissionWrite) + ",'" + itemType + "');"; String jsExpandNode; 1215 if (this.getDrawShifter()) { 1216 jsExpandNode = this.javascriptTree + ".expandNode('" + handle + "');"; } 1218 else { 1219 jsExpandNode = jsSelectNode; 1220 } 1221 String jsHighlightLine = this.javascriptTree + ".moveNodeHighlightLine('" + idPre + "_LineInter');"; String jsResetLine = this.javascriptTree + ".moveNodeResetLine('" + idPre + "_LineInter');"; 1224 int maskWidth = left; 1225 if (maskWidth < 1) { 1226 maskWidth = 1; 1227 } 1228 1229 if (permissionWriteParent) { 1232 html.append("<div id=\"" + idPre + "_LineInter\" class=\"mgnlTreeLineInter mgnlLineEnabled\" onmouseover=\"" + jsHighlightLine + "\" onmouseout=\"" + jsResetLine + "\" onmousedown=\"" + this.javascriptTree + ".pasteNode('" + handle + "'," + Tree.PASTETYPE_ABOVE + ",true);\" ></div>"); } 1240 else { 1241 html.append("<div id=\"" + idPre + "_LineInter\" class=\"mgnlTreeLineInter mgnlLineDisabled\"></div>"); } 1244 1245 html.append("<div id=\"" + idPre + "_DivMain\" style=\"position:relative;top:0;left:0;width:100%;height:18px;\">"); html.append(" "); int paddingLeft = left; 1249 if (paddingLeft < 0) { 1250 paddingLeft = 0; 1251 } 1252 html.append("<span id=\"" + idPre + "_Column0Outer\" class=\"mgnlTreeColumn " + this.javascriptTree + "CssClassColumn0\" style=\"padding-left:" + paddingLeft + "px;\">"); if (this.getDrawShifter()) { 1257 String shifter = StringUtils.EMPTY; 1258 if (hasSub) { 1259 if (showSub) { 1260 if (this.getShifterCollapse() != null) { 1261 shifter = this.getShifterCollapse(); 1262 } 1263 } 1264 else { 1265 if (this.getShifterExpand() != null) { 1266 shifter = this.getShifterExpand(); 1267 } 1268 } 1269 } 1270 else { 1271 if (this.getShifterEmpty() != null) { 1272 shifter = this.getShifterEmpty(); 1273 } 1274 } 1275 if (StringUtils.isNotEmpty(shifter)) { 1276 html.append("<img id=\"" + idPre + "_Shifter\" onmousedown=\"" + this.javascriptTree + ".shifterDown('" + handle + "');\" onmouseout=\"" + this.javascriptTree + ".shifterOut();\" class=\"mgnlTreeShifter\" SRC=\"" + this.getRequest().getContextPath() + shifter + "\" />"); } 1283 } 1284 html.append("<span id=" + idPre + "_Name onmouseover=\"" + jsHighlightNode + "\" onmouseout=\"" + jsResetNode + "\" onmousedown=\"" + jsSelectNode + this.javascriptTree + ".pasteNode('" + handle + "'," + Tree.PASTETYPE_SUB + "," + permissionWrite + ");\">"); if (StringUtils.isNotEmpty(icon)) { 1293 html.append("<img id=\"" + idPre + "_Icon\" class=\"mgnlTreeIcon\" SRC=\"" + this.getRequest().getContextPath() + icon + "\" onmousedown=\"" + jsExpandNode + "\""); if (this.getIconOndblclick() != null) { 1298 html.append(" ondblclick=\"" + this.getIconOndblclick() + "\""); } 1300 html.append(" />"); } 1302 String dblclick = StringUtils.EMPTY; 1303 if (permissionWrite && StringUtils.isNotEmpty(this.getColumns(0).getHtmlEdit())) { 1304 dblclick = " ondblclick=\"" + this.javascriptTree + ".editNodeData(this,'" + handle + "',0);\""; } 1306 html.append("<span class=\"mgnlTreeText\" id=\"" + idPre + "_Column0Main\"" + dblclick + ">"); html.append(name); 1308 html.append("</span></span></span>"); html.append(new Hidden(idPre + "_PermissionWrite", Boolean.toString(permissionWrite), false).getHtml()); html.append(new Hidden(idPre + "_ItemType", itemType, false).getHtml()); html.append(new Hidden(idPre + "_IsActivated", Boolean.toString(isActivated), false).getHtml()); for (int i = 1; i < this.getColumns().size(); i++) { 1313 String str = StringUtils.EMPTY; 1314 TreeColumn tc = this.getColumns(i); 1315 if (!itemType.equals(ItemType.NT_NODEDATA)) { 1316 if (!tc.getIsNodeDataType() && !tc.getIsNodeDataValue()) { 1318 tc.setWebsiteNode(c); 1319 tc.setId(handle); 1320 str = tc.getHtml(); 1321 } 1322 } 1323 else { 1324 NodeDataUtil util = new NodeDataUtil(d); 1325 if (tc.getIsNodeDataType()) { 1326 str = util.getTypeName(d.getType()); 1327 } 1328 else if (tc.getIsNodeDataValue()) { 1329 str = util.getValueString(); 1330 } 1331 if (StringUtils.isEmpty(str)) { 1332 str = TreeColumn.EMPTY; 1333 } 1334 tc.setName(name); } 1336 tc.setEvent("onmouseover", jsHighlightNode, true); tc.setEvent("onmouseout", jsResetNode, true); tc.setEvent("onmousedown", jsSelectNode, true); html.append("<span class=\"mgnlTreeColumn " + this.javascriptTree + "CssClassColumn" + i + "\"><span id=\"" + idPre + "_Column" + i + "Main\"" + tc.getHtmlCssClass() + tc.getHtmlEvents()); 1345 if (permissionWrite && StringUtils.isNotEmpty(tc.getHtmlEdit())) { 1346 html.append(" ondblclick=\"" + this.javascriptTree + ".editNodeData(this,'" + handle + "'," + i + ");\""); } 1351 html.append(">" + str + "</span></span>"); } 1353 html.append("</div>"); String display = "none"; if (showSub) { 1356 display = "block"; } 1358 html.append("<div id=\"" + idPre + "_DivSub\" style=\"display:" + display + ";\">"); if (hasSub) { 1360 if (showSub) { 1361 String pathRemaining = this.getPathOpen().substring(this.getPathCurrent().length()); 1362 if (pathRemaining.length() > 0) { 1363 String slash = "/"; if (this.getPathCurrent().equals("/")) { slash = StringUtils.EMPTY; } 1369 else { 1370 pathRemaining = pathRemaining.substring(1); 1371 } 1372 String nextChunk = StringUtils.substringBefore(pathRemaining, "/"); 1374 String pathNext = this.getPathCurrent() + slash + nextChunk; 1375 this.setPathCurrent(pathNext); 1376 html.append(this.getHtmlChildren()); 1377 } 1378 } 1379 } 1380 html.append("</div>\n"); } 1382 } 1383 catch (RepositoryException e) { 1384 log.debug("Exception caught: " + e.getMessage(), e); } 1386 return html.toString(); 1387 } 1388 1389 1390 1397 private boolean showNode(Content node, NodeData nodedata, String itemType) { 1398 return true; 1399 } 1400 1401 1408 protected String getIcon(Content node, NodeData nodedata, String itemType) { 1409 String icon = null; 1410 if (itemType.equals(ItemType.CONTENT.getSystemName())) { 1411 icon = this.getIconPage(); 1412 } 1413 else if (itemType.equals(ItemType.CONTENTNODE.getSystemName())) { 1414 icon = this.getIconContentNode(); 1415 } 1416 else if (itemType.equals(ItemType.NT_NODEDATA)) { 1417 icon = this.getIconNodeData(); 1418 } 1419 return icon; 1420 } 1421 1422 1425 public void addMenuItem(ContextMenuItem item) { 1426 menu.addMenuItem(item); 1427 } 1428 1429 1432 public void addSeparator() { 1433 menu.addMenuItem(null); 1434 } 1435 1436 protected ContextMenu getMenu() { 1437 return this.menu; 1438 } 1439 1440 protected void setMenu(ContextMenu menu) { 1441 this.menu = menu; 1442 } 1443 1444 1445 1448 public boolean isBrowseMode() { 1449 return browseMode; 1450 } 1451 1452 1453 1456 public void setBrowseMode(boolean browseMode) { 1457 this.browseMode = browseMode; 1458 } 1459 1460} 1461 | Popular Tags |