001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * InstancesSummaryPanel.java
019: * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.gui;
024:
025: import weka.core.Instances;
026:
027: import java.awt.BorderLayout;
028: import java.awt.GridBagConstraints;
029: import java.awt.GridBagLayout;
030:
031: import javax.swing.BorderFactory;
032: import javax.swing.JLabel;
033: import javax.swing.JPanel;
034: import javax.swing.SwingConstants;
035:
036: /**
037: * This panel just displays relation name, number of instances, and number of
038: * attributes.
039: *
040: * @author Len Trigg (trigg@cs.waikato.ac.nz)
041: * @version $Revision: 1.6 $
042: */
043: public class InstancesSummaryPanel extends JPanel {
044:
045: /** for serialization */
046: private static final long serialVersionUID = -5243579535296681063L;
047:
048: /** Message shown when no instances have been loaded */
049: protected static final String NO_SOURCE = "None";
050:
051: /** Displays the name of the relation */
052: protected JLabel m_RelationNameLab = new JLabel(NO_SOURCE);
053:
054: /** Displays the number of instances */
055: protected JLabel m_NumInstancesLab = new JLabel(NO_SOURCE);
056:
057: /** Displays the number of attributes */
058: protected JLabel m_NumAttributesLab = new JLabel(NO_SOURCE);
059:
060: /** The instances we're playing with */
061: protected Instances m_Instances;
062:
063: /**
064: * Creates the instances panel with no initial instances.
065: */
066: public InstancesSummaryPanel() {
067:
068: GridBagLayout gbLayout = new GridBagLayout();
069: setLayout(gbLayout);
070: JLabel lab = new JLabel("Relation:", SwingConstants.RIGHT);
071: lab.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
072: GridBagConstraints gbConstraints = new GridBagConstraints();
073: gbConstraints.anchor = GridBagConstraints.EAST;
074: gbConstraints.fill = GridBagConstraints.HORIZONTAL;
075: gbConstraints.gridy = 0;
076: gbConstraints.gridx = 0;
077: gbLayout.setConstraints(lab, gbConstraints);
078: add(lab);
079: gbConstraints = new GridBagConstraints();
080: gbConstraints.anchor = GridBagConstraints.WEST;
081: gbConstraints.fill = GridBagConstraints.HORIZONTAL;
082: gbConstraints.gridy = 0;
083: gbConstraints.gridx = 1;
084: gbConstraints.weightx = 100;
085: gbConstraints.gridwidth = 3;
086: gbLayout.setConstraints(m_RelationNameLab, gbConstraints);
087: add(m_RelationNameLab);
088: m_RelationNameLab.setBorder(BorderFactory.createEmptyBorder(0,
089: 5, 0, 10));
090:
091: lab = new JLabel("Instances:", SwingConstants.RIGHT);
092: lab.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
093: gbConstraints = new GridBagConstraints();
094: gbConstraints.anchor = GridBagConstraints.EAST;
095: gbConstraints.fill = GridBagConstraints.HORIZONTAL;
096: gbConstraints.gridy = 1;
097: gbConstraints.gridx = 0;
098: gbLayout.setConstraints(lab, gbConstraints);
099: add(lab);
100: gbConstraints = new GridBagConstraints();
101: gbConstraints.anchor = GridBagConstraints.WEST;
102: gbConstraints.fill = GridBagConstraints.HORIZONTAL;
103: gbConstraints.gridy = 1;
104: gbConstraints.gridx = 1;
105: gbConstraints.weightx = 100;
106: gbLayout.setConstraints(m_NumInstancesLab, gbConstraints);
107: add(m_NumInstancesLab);
108: m_NumInstancesLab.setBorder(BorderFactory.createEmptyBorder(0,
109: 5, 0, 10));
110:
111: lab = new JLabel("Attributes:", SwingConstants.RIGHT);
112: lab.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
113: gbConstraints = new GridBagConstraints();
114: gbConstraints.anchor = GridBagConstraints.EAST;
115: gbConstraints.fill = GridBagConstraints.HORIZONTAL;
116: gbConstraints.gridy = 1;
117: gbConstraints.gridx = 2;
118: gbLayout.setConstraints(lab, gbConstraints);
119: add(lab);
120: gbConstraints = new GridBagConstraints();
121: gbConstraints.anchor = GridBagConstraints.WEST;
122: gbConstraints.fill = GridBagConstraints.HORIZONTAL;
123: gbConstraints.gridy = 1;
124: gbConstraints.gridx = 3;
125: gbConstraints.weightx = 100;
126: gbLayout.setConstraints(m_NumAttributesLab, gbConstraints);
127: add(m_NumAttributesLab);
128: m_NumAttributesLab.setBorder(BorderFactory.createEmptyBorder(0,
129: 5, 0, 10));
130: }
131:
132: /**
133: * Tells the panel to use a new set of instances.
134: *
135: * @param inst a set of Instances
136: */
137: public void setInstances(Instances inst) {
138:
139: m_Instances = inst;
140: m_RelationNameLab.setText(m_Instances.relationName());
141: m_NumInstancesLab.setText("" + m_Instances.numInstances());
142: m_NumAttributesLab.setText("" + m_Instances.numAttributes());
143: }
144:
145: /**
146: * Tests out the instance summary panel from the command line.
147: *
148: * @param args optional name of dataset to load
149: */
150: public static void main(String[] args) {
151:
152: try {
153: final javax.swing.JFrame jf = new javax.swing.JFrame(
154: "Instances Panel");
155: jf.getContentPane().setLayout(new BorderLayout());
156: final InstancesSummaryPanel p = new InstancesSummaryPanel();
157: p.setBorder(BorderFactory.createTitledBorder("Relation"));
158: jf.getContentPane().add(p, BorderLayout.CENTER);
159: jf.addWindowListener(new java.awt.event.WindowAdapter() {
160: public void windowClosing(java.awt.event.WindowEvent e) {
161: jf.dispose();
162: System.exit(0);
163: }
164: });
165: jf.pack();
166: jf.setVisible(true);
167: if (args.length == 1) {
168: java.io.Reader r = new java.io.BufferedReader(
169: new java.io.FileReader(args[0]));
170: Instances i = new Instances(r);
171: p.setInstances(i);
172: }
173: } catch (Exception ex) {
174: ex.printStackTrace();
175: System.err.println(ex.getMessage());
176: }
177: }
178:
179: }
|