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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.dialogs.StandardDialog;
  19. import java.awt.Point;
  20. import java.awt.Window;
  21. import javax.swing.JDialog;
  22. import javax.swing.JPanel;
  23. import javax.swing.UIManager;
  24. import net.miginfocom.swing.MigLayout;
  25. /**
  26. * A popup window which is shown above a status bar component to provide more detailed information.
  27. *
  28. * @since 0.6.3m1
  29. */
  30. public abstract class StatusbarPopupWindow extends StandardDialog {
  31. /** A version number for this class. */
  32. private static final long serialVersionUID = 1;
  33. /** The parent JPanel. */
  34. private final JPanel parent;
  35. /** Parent window. */
  36. private final Window parentWindow;
  37. /**
  38. * Creates a new status bar popup window.
  39. *
  40. * @param parent The {@link JPanel} to use for positioning
  41. * @param parentWindow Parent window
  42. */
  43. public StatusbarPopupWindow(
  44. final JPanel parent,
  45. final Window parentWindow) {
  46. super(parentWindow, ModalityType.MODELESS);
  47. this.parent = parent;
  48. this.parentWindow = parentWindow;
  49. setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  50. }
  51. @Override
  52. public void setVisible(final boolean b) {
  53. if (b && parent.isVisible()) {
  54. final JPanel panel = new JPanel();
  55. initPanel(panel);
  56. initContent(panel);
  57. add(panel);
  58. setUndecorated(true);
  59. setFocusableWindowState(false);
  60. setFocusable(false);
  61. setResizable(false);
  62. pack();
  63. setLocation(getPopupLocation());
  64. }
  65. super.setVisible(b);
  66. }
  67. /**
  68. * Returns the location the popup window should appear at.
  69. *
  70. * @return Point for popup to appear
  71. */
  72. protected Point getPopupLocation() {
  73. final Point point = parent.getLocationOnScreen();
  74. point.translate(parent.getWidth() / 2 - getWidth() / 2,
  75. -getHeight());
  76. final int maxX = Math.max(parentWindow.getLocationOnScreen().x
  77. + parentWindow.getWidth() - 10 - getWidth(),
  78. parent.getLocationOnScreen().x + parent.getWidth() - 1
  79. - getWidth());
  80. point.x = Math.min(maxX, point.x);
  81. return point;
  82. }
  83. /**
  84. * Returns the parent panel for this popup window.
  85. *
  86. * @return Parent panel
  87. */
  88. public JPanel getParentPanel() {
  89. return parent;
  90. }
  91. /**
  92. * Initialises the panel used in the popup window.
  93. *
  94. * @param panel The {@link JPanel} to be initialised
  95. */
  96. protected void initPanel(final JPanel panel) {
  97. panel.setLayout(new MigLayout("ins 3 5 6 10, gap 10 5"));
  98. panel.setBackground(UIManager.getColor("ToolTip.background"));
  99. panel.setForeground(UIManager.getColor("ToolTip.foreground"));
  100. panel.setBorder(new GappedEtchedBorder(this));
  101. }
  102. /**
  103. * Initialises the content of the popup window.
  104. *
  105. * @param panel The {@link JPanel} to which content should be added
  106. */
  107. protected abstract void initContent(final JPanel panel);
  108. }