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.

StatusbarPopupWindow.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.components.StandardDialog;
  24. import java.awt.Component;
  25. import java.awt.Graphics;
  26. import java.awt.Point;
  27. import java.awt.Window;
  28. import javax.swing.JDialog;
  29. import javax.swing.JPanel;
  30. import javax.swing.UIManager;
  31. import javax.swing.border.EtchedBorder;
  32. import net.miginfocom.swing.MigLayout;
  33. /**
  34. * A popup window which is shown above a status bar component to provide more
  35. * detailed information.
  36. *
  37. * @since 0.6.3m1
  38. * @author chris
  39. */
  40. public abstract class StatusbarPopupWindow extends StandardDialog {
  41. /**
  42. * A version number for this class. It should be changed whenever the class
  43. * structure is changed (or anything else that would prevent serialized
  44. * objects being unserialized with the new class).
  45. */
  46. private static final long serialVersionUID = 1;
  47. /** The parent JPanel. */
  48. private final JPanel parent;
  49. /** Parent window. */
  50. private Window parentWindow;
  51. /**
  52. * Creates a new status bar popup window.
  53. *
  54. * @param parent The {@link JPanel} to use for positioning
  55. * @param parentWindow Parent window
  56. */
  57. public StatusbarPopupWindow(final JPanel parent, final Window parentWindow) {
  58. super(parentWindow, ModalityType.MODELESS);
  59. this.parent = parent;
  60. this.parentWindow = parentWindow;
  61. setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  62. }
  63. /** {@inheritDoc} */
  64. @Override
  65. public void setVisible(boolean b) {
  66. if (b) {
  67. final JPanel panel = new JPanel();
  68. panel.setLayout(new MigLayout("ins 3 5 6 10, gap 10 5"));
  69. panel.setBackground(UIManager.getColor("ToolTip.background"));
  70. panel.setForeground(UIManager.getColor("ToolTip.foreground"));
  71. initContent(panel);
  72. add(panel);
  73. setUndecorated(true);
  74. setFocusableWindowState(false);
  75. setFocusable(false);
  76. setResizable(false);
  77. pack();
  78. final Point point = parent.getLocationOnScreen();
  79. point.translate(parent.getWidth() / 2 - this.getWidth() / 2, - this.getHeight());
  80. final int maxX = Math.max(parentWindow.getLocationOnScreen().x
  81. + parentWindow.getWidth() - 10 - getWidth(),
  82. parent.getLocationOnScreen().x + parent.getWidth() - 1 - getWidth());
  83. point.x = Math.min(maxX, point.x);
  84. setLocation(point);
  85. panel.setBorder(new GappedEtchedBorder());
  86. }
  87. super.setVisible(b);
  88. }
  89. /**
  90. * Initialises the content of the popup window.
  91. *
  92. * @param panel The {@link JPanel} to which content should be added
  93. */
  94. protected abstract void initContent(final JPanel panel);
  95. /**
  96. * An {@link EtchedBorder} that leaves a gap in the bottom where the
  97. * lag display panel is.
  98. */
  99. private class GappedEtchedBorder extends EtchedBorder {
  100. /**
  101. * A version number for this class. It should be changed whenever the class
  102. * structure is changed (or anything else that would prevent serialized
  103. * objects being unserialized with the new class).
  104. */
  105. private static final long serialVersionUID = 1;
  106. /** {@inheritDoc} */
  107. @Override
  108. public void paintBorder(final Component c, final Graphics g,
  109. final int x, final int y, final int width, final int height) {
  110. int w = width;
  111. int h = height;
  112. g.translate(x, y);
  113. g.setColor(etchType == LOWERED? getShadowColor(c) : getHighlightColor(c));
  114. g.drawLine(0, 0, w-1, 0);
  115. g.drawLine(0, h-1, parent.getLocationOnScreen().x
  116. - getLocationOnScreen().x, h-1);
  117. g.drawLine(parent.getWidth() + parent.getLocationOnScreen().x
  118. - getLocationOnScreen().x - 2, h-1, w-1, h-1);
  119. g.drawLine(0, 0, 0, h-1);
  120. g.drawLine(w-1, 0, w-1, h-1);
  121. g.translate(-x, -y);
  122. }
  123. }
  124. }