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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/editors/chord/ChordList.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
/*
 
2
 
 
3
 
 
4
 * Created on 02-ene-2006
 
5
 
 
6
 
 
7
 *
 
8
 
 
9
 
 
10
 * TODO To change the template for this generated file go to
 
11
 
 
12
 
 
13
 * Window - Preferences - Java - Code Style - Code Templates
 
14
 
 
15
 
 
16
 */
 
17
 
 
18
package org.herac.tuxguitar.gui.editors.chord;
 
19
 
 
20
import java.util.ArrayList;
 
21
import java.util.Iterator;
 
22
import java.util.List;
 
23
 
 
24
import org.eclipse.swt.SWT;
 
25
import org.eclipse.swt.events.DisposeEvent;
 
26
import org.eclipse.swt.events.DisposeListener;
 
27
import org.eclipse.swt.events.MouseAdapter;
 
28
import org.eclipse.swt.events.MouseEvent;
 
29
import org.eclipse.swt.events.PaintEvent;
 
30
import org.eclipse.swt.events.PaintListener;
 
31
import org.eclipse.swt.graphics.Color;
 
32
import org.eclipse.swt.graphics.Font;
 
33
import org.eclipse.swt.graphics.FontData;
 
34
import org.eclipse.swt.graphics.GC;
 
35
import org.eclipse.swt.graphics.Point;
 
36
import org.eclipse.swt.graphics.Rectangle;
 
37
import org.eclipse.swt.layout.GridData;
 
38
import org.eclipse.swt.widgets.Composite;
 
39
import org.eclipse.swt.widgets.Event;
 
40
import org.eclipse.swt.widgets.Listener;
 
41
import org.eclipse.swt.widgets.ScrollBar;
 
42
import org.herac.tuxguitar.gui.editors.TGPainter;
 
43
import org.herac.tuxguitar.gui.editors.tab.TGChordImpl;
 
44
import org.herac.tuxguitar.gui.editors.tab.layout.ViewLayout;
 
45
import org.herac.tuxguitar.song.models.TGBeat;
 
46
import org.herac.tuxguitar.song.models.TGString;
 
47
/**
 
48
 * @author julian
 
49
 * 
 
50
 * Component that shows the list of (alternative) chords - bottom of the screen
 
51
 */
 
