1// BEGIN LICENSE BLOCK
2// Version: CMPL 1.1
3//
4// The contents of this file are subject to the Cisco-style Mozilla Public
5// License Version 1.1 (the "License"); you may not use this file except
6// in compliance with the License.  You may obtain a copy of the License
7// at www.eclipse-clp.org/license.
8//
9// Software distributed under the License is distributed on an "AS IS"
10// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11// the License for the specific language governing rights and limitations
12// under the License.
13//
14// The Original Code is  The ECLiPSe Constraint Logic Programming System.
15// The Initial Developer of the Original Code is  Cisco Systems, Inc.
16// Portions created by the Initial Developer are
17// Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
18//
19// Contributor(s):
20//
21// END LICENSE BLOCK
22
23package com.parctechnologies.eclipse.visualisation;
24
25import javax.swing.table.*;
26import com.parctechnologies.eclipse.*;
27import com.parctechnologies.eclipse.visualisation.*;
28
29/**
30 * Implements a SpreadSheetModel for an array of integers
31 */
32public class IntegerSpreadSheetModel
33    extends AbstractTableModel
34    implements SpreadSheetModel {
35
36    int[][] array;
37    int rowCount;
38    int columnCount;
39
40    /** We use this single object to return the contents of the int array */
41    private MutableInteger value = new MutableInteger(0);
42
43    public IntegerSpreadSheetModel(int rows, int cols) {
44	rowCount = rows;
45	columnCount = cols;
46	array = new int[rows][cols];
47    }
48
49    public int getRowCount() {
50	return rowCount;
51    }
52
53    public int getColumnCount() {
54	return columnCount;
55    }
56
57    public Object getValueAt(int row, int col) {
58	value.setValue(array[row][col]);
59	return value;
60    }
61
62    public void setIntAt(int row, int col, int i) {
63	array[row][col] = i;
64	fireTableCellUpdated(row, col);
65    }
66
67    /**
68     * Returns the name of the column at <code>rowIndex</code>.  This is used
69     * to initialize the table's row header name.  Note: this name does
70     * not need to be unique; two rows in a table can have the same name.
71     *
72     * @param	rowIndex	the index of the row
73     * @return  the name of the row
74     */
75    public String getRowName(int rowIndex) {
76	return "";
77    }
78
79
80    /**
81     * Moveall values in the table toward zero
82     */
83    public void moveToZero() {
84	for(int r = 0; r < rowCount; r++) {
85	    for(int c = 0; c < columnCount; c++) {
86		int v = array[r][c];
87		if (v!=0) {
88		    if (v<0) {
89			array[r][c]++;
90		    } else /* v>0 */{
91			array[r][c]--;
92		    }
93		    fireTableCellUpdated(r,c);
94		}
95	    }
96	}
97    }
98
99    public static class MutableInteger {
100	int i = 0;
101	public MutableInteger(int i) {
102	    this.i = i;
103	}
104
105	public int intValue() {
106	    return i;
107	}
108
109	public long longValue() {
110	    return (long)i;
111	}
112
113	public void setValue(int i) {
114	    this.i = i;
115	}
116
117	public String toString() {
118	    return ""+i;
119	}
120    }
121
122}
123