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

📄 warningscrolltable.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
字号:
/**    
  * Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano

  * 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
  * (at your option) 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
  
package jmt.gui.common.panels;

import jmt.gui.common.resources.ImageLoader;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

/**
 * <p>Title: Warning ScrollPanel for Tables</p>
 * <p>Description: This class will add scrollbars to a given table and displays a given warning
 * message if the table is empty of if any of specified Vectors is empty.</p>
 * 
 * @author Bertoli Marco
 *         Date: 7-ott-2005
 *         Time: 14.30.24
 */
public class WarningScrollTable extends JRootPane {
    protected static final int BORDERSIZE = 5;
    protected static final Dimension warningBoxSize = new Dimension(170, 90);
    protected JTable table;
    protected Vector vectors;

    /**
     * Constructs a new WarningScrollTable, given a table and a warning message to display if the
     * table is empty
     * @param table table to be shown in the panel
     * @param warningMsg message to be shown if the table is empty
     */
    public WarningScrollTable(JTable table, String warningMsg) {
        this.table = table;
        // Uses GlassPane to show overlay warning message
        getContentPane().add(new JScrollPane(table));
        setGlassPane(createWarningPanel(warningMsg));
    }

    /**
     * Constructs a new WarningScrollTable, given a component and a warning message to display if the
     * specified vectors are empty. This implementation does not add scrollBars despite the name
     * of this class.
     * @param component component to be shown in the panel
     * @param warningMsg message to be shown if vectors are empty
     */
    public WarningScrollTable(JComponent component, String warningMsg) {
        // Uses GlassPane to show overlay warning message
        getContentPane().add(component);
        setGlassPane(createWarningPanel(warningMsg));
    }

    /**
     * Adds a new Vector tobe check. If this methodis used behaviour of WarningScrollTable will
     * change and warning message will be shown only if any of input vectors is empty
     * @param v input vector to be checked
     */
    public void addCheckVector(Vector v) {
        if (vectors == null)
            vectors = new Vector();
        vectors.add(v);
    }

    /**
     * Cleans all check vectors
     */
    public void clearCheckVectors() {
        vectors = null;
    }

    /**
     * Override default paint method
     * @param g graphic object
     */
    public void paint(Graphics g) {
        if (vectors == null) {
            if (table != null)
                getGlassPane().setVisible(table.getRowCount() == 0 || table.getColumnCount() == 0);
        }
        else
            getGlassPane().setVisible(checkEmptyVectors());
        super.paint(g);
    }

    /**
     * Tells if any vector added with <code>addCheckVector(Vector v)</code> method is empty
     * @return true iff any vector is empty
     * @see this.addCheckVector(Vector)
     */
    protected boolean checkEmptyVectors() {
        boolean res = false;
        for (int i=0; i<vectors.size();i++)
            if (((Vector)vectors.get(i)).size() == 0) {
                res = true;
                break;
            }

        return res;
    }

    /**
     * Creates the panel to be shown when the table is empty
     * @param msg message to be shown on the panel
     * @return created warning panel
     */
    protected JPanel createWarningPanel(String msg) {
        JPanel warning = new JPanel (new GridBagLayout());
        JPanel innerPanel = new JPanel(new BorderLayout());
        // Adds image
        JLabel icon = new JLabel("");
        icon.setIcon(ImageLoader.loadImage("Triangle"));
        icon.setHorizontalAlignment(JLabel.CENTER);
        icon.setBorder(BorderFactory.createEmptyBorder(BORDERSIZE, BORDERSIZE, BORDERSIZE, BORDERSIZE));
        innerPanel.add(icon, BorderLayout.NORTH);
        // Adds Text Area
        JTextArea text = new JTextArea();
        text.setEditable(false);
        text.setWrapStyleWord(true);
        text.setLineWrap(true);
        text.setText(msg);
        text.setBorder(BorderFactory.createEmptyBorder(BORDERSIZE, BORDERSIZE, BORDERSIZE, BORDERSIZE));
        text.setBackground(icon.getBackground());
        innerPanel.add(text, BorderLayout.CENTER);
        innerPanel.setBorder(BorderFactory.createEtchedBorder());
        innerPanel.setPreferredSize(warningBoxSize);
        warning.add(innerPanel);
        return warning;
    }
}

⌨️ 快捷键说明

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