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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/actions/insert/InsertChordAction.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
 
 * Created on 17-dic-2005
3
 
 *
4
 
 * TODO To change the template for this generated file go to
5
 
 * Window - Preferences - Java - Code Style - Code Templates
6
 
 */
7
 
package org.herac.tuxguitar.gui.actions.insert;
8
 
 
9
 
import java.util.Iterator;
10
 
import java.util.List;
11
 
 
12
 
import org.eclipse.swt.SWT;
13
 
import org.eclipse.swt.events.SelectionAdapter;
14
 
import org.eclipse.swt.events.SelectionEvent;
15
 
import org.eclipse.swt.events.TypedEvent;
16
 
import org.eclipse.swt.layout.GridData;
17
 
import org.eclipse.swt.layout.GridLayout;
18
 
import org.eclipse.swt.widgets.Button;
19
 
import org.eclipse.swt.widgets.Composite;
20
 
import org.eclipse.swt.widgets.Shell;
21
 
import org.herac.tuxguitar.gui.TuxGuitar;
22
 
import org.herac.tuxguitar.gui.actions.Action;
23
 
import org.herac.tuxguitar.gui.editors.TablatureEditor;
24
 
import org.herac.tuxguitar.gui.editors.chord.ChordCustomList;
25
 
import org.herac.tuxguitar.gui.editors.chord.ChordEditor;
26
 
import org.herac.tuxguitar.gui.editors.chord.ChordList;
27
 
import org.herac.tuxguitar.gui.editors.chord.ChordSelector;
28
 
import org.herac.tuxguitar.gui.editors.tab.Caret;
29
 
import org.herac.tuxguitar.gui.editors.tab.MeasureComponent;
30
 
import org.herac.tuxguitar.gui.editors.tab.MeasureCoords;
31
 
import org.herac.tuxguitar.gui.editors.tab.NoteCoords;
32
 
import org.herac.tuxguitar.gui.editors.tab.SongTrackCoords;
33
 
import org.herac.tuxguitar.gui.undo.undoables.measure.UndoableMeasureGeneric;
34
 
import org.herac.tuxguitar.song.models.Chord;
35
 
import org.herac.tuxguitar.song.models.Duration;
36
 
import org.herac.tuxguitar.song.models.InstrumentString;
37
 
import org.herac.tuxguitar.song.models.Note;
38
 
import org.herac.tuxguitar.song.models.NoteEffect;
39
 
 
40
 
/**
41
 
 * @author julian
42
 
 * 
43
 
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
44
 
 */
45
 
public class InsertChordAction extends Action {
46
 
    public static final String NAME = "action.insert.chord";
47
 
    private ChordEditor editor;
48
 
    private ChordSelector selector;
49
 
    private ChordCustomList custom;
50
 
    private ChordList list;
51
 
    
52
 
    public InsertChordAction(TablatureEditor tablatureEditor) {
53
 
        super(NAME,true, tablatureEditor);
54
 
    }
55
 
 
56
 
    public boolean doAction(TypedEvent e) {
57
 
        Caret caret = getEditor().getTablature().getCaret();
58
 
        SongTrackCoords trackCoords = caret.getSongTrackCoords();
59
 
        MeasureCoords measureCoords = caret.getMeasureCoords();
60
 
        MeasureComponent component = caret.getSelectedComponent();      
61
 
        if (trackCoords != null && measureCoords != null && component != null) {
62
 
                //Si el acorde llego en el data del widget solo lo agrego
63
 
                if(e.widget.getData() instanceof Chord){    
64
 
                        insertChord((Chord)e.widget.getData(), trackCoords, measureCoords, component);
65
 
                }
66
 
                //sino muestro el editor de acordes
67
 
                else{
68
 
                        Shell shell = TuxGuitar.instance().getShell();                  
69
 
                        showInsertDialog(shell,trackCoords,measureCoords,component);
70
 
                }
71
 
        }
72
 
        return true;
73
 
    }
74
 
 
75
 
    protected void updateTablature() {
76
 
        fireUpdate(getEditor().getTablature().getCaret().getMeasureCoords().getMeasure().getNumber());
77
 
        redraw();
78
 
    }
79
 
 
80
 
    public void showInsertDialog(Shell shell,final SongTrackCoords trackCoords,final MeasureCoords measureCoords,final MeasureComponent component) {        
81
 
        final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
82
 
 
83
 
        dialog.setLayout(new GridLayout());            
84
 
        dialog.setText(TuxGuitar.getProperty("chord.editor"));
85
 
 
86
 
        Composite topComposite = new Composite(dialog, SWT.NONE);  
87
 
        topComposite.setLayout(new GridLayout(3,false));
88
 
        topComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));  
89
 
            
90
 
        Composite bottomComposite = new Composite(dialog, SWT.NONE);  
91
 
        bottomComposite.setLayout(new GridLayout());
92
 
        bottomComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));               
93
 
            
94
 
        //---------------SELECTOR--------------------------------            
95
 
        this.selector = new ChordSelector(topComposite,SWT.NONE);
96
 
        this.selector.pack();            
97
 
        //---------------EDITOR--------------------------------            
98
 
        this.editor = new ChordEditor(topComposite, SWT.NONE,(short)trackCoords.getTrack().getStrings().size());
99
 
        this.editor.pack();
100
 
        //---------------CUSTOM CHORDS---------------------------------
101
 
        this.custom = new ChordCustomList(topComposite, SWT.NONE,Math.max(selector.getBounds().height,editor.getBounds().height),this.editor);            
102
 
        //---------------LIST--------------------------------
103
 
        Composite listComposite = new Composite(bottomComposite, SWT.NONE);
104
 
        listComposite.setLayout(new GridLayout());
105
 
        listComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
106
 
 
107
 
        this.list = new ChordList(listComposite);                        
108
 
        this.selector.setEditor(this.editor);
109
 
        this.selector.setList(this.list);
110
 
        this.selector.setTunning(getTunning(trackCoords));
111
 
        this.list.setEditor(this.editor);
112
 
        //------------------BUTTONS--------------------------     
113
 
        Composite buttons = new Composite(dialog, SWT.NONE);
114
 
        buttons.setLayout(new GridLayout(2,false));
115
 
        buttons.setLayoutData(new GridData(SWT.END,SWT.FILL,true,true));        
116
 
            
117
 
        GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);    
118
 
        data.minimumWidth = 80;
119
 
        data.minimumHeight = 25;    
120
 
 
121
 
        final Button buttonOK = new Button(buttons, SWT.PUSH);
122
 
        buttonOK.setText(TuxGuitar.getProperty("ok"));
123
 
        buttonOK.setLayoutData(data);
124
 
        buttonOK.addSelectionListener(new SelectionAdapter() {
125
 
                public void widgetSelected(SelectionEvent arg0) {            
126
 
                insertChord(editor.getCurrentChord(), trackCoords, measureCoords, component);
127
 
                TuxGuitar.instance().getCustomChordManager().write();
128
 
                dialog.dispose();
129
 
            }
130
 
        });
131
 
 
132
 
        Button buttonCancel = new Button(buttons, SWT.PUSH);
133
 
        buttonCancel.setText(TuxGuitar.getProperty("cancel"));
134
 
        buttonCancel.setLayoutData(data);
135
 
        buttonCancel.addSelectionListener(new SelectionAdapter() {
136
 
                public void widgetSelected(SelectionEvent arg0) {
137
 
                        TuxGuitar.instance().getCustomChordManager().write();
138
 
                dialog.dispose();
139
 
            }
140
 
        });
141
 
 
142
 
        this.setCurrentChord();
143
 
            
144
 
        dialog.pack();
145
 
        dialog.open();
146
 
 
147
 
        int x = shell.getBounds().x + (shell.getBounds().width - dialog.getSize().x) / 2;
148
 
        int y = shell.getBounds().y + (shell.getBounds().height - dialog.getSize().y) / 2;
149
 
        dialog.setLocation(x, y);
150
 
            
151
 
        while (!dialog.isDisposed()) {
152
 
                if (!dialog.getDisplay().readAndDispatch()) {
153
 
                        dialog.getDisplay().sleep();
154
 
                }
155
 
        }                 
156
 
    }
157
 
        
158
 
    private int[] getTunning(SongTrackCoords trackCoords){
159
 
        int[] tunning = new int[trackCoords.getTrack().getStrings().size()];
160
 
        
161
 
        Iterator it = trackCoords.getTrack().getStrings().iterator();
162
 
        while(it.hasNext()){
163
 
            InstrumentString string = (InstrumentString)it.next();
164
 
            tunning[(tunning.length - string.getNumber())] = string.getValue();
165
 
        }                
166
 
        
167
 
        return tunning;
168
 
    }    
169
 
    
170
 
    private void insertChord(Chord chord, SongTrackCoords trackCoords, MeasureCoords measureCoords, MeasureComponent component) {
171
 
        //comienza el undoable
172
 
        UndoableMeasureGeneric undoable = UndoableMeasureGeneric.startUndo(); 
173
 
        
174
 
        getSongManager().getMeasureManager().removeAllComponentsAt(measureCoords.getMeasure(),component.getStart());        
175
 
        
176
 
        Iterator it = trackCoords.getTrack().getStrings().iterator();
177
 
        while (it.hasNext()) {
178
 
            InstrumentString string = (InstrumentString) it.next();
179
 
 
180
 
            int value = chord.getFretValue(string.getNumber() - 1);
181
 
            if (value >= 0) {                
182
 
                int velocity = getEditor().getTablature().getCaret().getVelocity();
183
 
                Note note = new Note(value, component.getStart(), (Duration) component.getDuration().clone(),velocity, string.getNumber(),false, new NoteEffect());
184
 
                getSongManager().getMeasureManager().addNote(measureCoords.getMeasure(),note);
185
 
            }
186
 
        }
187
 
 
188
 
        fireUpdate(measureCoords.getMeasure().getNumber());
189
 
        redraw();
190
 
        
191
 
        //termia el undoable
192
 
        getEditor().getUndoManager().addEdit(undoable.endUndo());   
193
 
    }    
194
 
    
195
 
    private void setCurrentChord(){
196
 
        Caret caret = getEditor().getTablature().getCaret();          
197
 
        if(caret.getSelectedComponent() instanceof NoteCoords){
198
 
            int maxValue = -1;
199
 
            int minValue = -1;
200
 
            List components = caret.getMeasureCoords().getComponents(caret.getSelectedComponent().getStart());
201
 
            
202
 
            //verifico el fret
203
 
            Iterator it = components.iterator();
204
 
            while(it.hasNext()){
205
 
                MeasureComponent component = (MeasureComponent)it.next();
206
 
                if(component instanceof NoteCoords){
207
 
                    NoteCoords noteCoords = (NoteCoords)component; 
208
 
                    if(maxValue < 0 || maxValue < noteCoords.getNote().getValue()){
209
 
                        maxValue = noteCoords.getNote().getValue();
210
 
                    }
211
 
                    if(minValue < 0 || minValue > noteCoords.getNote().getValue()){
212
 
                        minValue = noteCoords.getNote().getValue();
213
 
                    }                    
214
 
                }
215
 
            }            
216
 
            if(maxValue > ChordEditor.VIEWING_FRETS  && minValue >= 0){
217
 
                this.editor.setFret((short)(minValue));
218
 
            }
219
 
            
220
 
            //agrego los valores
221
 
            it = components.iterator();
222
 
            while(it.hasNext()){
223
 
                MeasureComponent component = (MeasureComponent)it.next();
224
 
                if(component instanceof NoteCoords){
225
 
                    NoteCoords noteCoords = (NoteCoords)component; 
226
 
                    this.editor.addValue(noteCoords.getNote().getValue(),noteCoords.getNote().getString());              
227
 
                }
228
 
            }              
229
 
        }
230
 
                
231
 
    }
232
 
 
233
 
}
 
 
b'\\ No newline at end of file'