Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2006-2010 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.addons.tabcompletion_mirc;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.ui.input.AdditionalTabTargets;
  25. import com.dmdirc.ui.input.TabCompleter;
  26. import com.dmdirc.ui.input.TabCompleterResult;
  27. import com.dmdirc.ui.input.tabstyles.TabCompletionResult;
  28. import com.dmdirc.ui.input.tabstyles.TabCompletionStyle;
  29. import com.dmdirc.ui.interfaces.InputWindow;
  30. import java.awt.Toolkit;
  31. import java.util.List;
  32. public class MircStyle implements TabCompletionStyle {
  33. /** The last set of results we retrieved. */
  34. private List<String> lastResult;
  35. /** The last word that was tab completed. */
  36. private String lastWord;
  37. /** The tab completer that we use. */
  38. protected final TabCompleter tabCompleter;
  39. /** The input window that we use. */
  40. protected final InputWindow 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 InputWindow 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 AdditionalTabTargets additional) {
  55. final String word = original.substring(start, end);
  56. String target = "";
  57. if (word.equals(lastWord)) {
  58. // We're continuing to tab through
  59. target = lastResult.get((lastResult.indexOf(lastWord) + 1) % lastResult.size());
  60. } else {
  61. // New tab target
  62. final TabCompleterResult res = tabCompleter.complete(word, additional);
  63. if (res.getResultCount() == 0) {
  64. Toolkit.getDefaultToolkit().beep();
  65. return null;
  66. } else {
  67. if (word.length() > 0 && window.getContainer() instanceof Channel
  68. && ((Channel) window.getContainer())
  69. .getChannelInfo().getName().startsWith(word)) {
  70. target = ((Channel) window.getContainer()).getChannelInfo().getName();
  71. } else {
  72. target = res.getResults().get(0);
  73. }
  74. lastResult = res.getResults();
  75. }
  76. }
  77. lastWord = target;
  78. return new TabCompletionResult(original.substring(0, start) + target
  79. + original.substring(end), start + target.length());
  80. }
  81. }