Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TabCompleterResult.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2006-2011 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.ui.input;
  23. import com.dmdirc.config.IdentityManager;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Locale;
  27. /**
  28. * Represents the result set from a tab completion operation.
  29. */
  30. public final class TabCompleterResult {
  31. /**
  32. * The result list for this tab completer.
  33. */
  34. private final List<String> results;
  35. /**
  36. * Creates a new instance of TabCompleterResult with an empty result set.
  37. */
  38. public TabCompleterResult() {
  39. this.results = new ArrayList<String>();
  40. }
  41. /**
  42. * Creates a new instance of TabCompleterResult.
  43. * @param newResults The list of results that this result set contains
  44. */
  45. public TabCompleterResult(final List<String> newResults) {
  46. results = newResults;
  47. }
  48. /**
  49. * Adds a result to this result set.
  50. * @param result The result to be added
  51. */
  52. public void addResult(final String result) {
  53. results.add(result);
  54. }
  55. /**
  56. * Determines if this result set contains the specified result.
  57. * @param result The result to be tested
  58. * @return True if this set contains the specified result, false otherwise
  59. */
  60. public boolean hasResult(final String result) {
  61. return results.contains(result);
  62. }
  63. /**
  64. * Merges the specified additional results with this result set.
  65. * @param additional The results to merge
  66. */
  67. public void merge(final TabCompleterResult additional) {
  68. for (String result : additional.getResults()) {
  69. if (!hasResult(result)) {
  70. addResult(result);
  71. }
  72. }
  73. }
  74. /**
  75. * Gets the total size of this result set.
  76. * @return the size of this result set
  77. */
  78. public int getResultCount() {
  79. return results.size();
  80. }
  81. /**
  82. * Returns the longest substring that matches all results.
  83. * @return longest possible substring matching all results
  84. */
  85. public String getBestSubstring() {
  86. if (getResultCount() == 0) {
  87. return "";
  88. }
  89. final boolean caseSensitive = IdentityManager.getIdentityManager()
  90. .getGlobalConfiguration().getOptionBool("tabcompletion", "casesensitive");
  91. String res = results.get(0);
  92. for (String entry : results) {
  93. if (caseSensitive) {
  94. while (!entry.startsWith(res)) {
  95. res = res.substring(0, res.length() - 1);
  96. }
  97. } else {
  98. while (!entry.toLowerCase(Locale.getDefault()).startsWith(
  99. res.toLowerCase(Locale.getDefault()))) {
  100. res = res.substring(0, res.length() - 1);
  101. }
  102. }
  103. }
  104. return res;
  105. }
  106. /**
  107. * Retrieves the list of results that this set contains.
  108. * @return An arraylist containing the results
  109. */
  110. public List<String> getResults() {
  111. return results;
  112. }
  113. /** {@inheritDoc} */
  114. @Override
  115. public String toString() {
  116. final StringBuffer buff = new StringBuffer();
  117. for (String entry : results) {
  118. if (buff.length() > 0) {
  119. buff.append(", ");
  120. }
  121. buff.append(entry);
  122. }
  123. return buff.toString();
  124. }
  125. }