52
public class ChordList extends Composite {
 
53
        
 
54
        private static final int MIN_HEIGHT = 160;
 
55
        private static final int SCROLL_INCREMENT = 25;
 
56
        private static final int CHORD_FIRST_FRET_SPACING = 12;
 
57
        private static final int CHORD_STRING_SPACING = 8;
 
58
        private static final int CHORD_FRET_SPACING = 10;
 
59
        private static final int CHORD_NOTE_SIZE = 6;
 
60
        
 
61
        private ChordDialog dialog;
 
62
        private TGBeat beat;
 
63
        private List graphicChords;
 
64
        private int height;
 
65
        private TGChordImpl selectedChord;
 
66
        private Composite composite;
 
67
        private Font font;
 
68
        
 
69
        public ChordList(ChordDialog dialog,Composite parent,TGBeat beat) {
 
70
                super(parent, SWT.NONE);
 
71
                this.setLayout(dialog.gridLayout(1,false,0,0));
 
72
                this.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
73
                this.graphicChords = new ArrayList();
 
74
                this.dialog = dialog;
 
75
                this.beat = beat;
 
76
                this.init();
 
77
        }
 
78
        
 
79
        private void init(){
 
80
                this.composite = new Composite(this,SWT.BORDER | SWT.V_SCROLL | SWT.DOUBLE_BUFFERED);
 
81
                this.composite.setBackground(this.getDisplay().getSystemColor(SWT.COLOR_WHITE));
 
82
                this.composite.addPaintListener(new PaintListener() {
 
83
                        public void paintControl(PaintEvent e) {
 
84
                                TGPainter painter = new TGPainter(e.gc);
 
85
                                paintChords(painter);
 
86
                        }
 
87
                });
 
88
                this.composite.addMouseListener(new MouseAdapter() {
 
89
                        public void mouseUp(MouseEvent e) {
 
90
                                getComposite().setFocus();
 
91
                                getDialog().getEditor().setChord(getChord(e.x, e.y,true));
 
92
                                redraw();
 
93
                        }
 
94
                });
 
95
                
 
96
                final Point origin = new Point(0, 0);
 
97
                final ScrollBar vBar = this.composite.getVerticalBar();
 
98
                vBar.setIncrement(SCROLL_INCREMENT);
 
99
                vBar.addListener(SWT.Selection, new Listener() {
 
100
                        public void handleEvent(Event e) {
 
101
                                int vSelection = vBar.getSelection();
 
102
                                int destY = -vSelection - origin.y;
 
103
                                Rectangle rect = getComposite().getBounds();
 
104
                                getShell().scroll(0, destY, 0, 0, rect.width, rect.height, false);
 
105
                                origin.y = -vSelection;
 
106
                                redraw();
 
107
                        }
 
108
                });
 
109
                
 
110
                GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
 
111
                data.minimumHeight = MIN_HEIGHT;
 
112
                this.composite.setLayoutData(data);
 
113
                this.addDisposeListener(new DisposeListener() {
 
114
                        public void widgetDisposed(DisposeEvent arg0) {
 
115
                                disposeChords();
 
116
                                disposeFont();
 
117
                        }
 
118
                });
 
119
        }
 
120
        
 
121
        public void redraw(){
 
122
                super.redraw();
 
123
                this.composite.redraw();
 
124
        }
 
125
        
 
126
        protected void paintChords(TGPainter painter) {
 
127
                int maxHeight = 0;
 
128
                int fromX = 15;
 
129
                int fromY = 10;
 
130
                int vScroll = this.composite.getVerticalBar().getSelection();
 
131
                Iterator it = this.graphicChords.iterator();
 
132
                while (it.hasNext()) {
 
133
                        TGChordImpl chord = (TGChordImpl) it.next();
 
134
                        
 
135
                        Color color = getChordColor(chord);
 
136
                        chord.setBackgroundColor(this.composite.getBackground());
 
137
                        chord.setColor(color);
 
138
                        chord.setNoteColor(color);
 
139
                        chord.setTonicColor(getDisplay().getSystemColor(SWT.COLOR_DARK_RED));
 
140
                        chord.setFirstFretSpacing(CHORD_FIRST_FRET_SPACING);
 
141
                        chord.setStringSpacing(CHORD_STRING_SPACING);
 
142
                        chord.setFretSpacing(CHORD_FRET_SPACING);
 
143
                        chord.setNoteSize(CHORD_NOTE_SIZE);
 
144
                        chord.setFirstFretFont(getFont(painter.getGC()));
 
145
                        chord.setStyle(ViewLayout.DISPLAY_CHORD_DIAGRAM);
 
146
                        chord.update(painter, true);
 
147
                        if(fromX + chord.getWidth() >= ((getBounds().x + getBounds().width) - 20)){
 
148
                                fromX = 15;
 
149
                                fromY += chord.getHeight() + 10;
 
150
                        }
 
151
                        chord.setEditing(true);
 
152
                        chord.setPosX( fromX );
 
153
                        chord.setPosY( fromY - vScroll);
 
154
                        chord.paint(painter,(chord.getWidth() / 2),0);
 
155
                        
 
156
                        fromX += chord.getWidth() + 10;
 
157
                        maxHeight = Math.max(maxHeight,chord.getHeight());
 
158
                }
 
159
                this.height = (fromY + maxHeight + 10);
 
160
                this.updateScroll();
 
161
        }
 
162
        
 
163
        private Color getChordColor(TGChordImpl chord){
 
164
                if(this.selectedChord != null && this.selectedChord.equals(chord)){
 
165
                        return getDisplay().getSystemColor(SWT.COLOR_BLUE);
 
166
                }
 
167
                return getDisplay().getSystemColor(SWT.COLOR_BLACK);
 
168
        }
 
169
        
 
170
        public void updateScroll(){
 
171
                Rectangle rect = this.composite.getBounds();
 
172
                Rectangle client = this.composite.getClientArea();
 
173
                ScrollBar vBar = this.composite.getVerticalBar();
 
174
                vBar.setMaximum(this.height);
 
175
                vBar.setThumb(Math.min(rect.height, client.height));
 
176
        }
 
177
        
 
178
        protected int getTrackString(int number){
 
179
                TGString string = ChordList.this.beat.getMeasure().getTrack().getString(number);
 
180
                return string.getValue();
 
181
        }
 
182
        
 
183
        protected Font getFont(GC painter){
 
184
                if(this.font == null || this.font.isDisposed()){ 
 
185
                        Font available = painter.getFont();
 
186
                        if(available == null || available.isDisposed()){
 
187
                                available = getDisplay().getSystemFont();
 
188
                        }
 
189
                        FontData[] datas = available.getFontData();
 
190
                        if(datas.length > 0){
 
191
                                this.font = new Font(getDisplay(),datas[0].getName(),Math.min(7,datas[0].getHeight()),SWT.BOLD);
 
192
                        }
 
193
                }
 
194
                return this.font;
 
195
        }
 
196
        
 
197
        protected TGChordImpl getChord(int x, int y,boolean setAsSelected) {
 
198
                Iterator it = this.graphicChords.iterator();
 
199
                while (it.hasNext()) {
 
200
                        TGChordImpl chord = (TGChordImpl) it.next();
 
201
                        int x1 = chord.getPosX();
 
202
                        int x2 = x1 + chord.getWidth();
 
203
                        int y1 = chord.getPosY();
 
204
                        int y2 = y1 + chord.getHeight();
 
205
                        if (x > x1 && x < x2 && y > y1 && y < y2) {
 
206
                                if(setAsSelected){
 
207
                                        if(this.selectedChord != null){
 
208
                                                this.selectedChord.dispose();
 
209
                                        }
 
210
                                        this.selectedChord = chord;
 
211
                                        chord.dispose();
 
212
                                }
 
213
                                return chord;
 
214
                        }
 
215
                }
 
216
                return null;
 
217
        }
 
218
        
 
219
        public void setChords(List chords) {
 
220
                this.disposeChords();
 
221
                this.selectedChord = null;
 
222
                
 
223
                Iterator it = chords.iterator();
 
224
                while (it.hasNext()) {
 
225
                        TGChordImpl chord = (TGChordImpl) it.next();
 
226
                        chord.setTonic( ChordList.this.dialog.getSelector().getTonicList().getSelectionIndex() );
 
227
                        chord.setBeat(ChordList.this.beat);
 
228
                        this.graphicChords.add(chord);
 
229
                }
 
230
                this.redraw();
 
231
        }
 
232
        
 
233
        public void disposeFont(){
 
234
                if(this.font != null){
 
235
                        this.font.dispose();
 
236
                }
 
237
        }
 
238
        
 
239
        public void disposeChords(){
 
240
                Iterator it = this.graphicChords.iterator();
 
241
                while (it.hasNext()) {
 
242
                        ((TGChordImpl) it.next()).dispose();
 
243
                }
 
244
                this.graphicChords.clear();
 
245
        }
 
246
        
 
247
        protected Composite getComposite(){
 
248
                return this.composite;
 
249
        }
 
250
        
 
251
        protected ChordDialog getDialog(){
 
252
                return this.dialog;
 
253
        }
 
254
}
 
 
b'\\ No newline at end of file'