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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/marker/MarkerEditor.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.marker;
2
 
 
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;
24
 
 
25
 
public class MarkerEditor {
26
 
        public static final int STATUS_NEW = 1;
27
 
        public static final int STATUS_EDIT = 2;
28
 
        
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;        
32
 
        
33
 
        private int status;
34
 
        private Marker marker;
35
 
        private Shell dialog;
36
 
        private Spinner measureSpinner;
37
 
        private Text titleText;
38
 
        private Button colorButton;
39
 
 
40
 
        public MarkerEditor(Marker marker) {
41
 
                this(marker,STATUS_NEW);
42
 
        }       
43
 
        
44
 
        public MarkerEditor(Marker marker,int status) {
45
 
                this.marker = (Marker)marker.clone();
46
 
                this.status = status;
47
 
        }               
48
 
 
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"));
53
 
 
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));
58
 
 
59
 
                // Measure Number
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"));
64
 
 
65
 
                this.measureSpinner = new Spinner(composite, SWT.BORDER);
66
 
                this.measureSpinner.setLayoutData(getAlignmentData(MINIMUN_CONTROL_WIDTH,SWT.FILL));
67
 
                
68
 
                
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();
75
 
                                if (selection < 1) {
76
 
                                        measureSpinner.setSelection(1);
77
 
                                } else if (selection > measureCount) {
78
 
                                        measureSpinner.setSelection(measureCount);
79
 
                                }
80
 
                        }
81
 
                });
82
 
 
83
 
                // Title
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());
90
 
 
91
 
                // Color
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();
103
 
                                if (rgb != null) {
104
 
                                        marker.getColor().setR(rgb.red);
105
 
                                        marker.getColor().setG(rgb.green);
106
 
                                        marker.getColor().setB(rgb.blue);       
107
 
                    colorButton.redraw();                                       
108
 
                                }
109
 
                        }
110
 
                });             
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);
116
 
                        color.dispose();
117
 
                        }               
118
 
                });             
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));
123
 
 
124
 
                GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
125
 
                data.minimumWidth = MINIMUN_BUTTON_WIDTH;
126
 
                data.minimumHeight = MINIMUN_BUTTON_HEIGHT;
127
 
 
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) {
133
 
                                updateMarker();
134
 
                                dialog.dispose();
135
 
                        }
136
 
                });
137
 
 
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) {
143
 
                                dialog.dispose();
144
 
                        }
145
 
                });
146
 
 
147
 
                dialog.pack();
148
 
                dialog.open();
149
 
 
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);
153
 
 
154
 
                while (!dialog.isDisposed()) {
155
 
                        if (!dialog.getDisplay().readAndDispatch()) {
156
 
                                dialog.getDisplay().sleep();
157
 
                        }
158
 
 
159
 
                }
160
 
 
161
 
        }
162
 
 
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;
170
 
        return data;
171
 
    }   
172
 
 
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();
178
 
                
179
 
                SongManager manager = TuxGuitar.instance().getSongManager();
180
 
                
181
 
                //comienza el undoable
182
 
                JoinedUndoable joinedUndoable = new JoinedUndoable();
183
 
                
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));
188
 
                }
189
 
                UndoableChangeMarker undoable = UndoableChangeMarker.startUndo(manager.getMarker(this.marker.getMeasure()));            
190
 
                TuxGuitar.instance().getSongManager().updateMarker(this.marker);
191
 
                joinedUndoable.addUndoableEdit(undoable.endUndo(this.marker));
192
 
 
193
 
                // termia el undoable
194
 
                TuxGuitar.instance().getTablatureEditor().getUndoManager().addEdit(joinedUndoable.endUndo());
195
 
        }
196
 
}