2
* Created on 17-dic-2005
4
* TODO To change the template for this generated file go to
5
* Window - Preferences - Java - Code Style - Code Templates
7
package org.herac.tuxguitar.gui.actions.insert;
9
import java.util.Iterator;
10
import java.util.List;
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;
43
* TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
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;
52
public InsertChordAction(TablatureEditor tablatureEditor) {
53
super(NAME,true, tablatureEditor);
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);
66
//sino muestro el editor de acordes
68
Shell shell = TuxGuitar.instance().getShell();
69
showInsertDialog(shell,trackCoords,measureCoords,component);
75
protected void updateTablature() {
76
fireUpdate(getEditor().getTablature().getCaret().getMeasureCoords().getMeasure().getNumber());
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);
83
dialog.setLayout(new GridLayout());
84
dialog.setText(TuxGuitar.getProperty("chord.editor"));
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));
90
Composite bottomComposite = new Composite(dialog, SWT.NONE);
91
bottomComposite.setLayout(new GridLayout());
92
bottomComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
94
//---------------SELECTOR--------------------------------
95
this.selector = new ChordSelector(topComposite,SWT.NONE);
97
//---------------EDITOR--------------------------------
98
this.editor = new ChordEditor(topComposite, SWT.NONE,(short)trackCoords.getTrack().getStrings().size());
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));
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));
117
GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
118
data.minimumWidth = 80;
119
data.minimumHeight = 25;
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();
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();
142
this.setCurrentChord();
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);
151
while (!dialog.isDisposed()) {
152
if (!dialog.getDisplay().readAndDispatch()) {
153
dialog.getDisplay().sleep();
158
private int[] getTunning(SongTrackCoords trackCoords){
159
int[] tunning = new int[trackCoords.getTrack().getStrings().size()];
161
Iterator it = trackCoords.getTrack().getStrings().iterator();
163
InstrumentString string = (InstrumentString)it.next();
164
tunning[(tunning.length - string.getNumber())] = string.getValue();
170
private void insertChord(Chord chord, SongTrackCoords trackCoords, MeasureCoords measureCoords, MeasureComponent component) {
171
//comienza el undoable
172
UndoableMeasureGeneric undoable = UndoableMeasureGeneric.startUndo();
174
getSongManager().getMeasureManager().removeAllComponentsAt(measureCoords.getMeasure(),component.getStart());
176
Iterator it = trackCoords.getTrack().getStrings().iterator();
177
while (it.hasNext()) {
178
InstrumentString string = (InstrumentString) it.next();
180
int value = chord.getFretValue(string.getNumber() - 1);
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);
188
fireUpdate(measureCoords.getMeasure().getNumber());
192
getEditor().getUndoManager().addEdit(undoable.endUndo());
195
private void setCurrentChord(){
196
Caret caret = getEditor().getTablature().getCaret();
197
if(caret.getSelectedComponent() instanceof NoteCoords){
200
List components = caret.getMeasureCoords().getComponents(caret.getSelectedComponent().getStart());
203
Iterator it = components.iterator();
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();
211
if(minValue < 0 || minValue > noteCoords.getNote().getValue()){
212
minValue = noteCoords.getNote().getValue();
216
if(maxValue > ChordEditor.VIEWING_FRETS && minValue >= 0){
217
this.editor.setFret((short)(minValue));
221
it = components.iterator();
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());
b'\\ No newline at end of file'