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.

TabCompleter.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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 java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Locale;
  26. import com.dmdirc.Config;
  27. /**
  28. * The tab completer handles a user's request to tab complete some word.
  29. * @author chris
  30. */
  31. public final class TabCompleter {
  32. /**
  33. * The parent TabCompleter. Results from parents are merged with results
  34. * from this completer.
  35. */
  36. private TabCompleter parent;
  37. /**
  38. * The entries in this completer.
  39. */
  40. private List<String> entries = new ArrayList<String>();
  41. /** Creates a new instance of TabCompleter. */
  42. public TabCompleter() {
  43. //Do nothing.
  44. }
  45. /**
  46. * Creates a new instance of TabCompleter, with a designated parent.
  47. * @param newParent The parent TabCompleter, which is checked for matches if
  48. * local ones fail
  49. */
  50. public TabCompleter(final TabCompleter newParent) {
  51. this.parent = newParent;
  52. }
  53. /**
  54. * Attempts to complete the partial string.
  55. * @param partial The string to tab complete
  56. * @return A TabCompleterResult containing any matches found
  57. */
  58. public TabCompleterResult complete(final String partial) {
  59. final TabCompleterResult result = new TabCompleterResult();
  60. for (String entry : entries) {
  61. if (Config.getOptionBool("tabcompletion", "casesensitive")) {
  62. if (entry.startsWith(partial)) {
  63. result.addResult(entry);
  64. }
  65. } else {
  66. if (entry.toLowerCase(Locale.getDefault())
  67. .startsWith(partial.toLowerCase(Locale.getDefault()))) {
  68. result.addResult(entry);
  69. }
  70. }
  71. }
  72. if (parent != null) {
  73. result.merge(parent.complete(partial));
  74. }
  75. return result;
  76. }
  77. /**
  78. * Adds a new entry to this tab completer's list.
  79. * @param entry The new entry to be added
  80. */
  81. public void addEntry(final String entry) {
  82. entries.add(entry);
  83. }
  84. /**
  85. * Adds multiple new entries to this tab completer's list.
  86. * @param newEntries Entries to be added
  87. */
  88. public void addEntries(final List<String> newEntries) {
  89. if (newEntries == null) {
  90. return;
  91. }
  92. for (String entry : newEntries) {
  93. addEntry(entry);
  94. }
  95. }
  96. /**
  97. * Removes a specified entry from this tab completer's list.
  98. * @param entry The entry to be removed
  99. */
  100. public void removeEntry(final String entry) {
  101. entries.remove(entry);
  102. }
  103. /**
  104. * Replaces the current entries with the new list.
  105. * @param newEntries the new entries to use
  106. */
  107. public void replaceEntries(final List<String> newEntries) {
  108. entries = newEntries;
  109. }
  110. }