📄 integercelleditor.java
字号:
/*
* 08/10/2005
*
* IntegerCellEditor.java - Editor component for ints.
* Copyright (C) 2005 Robert Futrell
* email@address.com
* www.website.com
*
* 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 2
* of the License, or 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.fife.ui.propertysheet.editors;
import java.awt.Component;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import javax.swing.DefaultCellEditor;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import org.fife.ui.propertysheet.PropertySheetManager;
import org.fife.ui.propertysheet.PropertySheetUtilities;
import org.fife.ui.propertysheet.infos.IntegerPropertyInfo;
/**
* Table cell editor for integers.
*
* @author Robert Futrell
* @version 1.0
*/
public class IntegerCellEditor extends DefaultCellEditor {
/**
*
*/
private static final long serialVersionUID = -7036077578914401796L;
protected IntegerPropertyInfo cachedInfo;
/*****************************************************************************/
public IntegerCellEditor() {
super(PropertySheetUtilities.createTextField());
setClickCountToStart(1);
}
/*****************************************************************************/
protected boolean doCancel(String param, String errorDescription) {
return doCancel(new String[] { param }, errorDescription);
}
/*****************************************************************************/
protected boolean doCancel(String[] params, String errorDescription) {
// They can either click "OK" and continue the edit, or click
// "Cancel" or x-out the dialog and cancel their edit.
ResourceBundle msg = PropertySheetManager.getInstance().
getErrorBundle();
String desc = msg.getString(errorDescription);
if (params!=null) {
desc = MessageFormat.format(desc, params);
}
int rc = JOptionPane.showConfirmDialog(null,
msg.getString("Editor.ErrorMessage.General") + "\n" +
desc,
msg.getString("Editor.ErrorMessage.Title"),
JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE);
boolean doCancel = rc!=JOptionPane.OK_OPTION;
if (doCancel)
fireEditingCanceled(); // Revert to previous valid value.
return doCancel;
}
/*****************************************************************************/
public Object getCellEditorValue() {
int value = 0;
try {
String text = (String)((JTextField)editorComponent).getText();
value = Integer.parseInt(text);
} catch (NumberFormatException nfe) { // Should never happen.
value = Integer.MIN_VALUE;
}
cachedInfo.setValue(new Integer(value));
return cachedInfo;
}
/*****************************************************************************/
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
// DefaultCellEditor uses "value" as the initial text of the text
// field.
cachedInfo = (IntegerPropertyInfo)value;
return super.getTableCellEditorComponent(table,
cachedInfo.getValue().toString(),
isSelected, row, column);
}
/*****************************************************************************/
/**
* Called when an editor is about to stop editing. This method is used
* to determine whether or not the editing should actually stop. It
* checks whether the entered value is a Java integer; if not, an error
* is displayed and editing continues.
*
* @return Whether or not editing should cease.
*/
public boolean stopCellEditing() {
JTextField component = (JTextField)getComponent();
String text = component.getText();
int value = 0;
// First, just make sure that what they entered was an int.
try {
value = Integer.parseInt(text);
// Check bounds, if any are set.
} catch (NumberFormatException nfe) {
return doCancel(text, "Editor.ErrorMessage.Integer");
}
// Then, if this int property has special bounds, check them too.
IntegerPropertyInfo.IntegerBounds bounds = cachedInfo.getBounds();
if (bounds!=null) {
int min = bounds.getMinValue();
int max = bounds.getMaxValue();
if (value<min || value>max) {
return doCancel(new String[] { ""+min, ""+max, },
"Editor.ErrorMessage.Integer.Bounds");
}
}
// Otherwise, just do normal checking for editor canceling.
return super.stopCellEditing();
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -