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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2006-2014 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.commandparser.commands.global;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.CustomWindow;
  25. import com.dmdirc.DMDircMBassador;
  26. import com.dmdirc.FrameContainer;
  27. import com.dmdirc.commandparser.BaseCommandInfo;
  28. import com.dmdirc.commandparser.CommandArguments;
  29. import com.dmdirc.commandparser.CommandInfo;
  30. import com.dmdirc.commandparser.CommandType;
  31. import com.dmdirc.commandparser.commands.Command;
  32. import com.dmdirc.commandparser.commands.IntelligentCommand;
  33. import com.dmdirc.commandparser.commands.context.CommandContext;
  34. import com.dmdirc.interfaces.CommandController;
  35. import com.dmdirc.interfaces.Connection;
  36. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  37. import com.dmdirc.ui.WindowManager;
  38. import com.dmdirc.ui.messages.BackBufferFactory;
  39. import com.dmdirc.ui.input.AdditionalTabTargets;
  40. import com.dmdirc.util.URLBuilder;
  41. import java.util.Optional;
  42. import javax.annotation.Nonnull;
  43. import javax.inject.Inject;
  44. /**
  45. * Opens a new window.
  46. */
  47. public class OpenWindow extends Command implements IntelligentCommand {
  48. /** A command info object for this command. */
  49. public static final CommandInfo INFO = new BaseCommandInfo("openwindow",
  50. "openwindow [--server|--child] <name> [title] "
  51. + "- opens a window with the specified name and title",
  52. CommandType.TYPE_GLOBAL);
  53. /** Window management. */
  54. private final WindowManager windowManager;
  55. /** The URL builder to use when finding icons. */
  56. private final URLBuilder urlBuilder;
  57. /** The bus to dispatch events on. */
  58. private final DMDircMBassador eventBus;
  59. /** The config provider to retrieve settings from. */
  60. private final AggregateConfigProvider configProvider;
  61. /** Back buffer factory. */
  62. private final BackBufferFactory backBufferFactory;
  63. /**
  64. * Creates a new instance of this command.
  65. */
  66. @Inject
  67. public OpenWindow(
  68. final CommandController controller,
  69. final WindowManager windowManager,
  70. final URLBuilder urlBuilder,
  71. final DMDircMBassador eventBus,
  72. @GlobalConfig final AggregateConfigProvider configProvider,
  73. final BackBufferFactory backBufferFactory) {
  74. super(controller);
  75. this.windowManager = windowManager;
  76. this.urlBuilder = urlBuilder;
  77. this.eventBus = eventBus;
  78. this.configProvider = configProvider;
  79. this.backBufferFactory = backBufferFactory;
  80. }
  81. @Override
  82. public void execute(@Nonnull final FrameContainer origin,
  83. final CommandArguments args, final CommandContext context) {
  84. int start = 0;
  85. FrameContainer parent = null;
  86. if (args.getArguments().length > 0 && "--server".equals(args.getArguments()[0])) {
  87. final Optional<Connection> connection = origin.getConnection();
  88. if (!connection.isPresent()) {
  89. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  90. "This window doesn't have an associated server.");
  91. return;
  92. }
  93. parent = connection.get().getWindowModel();
  94. start = 1;
  95. } else if (args.getArguments().length > 0 && "--child".equals(args.getArguments()[0])) {
  96. parent = origin;
  97. start = 1;
  98. }
  99. if (args.getArguments().length == start || args.getArguments()[start].isEmpty()) {
  100. showUsage(origin, args.isSilent(), "openwindow",
  101. "[--server|--child] <name> [title]");
  102. } else {
  103. final FrameContainer window;
  104. if (parent == null) {
  105. window = windowManager.findCustomWindow(args.getArguments()[start]);
  106. } else {
  107. window = windowManager.findCustomWindow(parent, args.getArguments()[start]);
  108. }
  109. final String title = args.getArguments().length > start + 1
  110. ? args.getArgumentsAsString(start + 1) : args.getArguments()[start];
  111. if (window == null) {
  112. final CustomWindow newWindow;
  113. if (parent == null) {
  114. newWindow = new CustomWindow(args.getArguments()[start], title,
  115. configProvider, urlBuilder, eventBus, backBufferFactory);
  116. windowManager.addWindow(newWindow);
  117. } else {
  118. newWindow = new CustomWindow(args.getArguments()[start], title, parent,
  119. urlBuilder, backBufferFactory);
  120. windowManager.addWindow(parent, newWindow);
  121. }
  122. } else {
  123. sendLine(origin, args.isSilent(), FORMAT_ERROR,
  124. "A custom window by that name already exists.");
  125. }
  126. }
  127. }
  128. @Override
  129. public AdditionalTabTargets getSuggestions(final int arg,
  130. final IntelligentCommandContext context) {
  131. final AdditionalTabTargets res = new AdditionalTabTargets();
  132. if (arg == 0) {
  133. res.add("--server");
  134. res.add("--child");
  135. }
  136. return res;
  137. }
  138. }