Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

TreeViewModel.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.framemanager.tree;
  23. import com.dmdirc.GlobalWindow;
  24. import com.dmdirc.addons.ui_swing.WindowComparator;
  25. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  26. import javax.swing.tree.DefaultTreeModel;
  27. import javax.swing.tree.MutableTreeNode;
  28. import javax.swing.tree.TreeNode;
  29. /**
  30. * A simple sorted tree data model based on DefaultTreeModel.
  31. */
  32. public class TreeViewModel extends DefaultTreeModel {
  33. /** A version number for this class. */
  34. private static final long serialVersionUID = 1;
  35. /** Window comparator. */
  36. private final WindowComparator comparator;
  37. /** Configuration provider to read settings from. */
  38. private final AggregateConfigProvider globalConfig;
  39. /**
  40. * Creates a tree in which any node can have children.
  41. *
  42. * @param globalConfig The configuration provider to read settings from.
  43. * @param root a TreeNode object that is the root of the tree.
  44. */
  45. public TreeViewModel(final AggregateConfigProvider globalConfig, final TreeNode root) {
  46. super(root, false);
  47. this.globalConfig = globalConfig;
  48. comparator = new WindowComparator();
  49. }
  50. /**
  51. * Inserts a new node into the tree and fires the appropriate events.
  52. *
  53. * @param newChild child to be added.
  54. * @param parent parent child is to be added too.
  55. */
  56. public final void insertNodeInto(final TreeViewNode newChild, final MutableTreeNode parent) {
  57. insertNodeInto(newChild, parent, getIndex(newChild, parent));
  58. }
  59. /**
  60. * Compares the new child with the existing children or parent to decide where it needs to be
  61. * inserted.
  62. *
  63. * @param newChild new node to be inserted.
  64. * @param parent node the new node will be inserted into.
  65. *
  66. * @return index where new node is to be inserted.
  67. */
  68. private int getIndex(final TreeViewNode newChild, final TreeNode parent) {
  69. if (newChild.getWindow().getContainer() instanceof GlobalWindow) {
  70. return 0;
  71. }
  72. if (parent.equals(root) && !globalConfig.getOptionBool("ui", "sortrootwindows")) {
  73. return parent.getChildCount();
  74. }
  75. if (globalConfig.getOptionBool("ui", "sortchildwindows")) {
  76. for (int i = 0; i < parent.getChildCount(); i++) {
  77. final TreeViewNode child = (TreeViewNode) parent.getChildAt(i);
  78. if (sortBefore(newChild, child)
  79. || !sortAfter(newChild, child)
  80. && newChild.getUserObject().toString().compareToIgnoreCase(
  81. child.getUserObject().toString()) < 0) {
  82. return i;
  83. }
  84. }
  85. }
  86. return parent.getChildCount();
  87. }
  88. /**
  89. * Compares the types of the specified nodes' objects to see if the new node should be sorted
  90. * before the other.
  91. *
  92. * @param newChild The new child to be tested
  93. * @param child The existing child that it's being tested against
  94. *
  95. * @return True iff newChild should be sorted before child
  96. */
  97. private boolean sortBefore(final TreeViewNode newChild, final TreeViewNode child) {
  98. return comparator.compare(newChild.getWindow(), child.getWindow()) <= -1;
  99. }
  100. /**
  101. * Compares the types of the specified nodes' objects to see if the new node should be sorted
  102. * after the other.
  103. *
  104. * @param newChild The new child to be tested
  105. * @param child The existing child that it's being tested against
  106. *
  107. * @return True iff newChild should be sorted before child
  108. */
  109. private boolean sortAfter(final TreeViewNode newChild, final TreeViewNode child) {
  110. return comparator.compare(newChild.getWindow(), child.getWindow()) >= 1;
  111. }
  112. /**
  113. * Returns the root node for this model.
  114. *
  115. * @return Root node
  116. */
  117. public TreeViewNode getRootNode() {
  118. return (TreeViewNode) getRoot();
  119. }
  120. }