1
package org.herac.tuxguitar.gui.marker;
3
import org.eclipse.swt.SWT;
4
import org.eclipse.swt.events.PaintEvent;
5
import org.eclipse.swt.events.PaintListener;
6
import org.eclipse.swt.events.SelectionAdapter;
7
import org.eclipse.swt.events.SelectionEvent;
8
import org.eclipse.swt.graphics.Color;
9
import org.eclipse.swt.graphics.RGB;
10
import org.eclipse.swt.layout.GridData;
11
import org.eclipse.swt.layout.GridLayout;
12
import org.eclipse.swt.widgets.Button;
13
import org.eclipse.swt.widgets.ColorDialog;
14
import org.eclipse.swt.widgets.Composite;
15
import org.eclipse.swt.widgets.Label;
16
import org.eclipse.swt.widgets.Shell;
17
import org.eclipse.swt.widgets.Spinner;
18
import org.eclipse.swt.widgets.Text;
19
import org.herac.tuxguitar.gui.TuxGuitar;
20
import org.herac.tuxguitar.gui.undo.undoables.JoinedUndoable;
21
import org.herac.tuxguitar.gui.undo.undoables.custom.UndoableChangeMarker;
22
import org.herac.tuxguitar.song.managers.SongManager;
23
import org.herac.tuxguitar.song.models.Marker;
25
public class MarkerEditor {
26
public static final int STATUS_NEW = 1;
27
public static final int STATUS_EDIT = 2;
29
private static final int MINIMUN_CONTROL_WIDTH = 180;
30
private static final int MINIMUN_BUTTON_WIDTH = 80;
31
private static final int MINIMUN_BUTTON_HEIGHT = 25;
34
private Marker marker;
36
private Spinner measureSpinner;
37
private Text titleText;
38
private Button colorButton;
40
public MarkerEditor(Marker marker) {
41
this(marker,STATUS_NEW);
44
public MarkerEditor(Marker marker,int status) {
45
this.marker = (Marker)marker.clone();
49
public void show(Shell shell) {
50
dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
51
dialog.setLayout(new GridLayout());
52
dialog.setText(TuxGuitar.getProperty("marker"));
54
// ----------------------------------------------------------------------
55
Composite composite = new Composite(dialog, SWT.NONE);
56
composite.setLayout(new GridLayout(2, false));
57
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
60
final int measureCount = TuxGuitar.instance().getSongManager().countMeasures();
61
Label measureLabel = new Label(composite, SWT.NULL);
62
measureLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,true));
63
measureLabel.setText(TuxGuitar.getProperty("measure"));
65
this.measureSpinner = new Spinner(composite, SWT.BORDER);
66
this.measureSpinner.setLayoutData(getAlignmentData(MINIMUN_CONTROL_WIDTH,SWT.FILL));
69
this.measureSpinner.setMinimum(1);
70
this.measureSpinner.setMaximum(measureCount);
71
this.measureSpinner.setSelection(this.marker.getMeasure());
72
this.measureSpinner.addSelectionListener(new SelectionAdapter() {
73
public void widgetSelected(SelectionEvent e) {
74
int selection = measureSpinner.getSelection();
76
measureSpinner.setSelection(1);
77
} else if (selection > measureCount) {
78
measureSpinner.setSelection(measureCount);
84
Label titleLabel = new Label(composite, SWT.NULL);
85
titleLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true));
86
titleLabel.setText(TuxGuitar.getProperty("title"));
87
this.titleText = new Text(composite, SWT.BORDER);
88
this.titleText.setLayoutData(getAlignmentData(MINIMUN_CONTROL_WIDTH,SWT.FILL));
89
this.titleText.setText(this.marker.getTitle());
92
Label colorLabel = new Label(composite, SWT.NULL);
93
colorLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true));
94
colorLabel.setText(TuxGuitar.getProperty("color"));
95
this.colorButton = new Button(composite, SWT.PUSH);
96
this.colorButton.setLayoutData(getAlignmentData(MINIMUN_CONTROL_WIDTH,SWT.FILL));
97
this.colorButton.addSelectionListener(new SelectionAdapter() {
98
public void widgetSelected(SelectionEvent event) {
99
ColorDialog dlg = new ColorDialog(dialog);
100
dlg.setRGB(dialog.getDisplay().getSystemColor(SWT.COLOR_BLACK).getRGB());
101
dlg.setText(TuxGuitar.getProperty("choose-color"));
102
RGB rgb = dlg.open();
104
marker.getColor().setR(rgb.red);
105
marker.getColor().setG(rgb.green);
106
marker.getColor().setB(rgb.blue);
107
colorButton.redraw();
111
this.colorButton.addPaintListener(new PaintListener() {
112
public void paintControl(PaintEvent e) {
113
Color color = new Color(dialog.getDisplay(), marker.getColor().getR(), marker.getColor().getG(), marker.getColor().getB());
114
e.gc.setBackground(color);
115
e.gc.fillRectangle(5,5,colorButton.getSize().x - 10,colorButton.getSize().y - 10);
119
// ------------------BUTTONS--------------------------
120
Composite buttons = new Composite(dialog, SWT.NONE);
121
buttons.setLayout(new GridLayout(2, false));
122
buttons.setLayoutData(new GridData(SWT.END, SWT.FILL, true, true));
124
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
125
data.minimumWidth = MINIMUN_BUTTON_WIDTH;
126
data.minimumHeight = MINIMUN_BUTTON_HEIGHT;
128
final Button buttonOK = new Button(buttons, SWT.PUSH);
129
buttonOK.setText(TuxGuitar.getProperty("ok"));
130
buttonOK.setLayoutData(data);
131
buttonOK.addSelectionListener(new SelectionAdapter() {
132
public void widgetSelected(SelectionEvent arg0) {
138
Button buttonCancel = new Button(buttons, SWT.PUSH);
139
buttonCancel.setText(TuxGuitar.getProperty("cancel"));
140
buttonCancel.setLayoutData(data);
141
buttonCancel.addSelectionListener(new SelectionAdapter() {
142
public void widgetSelected(SelectionEvent arg0) {
150
int x = shell.getBounds().x + (shell.getBounds().width - dialog.getSize().x) / 2;
151
int y = shell.getBounds().y + (shell.getBounds().height - dialog.getSize().y) / 2;
152
dialog.setLocation(x, y);
154
while (!dialog.isDisposed()) {
155
if (!dialog.getDisplay().readAndDispatch()) {
156
dialog.getDisplay().sleep();
163
private GridData getAlignmentData(int minimumWidth,int horizontalAlignment){
164
GridData data = new GridData();
165
data.minimumWidth = minimumWidth;
166
data.horizontalAlignment = horizontalAlignment;
167
data.verticalAlignment = SWT.DEFAULT;
168
data.grabExcessHorizontalSpace = true;
169
data.grabExcessVerticalSpace = true;
173
private void updateMarker() {
174
int oldMeasure = this.marker.getMeasure();
175
this.marker.setMeasure(this.measureSpinner.getSelection());
176
this.marker.setTitle(this.titleText.getText());
177
this.marker = (Marker)this.marker.clone();
179
SongManager manager = TuxGuitar.instance().getSongManager();
181
//comienza el undoable
182
JoinedUndoable joinedUndoable = new JoinedUndoable();
184
if(this.status == STATUS_EDIT && oldMeasure != this.marker.getMeasure()){
185
UndoableChangeMarker undoable = UndoableChangeMarker.startUndo(manager.getMarker(oldMeasure));
186
TuxGuitar.instance().getSongManager().removeMarker(oldMeasure);
187
joinedUndoable.addUndoableEdit(undoable.endUndo(null));
189
UndoableChangeMarker undoable = UndoableChangeMarker.startUndo(manager.getMarker(this.marker.getMeasure()));
190
TuxGuitar.instance().getSongManager().updateMarker(this.marker);
191
joinedUndoable.addUndoableEdit(undoable.endUndo(this.marker));
193
// termia el undoable
194
TuxGuitar.instance().getTablatureEditor().getUndoManager().addEdit(joinedUndoable.endUndo());