Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MircStyle.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.tabcompletion_mirc;
  23. import com.dmdirc.interfaces.GroupChat;
  24. import com.dmdirc.interfaces.WindowModel;
  25. import com.dmdirc.ui.input.AdditionalTabTargets;
  26. import com.dmdirc.ui.input.TabCompleter;
  27. import com.dmdirc.ui.input.TabCompletionMatches;
  28. import com.dmdirc.ui.input.tabstyles.TabCompletionResult;
  29. import com.dmdirc.ui.input.tabstyles.TabCompletionStyle;
  30. import com.google.common.collect.Lists;
  31. import java.awt.Toolkit;
  32. import java.util.Collections;
  33. import java.util.List;
  34. public class MircStyle implements TabCompletionStyle {
  35. /** The last word that was tab completed. */
  36. private String lastWord;
  37. /** The last string we tried to tab complete. */
  38. private String tabString;
  39. /** The tab completer that we use. */
  40. protected final TabCompleter tabCompleter;
  41. /** The input window that we use. */
  42. protected final WindowModel window;
  43. /**
  44. * Creates a new mIRC-style tab completer.
  45. *
  46. * @param completer The tab completer this style is for
  47. * @param window The window this tab style is for
  48. */
  49. public MircStyle(final TabCompleter completer, final WindowModel window) {
  50. this.tabCompleter = completer;
  51. this.window = window;
  52. }
  53. @Override
  54. public TabCompletionResult getResult(final String original, final int start,
  55. final int end, final boolean shiftPressed,
  56. final AdditionalTabTargets additional) {
  57. final String word = original.substring(start, end);
  58. final String target;
  59. if (word.equals(lastWord)) {
  60. final TabCompletionMatches res = tabCompleter.complete(tabString, additional);
  61. final List<String> results = Lists.newArrayList(res.getResults());
  62. Collections.sort(results, String.CASE_INSENSITIVE_ORDER);
  63. // We're continuing to tab through
  64. target = results.get((results.indexOf(lastWord) + (shiftPressed ? -1: 1) + results.size()) % results.size());
  65. } else {
  66. // New tab target
  67. final TabCompletionMatches res = tabCompleter.complete(word, additional);
  68. final List<String> results = Lists.newArrayList(res.getResults());
  69. if (res.getResultCount() == 0) {
  70. Toolkit.getDefaultToolkit().beep();
  71. return null;
  72. } else {
  73. Collections.sort(results, String.CASE_INSENSITIVE_ORDER);
  74. if (!word.isEmpty()
  75. && window instanceof GroupChat
  76. && window.getName().startsWith(word)) {
  77. target = window.getName();
  78. } else {
  79. target = results.get(0);
  80. }
  81. tabString = word;
  82. }
  83. }
  84. lastWord = target;
  85. return new TabCompletionResult(original.substring(0, start) + target
  86. + original.substring(end), start + target.length());
  87. }
  88. }