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.

ErrorPanel.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.statusbar;
  18. import com.dmdirc.addons.ui_swing.UIUtilities;
  19. import com.dmdirc.addons.ui_swing.components.IconManager;
  20. import com.dmdirc.addons.ui_swing.components.menubar.JMenuItemBuilder;
  21. import com.dmdirc.addons.ui_swing.dialogs.errors.ErrorsDialog;
  22. import com.dmdirc.addons.ui_swing.injection.DialogProvider;
  23. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  24. import com.dmdirc.interfaces.ui.ErrorsDialogModel;
  25. import com.dmdirc.interfaces.ui.ErrorsDialogModelListener;
  26. import com.dmdirc.logger.ErrorLevel;
  27. import com.dmdirc.ui.core.errors.DisplayableError;
  28. import java.awt.Window;
  29. import java.awt.event.MouseEvent;
  30. import java.util.Optional;
  31. import java.util.Set;
  32. import javax.inject.Inject;
  33. import javax.inject.Singleton;
  34. import javax.swing.JLabel;
  35. import javax.swing.JPopupMenu;
  36. /**
  37. * Shows error status in the status bar.
  38. */
  39. @Singleton
  40. public class ErrorPanel extends StatusbarPopupPanel<JLabel> implements ErrorsDialogModelListener {
  41. /** Serial version UID. */
  42. private static final long serialVersionUID = 2;
  43. /** Parent window that will own popups. */
  44. private final Window parentWindow;
  45. /** The manager to use to retrieve icons. */
  46. private final IconManager iconManager;
  47. /** Error manager. */
  48. private final ErrorsDialogModel model;
  49. /** Dismiss menu. */
  50. private final JPopupMenu menu;
  51. /** Error list dialog provider. */
  52. private final DialogProvider<ErrorsDialog> errorListDialogProvider;
  53. /**
  54. * Creates a new ErrorPanel for the specified status bar.
  55. *
  56. * @param iconManager The manager to use to retrieve icons.
  57. * @param parentWindow Main frame
  58. * @param errorListDialogProvider Error list dialog provider.
  59. */
  60. @Inject
  61. public ErrorPanel(
  62. final IconManager iconManager,
  63. @MainWindow final Window parentWindow,
  64. final DialogProvider<ErrorsDialog> errorListDialogProvider,
  65. final ErrorsDialogModel model) {
  66. super(new JLabel());
  67. this.parentWindow = parentWindow;
  68. this.iconManager = iconManager;
  69. this.errorListDialogProvider = errorListDialogProvider;
  70. this.model = model;
  71. model.addListener(this);
  72. model.load();
  73. menu = new JPopupMenu();
  74. menu.add(JMenuItemBuilder.create().setText("Open")
  75. .addActionListener(e -> errorListDialogProvider.displayOrRequestFocus()).build());
  76. menu.add(JMenuItemBuilder.create()
  77. .setText("Clear All")
  78. .addActionListener(e -> model.deleteAllErrors())
  79. .build());
  80. checkErrors();
  81. }
  82. @Override
  83. protected StatusbarPopupWindow getWindow() {
  84. return new ErrorPopup(model, iconManager, this, parentWindow);
  85. }
  86. /** Checks all the errors for the most significant error. */
  87. private void checkErrors() {
  88. final Set<DisplayableError> errors = model.getErrors();
  89. label.setIcon(iconManager.getIcon(errors.stream().map(DisplayableError::getSeverity)
  90. .reduce(ErrorLevel.UNKNOWN, ErrorLevel::getMoreImportant).getIcon()));
  91. setVisible(!errors.isEmpty());
  92. }
  93. @Override
  94. public void mousePressed(final MouseEvent mouseEvent) {
  95. super.mousePressed(mouseEvent);
  96. checkMouseEvent(mouseEvent);
  97. }
  98. @Override
  99. public void mouseReleased(final MouseEvent mouseEvent) {
  100. super.mouseReleased(mouseEvent);
  101. if (mouseEvent.getButton() == MouseEvent.BUTTON1) {
  102. errorListDialogProvider.displayOrRequestFocus();
  103. }
  104. checkMouseEvent(mouseEvent);
  105. }
  106. @Override
  107. public void mouseEntered(final MouseEvent mouseEvent) {
  108. super.mouseEntered(mouseEvent);
  109. checkMouseEvent(mouseEvent);
  110. }
  111. @Override
  112. public void mouseExited(final MouseEvent mouseEvent) {
  113. super.mouseExited(mouseEvent);
  114. checkMouseEvent(mouseEvent);
  115. }
  116. @Override
  117. public void mouseClicked(final MouseEvent mouseEvent) {
  118. super.mouseClicked(mouseEvent);
  119. checkMouseEvent(mouseEvent);
  120. }
  121. /**
  122. * Checks a mouse event for a popup trigger.
  123. *
  124. * @param e Mouse event
  125. */
  126. private void checkMouseEvent(final MouseEvent e) {
  127. if (e.isPopupTrigger()) {
  128. menu.show(this, e.getX(), e.getY());
  129. }
  130. }
  131. @Override
  132. public void errorDeleted(final DisplayableError error) {
  133. UIUtilities.invokeLater(this::checkErrors);
  134. }
  135. @Override
  136. public void errorAdded(final DisplayableError error) {
  137. UIUtilities.invokeLater(this::checkErrors);
  138. }
  139. @Override
  140. public void selectedErrorChanged(final Optional<DisplayableError> selectedError) {
  141. //Ignore
  142. }
  143. @Override
  144. public void errorStatusChanged(final DisplayableError error) {
  145. //Ignore
  146. }
  147. }