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.

OpenWindow.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2006-2011 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.CustomWindow;
  24. import com.dmdirc.FrameContainer;
  25. import com.dmdirc.commandparser.CommandArguments;
  26. import com.dmdirc.commandparser.CommandInfo;
  27. import com.dmdirc.commandparser.CommandType;
  28. import com.dmdirc.commandparser.commands.Command;
  29. import com.dmdirc.commandparser.commands.IntelligentCommand;
  30. import com.dmdirc.commandparser.commands.context.CommandContext;
  31. import com.dmdirc.ui.WindowManager;
  32. import com.dmdirc.ui.input.AdditionalTabTargets;
  33. /**
  34. * Opens a new window.
  35. *
  36. * @author chris
  37. */
  38. public class OpenWindow extends Command implements IntelligentCommand, CommandInfo {
  39. /**
  40. * Creates a new instance of OpenWindow.
  41. */
  42. public OpenWindow() {
  43. super();
  44. }
  45. /** {@inheritDoc} */
  46. @Override
  47. public void execute(final FrameContainer origin,
  48. final CommandArguments args, final CommandContext context) {
  49. int start = 0;
  50. FrameContainer parent = null;
  51. if (args.getArguments().length > 0 && "--server".equals(args.getArguments()[0])) {
  52. if (origin.getServer() == null) {
  53. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  54. "This window doesn't have an associated server.");
  55. return;
  56. }
  57. parent = origin.getServer();
  58. start = 1;
  59. } else if (args.getArguments().length > 0 && "--child".equals(args.getArguments()[0])) {
  60. parent = origin;
  61. start = 1;
  62. }
  63. if (args.getArguments().length == start || args.getArguments()[start].isEmpty()) {
  64. showUsage(origin, args.isSilent(), "openwindow",
  65. "[--server|--child] <name> [title]");
  66. } else {
  67. final FrameContainer window;
  68. if (parent == null) {
  69. window = WindowManager.findCustomWindow(args.getArguments()[start]);
  70. } else {
  71. window = WindowManager.findCustomWindow(parent, args.getArguments()[start]);
  72. }
  73. final String title = args.getArguments().length > start + 1
  74. ? args.getArgumentsAsString(start + 1) : args.getArguments()[start];
  75. if (window == null) {
  76. if (parent == null) {
  77. new CustomWindow(args.getArguments()[start], title);
  78. } else {
  79. new CustomWindow(args.getArguments()[start], title, parent);
  80. }
  81. } else {
  82. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  83. "A custom window by that name already exists.");
  84. }
  85. }
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public String getHelp() {
  90. return "openwindow [--server|--child] <name> [title] "
  91. + "- opens a window with the specified name and title";
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public String getName() {
  96. return "openwindow";
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. public CommandType getType() {
  101. return CommandType.TYPE_GLOBAL;
  102. }
  103. /** {@inheritDoc} */
  104. @Override
  105. public boolean showInHelp() {
  106. return true;
  107. }
  108. /** {@inheritDoc} */
  109. @Override
  110. public AdditionalTabTargets getSuggestions(final int arg,
  111. final IntelligentCommandContext context) {
  112. final AdditionalTabTargets res = new AdditionalTabTargets();
  113. if (arg == 0) {
  114. res.add("--server");
  115. res.add("--child");
  116. }
  117. return res;
  118. }
  119. }