⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rectanglecelleditor.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 08/10/2005
 *
 * RectangleCellEditor.java - Editor component for rectangles.
 * 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.awt.Rectangle;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.regex.Matcher;
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.PropertyInfo;


/**
 * Table cell editor for <code>Rectangle</code>s.
 *
 * @author Robert Futrell
 * @version 1.0
 */
public class RectangleCellEditor extends DefaultCellEditor {

	/**
	 * 
	 */
	private static final long serialVersionUID = -304968116249966264L;

	protected PropertyInfo cachedInfo;
	private Pattern pattern;


/*****************************************************************************/


	public RectangleCellEditor() {
		super(PropertySheetUtilities.createTextField());
		setClickCountToStart(1);
		try {
			pattern = Pattern.compile("\\s*\\[\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\]\\s*");
		} catch (PatternSyntaxException pse) {
			// Should never happen.
			pse.printStackTrace();
		}
	}


/*****************************************************************************/


	protected boolean doCancel(String badValue, 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 = MessageFormat.format(
				msg.getString(errorDescription),
				new Object[] { badValue });
		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() {
		String text = ((JTextField)editorComponent).getText();
		Matcher m = pattern.matcher(text);
		if (m.matches()) {
			Rectangle r = (Rectangle)cachedInfo.getValue();
			try {
				r.x = Integer.parseInt(m.group(1));
				r.y = Integer.parseInt(m.group(2));
				r.width = Integer.parseInt(m.group(3));
				r.height = Integer.parseInt(m.group(4));
			} catch (NumberFormatException nfe) {
				// Should never happen.
				nfe.printStackTrace();
			}
		}
		else {
			// Should never happen.
		}
		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 = (PropertyInfo)value;
		String displayText = PropertySheetUtilities.getRectangleString(
									(Rectangle)cachedInfo.getValue());
		return super.getTableCellEditorComponent(table,
										displayText,
										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();
		Matcher m = pattern.matcher(text);
		boolean matches = m.matches();
		if (!matches)
			return doCancel(text, "Editor.ErrorMessage.Rectangle");
		return super.stopCellEditing();
	}


/*****************************************************************************/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -