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

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