net.sf.sfac.gui.utils.ComboBoxKeyManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfac-core Show documentation
Show all versions of sfac-core Show documentation
This project is the core part of the Swing Framework and Components (SFaC).
It contains the Swing framework classes and the graphical component classes.
The newest version!
/*-------------------------------------------------------------------------
Copyright 2009 Olivier Berlanger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-------------------------------------------------------------------------*/
package net.sf.sfac.gui.utils;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import net.sf.sfac.string.StringUtils;
/**
* An simple implementation of JComboBox.KeySelectionManager.
* It allows to:
*
* - Un-select the element (= select 'null' element) by pressing 'backspace' or 'delete'
*
- Go (select) to an element by typing the first letters of it (case-insensitive).
*
*
* This class can also be reused in other context for:
*
* - It's ability to combine characters when typed with less that one second of interval (method
getStringStart
)
* - It's comparison method ignoring case and diacritics (method
startsWithFormatted
)
*
*
*
* @author Olivier Berlanger
*/
public class ComboBoxKeyManager implements JComboBox.KeySelectionManager {
private static final long TIME_DIFF = 800;
private long lastKeyTimestamp;
private String stringStart;
public ComboBoxKeyManager() {
}
public int selectionForKey(char aKey, ComboBoxModel model) {
String cmpString = getStringStart(aKey);
if ((aKey == 127) || (aKey == 8)) {
// special cases: the 'back' or 'del' keys
model.setSelectedItem(null);
} else {
// search the string
int i;
int len = model.getSize();
if (len > 0) {
String str;
for (i = 0; i < len; i++) {
Object element = model.getElementAt(i);
str = (element == null) ? null : element.toString();
if (startsWithFormatted(str, cmpString)) return i;
}
}
}
return -1;
}
public String getStringStart(char aKey) {
long curTime = System.currentTimeMillis();
if ((aKey < 32) || (aKey == 127)) {
stringStart = "";
} else {
// calculate the search string
aKey = formatChar(aKey);
if ((curTime - lastKeyTimestamp) < TIME_DIFF) stringStart += aKey;
else stringStart = String.valueOf(aKey);
}
lastKeyTimestamp = curTime;
return stringStart;
}
public static boolean startsWithFormatted(String tested, String prefix) {
int len = prefix.length();
if (len == 0) return true;
if ((tested == null) || (len > tested.length())) return false;
for (int i = 0; i < len; i++) {
if (formatChar(tested.charAt(i)) != prefix.charAt(i)) return false;
}
return true;
}
private static char formatChar(char aKey) {
aKey = StringUtils.removeDiacritic(aKey);
aKey = Character.toUpperCase(aKey);
return aKey;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy