view java/src/uk/ac/qmul/eecs/ccmi/gui/LoopComboBox.java @ 8:ea7885bd9bff tip

fixed bug : render solid line as dotted/dashed when moving the stylus from dotted/dashed to solid
author ccmi-guest
date Thu, 03 Jul 2014 16:12:20 +0100
parents d66dd5880081
children
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package uk.ac.qmul.eecs.ccmi.gui;

import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;

/**
 * A ComboBox component which overrides the default behaviour when selecting items by the keyboard
 * up and down arrow keys. When the top is reached, if the user presses the up arrow key  
 * instead of blocking on the first item, the LoopComboBox loops forward to the last item. Likewise,
 * when the bottom is reached and the user presses the down arrow key the LoopComboBox 
 * loops back to the first item.  
 *
 */
@SuppressWarnings("serial")
public class LoopComboBox extends JComboBox {
	public LoopComboBox(){
		super();
	}
	
	public LoopComboBox(Object[] items){
		super(items);
	}
	
	public LoopComboBox(ComboBoxModel aModel){
		super(aModel);
	}
	
	public LoopComboBox(Vector<?> items){
		super(items);
	}
	
	@Override
	public void	processKeyEvent(KeyEvent e) {
		if(dataModel.getSize() == 0){
			super.processKeyEvent(e);
		}else{
			if(e.getKeyCode() == KeyEvent.VK_DOWN 
					&& e.getID()==KeyEvent.KEY_PRESSED 
					&& getSelectedIndex() == getItemCount()-1){
				setSelectedIndex(0);
				if(getItemCount() == 1)
					fireOneItemStateChanged();
			}else if(e.getKeyCode() == KeyEvent.VK_UP 
					&& e.getID()==KeyEvent.KEY_PRESSED 
					&& getSelectedIndex() == 0){
				setSelectedIndex(getItemCount()-1);
				if(getItemCount() == 1)
					fireOneItemStateChanged();
			}else
				super.processKeyEvent(e);
		}
	}
	
	/*
	 * when the comboBox has only one item the ItemStateChanged listeners ain't fired by default.
	 * This behaviour has to be forced in order to have the item label to be spoken out by  
	 * the narrator, in spite of the item number .  
	 */
	private void fireOneItemStateChanged(){
		fireItemStateChanged(new ItemEvent(
				this,
				ItemEvent.ITEM_STATE_CHANGED,
				getSelectedItem(),
				ItemEvent.SELECTED
		));
	}
}