source: src/main/java/weka/gui/JListHelper.java @ 28

Last change on this file since 28 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 4.8 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * JListHelper.java
19 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23
24package weka.gui;
25
26import javax.swing.DefaultListModel;
27import javax.swing.JList;
28
29/**
30 * A helper class for JList GUI elements with DefaultListModel or
31 * derived models.
32 *
33 * @author  FracPete (fracpete at waikato dot ac dot nz)
34 * @version $Revision: 1.1 $
35 * @see     JList
36 * @see     DefaultListModel
37 */
38public class JListHelper {
39 
40  /** moves items up */
41  public final static int MOVE_UP = 0;
42
43  /** moves items down */
44  public final static int MOVE_DOWN = 1;
45 
46  /**
47   * moves the selected items by a certain amount of items in a given direction
48   *
49   * @param list        the JList to work on
50   * @param moveby      the number of items to move by
51   * @param direction   the direction to move in
52   * @see               #MOVE_UP
53   * @see               #MOVE_DOWN
54   */
55  protected static void moveItems(JList list, int moveby, int direction) {
56    int[]               indices;
57    int                 i;
58    Object              o;
59    DefaultListModel    model;
60
61    model = (DefaultListModel) list.getModel();
62
63    switch (direction) {
64      case MOVE_UP:
65        indices = list.getSelectedIndices();
66        for (i = 0; i < indices.length; i++) {
67          if (indices[i] == 0)
68            continue;
69          o = model.remove(indices[i]);
70          indices[i] -= moveby;
71          model.insertElementAt(o, indices[i]);
72        }
73        list.setSelectedIndices(indices);
74        break;
75
76      case MOVE_DOWN:
77        indices = list.getSelectedIndices();
78        for (i = indices.length - 1; i >= 0; i--) {
79          if (indices[i] == model.getSize() - 1)
80            continue;
81          o = model.remove(indices[i]);
82          indices[i] += moveby;
83          model.insertElementAt(o, indices[i]);
84        }
85        list.setSelectedIndices(indices);
86        break;
87
88      default:
89        System.err.println(
90            JListHelper.class.getName() + ": direction '" 
91            + direction + "' is unknown!");
92    }
93  }
94
95  /**
96   * moves the selected items up by 1
97   *
98   * @param list        the JList to work on
99   */
100  public static void moveUp(JList list) {
101    if (canMoveUp(list))
102      moveItems(list, 1, MOVE_UP);
103  }
104
105  /**
106   * moves the selected item down by 1
107   *
108   * @param list        the JList to work on
109   */
110  public static void moveDown(JList list) {
111    if (canMoveDown(list))
112      moveItems(list, 1, MOVE_DOWN);
113  }
114
115  /**
116   * moves the selected items to the top
117   *
118   * @param list        the JList to work on
119   */
120  public static void moveTop(JList list) {
121    int[]     indices;
122    int       diff;
123
124    if (canMoveUp(list)) {
125      indices = list.getSelectedIndices();
126      diff    = indices[0];
127      moveItems(list, diff, MOVE_UP);
128    }
129  }
130
131  /**
132   * moves the selected items to the end
133   *
134   * @param list        the JList to work on
135   */
136  public static void moveBottom(JList list) {
137    int[]     indices;
138    int       diff;
139
140    if (canMoveDown(list)) {
141      indices = list.getSelectedIndices();
142      diff    = list.getModel().getSize() - 1 - indices[indices.length - 1];
143      moveItems(list, diff, MOVE_DOWN);
144    }
145  }
146
147  /**
148   * checks whether the selected items can be moved up
149   *
150   * @param list        the JList to work on
151   */
152  public static boolean canMoveUp(JList list) {
153    boolean   result;
154    int[]     indices;
155
156    result = false;
157   
158    indices = list.getSelectedIndices();
159    if (indices.length > 0) {
160      if (indices[0] > 0)
161        result = true;
162    }
163
164    return result;
165  }
166
167  /**
168   * checks whether the selected items can be moved down
169   *
170   * @param list        the JList to work on
171   */
172  public static boolean canMoveDown(JList list) {
173    boolean   result;
174    int[]     indices;
175
176    result = false;
177   
178    indices = list.getSelectedIndices();
179    if (indices.length > 0) {
180      if (indices[indices.length - 1] < list.getModel().getSize() - 1)
181        result = true;
182    }
183
184    return result;
185  }
186}
Note: See TracBrowser for help on using the repository browser.