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.

ListReorderButtonPanel.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2006-2015 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.reorderablelist;
  23. import com.dmdirc.addons.ui_swing.components.GenericListModel;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import javax.swing.JButton;
  27. import javax.swing.JList;
  28. import javax.swing.JPanel;
  29. import javax.swing.event.ListSelectionEvent;
  30. import javax.swing.event.ListSelectionListener;
  31. import net.miginfocom.swing.MigLayout;
  32. /**
  33. * Simple panel to reorder a JList using buttons.
  34. *
  35. * @param <T> Type contained in the list
  36. */
  37. public class ListReorderButtonPanel<T> extends JPanel implements
  38. ListSelectionListener, ActionListener {
  39. /** A version number for this class. */
  40. private static final long serialVersionUID = 1;
  41. /** List to reorder. */
  42. private final JList<T> list;
  43. /** Default list model. */
  44. private final GenericListModel<T> model;
  45. /** Up button. */
  46. private final JButton up;
  47. /** Down button. */
  48. private final JButton down;
  49. /**
  50. * Creates a panel to reorder a JList.
  51. *
  52. * @param list JList
  53. */
  54. public ListReorderButtonPanel(final ReorderableJList<T> list) {
  55. this.list = list;
  56. this.model = list.getModel();
  57. list.addListSelectionListener(this);
  58. up = new JButton("/\\");
  59. down = new JButton("\\/");
  60. up.addActionListener(this);
  61. down.addActionListener(this);
  62. setLayout(new MigLayout("fill"));
  63. add(up, "wrap");
  64. add(down, "");
  65. valueChanged(null);
  66. }
  67. @Override
  68. public void valueChanged(final ListSelectionEvent e) {
  69. up.setEnabled(list.getSelectedIndex() != -1);
  70. down.setEnabled(list.getSelectedIndex() != -1);
  71. }
  72. @Override
  73. public void actionPerformed(final ActionEvent e) {
  74. final T object = list.getSelectedValue();
  75. final int currentIndex = list.getSelectedIndex();
  76. if (e.getSource() == up) {
  77. moveUp(object, currentIndex);
  78. } else {
  79. moveDown(object, currentIndex);
  80. }
  81. }
  82. /**
  83. * Moves an object up in the list.
  84. *
  85. * @param object Object to move
  86. * @param currentIndex Current index
  87. */
  88. private void moveUp(final T object, final int currentIndex) {
  89. int newIndex = currentIndex - 1;
  90. if (newIndex < 0) {
  91. newIndex = model.getSize() - 1;
  92. }
  93. moveElement(object, newIndex);
  94. }
  95. /**
  96. * Moves an object down in the list.
  97. *
  98. * @param object Object to move
  99. * @param currentIndex Current index
  100. */
  101. private void moveDown(final T object, final int currentIndex) {
  102. int newIndex = currentIndex + 1;
  103. if (newIndex > model.getSize() - 1) {
  104. newIndex = 0;
  105. }
  106. moveElement(object, newIndex);
  107. }
  108. /**
  109. * Moves an object from its current position to a new position in a list.
  110. *
  111. * @param object Object to move
  112. * @param newIndex Current index
  113. */
  114. private void moveElement(final T object, final int newIndex) {
  115. model.remove(object);
  116. model.add(newIndex, object);
  117. list.setSelectedIndex(newIndex);
  118. }
  119. }