comparison java/src/uk/ac/qmul/eecs/ccmi/gui/LoopComboBox.java @ 0:78b7fc5391a2

first import, outcome of NIME 2014 hackaton
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 16:28:59 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78b7fc5391a2
1 /*
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
3
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package uk.ac.qmul.eecs.ccmi.gui;
20
21 import java.awt.event.ItemEvent;
22 import java.awt.event.KeyEvent;
23 import java.util.Vector;
24
25 import javax.swing.ComboBoxModel;
26 import javax.swing.JComboBox;
27
28 /**
29 * A ComboBox component which overrides the default behaviour when selecting items by the keyboard
30 * up and down arrow keys. When the top is reached, if the user presses the up arrow key
31 * instead of blocking on the first item, the LoopComboBox loops forward to the last item. Likewise,
32 * when the bottom is reached and the user presses the down arrow key the LoopComboBox
33 * loops back to the first item.
34 *
35 */
36 @SuppressWarnings("serial")
37 public class LoopComboBox extends JComboBox {
38 public LoopComboBox(){
39 super();
40 }
41
42 public LoopComboBox(Object[] items){
43 super(items);
44 }
45
46 public LoopComboBox(ComboBoxModel aModel){
47 super(aModel);
48 }
49
50 public LoopComboBox(Vector<?> items){
51 super(items);
52 }
53
54 @Override
55 public void processKeyEvent(KeyEvent e) {
56 if(dataModel.getSize() == 0){
57 super.processKeyEvent(e);
58 }else{
59 if(e.getKeyCode() == KeyEvent.VK_DOWN
60 && e.getID()==KeyEvent.KEY_PRESSED
61 && getSelectedIndex() == getItemCount()-1){
62 setSelectedIndex(0);
63 if(getItemCount() == 1)
64 fireOneItemStateChanged();
65 }else if(e.getKeyCode() == KeyEvent.VK_UP
66 && e.getID()==KeyEvent.KEY_PRESSED
67 && getSelectedIndex() == 0){
68 setSelectedIndex(getItemCount()-1);
69 if(getItemCount() == 1)
70 fireOneItemStateChanged();
71 }else
72 super.processKeyEvent(e);
73 }
74 }
75
76 /*
77 * when the comboBox has only one item the ItemStateChanged listeners ain't fired by default.
78 * This behaviour has to be forced in order to have the item label to be spoken out by
79 * the narrator, in spite of the item number .
80 */
81 private void fireOneItemStateChanged(){
82 fireItemStateChanged(new ItemEvent(
83 this,
84 ItemEvent.ITEM_STATE_CHANGED,
85 getSelectedItem(),
86 ItemEvent.SELECTED
87 ));
88 }
89 }