KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > jms > StockExample > StocksTable


1 package sample.jms.StockExample;
2
3 /*
4  * Copyright 2002 by
5  * <a HREF="http://www.coridan.com">Coridan</a>
6  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
7  *
8  * The contents of this file are subject to the Mozilla Public License Version
9  * 1.1 (the "License"); you may not use this file except in compliance with the
10  * License. You may obtain a copy of the License at
11  * http://www.mozilla.org/MPL/
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15  * for the specific language governing rights and limitations under the
16  * License.
17  *
18  * The Original Code is "MantaRay" (TM).
19  *
20  * The Initial Developer of the Original Code is lital kasif.
21  * Portions created by the Initial Developer are Copyright (C) 2006
22  * Coridan Inc. All Rights Reserved.
23  *
24  * Contributor(s): all the names of the contributors are added in the source
25  * code where applicable.
26  *
27  * Alternatively, the contents of this file may be used under the terms of the
28  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
29  * provisions of LGPL are applicable instead of those above. If you wish to
30  * allow use of your version of this file only under the terms of the LGPL
31  * License and not to allow others to use your version of this file under
32  * the MPL, indicate your decision by deleting the provisions above and
33  * replace them with the notice and other provisions required by the LGPL.
34  * If you do not delete the provisions above, a recipient may use your version
35  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
36  
37  *
38  * This library is free software; you can redistribute it and/or modify it
39  * under the terms of the MPL as stated above or under the terms of the GNU
40  * Lesser General Public License as published by the Free Software Foundation;
41  * either version 2.1 of the License, or any later version.
42  *
43  * This library is distributed in the hope that it will be useful, but WITHOUT
44  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
46  * License for more details.
47  *
48  * Created on Jan 2, 2005
49  *
50  * Coridan LTD
51  */

52 import javax.swing.table.*;
53
54 import java.util.ArrayList JavaDoc;
55 import java.util.HashMap JavaDoc;
56 /**
57  * ResultTable specifies a model that would update a <code>JTable</code>
58  * directly from the stocks data, which is kept updates in two structures of
59  * an ArrayList and a HashMap.
60  * This is used by 'StockPublisherExample' in order to store the stocks information
61  * received from the 'stock status' topic and update that inforamtion into the GUI
62  * table ressening it.
63  *
64  * @author lital kasif
65  */

66 public class StocksTable extends AbstractTableModel {
67     /**
68      * Comment for <code>serialVersionUID</code>
69      */

70     private static final long serialVersionUID = 2183680672901551040L;
71     private String JavaDoc[] columnNames = {"Company","Value"};
72     protected static int NUM_COLUMNS = 2;
73     protected static int START_NUM_ROWS = 0;
74     protected int nextEmptyRow = 0;
75     protected int numRows = 0;
76
77     static final public String JavaDoc COMPANY = "Company";
78     static final public String JavaDoc VALUE = "Value";
79     // companies holds the names of the companies to be inputed to the table
80
private ArrayList JavaDoc companies;
81     // companiesToValue holds the stock values of the companies to be inputed to the table
82
private HashMap JavaDoc companiesToValue;
83
84     public StocksTable() {
85         //initializing
86
companies = new ArrayList JavaDoc();
87         companiesToValue = new HashMap JavaDoc();
88     }
89
90 /**
91  * returns the the name of the column inserted
92  */

93     public String JavaDoc getColumnName(int column) {
94             return columnNames[column];
95     }
96
97     /**
98      * returns the number of exisiting column
99      */

100     public synchronized int getColumnCount() {
101         return NUM_COLUMNS;
102     }
103
104     /**
105      * returns the current number of rows
106      */

107     public synchronized int getRowCount() {
108         if (numRows < START_NUM_ROWS) {
109             return START_NUM_ROWS;
110         } else {
111             return numRows;
112         }
113     }
114
115     /**
116      * returns the current value of a cube in the stock table
117      */

118     public synchronized Object JavaDoc getValueAt(int row, int column) {
119         try {
120                 switch (column) {
121                 //returns the name of the company
122
case 0:
123                     return companies.get(row);
124                 //retuns the value of the company
125
case 1:
126                       return companiesToValue.get(companies.get(row));
127                 }
128         } catch (Exception JavaDoc e) {
129         }
130         return "N/A";
131     }//getValueAt
132

133     /**
134      * updates a stock value of a company
135      * @param companyName the name of the company
136      * @param value its new value
137      */

138     public synchronized void updateCompany(String JavaDoc companyName, String JavaDoc value) {
139     //the method either adds the new information to a new row or updates an existing row
140
boolean addedRow = !companies.contains(companyName) ;
141         companiesToValue.put(companyName, value);
142         //Notify listeners that the stocksData changed.
143
if (addedRow) {
144             numRows++;
145             companies.add(companyName);
146             fireTableRowsInserted(companies.size(), companies.size());
147             
148         } else {
149             fireTableRowsUpdated(companies.indexOf(companyName), companies.indexOf(companyName));
150         }
151     }//updateCompany
152

153 }//ResultTable
Popular Tags