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.

ServerFrame.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.ui_swing.components.frames;
  23. import com.dmdirc.ServerState;
  24. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  25. import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputField;
  26. import com.dmdirc.addons.ui_swing.dialogs.serversetting.ServerSettingsDialog;
  27. import com.dmdirc.addons.ui_swing.dialogs.sslcertificate.SSLCertificateDialog;
  28. import com.dmdirc.addons.ui_swing.dialogs.sslcertificate.SSLCertificateDialogFactory;
  29. import com.dmdirc.addons.ui_swing.injection.KeyedDialogProvider;
  30. import com.dmdirc.commandparser.PopupType;
  31. import com.dmdirc.events.FrameClosingEvent;
  32. import com.dmdirc.events.ServerCertificateProblemEncounteredEvent;
  33. import com.dmdirc.events.ServerCertificateProblemResolvedEvent;
  34. import com.dmdirc.interfaces.Connection;
  35. import javax.inject.Provider;
  36. import javax.swing.JMenuItem;
  37. import javax.swing.JPopupMenu;
  38. import net.miginfocom.swing.MigLayout;
  39. import net.engio.mbassy.listener.Handler;
  40. /**
  41. * The ServerFrame is the MDI window that shows server messages to the user.
  42. */
  43. public final class ServerFrame extends InputTextFrame {
  44. /** Serial version UID. */
  45. private static final long serialVersionUID = 9;
  46. /** Dialog provider to close SSD. */
  47. private final KeyedDialogProvider<Connection, ServerSettingsDialog> dialogProvider;
  48. /** popup menu item. */
  49. private JMenuItem settingsMI;
  50. /** The SSL certificate dialog we're displaying for this server, if any. */
  51. private SSLCertificateDialog sslDialog;
  52. /** Server instance. */
  53. private final Connection connection;
  54. /** Factory to use to create SSL certificate dialogs. */
  55. private final SSLCertificateDialogFactory sslDialogFactory;
  56. /**
  57. * Creates a new ServerFrame.
  58. *
  59. * @param deps The dependencies required by text frames.
  60. * @param inputFieldProvider The provider to use to create a new input field.
  61. * @param dialogProvider Dialog provider to close SSD with
  62. * @param owner Parent Frame container
  63. */
  64. public ServerFrame(
  65. final TextFrameDependencies deps,
  66. final Provider<SwingInputField> inputFieldProvider,
  67. final InputTextFramePasteActionFactory inputTextFramePasteActionFactory,
  68. final KeyedDialogProvider<Connection, ServerSettingsDialog> dialogProvider,
  69. final SSLCertificateDialogFactory sslDialogFactory,
  70. final Connection owner) {
  71. super(deps, inputFieldProvider, inputTextFramePasteActionFactory, owner.getWindowModel());
  72. this.sslDialogFactory = sslDialogFactory;
  73. this.dialogProvider = dialogProvider;
  74. this.connection = owner;
  75. initComponents();
  76. }
  77. /**
  78. * Initialises the instance, adding any required listeners.
  79. */
  80. @Override
  81. public void init() {
  82. connection.getWindowModel().getEventBus().subscribe(this);
  83. super.init();
  84. }
  85. /**
  86. * Initialises components in this frame.
  87. */
  88. private void initComponents() {
  89. settingsMI = new JMenuItem("Settings");
  90. settingsMI.addActionListener(l -> dialogProvider.displayOrRequestFocus(connection));
  91. setLayout(new MigLayout("ins 0, fill, hidemode 3, wrap 1"));
  92. add(getTextPane(), "grow, push");
  93. add(getSearchBar(), "growx, pushx");
  94. add(inputPanel, "growx, pushx");
  95. }
  96. @Override
  97. public PopupType getNicknamePopupType() {
  98. return PopupType.CHAN_NICK;
  99. }
  100. @Override
  101. public PopupType getChannelPopupType() {
  102. return PopupType.CHAN_NORMAL;
  103. }
  104. @Override
  105. public PopupType getHyperlinkPopupType() {
  106. return PopupType.CHAN_HYPERLINK;
  107. }
  108. @Override
  109. public PopupType getNormalPopupType() {
  110. return PopupType.CHAN_NORMAL;
  111. }
  112. @Override
  113. public void addCustomPopupItems(final JPopupMenu popupMenu) {
  114. if (getContainer().getConnection().get().getState() == ServerState.CONNECTED) {
  115. settingsMI.setEnabled(true);
  116. } else {
  117. settingsMI.setEnabled(false);
  118. }
  119. if (popupMenu.getComponentCount() > 0) {
  120. popupMenu.addSeparator();
  121. }
  122. popupMenu.add(settingsMI);
  123. }
  124. @Handler(invocation = EdtHandlerInvocation.class)
  125. public void handleCertProblem(final ServerCertificateProblemEncounteredEvent event) {
  126. if (sslDialog != null) {
  127. sslDialog.dispose();
  128. }
  129. sslDialog = sslDialogFactory.create(event);
  130. sslDialog.display();
  131. }
  132. @Handler(invocation = EdtHandlerInvocation.class)
  133. public void handleCertProblemResolved(final ServerCertificateProblemResolvedEvent event) {
  134. if (sslDialog != null) {
  135. sslDialog.dispose();
  136. }
  137. }
  138. @Override
  139. @Handler(invocation = EdtHandlerInvocation.class)
  140. public void windowClosing(final FrameClosingEvent event) {
  141. connection.getWindowModel().getEventBus().unsubscribe(this);
  142. dialogProvider.dispose(connection);
  143. super.windowClosing(event);
  144. }
  145. @Override
  146. public void dispose() {
  147. connection.getWindowModel().getEventBus().unsubscribe(this);
  148. super.dispose();
  149. }
  150. }