001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020:
021: package com.salmonllc.swing;
022:
023: import java.awt.Component;
024: import java.awt.Container;
025:
026: import javax.swing.DefaultListModel;
027: import javax.swing.Icon;
028: import javax.swing.JLabel;
029: import javax.swing.JList;
030: import javax.swing.JPanel;
031: import javax.swing.JScrollPane;
032: import javax.swing.ListCellRenderer;
033: import javax.swing.ListModel;
034: import javax.swing.SwingConstants;
035:
036: import com.salmonllc.sql.DataStoreBuffer;
037: import com.salmonllc.sql.ModelChangedEvent;
038: import com.salmonllc.sql.ModelChangedListener;
039:
040: /**
041: * This component will watch for changes in a model, after each model change it will search through a specifed panel and find and list any errors found in any SLabel components in the panel.
042: */
043: public class SErrorList extends JScrollPane implements
044: ModelChangedListener {
045:
046: JPanel _pan;
047: DataStoreBuffer _ds;
048: JList _list;
049:
050: /**
051: * Create a new Error List Component
052: */
053: public SErrorList() {
054: _list = new JList(new DefaultListModel());
055: _list.setCellRenderer(new ListCellRenderer() {
056: public Component getListCellRendererComponent(JList list,
057: Object value, int index, boolean isSelected,
058: boolean cellHasFocus) {
059: return (JLabel) value;
060: }
061: });
062: getViewport().add(_list);
063: setVisible(false);
064: }
065:
066: /**
067: * Sets the panel and datastore this component should check for errors
068: */
069: public void setDataStoreAndPanel(DataStoreBuffer ds, JPanel p) {
070: _pan = p;
071: _ds = ds;
072: ds.addModelChangedListener(this );
073: }
074:
075: /**
076: * @see com.salmonllc.sql.ModelChangedListener#modelChanged(ModelChangedEvent)
077: */
078: public void modelChanged(ModelChangedEvent evt) {
079: ((DefaultListModel) _list.getModel()).removeAllElements();
080: getErrors(_pan);
081: ListModel m = _list.getModel();
082: _list.setVisible(m.getSize() > 0);
083: setVisible(m.getSize() > 0);
084: }
085:
086: private void getErrors(Container cont) {
087: int count = cont.getComponentCount();
088: for (int i = 0; i < count; i++) {
089: Component comp = cont.getComponent(i);
090: if (comp instanceof SLabel) {
091: Icon errorIcon = ((SLabel) comp).getErrorIcon();
092: String errorText = ((SLabel) comp).getErrorMessage();
093: if (errorText != null) {
094: JLabel l = new JLabel(errorText, errorIcon,
095: SwingConstants.LEFT);
096: ((DefaultListModel) _list.getModel()).addElement(l);
097: }
098: } else if (comp instanceof Container) {
099: getErrors(((Container) comp));
100: }
101: }
102: }
103: }
|