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.

ButtonPanel.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes,
  3. * Simon Mott
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package com.dmdirc.addons.ui_swing.framemanager.buttonbar;
  24. import java.awt.Component;
  25. import java.awt.Dimension;
  26. import java.awt.Rectangle;
  27. import java.awt.event.MouseWheelEvent;
  28. import java.awt.event.MouseWheelListener;
  29. import javax.swing.JPanel;
  30. import javax.swing.Scrollable;
  31. import net.miginfocom.swing.MigLayout;
  32. /**
  33. * Implements scrollable onto a JPanel so we have more control over scrolling.
  34. *
  35. * @since 0.6.4
  36. */
  37. public class ButtonPanel extends JPanel implements Scrollable,
  38. MouseWheelListener {
  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 = 1;
  45. /** The ButtonBar that created this Panel. */
  46. private final ButtonBar buttonBar;
  47. /**
  48. * Constructor for ButtonPanel.
  49. *
  50. * @param layout Layout settings for this ButtonPanel
  51. * @param buttonBar the buttonBar that created this Panel
  52. */
  53. public ButtonPanel(final MigLayout layout, final ButtonBar buttonBar) {
  54. super(layout);
  55. this.buttonBar = buttonBar;
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. public Dimension getPreferredScrollableViewportSize() {
  60. return super.getPreferredSize();
  61. }
  62. /** {@inheritDoc} */
  63. @Override
  64. public int getScrollableUnitIncrement(final Rectangle visibleRect,
  65. final int orientation, final int direction) {
  66. return buttonBar.getButtonHeight();
  67. }
  68. /** {@inheritDoc} */
  69. @Override
  70. public int getScrollableBlockIncrement(final Rectangle visibleRect,
  71. final int orientation, final int direction) {
  72. return buttonBar.getButtonHeight();
  73. }
  74. /** {@inheritDoc} */
  75. @Override
  76. public boolean getScrollableTracksViewportWidth() {
  77. return true;
  78. }
  79. /** {@inheritDoc} */
  80. @Override
  81. public boolean getScrollableTracksViewportHeight() {
  82. return false;
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. public void mouseWheelMoved(final MouseWheelEvent e) {
  87. e.consume();
  88. final int selectedIndex = getSelectedIndex();
  89. int newIndex = 0;
  90. if (e.getWheelRotation() < 0) {
  91. //Up
  92. newIndex = selectedIndex > 0 ? selectedIndex - 1
  93. : getComponentCount() - 1;
  94. } else if (e.getWheelRotation() > 0) {
  95. //Down
  96. newIndex = (selectedIndex + 1) % getComponentCount();
  97. }
  98. ((FrameToggleButton) getComponent(newIndex)).getWindow()
  99. .activateFrame();
  100. }
  101. /**
  102. * Gets the component index of the button associated with the current
  103. * selected window.
  104. *
  105. * @return Integer Index for the button of the selected window
  106. *
  107. * @since 0.6.4
  108. */
  109. private int getSelectedIndex() {
  110. int selectedIndex = 0;
  111. final FrameToggleButton selectedButton = buttonBar.getSelectedButton();
  112. for (Component c : getComponents()) {
  113. if (c == selectedButton) {
  114. break;
  115. }
  116. selectedIndex++;
  117. }
  118. return selectedIndex;
  119. }
  120. }