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.

TabCompleterFactory.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.ui.input;
  23. import com.dmdirc.commandparser.CommandType;
  24. import com.dmdirc.interfaces.CommandController;
  25. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  26. import javax.inject.Inject;
  27. import javax.inject.Provider;
  28. import javax.inject.Singleton;
  29. /**
  30. * Factory for {@link TabCompleter}s which can inject the appropriate default tab completion
  31. * targets.
  32. */
  33. @Singleton
  34. public class TabCompleterFactory {
  35. /** The command controller to use to find commands. */
  36. private final Provider<CommandController> commandController;
  37. /**
  38. * Creates a new instance of {@link TabCompleterFactory}.
  39. *
  40. * @param commandController The command controller to use to find commands.
  41. */
  42. @Inject
  43. public TabCompleterFactory(
  44. final Provider<CommandController> commandController) {
  45. this.commandController = commandController;
  46. }
  47. /**
  48. * Gets a new root tab completer with the specified command types added as completion targets.
  49. *
  50. * @param configProvider The configuration provider to use for completion settings.
  51. * @param commandTypes The types of command to be added
  52. *
  53. * @return A new tab completer with the appropriate configuration.
  54. */
  55. public TabCompleter getTabCompleter(
  56. final AggregateConfigProvider configProvider,
  57. final CommandType... commandTypes) {
  58. final TabCompleter tabCompleter = new TabCompleterImpl(configProvider);
  59. addCommands(tabCompleter, commandTypes);
  60. return tabCompleter;
  61. }
  62. /**
  63. * Gets a new child tab completer with the specified additional command types added as
  64. * completion targets.
  65. *
  66. * @param parent The parent tab completer to inherit completions from.
  67. * @param configProvider The configuration provider to use for completion settings.
  68. * @param commandTypes The types of command to be added
  69. *
  70. * @return A new tab completer with the appropriate configuration.
  71. */
  72. public TabCompleter getTabCompleter(
  73. final TabCompleter parent,
  74. final AggregateConfigProvider configProvider,
  75. final CommandType... commandTypes) {
  76. final TabCompleter tabCompleter = new TabCompleterImpl(configProvider, parent);
  77. addCommands(tabCompleter, commandTypes);
  78. return tabCompleter;
  79. }
  80. /**
  81. * Adds all commands of the specified type to the given completer.
  82. *
  83. * @param tabCompleter The completer to add commands to.
  84. * @param commandTypes The types of command that should be added to the completer.
  85. */
  86. private void addCommands(final TabCompleter tabCompleter, final CommandType... commandTypes) {
  87. for (CommandType commandType : commandTypes) {
  88. tabCompleter.addEntries(TabCompletionType.COMMAND,
  89. commandController.get().getCommandNames(commandType));
  90. }
  91. }
  92. }