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.

MircStyle.java 3.9KB

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