~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/table/TGTable.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.gui.table;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.List;
 
5
 
 
6
import org.eclipse.swt.SWT;
 
7
import org.eclipse.swt.custom.SashForm;
 
8
import org.eclipse.swt.custom.ScrolledComposite;
 
9
import org.eclipse.swt.layout.GridData;
 
10
import org.eclipse.swt.layout.GridLayout;
 
11
import org.eclipse.swt.widgets.Composite;
 
12
import org.eclipse.swt.widgets.Control;
 
13
 
 
14
public class TGTable {
 
15
        private ScrolledComposite sComposite;
 
16
        private Composite table;
 
17
        private SashForm columnControl;
 
18
        private Composite rowControl;
 
19
        private TGTableColumn columnNumber;
 
20
        private TGTableColumn columnName;
 
21
        private TGTableColumn columnInstrument;
 
22
        private TGTableColumn columnCanvas;
 
23
        private List rows;
 
24
        private int rowHeight;
 
25
        private int scrollIncrement;
 
26
        
 
27
        public TGTable(Composite parent){
 
28
                this.rows = new ArrayList();
 
29
                this.newTable(parent);
 
30
        }
 
31
        
 
32
        public void newTable(Composite parent){
 
33
                this.sComposite = new ScrolledComposite(parent,SWT.BORDER | SWT.V_SCROLL);
 
34
                this.sComposite.setLayout(new GridLayout());
 
35
                this.sComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
36
                this.sComposite.setAlwaysShowScrollBars(true);
 
37
                this.sComposite.setExpandHorizontal(true);
 
38
                this.sComposite.setExpandVertical(true);
 
39
                this.table = new Composite(this.sComposite,SWT.NONE);
 
40
                this.table.setLayout(newGridLayout(1,0,0,0,0));
 
41
                this.table.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
42
                
 
43
                this.columnControl = new SashForm(this.table,SWT.HORIZONTAL);
 
44
                this.columnControl.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,false));
 
45
                
 
46
                this.columnNumber = new TGTableColumn(this,SWT.LEFT);
 
47
                this.columnName = new TGTableColumn(this,SWT.LEFT);
 
48
                this.columnInstrument = new TGTableColumn(this,SWT.LEFT);
 
49
                this.columnCanvas = new TGTableColumn(this,SWT.CENTER);
 
50
                this.columnControl.setWeights(new int[]{1,7,7,20});
 
51
                
 
52
                this.rowControl = new Composite(this.table,SWT.NONE);
 
53
                this.rowControl.setLayout(newGridLayout(1,0,1,0,1));
 
54
                this.rowControl.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
55
                
 
56
                this.sComposite.setContent(this.table);
 
57
        }
 
58
        
 
59
        public Composite getControl(){
 
60
                return this.table;
 
61
        }
 
62
        
 
63
        public void newRow(){
 
64
                this.rows.add(new TGTableRow(this));
 
65
        }
 
66
        
 
67
        private GridLayout newGridLayout(int cols,int marginWidth,int marginHeight,int horizontalSpacing,int verticalSpacing){
 
68
                GridLayout layout = new GridLayout(cols,false);
 
69
                layout.marginWidth = marginWidth;
 
70
                layout.marginHeight = marginHeight;
 
71
                layout.horizontalSpacing = horizontalSpacing;
 
72
                layout.verticalSpacing = verticalSpacing;
 
73
                return layout;
 
74
        }
 
75
        
 
76
        public void addRowItem(TGTableColumn column,Control control,boolean computeSize){
 
77
                if(computeSize){
 
78
                        this.rowHeight = Math.max(this.rowHeight,control.computeSize(SWT.DEFAULT,SWT.DEFAULT).y);
 
79
                        this.scrollIncrement = this.rowHeight;
 
80
                }
 
81
                column.addControl(control);
 
82
        }
 
83
        
 
84
        public int getMinHeight(){
 
85
                return (this.sComposite.getMinHeight() + ( this.sComposite.getBorderWidth() * 2 ) );
 
86
        }
 
87
        
 
88
        public Composite getColumnControl(){
 
89
                return this.columnControl;
 
90
        }
 
91
        
 
92
        public Composite getRowControl(){
 
93
                return this.rowControl;
 
94
        }
 
95
        
 
96
        public int getRowHeight(){
 
97
                return this.rowHeight;
 
98
        }
 
99
        
 
100
        public int getScrollIncrement(){
 
101
                return this.scrollIncrement;
 
102
        }       
 
103
        
 
104
        public TGTableColumn getColumnInstrument() {
 
105
                return this.columnInstrument;
 
106
        }
 
107
        
 
108
        public TGTableColumn getColumnName() {
 
109
                return this.columnName;
 
110
        }
 
111
        
 
112
        public TGTableColumn getColumnNumber() {
 
113
                return this.columnNumber;
 
114
        }       
 
115
        
 
116
        public TGTableColumn getColumnCanvas() {
 
117
                return this.columnCanvas;
 
118
        }
 
119
        
 
120
        public TGTableRow getRow(int index){
 
121
                if(index >= 0 && index < this.rows.size()){
 
122
                        return (TGTableRow)this.rows.get(index);
 
123
                }
 
124
                return null;
 
125
        }
 
126
        
 
127
        public void removeRowsAfter(int index){
 
128
                while(index < this.rows.size()){
 
129
                        TGTableRow row = (TGTableRow)this.rows.get(index);
 
130
                        row.dispose();
 
131
                        this.rows.remove(index);
 
132
                }
 
133
                this.notifyRemoved();
 
134
        }
 
135
        
 
136
        public int getRowCount(){
 
137
                return this.rows.size();
 
138
        }
 
139
        
 
140
        public void update(){
 
141
                this.layoutColumns();
 
142
                this.table.layout(true,true);
 
143
                this.sComposite.setMinHeight(this.table.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
 
144
                this.sComposite.getVerticalBar().setIncrement( (getScrollIncrement() + this.sComposite.getBorderWidth() ) );
 
145
        }
 
146
        
 
147
        private void notifyRemoved(){
 
148
                this.columnNumber.notifyRemoved();
 
149
                this.columnName.notifyRemoved();
 
150
                this.columnInstrument.notifyRemoved();
 
151
                this.columnCanvas.notifyRemoved();
 
152
        }
 
153
        
 
154
        private void layoutColumns(){
 
155
                this.columnNumber.layout();
 
156
                this.columnName.layout();
 
157
                this.columnInstrument.layout();
 
158
                this.columnCanvas.layout();
 
159
        }
 
160
        
 
161
}