Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SwingStatusBar.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2006-2013 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.statusbar;
  23. import com.dmdirc.addons.ui_swing.MainFrame;
  24. import com.dmdirc.addons.ui_swing.SwingController;
  25. import com.dmdirc.interfaces.ui.StatusBar;
  26. import com.dmdirc.interfaces.ui.StatusBarComponent;
  27. import com.dmdirc.logger.ErrorLevel;
  28. import com.dmdirc.logger.Logger;
  29. import com.dmdirc.ui.StatusMessage;
  30. import java.awt.Component;
  31. import java.util.Arrays;
  32. import javax.swing.JPanel;
  33. import javax.swing.SwingUtilities;
  34. import javax.swing.UIManager;
  35. import net.miginfocom.layout.PlatformDefaults;
  36. import net.miginfocom.swing.MigLayout;
  37. /** Status bar, shows message and info on the GUI. */
  38. public final class SwingStatusBar extends JPanel implements StatusBar {
  39. /**
  40. * A version number for this class. It should be changed whenever the class
  41. * structure is changed (or anything else that would prevent serialized
  42. * objects being unserialized with the new class).
  43. */
  44. private static final long serialVersionUID = 5;
  45. /** Mig layout component restraints. */
  46. private final String componentConstraints;
  47. /** Height for the status bar. */
  48. private final int height;
  49. /** message label. */
  50. private final MessageLabel messageLabel;
  51. /** error panel. */
  52. private final ErrorPanel errorPanel;
  53. /** update label. */
  54. private final UpdaterLabel updateLabel;
  55. /** Invite label. */
  56. private final InviteLabel inviteLabel;
  57. /**
  58. * Creates a new instance of SwingStatusBar.
  59. *
  60. * @param controller Swing controller
  61. * @param mainFrame Main frame
  62. */
  63. public SwingStatusBar(final SwingController controller,
  64. final MainFrame mainFrame) {
  65. super();
  66. height = getFontMetrics(UIManager.getFont("Table.font")).getHeight()
  67. + (int) PlatformDefaults.getUnitValueX("related").getValue()
  68. + (int) PlatformDefaults.getUnitValueX("related").getValue();
  69. componentConstraints = "sgy components, hmax "+height+", hmin "+height
  70. +", wmin 20, shrink 0";
  71. messageLabel = new MessageLabel(controller, mainFrame);
  72. errorPanel = new ErrorPanel(controller, mainFrame, this);
  73. updateLabel = new UpdaterLabel(controller);
  74. inviteLabel = new InviteLabel(controller, mainFrame);
  75. setLayout(new MigLayout("fill, ins 0, hidemode 3"));
  76. add(messageLabel, "grow, push, sgy components, hmax " + height
  77. + ", hmin " + height);
  78. add(updateLabel, componentConstraints);
  79. add(errorPanel, componentConstraints);
  80. add(inviteLabel, componentConstraints);
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. public void setMessage(final StatusMessage message) {
  85. messageLabel.setMessage(message);
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public void clearMessage() {
  90. messageLabel.clearMessage();
  91. }
  92. /** {@inheritDoc} */
  93. @Override
  94. public void addComponent(final StatusBarComponent component) {
  95. if (!(component instanceof Component)) {
  96. Logger.appError(ErrorLevel.HIGH, "Error adding status bar component",
  97. new IllegalArgumentException("Component must be an " +
  98. "instance of java.awt.component"));
  99. return;
  100. }
  101. if (!Arrays.asList(getComponents()).contains(component)) {
  102. SwingUtilities.invokeLater(new Runnable() {
  103. /** {@inheritDoc} */
  104. @Override
  105. public void run() {
  106. remove(updateLabel);
  107. remove(errorPanel);
  108. remove(inviteLabel);
  109. add((Component) component, componentConstraints);
  110. add(updateLabel, componentConstraints);
  111. add(inviteLabel, componentConstraints);
  112. add(errorPanel, componentConstraints);
  113. validate();
  114. }
  115. });
  116. }
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public void removeComponent(final StatusBarComponent component) {
  121. if (!(component instanceof Component)) {
  122. Logger.appError(ErrorLevel.HIGH, "Error removing status bar "
  123. + "component", new IllegalArgumentException("Component "
  124. + "must be an instance of java.awt.component"));
  125. return;
  126. }
  127. SwingUtilities.invokeLater(new Runnable() {
  128. /** {@inheritDoc} */
  129. @Override
  130. public void run() {
  131. remove((Component) component);
  132. validate();
  133. }
  134. });
  135. }
  136. /**
  137. * Returns the message label for this status bar. This is intended to be
  138. * used for advanced plugins that wish to do compliated things with
  139. * messages.
  140. *
  141. * @return Message label component
  142. */
  143. public MessageLabel getMessageComponent() {
  144. return messageLabel;
  145. }
  146. }