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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2006-2013 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.WritableFrameContainer;
  25. import com.dmdirc.ui.input.AdditionalTabTargets;
  26. import com.dmdirc.ui.input.TabCompleter;
  27. import com.dmdirc.ui.input.TabCompleterResult;
  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 WritableFrameContainer 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 WritableFrameContainer window) {
  48. this.tabCompleter = completer;
  49. this.window = window;
  50. }
  51. /** {@inheritDoc} */
  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. String target = "";
  58. if (word.equals(lastWord)) {
  59. final TabCompleterResult res = tabCompleter.complete(tabString, additional);
  60. Collections.sort(res.getResults(), String.CASE_INSENSITIVE_ORDER);
  61. // We're continuing to tab through
  62. target = res.getResults().get((res.getResults().indexOf(lastWord) +
  63. (shiftPressed ? -1 : 1) + res.getResults().size()) % res
  64. .getResults().size());
  65. } else {
  66. // New tab target
  67. final TabCompleterResult res = tabCompleter.complete(word, additional);
  68. if (res.getResultCount() == 0) {
  69. Toolkit.getDefaultToolkit().beep();
  70. return null;
  71. } else {
  72. Collections.sort(res.getResults(), String.CASE_INSENSITIVE_ORDER);
  73. if (word.length() > 0 && window instanceof Channel
  74. && ((Channel) window)
  75. .getChannelInfo().getName().startsWith(word)) {
  76. target = ((Channel) window).getChannelInfo().getName();
  77. } else {
  78. target = res.getResults().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. }