]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/SheetNode.java.keep
Improved usability of shared library export wizard
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / SheetNode.java.keep
1 package org.simantics.modeling.ui.diagram;
2
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;
10
11 import javax.swing.event.TableModelListener;
12 import javax.swing.table.TableModel;
13
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;
19
20 public class SheetNode extends ComponentNode implements ActionListener, FocusListener {
21
22     private static final long serialVersionUID = -8212264868316567181L;
23
24     private SpreadsheetModel sm;
25
26     class Proxy implements Model, TableModel {
27
28         TableModelListener listener;
29         Model model;
30         TableModel tableModel;
31
32         Proxy(Model model, TableModel tableModel) {
33             this.model = model;
34             this.tableModel = tableModel;
35         }
36
37         @Override
38         public Cell get(int row, int column) {
39             throw new Error();
40         }
41
42         @Override
43         public <T> T getAdapter(Class<?> clazz) {
44             return model.getAdapter(clazz);
45         }
46
47         @Override
48         public void dispose() {
49             throw new Error();
50         }
51
52         @Override
53         public void addTableModelListener(TableModelListener l) {
54             assert(listener == null);
55             listener = l;
56         }
57
58         @Override
59         public Class<?> getColumnClass(int columnIndex) {
60             return tableModel.getColumnClass(columnIndex);
61         }
62
63         @Override
64         public int getColumnCount() {
65             return 2;
66         }
67
68         @Override
69         public String getColumnName(int columnIndex) {
70             return "" + columnIndex;
71         }
72
73         @Override
74         public int getRowCount() {
75             return 15;
76         }
77
78         @Override
79         public Object getValueAt(int rowIndex, int columnIndex) {
80             return tableModel.getValueAt(rowIndex, columnIndex);
81         }
82
83         @Override
84         public boolean isCellEditable(int rowIndex, int columnIndex) {
85             return true;
86         }
87
88         @Override
89         public void removeTableModelListener(TableModelListener l) {
90             throw new Error();
91         }
92
93         @Override
94         public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
95             throw new Error();
96         }
97
98     }
99
100     public void init(Model model, TableModel tableModel) {
101
102         Proxy proxy = new Proxy(model, tableModel);
103
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));
108
109     }
110
111     protected ActionListener actionListener = null;
112
113     public void setActionListener(ActionListener actionListener) {
114         this.actionListener = actionListener;
115     }
116
117     @ServerSide
118     @Override
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
124     }
125
126     @Override
127     @SyncField({"width", "height"})
128     public void setSize(int width, int height) {
129         this.width = width;
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());
136 //
137     }
138
139     @Override
140     public void focusGained(FocusEvent arg0) {
141     }
142
143     @Override
144     public void focusLost(FocusEvent arg0) {
145     }
146
147     @Override
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();
152         super.render(g2d);
153     }
154
155 }