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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.components.frames;
  18. import com.dmdirc.ServerState;
  19. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  20. import com.dmdirc.addons.ui_swing.components.inputfields.SwingInputField;
  21. import com.dmdirc.addons.ui_swing.dialogs.serversetting.ServerSettingsDialog;
  22. import com.dmdirc.addons.ui_swing.dialogs.sslcertificate.SSLCertificateDialog;
  23. import com.dmdirc.addons.ui_swing.dialogs.sslcertificate.SSLCertificateDialogFactory;
  24. import com.dmdirc.addons.ui_swing.injection.KeyedDialogProvider;
  25. import com.dmdirc.commandparser.PopupType;
  26. import com.dmdirc.events.FrameClosingEvent;
  27. import com.dmdirc.events.ServerCertificateProblemEncounteredEvent;
  28. import com.dmdirc.events.ServerCertificateProblemResolvedEvent;
  29. import com.dmdirc.interfaces.Connection;
  30. import javax.inject.Provider;
  31. import javax.swing.JMenuItem;
  32. import javax.swing.JPopupMenu;
  33. import net.miginfocom.swing.MigLayout;
  34. import net.engio.mbassy.listener.Handler;
  35. /**
  36. * The ServerFrame is the MDI window that shows server messages to the user.
  37. */
  38. public final class ServerFrame extends InputTextFrame {
  39. /** Serial version UID. */
  40. private static final long serialVersionUID = 9;
  41. /** Dialog provider to close SSD. */
  42. private final KeyedDialogProvider<Connection, ServerSettingsDialog> dialogProvider;
  43. /** popup menu item. */
  44. private JMenuItem settingsMI;
  45. /** The SSL certificate dialog we're displaying for this server, if any. */
  46. private SSLCertificateDialog sslDialog;
  47. /** Server instance. */
  48. private final Connection connection;
  49. /** Factory to use to create SSL certificate dialogs. */
  50. private final SSLCertificateDialogFactory sslDialogFactory;
  51. /**
  52. * Creates a new ServerFrame.
  53. *
  54. * @param deps The dependencies required by text frames.
  55. * @param inputFieldProvider The provider to use to create a new input field.
  56. * @param dialogProvider Dialog provider to close SSD with
  57. * @param owner Parent Frame container
  58. */
  59. public ServerFrame(
  60. final TextFrameDependencies deps,
  61. final Provider<SwingInputField> inputFieldProvider,
  62. final InputTextFramePasteActionFactory inputTextFramePasteActionFactory,
  63. final KeyedDialogProvider<Connection, ServerSettingsDialog> dialogProvider,
  64. final SSLCertificateDialogFactory sslDialogFactory,
  65. final Connection owner) {
  66. super(deps, inputFieldProvider, inputTextFramePasteActionFactory, owner.getWindowModel());
  67. this.sslDialogFactory = sslDialogFactory;
  68. this.dialogProvider = dialogProvider;
  69. this.connection = owner;
  70. initComponents();
  71. }
  72. /**
  73. * Initialises the instance, adding any required listeners.
  74. */
  75. @Override
  76. public void init() {
  77. connection.getWindowModel().getEventBus().subscribe(this);
  78. super.init();
  79. }
  80. /**
  81. * Initialises components in this frame.
  82. */
  83. private void initComponents() {
  84. settingsMI = new JMenuItem("Settings");
  85. settingsMI.addActionListener(l -> dialogProvider.displayOrRequestFocus(connection));
  86. setLayout(new MigLayout("ins 0, fill, hidemode 3, wrap 1"));
  87. add(getTextPane(), "grow, push");
  88. add(getSearchBar(), "growx, pushx");
  89. add(inputPanel, "growx, pushx");
  90. }
  91. @Override
  92. public PopupType getNicknamePopupType() {
  93. return PopupType.CHAN_NICK;
  94. }
  95. @Override
  96. public PopupType getChannelPopupType() {
  97. return PopupType.CHAN_NORMAL;
  98. }
  99. @Override
  100. public PopupType getHyperlinkPopupType() {
  101. return PopupType.CHAN_HYPERLINK;
  102. }
  103. @Override
  104. public PopupType getNormalPopupType() {
  105. return PopupType.CHAN_NORMAL;
  106. }
  107. @Override
  108. public void addCustomPopupItems(final JPopupMenu popupMenu) {
  109. if (getContainer().getConnection().get().getState() == ServerState.CONNECTED) {
  110. settingsMI.setEnabled(true);
  111. } else {
  112. settingsMI.setEnabled(false);
  113. }
  114. if (popupMenu.getComponentCount() > 0) {
  115. popupMenu.addSeparator();
  116. }
  117. popupMenu.add(settingsMI);
  118. }
  119. @Handler(invocation = EdtHandlerInvocation.class)
  120. public void handleCertProblem(final ServerCertificateProblemEncounteredEvent event) {
  121. if (event.getConnection().equals(connection)) {
  122. if (sslDialog != null) {
  123. sslDialog.dispose();
  124. }
  125. sslDialog = sslDialogFactory.create(event);
  126. sslDialog.display();
  127. }
  128. }
  129. @Handler(invocation = EdtHandlerInvocation.class)
  130. public void handleCertProblemResolved(final ServerCertificateProblemResolvedEvent event) {
  131. if (event.getConnection().equals(connection)) {
  132. if (sslDialog != null) {
  133. sslDialog.dispose();
  134. }
  135. }
  136. }
  137. @Override
  138. @Handler(invocation = EdtHandlerInvocation.class)
  139. public void windowClosing(final FrameClosingEvent event) {
  140. if (event.getSource().equals(frameParent)) {
  141. connection.getWindowModel().getEventBus().unsubscribe(this);
  142. dialogProvider.dispose(connection);
  143. super.windowClosing(event);
  144. }
  145. }
  146. @Override
  147. public void dispose() {
  148. connection.getWindowModel().getEventBus().unsubscribe(this);
  149. super.dispose();
  150. }
  151. }