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ů.

MircStyle.java 3.9KB

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