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.

OsdCommand.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.addons.osd;
  18. import com.dmdirc.commandparser.BaseCommandInfo;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandType;
  21. import com.dmdirc.commandparser.commands.BaseCommand;
  22. import com.dmdirc.commandparser.commands.IntelligentCommand;
  23. import com.dmdirc.commandparser.commands.context.CommandContext;
  24. import com.dmdirc.interfaces.CommandController;
  25. import com.dmdirc.interfaces.WindowModel;
  26. import com.dmdirc.ui.input.AdditionalTabTargets;
  27. import com.dmdirc.ui.messages.StyledMessageUtils;
  28. import javax.annotation.Nonnull;
  29. import javax.inject.Inject;
  30. /**
  31. * The osd command shows an on screen message.
  32. */
  33. public class OsdCommand extends BaseCommand implements IntelligentCommand {
  34. /** A command info object for this command. */
  35. public static final BaseCommandInfo INFO = new BaseCommandInfo("osd",
  36. "osd --close - closes all OSD windows\n"
  37. + "osd [--timeout <delay in seconds>] <message> - show the "
  38. + "specified message in an OSD window",
  39. CommandType.TYPE_GLOBAL);
  40. /** The OSDManager that this command should use. */
  41. private final OsdManager osdManager;
  42. /**
  43. * Creates a new instance of OsdCommand.
  44. *
  45. * @param controller The controller to use for command information.
  46. * @param osdManager OSD Manager used to control OSD windows
  47. */
  48. @Inject
  49. public OsdCommand(final CommandController controller, final OsdManager osdManager) {
  50. super(controller);
  51. this.osdManager = osdManager;
  52. }
  53. /**
  54. * Used to show a notification using this plugin.
  55. *
  56. * @param timeout Timeout for the OSD window. If negative then the value from the config will be
  57. * used
  58. * @param title Title of dialog if applicable
  59. * @param message Message to show
  60. *
  61. * @return True if the notification was shown.
  62. */
  63. public boolean showOSD(final int timeout, final String title, final String message) {
  64. osdManager.showWindow(timeout, new StyledMessageUtils().stripControlCodes(message));
  65. return true;
  66. }
  67. @Override
  68. public void execute(@Nonnull final WindowModel origin,
  69. final CommandArguments args, final CommandContext context) {
  70. if (args.getArguments().length > 0
  71. && "--close".equalsIgnoreCase(args.getArguments()[0])) {
  72. osdManager.closeAll();
  73. } else if (args.getArguments().length > 0
  74. && "--timeout".equalsIgnoreCase(args.getArguments()[0])) {
  75. if (args.getArguments().length < 2) {
  76. showError(origin, args.isSilent(),
  77. "You must specify a valid number for the OSD timeout.");
  78. return;
  79. }
  80. try {
  81. showOSD(Integer.parseInt(args.getArguments()[1]), null,
  82. args.getArgumentsAsString(2));
  83. } catch (NumberFormatException ex) {
  84. showError(origin, args.isSilent(),
  85. "You must specify a valid number for the OSD timeout.");
  86. }
  87. } else {
  88. showOSD(-1, null, args.getArgumentsAsString());
  89. }
  90. }
  91. @Override
  92. public AdditionalTabTargets getSuggestions(final int arg,
  93. final IntelligentCommandContext context) {
  94. final AdditionalTabTargets res = new AdditionalTabTargets();
  95. if (arg == 0) {
  96. res.add("--close");
  97. res.add("--timeout");
  98. } else if (arg > 0 && "--close".equals(context.getPreviousArgs().get(0))) {
  99. res.excludeAll();
  100. }
  101. return res;
  102. }
  103. }