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 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.ui.input;
  18. import com.dmdirc.commandparser.CommandType;
  19. import com.dmdirc.interfaces.CommandController;
  20. import com.dmdirc.config.provider.AggregateConfigProvider;
  21. import javax.inject.Inject;
  22. import javax.inject.Provider;
  23. import javax.inject.Singleton;
  24. /**
  25. * Factory for {@link TabCompleter}s which can inject the appropriate default tab completion
  26. * targets.
  27. */
  28. @Singleton
  29. public class TabCompleterFactory {
  30. /** The command controller to use to find commands. */
  31. private final Provider<CommandController> commandController;
  32. /**
  33. * Creates a new instance of {@link TabCompleterFactory}.
  34. *
  35. * @param commandController The command controller to use to find commands.
  36. */
  37. @Inject
  38. public TabCompleterFactory(
  39. final Provider<CommandController> commandController) {
  40. this.commandController = commandController;
  41. }
  42. /**
  43. * Gets a new root tab completer with the specified command types added as completion targets.
  44. *
  45. * @param configProvider The configuration provider to use for completion settings.
  46. * @param commandTypes The types of command to be added
  47. *
  48. * @return A new tab completer with the appropriate configuration.
  49. */
  50. public TabCompleter getTabCompleter(
  51. final AggregateConfigProvider configProvider,
  52. final CommandType... commandTypes) {
  53. final TabCompleter tabCompleter = new TabCompleterImpl(configProvider);
  54. addCommands(tabCompleter, commandTypes);
  55. return tabCompleter;
  56. }
  57. /**
  58. * Gets a new child tab completer with the specified additional command types added as
  59. * completion targets.
  60. *
  61. * @param parent The parent tab completer to inherit completions from.
  62. * @param configProvider The configuration provider to use for completion settings.
  63. * @param commandTypes The types of command to be added
  64. *
  65. * @return A new tab completer with the appropriate configuration.
  66. */
  67. public TabCompleter getTabCompleter(
  68. final TabCompleter parent,
  69. final AggregateConfigProvider configProvider,
  70. final CommandType... commandTypes) {
  71. final TabCompleter tabCompleter = new TabCompleterImpl(configProvider, parent);
  72. addCommands(tabCompleter, commandTypes);
  73. return tabCompleter;
  74. }
  75. /**
  76. * Adds all commands of the specified type to the given completer.
  77. *
  78. * @param tabCompleter The completer to add commands to.
  79. * @param commandTypes The types of command that should be added to the completer.
  80. */
  81. private void addCommands(final TabCompleter tabCompleter, final CommandType... commandTypes) {
  82. for (CommandType commandType : commandTypes) {
  83. tabCompleter.addEntries(TabCompletionType.COMMAND,
  84. commandController.get().getCommandNames(commandType));
  85. }
  86. }
  87. }