You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TreeViewModel.java 5.0KB

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