PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/CommonControls/common/gui/SelectionListPanel.java

#
Java | 190 lines | 109 code | 23 blank | 58 comment | 6 complexity | 0ce7d52b6eb35dbbc59a231c92140863 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. package common.gui;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.util.*;
  6. import org.gjt.sp.jedit.*;
  7. import common.gui.actions.*;
  8. import common.gui.util.*;
  9. /**
  10. * A panel with two lists, allowing the user to move items between them.
  11. * All methods ensure that an element cannot appear in both lists simultaneously.
  12. *
  13. * @author mace
  14. * @version $Revision: 21349 $ modified $Date: 2012-03-13 17:07:11 +0100 (Tue, 13 Mar 2012) $ by $Author: ezust $
  15. * @see org.gjt.sp.jedit.gui.PingPongList
  16. * @deprecated use org.gjt.sp.jedit.gui.PingPongList
  17. *
  18. */
  19. @Deprecated
  20. public class SelectionListPanel extends JPanel {
  21. /** The constant referring to the left list */
  22. public static final int LEFT = 0;
  23. /** The constant referring to the right list */
  24. public static final int RIGHT = 1;
  25. protected String title = "Select some options";
  26. protected ConstraintFactory cf;
  27. protected JButton moveRight;
  28. protected JButton moveLeft;
  29. protected ListPanel[] lists = new ListPanel[2];
  30. /**
  31. * Creates a basic panel.
  32. * You will want to use the other methods to customize the panel before using it.
  33. */
  34. public SelectionListPanel() {
  35. cf = new ConstraintFactory();
  36. setLayout(new GridBagLayout());
  37. setBorder(BorderFactory.createEtchedBorder());
  38. for (int i = 0; i < lists.length; i++) {
  39. lists[i] = new ListPanel("List " + i);
  40. }
  41. add(lists[LEFT], cf.buildConstraints(0, 0, 10, 20, cf.CENTER, cf.BOTH, 100, 100));
  42. add(buildMovePanel(), cf.buildConstraints(10, 0, 2, 10, cf.CENTER, cf.V, 0, 100));
  43. add(lists[RIGHT], cf.buildConstraints(12, 0, 10, 20, cf.CENTER, cf.BOTH, 100, 100));
  44. }
  45. private JPanel buildMovePanel() {
  46. CustomAction moveRightAction =
  47. new CustomAction("Move Right", GUIUtilities.loadIcon("ArrowR.png")) {
  48. public void actionPerformed(ActionEvent ae) {
  49. moveElements(LEFT, RIGHT);
  50. }
  51. };
  52. moveRight = new JButton(moveRightAction);
  53. moveRight.setText("");
  54. CustomAction moveLeftAction =
  55. new CustomAction("Move Left", GUIUtilities.loadIcon("ArrowL.png")) {
  56. public void actionPerformed(ActionEvent ae) {
  57. moveElements(RIGHT, LEFT);
  58. }
  59. };
  60. moveLeft = new JButton(moveLeftAction);
  61. moveLeft.setText("");
  62. JPanel panel = new JPanel(new GridBagLayout());
  63. panel.add(moveRight, cf.buildConstraints(0, 0, 1, 1, cf.S, cf.NONE, 0, 0));
  64. panel.add(moveLeft, cf.buildConstraints(0, 1, 1, 1, cf.N, cf.NONE, 0, 0));
  65. return panel;
  66. }
  67. /**
  68. * Returns the list which is not the given list.
  69. */
  70. protected int otherList(int list) {
  71. return Math.abs(list-1);
  72. }
  73. protected void addElement(int list, Object element) {
  74. removeElement(otherList(list),element);
  75. lists[list].addElement(element);
  76. }
  77. protected boolean removeElement(int list, Object element) {
  78. return lists[list].removeElement(element);
  79. }
  80. protected void moveElements(int srcList, int destList) {
  81. Object[] selected = lists[srcList].getSelectedValues();
  82. for (int i = 0; i < selected.length; i++) {
  83. int index = lists[srcList].getLastSelectedIndex();
  84. removeElement(srcList, selected[i]);
  85. addElement(destList, selected[i]);
  86. lists[srcList].setSelectedIndex(index);
  87. }
  88. }
  89. /**
  90. * Saves a list to a property array (first item stored in "propertyName.0"). All elements are treated as strings.
  91. *
  92. * @param list Description of the Parameter
  93. * @param propertyName Description of the Parameter
  94. */
  95. public void saveToPropertyAsString(int list, String propertyName) {
  96. Object[] elements = lists[list].toArray();
  97. int i;
  98. for (i = 0; i < elements.length; i++) {
  99. jEdit.setProperty(propertyName + "." + i, elements[i].toString());
  100. }
  101. jEdit.unsetProperty(propertyName+"."+i);
  102. }
  103. public void loadFromPropertyAsString(int list, String propertyName) {
  104. ArrayList elements = new ArrayList();
  105. int index = 0;
  106. String s = jEdit.getProperty(propertyName + "." + index);
  107. index++;
  108. while (s != null) {
  109. addElement(list, new String(s));
  110. s = jEdit.getProperty(propertyName + "." + index);
  111. index++;
  112. }
  113. }
  114. //{{{ Mutators
  115. /**
  116. * Sets the title of the panel.
  117. *
  118. * @param text The new title value
  119. */
  120. public void setTitle(String text) {
  121. title = text;
  122. setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title));
  123. }
  124. /**
  125. * Sets the label for a list.
  126. *
  127. * @param list LEFT or RIGHT
  128. * @param text The new list label
  129. */
  130. public void setListLabel(int list, String text) {
  131. lists[list].setLabel(text);
  132. }
  133. /**
  134. * Sets the contents of a list to the given options.
  135. *
  136. * @param list LEFT or RIGHT
  137. * @param options The new listContents value
  138. */
  139. public void setListContents(int list, Object[] options) {
  140. int otherList = Math.abs(list - 1);
  141. for (int i = 0; i < options.length; i++) {
  142. removeElement(otherList, options[i]);
  143. addElement(list, options[i]);
  144. }
  145. }
  146. //}}}
  147. //{{{ Accessors
  148. /**
  149. * Returns all selected values in the given list.
  150. *
  151. * @param list LEFT or RIGHT
  152. * @return all selected values in the given list
  153. */
  154. public Object[] getSelectedValues(int list) {
  155. return lists[list].getSelectedValues();
  156. }
  157. /**
  158. * Returns all values in the given list.
  159. *
  160. * @param list LEFT or RIGHT
  161. * @return all values in the given list
  162. */
  163. public Object[] getValues(int list) {
  164. return lists[list].toArray();
  165. }
  166. //}}}
  167. }