Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Echo.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.commandparser.commands.global;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.CommandManager;
  26. import com.dmdirc.commandparser.commands.GlobalCommand;
  27. import com.dmdirc.commandparser.commands.IntelligentCommand;
  28. import com.dmdirc.ui.WindowManager;
  29. import com.dmdirc.ui.input.AdditionalTabTargets;
  30. import com.dmdirc.ui.interfaces.InputWindow;
  31. import com.dmdirc.ui.interfaces.Window;
  32. import java.util.List;
  33. /**
  34. * The echo commands simply echos text to the current window.
  35. *
  36. * @author chris
  37. */
  38. public final class Echo extends GlobalCommand implements IntelligentCommand {
  39. /**
  40. * Creates a new instance of Echo.
  41. */
  42. public Echo() {
  43. super();
  44. CommandManager.registerCommand(this);
  45. }
  46. /** {@inheritDoc} */
  47. @Override
  48. public void execute(final InputWindow origin, final boolean isSilent,
  49. final CommandArguments args) {
  50. if (args.getArguments().length > 0
  51. && args.getArguments()[0].equalsIgnoreCase("--active")) {
  52. final Window frame = Main.getUI().getActiveWindow();
  53. if (frame instanceof InputWindow) {
  54. ((InputWindow) frame).addLine(FORMAT_OUTPUT, args.getArgumentsAsString(1));
  55. }
  56. } else if (args.getArguments().length > 1
  57. && args.getArguments()[0].equalsIgnoreCase("--target")) {
  58. Window frame = null;
  59. Window target = origin;
  60. while (frame == null && target != null) {
  61. frame = WindowManager.findCustomWindow(target, args.getArguments()[1]);
  62. target = WindowManager.getParent(target);
  63. }
  64. if (frame == null) {
  65. frame = WindowManager.findCustomWindow(args.getArguments()[1]);
  66. }
  67. if (frame == null) {
  68. sendLine(origin, isSilent, FORMAT_ERROR,
  69. "Unable to find target window");
  70. } else {
  71. frame.addLine(FORMAT_OUTPUT, args.getArgumentsAsString(2));
  72. }
  73. } else {
  74. sendLine(origin, isSilent, FORMAT_OUTPUT, args.getArgumentsAsString());
  75. }
  76. }
  77. /** {@inheritDoc} */
  78. @Override
  79. public String getName() {
  80. return "echo";
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. public boolean showInHelp() {
  85. return true;
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public String getHelp() {
  90. return "echo [--active|--target <window>] <line> "
  91. + "- echos the specified line to the window";
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public AdditionalTabTargets getSuggestions(final int arg, final List<String> previousArgs) {
  96. final AdditionalTabTargets targets = new AdditionalTabTargets();
  97. if (arg == 0) {
  98. targets.add("--active");
  99. targets.add("--target");
  100. } else if (arg == 1 && previousArgs.get(0).equals("--target")) {
  101. targets.excludeAll();
  102. // TODO: Include window names
  103. }
  104. return targets;
  105. }
  106. }