fiore@0: /* fiore@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool fiore@0: fiore@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/) fiore@0: fiore@0: This program is free software: you can redistribute it and/or modify fiore@0: it under the terms of the GNU General Public License as published by fiore@0: the Free Software Foundation, either version 3 of the License, or fiore@0: (at your option) any later version. fiore@0: fiore@0: This program is distributed in the hope that it will be useful, fiore@0: but WITHOUT ANY WARRANTY; without even the implied warranty of fiore@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fiore@0: GNU General Public License for more details. fiore@0: fiore@0: You should have received a copy of the GNU General Public License fiore@0: along with this program. If not, see . fiore@0: */ fiore@0: package uk.ac.qmul.eecs.ccmi.gui; fiore@0: fiore@0: import java.awt.event.ItemEvent; fiore@0: import java.awt.event.KeyEvent; fiore@0: import java.util.Vector; fiore@0: fiore@0: import javax.swing.ComboBoxModel; fiore@0: import javax.swing.JComboBox; fiore@0: fiore@0: /** fiore@0: * A ComboBox component which overrides the default behaviour when selecting items by the keyboard fiore@0: * up and down arrow keys. When the top is reached, if the user presses the up arrow key fiore@0: * instead of blocking on the first item, the LoopComboBox loops forward to the last item. Likewise, fiore@0: * when the bottom is reached and the user presses the down arrow key the LoopComboBox fiore@0: * loops back to the first item. fiore@0: * fiore@0: */ fiore@0: @SuppressWarnings("serial") fiore@0: public class LoopComboBox extends JComboBox { fiore@0: public LoopComboBox(){ fiore@0: super(); fiore@0: } fiore@0: fiore@0: public LoopComboBox(Object[] items){ fiore@0: super(items); fiore@0: } fiore@0: fiore@0: public LoopComboBox(ComboBoxModel aModel){ fiore@0: super(aModel); fiore@0: } fiore@0: fiore@0: public LoopComboBox(Vector items){ fiore@0: super(items); fiore@0: } fiore@0: fiore@0: @Override fiore@0: public void processKeyEvent(KeyEvent e) { fiore@0: if(dataModel.getSize() == 0){ fiore@0: super.processKeyEvent(e); fiore@0: }else{ fiore@0: if(e.getKeyCode() == KeyEvent.VK_DOWN fiore@0: && e.getID()==KeyEvent.KEY_PRESSED fiore@0: && getSelectedIndex() == getItemCount()-1){ fiore@0: setSelectedIndex(0); fiore@0: if(getItemCount() == 1) fiore@0: fireOneItemStateChanged(); fiore@0: }else if(e.getKeyCode() == KeyEvent.VK_UP fiore@0: && e.getID()==KeyEvent.KEY_PRESSED fiore@0: && getSelectedIndex() == 0){ fiore@0: setSelectedIndex(getItemCount()-1); fiore@0: if(getItemCount() == 1) fiore@0: fireOneItemStateChanged(); fiore@0: }else fiore@0: super.processKeyEvent(e); fiore@0: } fiore@0: } fiore@0: fiore@5: /* fiore@0: * when the comboBox has only one item the ItemStateChanged listeners ain't fired by default. fiore@0: * This behaviour has to be forced in order to have the item label to be spoken out by fiore@0: * the narrator, in spite of the item number . fiore@0: */ fiore@0: private void fireOneItemStateChanged(){ fiore@0: fireItemStateChanged(new ItemEvent( fiore@0: this, fiore@0: ItemEvent.ITEM_STATE_CHANGED, fiore@0: getSelectedItem(), fiore@0: ItemEvent.SELECTED fiore@0: )); fiore@0: } fiore@0: }