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.

SwingStatusBar.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.events.StatusBarComponentAddedEvent;
  19. import com.dmdirc.events.StatusBarComponentRemovedEvent;
  20. import com.google.common.base.Preconditions;
  21. import java.awt.Component;
  22. import java.util.Arrays;
  23. import javax.swing.JPanel;
  24. import javax.swing.SwingUtilities;
  25. import javax.swing.UIManager;
  26. import net.miginfocom.layout.PlatformDefaults;
  27. import net.miginfocom.swing.MigLayout;
  28. import net.engio.mbassy.listener.Handler;
  29. import static com.dmdirc.addons.ui_swing.SwingPreconditions.checkOnEDT;
  30. /** Status bar, shows message and info on the GUI. */
  31. public class SwingStatusBar extends JPanel {
  32. /** A version number for this class. */
  33. private static final long serialVersionUID = 5;
  34. /** Mig layout component restraints. */
  35. private final String componentConstraints;
  36. /** error panel. */
  37. private final ErrorPanel errorLabel;
  38. /** update label. */
  39. private final UpdaterLabel updaterLabel;
  40. /** Invite label. */
  41. private final InviteLabel inviteLabel;
  42. /**
  43. * Creates a new instance of SwingStatusBar.
  44. *
  45. * @param inviteLabel The invite label to add to the status bar.
  46. * @param updaterLabel The updater label to add to the status bar.
  47. * @param errorLabel The error label to add to the status bar.
  48. * @param messageLabel The message label to add to the status bar.
  49. */
  50. public SwingStatusBar(
  51. final InviteLabel inviteLabel,
  52. final UpdaterLabel updaterLabel,
  53. final ErrorPanel errorLabel,
  54. final MessageLabel messageLabel) {
  55. checkOnEDT();
  56. final int height = getFontMetrics(UIManager.getFont("Label.font")).getHeight()
  57. + (int) PlatformDefaults.getUnitValueY("related").getValue();
  58. componentConstraints = "sgy components, hmax " + height + ", hmin " + height
  59. + ", wmin 20, shrink 0";
  60. this.errorLabel = errorLabel;
  61. this.updaterLabel = updaterLabel;
  62. this.inviteLabel = inviteLabel;
  63. inviteLabel.init();
  64. setLayout(new MigLayout("fill, ins 0, hidemode 3"));
  65. add(messageLabel, "grow, push, sgy components, hmax " + height
  66. + ", hmin " + height);
  67. add(updaterLabel, componentConstraints);
  68. add(errorLabel, componentConstraints);
  69. add(inviteLabel, componentConstraints);
  70. }
  71. @Handler
  72. public void addComponent(final StatusBarComponentAddedEvent event) {
  73. Preconditions.checkArgument(event.getComponent() instanceof Component,
  74. "Error adding status bar component, " +
  75. "component must be an instance of java.awt.component");
  76. if (!Arrays.asList(getComponents()).contains(event.getComponent())) {
  77. SwingUtilities.invokeLater(() -> {
  78. remove(updaterLabel);
  79. remove(errorLabel);
  80. remove(inviteLabel);
  81. add((Component) event.getComponent(), componentConstraints);
  82. add(updaterLabel, componentConstraints);
  83. add(inviteLabel, componentConstraints);
  84. add(errorLabel, componentConstraints);
  85. validate();
  86. });
  87. }
  88. }
  89. @Handler
  90. public void removeComponent(final StatusBarComponentRemovedEvent event) {
  91. Preconditions.checkArgument(event.getComponent() instanceof Component,
  92. "Error removing status bar component" +
  93. "component must be an instance of java.awt.component");
  94. SwingUtilities.invokeLater(() -> {
  95. remove((Component) event.getComponent());
  96. validate();
  97. });
  98. }
  99. }