1 package org.simantics.modeling.ui.diagram;
3 import java.awt.Graphics2D;
4 import java.awt.Rectangle;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.FocusEvent;
8 import java.awt.event.FocusListener;
9 import java.beans.PropertyChangeEvent;
11 import javax.swing.event.TableModelListener;
12 import javax.swing.table.TableModel;
14 import org.simantics.scenegraph.g2d.G2DRenderingHints;
15 import org.simantics.scenegraph.g2d.nodes.ComponentNode;
16 import org.simantics.spreadsheet.Cell;
17 import org.simantics.spreadsheet.Model;
18 import org.simantics.spreadsheet.ui.Spreadsheet.SpreadsheetModel;
20 public class SheetNode extends ComponentNode implements ActionListener, FocusListener {
22 private static final long serialVersionUID = -8212264868316567181L;
24 private SpreadsheetModel sm;
26 class Proxy implements Model, TableModel {
28 TableModelListener listener;
30 TableModel tableModel;
32 Proxy(Model model, TableModel tableModel) {
34 this.tableModel = tableModel;
38 public Cell get(int row, int column) {
43 public <T> T getAdapter(Class<?> clazz) {
44 return model.getAdapter(clazz);
48 public void dispose() {
53 public void addTableModelListener(TableModelListener l) {
54 assert(listener == null);
59 public Class<?> getColumnClass(int columnIndex) {
60 return tableModel.getColumnClass(columnIndex);
64 public int getColumnCount() {
69 public String getColumnName(int columnIndex) {
70 return "" + columnIndex;
74 public int getRowCount() {
79 public Object getValueAt(int rowIndex, int columnIndex) {
80 return tableModel.getValueAt(rowIndex, columnIndex);
84 public boolean isCellEditable(int rowIndex, int columnIndex) {
89 public void removeTableModelListener(TableModelListener l) {
94 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
100 public void init(Model model, TableModel tableModel) {
102 Proxy proxy = new Proxy(model, tableModel);
104 sm = new SpreadsheetModel(proxy, proxy, null);
105 component = sm.createComponent(false);
106 component.addFocusListener(this);
107 super.propertyChange(new PropertyChangeEvent(this, "component", null, component));
111 protected ActionListener actionListener = null;
113 public void setActionListener(ActionListener actionListener) {
114 this.actionListener = actionListener;
119 public void actionPerformed(ActionEvent e) {
120 if(actionListener != null)
121 actionListener.actionPerformed(e);
122 if(container.getParent() != null)
123 container.getParent().requestFocusInWindow(); // Loose focus
127 @SyncField({"width", "height"})
128 public void setSize(int width, int height) {
130 this.height = height;
131 // container.setBounds(0, 0, width, height);
132 // component.setBounds(0, 0, width, height);
133 // component.validate();
134 // container.validate();
135 // System.out.println("table bounds=" + sm.getTable().getBounds());
140 public void focusGained(FocusEvent arg0) {
144 public void focusLost(FocusEvent arg0) {
148 public void render(Graphics2D g2d) {
149 Rectangle bounds = (Rectangle)g2d.getRenderingHint(G2DRenderingHints.KEY_CONTROL_BOUNDS);
150 width = (int)bounds.getWidth();
151 height = (int)bounds.getHeight();