errorlist.java

来自「主要是对串口驱动的的一些控制源码!!! 在下载javacomm20-win32」· Java 代码 · 共 263 行

JAVA
263
字号
/**
 * $Log: ErrorList.java,v $
 * Revision 1.4  2003/01/30 14:40:59  mwulff
 * no message
 *
 * Revision 1.3  2003/01/30 10:19:49  willaxt
 * adjusted to InputValidatorItem
 *
 * Revision 1.2  2003/01/17 15:55:03  mwulff
 * now JKFUtilities is used to postion the Frame
 *
 * Revision 1.1  2003/01/16 13:10:54  mwulff
 * initial version
 *
 * Revision 1.6  2003/01/03 15:33:24  mwulff
 * no message
 *
 * Revision 1.5  2003/01/02 18:47:39  mwulff
 * no message
 *
 * Revision 1.4  2002/12/20 18:50:53  mwulff
 * no message
 *
 * Revision 1.3  2002/12/15 16:53:18  mwulff
 * no message
 *
 * Revision 1.2  2002/12/08 17:09:37  mwulff
 * no message
 *
 * Revision 1.1  2002/12/08 11:23:38  mwulff
 * initial version
 *
 */

package de.fhm.jkf.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import de.fhm.jkf.utils.cl.JKFUtilities;

/**
 * @author	marten wulff
 * 
 * * <br><br><center><table border="1" width="80%"><hr>
 * <strong><a href="http://jkf.sourceforge.net">The JKF Project</a></strong>
 * <p>
 * Copyright (C) 2002 by Marten Wulff
 * <p>
 * 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.
 * <p>
 * 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.
 * <p>
 * You should have received a copy of the <a href="http://www.gnu.org/copyleft/lesser.html">
 * GNU Lesser General Public License</a> along with this library; if not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 * <hr></table></center>
 * @stereotype implementationClass
 */
public class ErrorList extends JList implements ErrorHandler {

	private static ErrorHandler instance = null;
	private static Object semaphore = new Object();

	private static String ERRORLIST_TITLE = "Tasks";

	private JFrame display = null;

	private DefaultListModel model = null;
	private int currentSelectedIndex = -1;

	private static JKFAbstractMainApplication mainApp = null;

	/**
	 * Method for retrieving an instance for the singleton 
	 * <code>ErrorList</code>.
	 */
	static ErrorHandler instance() {
		if (mainApp == null) {
			throw new IllegalStateException("ErrorHandler is not initialized");
		}
		if (instance == null) {
			synchronized (semaphore) {
				if (instance == null) {
					instance = new ErrorList();
				}
			}
		}

		return instance;
	}

	private ErrorList() {
		super();

		init();
	}

	/**
	 * Has to be called from the JKFAbstractMainApplication
	 * constructor, so the ErrorList can find the SubApplication
	 * to activate.
	 */
	static void setMainApplication(JKFAbstractMainApplication mainApp) {
		ErrorList.mainApp = mainApp;
	}
	
	public void removeSelection() {
		this.removeSelectionInterval(0, model.size());
		this.invalidate();
		this.repaint();
	}

	private void init() {

		model = new DefaultListModel();
		this.setModel(model);
		this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.setSelectionForeground(Color.blue);
		this.addListSelectionListener(new ErrorSelectionListener());
		this.setBackground(Color.LIGHT_GRAY);

		display = new JFrame();
		display.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		display.setUndecorated(true);
		display.setTitle(ERRORLIST_TITLE);

		Container c = display.getContentPane();
		c.setLayout(new BorderLayout());
		JScrollPane scroll = new JScrollPane();
		scroll.getViewport().setView(this);
		c.add(scroll, BorderLayout.CENTER);

		display.setSize(600, 150);
		JKFUtilities.lowerLeftComponent(null, display);
	}

	/**
	 * Set's the result of a validation in this error list.
	 * 
	 * @param item <code>InputValidatorItem</code> as result of a validation.
	 */
	public void setValidationResult(InputValidatorItem item) {
		if (item.hasError()) {
			setValidationItem(item);
		} else {
			removeValidationItem(item);
		}
	}

	/**
	 * Method for adding a new <code>InputValidatorItem</code> to this <code>
	 * ErrorHandler</code>. If the <codeInputValidatorItem</code> already exists
	 * for this <code>ErrorHandler</code>, that means an <code>InputValidatorItem</code>
	 * which is produced by the same <code>Validatable</code>, than it's updated.
	 * 
	 * @param toSet the error
	 */
	private void setValidationItem(InputValidatorItem toSet) {

		if (!display.isVisible()) {
			display.setVisible(true);
		}

		// let's look if we have already displayed an
		// error for the same component
		int index = model.indexOf(toSet);

		// if the error already exists, update it
		if (index > -1) {
			model.setElementAt(toSet, index);
		} else {
			// add the new error
			model.addElement(toSet);
			index = model.size() - 1;
		}
	}

	/**
	 * Removes an <code>InputValidatorItem</code> from this ErrorHanlder.
	 * 
	 * @param toRemove	the <code>InputValidatorItem</code> that should
	 * remove the corresponding <code>Validatable</code>S must be equal.
	 */
	private void removeValidationItem(InputValidatorItem toRemove) {
		int index = model.indexOf(toRemove);

		if (index > -1) {
			toRemove.getValidatable().unmarkError();
			model.remove(index);
		}

		if (model.size() == 0) {
			display.setVisible(false);
		}
	}

	/**
	 * Inner class for handling ListSelectionEvents. If the selection
	 * of the list is changed, first the new selected InputValidatorItem object
	 * is retrieved and than highlighted the component on which the error
	 * occured.
	 */
	private class ErrorSelectionListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			InputValidatorItem item;

			if (!e.getValueIsAdjusting()) {
				if (currentSelectedIndex != -1
					&& currentSelectedIndex < model.size()) {
					item =
						(InputValidatorItem) model.getElementAt(
							currentSelectedIndex);
					item.getValidatable().unmarkError();
				}
				currentSelectedIndex = ErrorList.this.getSelectedIndex();
				if (currentSelectedIndex > -1
					&& currentSelectedIndex < model.size()) {
					item =
						(InputValidatorItem) model.getElementAt(
							currentSelectedIndex);
					mainApp.showSubApplicationForError(item.getValidatable().getValidatableComponent());
					item.getValidatable().markError();
				}

			}
		}

		private Component getJKFViewForComponent(Component c) {
			if (c == null) {
				return null;
			}

			while (!(c instanceof JKFView)) {
				c = c.getParent();
				if (c == null) {
					break;
				}
			}

			return c;
		}
	}
}

⌨️ 快捷键说明

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