/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Markus Kraetzig
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.ts;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
/**
* A KeyListener that is especially designed to be used with the
* TSList. It is meant to be shared between TSList
* instances, but can also be created and subclassed if necessary.
*
* @author Markus Kraetzig
*/
public class DefaultTSKeyListener extends KeyAdapter {
private final String name;
/**
* Default instance that can be shared between TSList instances.
*/
private static final DefaultTSKeyListener defaultTSKeyListener = new DefaultTSKeyListener(
"DEFAULT_TSList_KeyListener");
/**
* Creates a DefaultTSKeyListener with a descriptive name.
* Use the constructor only, if you need a instance that should be different
* from getSharedInstance via subclassing.
*
*/
public DefaultTSKeyListener(String name) {
super();
this.name = name;
}
/**
* Returns an instance of the DefaultTSKeyListener, that can
* be shared across all TSList instances. It is also possible
* to create subclasses that can be individually configured and set to a
* TSList that should have a different
* KeyListener.
*
* @return an instance of this class that can be shared
*/
public static DefaultTSKeyListener getSharedInstance() {
return defaultTSKeyListener;
}
/**
* The following keys are recognized:
* KeyEvent.VK_DELETE- all selected series are deleted
*
* @param keyEvent
* a key event
*/
public void keyPressed(KeyEvent keyEvent) {
Object obj = keyEvent.getSource();
if (!(obj instanceof TSList))
return;
TSList tsList = (TSList) obj;
if (keyEvent.getKeyChar() == KeyEvent.VK_DELETE)
TSActionTypes.DELETE.doAction(tsList);
// Ctrl+A KeyEvent, fire selection event as well
if (keyEvent.getKeyCode() == java.awt.event.KeyEvent.VK_A
&& keyEvent.getModifiers() == java.awt.event.InputEvent.CTRL_MASK) {
if (tsList.getModel().getSize() > 0) {
int[] indices = new int[tsList.getModel().getSize()];
for (int i = 0; i < indices.length; i++)
indices[i] = i;
tsList.setSelectedIndices(indices);
}
}
}
/**
* Returns a string representation of DefaultTSKeyListener.
*
* @return string representation
*/
public String toString() {
return name;
}
}
Ads help maintain this website.