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