選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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