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.

QueryCommandParser.java 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.commandparser.parsers;
  18. import com.dmdirc.commandparser.CommandArguments;
  19. import com.dmdirc.commandparser.CommandInfo;
  20. import com.dmdirc.commandparser.CommandType;
  21. import com.dmdirc.commandparser.commands.Command;
  22. import com.dmdirc.commandparser.commands.context.CommandContext;
  23. import com.dmdirc.commandparser.commands.context.QueryCommandContext;
  24. import com.dmdirc.interfaces.CommandController;
  25. import com.dmdirc.events.eventbus.EventBus;
  26. import com.dmdirc.interfaces.PrivateChat;
  27. import com.dmdirc.interfaces.WindowModel;
  28. import javax.annotation.Nonnull;
  29. /**
  30. * A command parser that is tailored for use in a query environment. Handles both query and server
  31. * commands.
  32. */
  33. public class QueryCommandParser extends ChatCommandParser {
  34. /** A version number for this class. */
  35. private static final long serialVersionUID = 1;
  36. /**
  37. * The query instance that this parser is attached to.
  38. */
  39. private final PrivateChat query;
  40. /**
  41. * Creates a new instance of QueryCommandParser.
  42. *
  43. * @param owner The container this parser's query belongs to
  44. * @param commandController The controller to load commands from.
  45. * @param eventBus Event bus to post events on
  46. */
  47. public QueryCommandParser(final WindowModel owner, final CommandController commandController,
  48. final EventBus eventBus, final PrivateChat query) {
  49. super(owner, commandController, eventBus, query);
  50. this.query = query;
  51. }
  52. /** Loads the relevant commands into the parser. */
  53. @Override
  54. protected void loadCommands() {
  55. commandManager.loadCommands(this, CommandType.TYPE_GLOBAL,
  56. CommandType.TYPE_SERVER, CommandType.TYPE_QUERY);
  57. }
  58. @Override
  59. protected CommandContext getCommandContext(
  60. final WindowModel origin,
  61. final CommandInfo commandInfo,
  62. final Command command,
  63. final CommandArguments args) {
  64. return new QueryCommandContext(origin, commandInfo, query);
  65. }
  66. @Override
  67. protected void executeCommand(
  68. @Nonnull final WindowModel origin,
  69. final CommandInfo commandInfo,
  70. final Command command,
  71. final CommandArguments args,
  72. final CommandContext context) {
  73. if (commandInfo.getType() == CommandType.TYPE_QUERY) {
  74. command.execute(origin, args, context);
  75. } else {
  76. super.executeCommand(origin, commandInfo, command, args, context);
  77. }
  78. }
  79. }