1
package org.herac.tuxguitar.gui.marker;
3
import java.util.Iterator;
6
import org.eclipse.swt.SWT;
7
import org.eclipse.swt.events.SelectionAdapter;
8
import org.eclipse.swt.events.SelectionEvent;
9
import org.eclipse.swt.layout.GridData;
10
import org.eclipse.swt.layout.GridLayout;
11
import org.eclipse.swt.widgets.Button;
12
import org.eclipse.swt.widgets.Composite;
13
import org.eclipse.swt.widgets.Event;
14
import org.eclipse.swt.widgets.Listener;
15
import org.eclipse.swt.widgets.Shell;
16
import org.eclipse.swt.widgets.Table;
17
import org.eclipse.swt.widgets.TableColumn;
18
import org.eclipse.swt.widgets.TableItem;
19
import org.herac.tuxguitar.gui.TuxGuitar;
20
import org.herac.tuxguitar.gui.actions.ActionLock;
21
import org.herac.tuxguitar.gui.editors.tab.Caret;
22
import org.herac.tuxguitar.gui.helper.SyncThread;
23
import org.herac.tuxguitar.gui.system.icons.IconLoader;
24
import org.herac.tuxguitar.gui.system.language.LanguageLoader;
25
import org.herac.tuxguitar.gui.undo.undoables.custom.UndoableChangeMarker;
26
import org.herac.tuxguitar.gui.util.DialogUtils;
27
import org.herac.tuxguitar.song.models.TGMarker;
29
public class MarkerList implements IconLoader,LanguageLoader{
31
private static MarkerList instance;
33
protected Shell dialog;
37
private Composite compositeTable;
38
private TableColumn measureColumn;
39
private TableColumn titleColumn;
41
private Composite compositeButtons;
42
private Button buttonAdd;
43
private Button buttonEdit;
44
private Button buttonDelete;
45
private Button buttonGo;
46
private Button buttonClose;
48
public static MarkerList instance(){
50
instance = new MarkerList();
55
private MarkerList() {
56
TuxGuitar.instance().getIconManager().addLoader(this);
57
TuxGuitar.instance().getLanguageManager().addLoader(this);
61
this.dialog = DialogUtils.newDialog(TuxGuitar.instance().getShell(), SWT.DIALOG_TRIM);
62
this.dialog.setLayout(new GridLayout(2,false));
63
this.dialog.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
64
// ----------------------------------------------------------------------
65
this.compositeTable = new Composite(this.dialog, SWT.NONE);
66
this.compositeTable.setLayout(new GridLayout());
67
this.compositeTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
69
this.table = new Table(this.compositeTable, SWT.BORDER | SWT.FULL_SELECTION);
70
this.table.setLayoutData(new GridData(250,200));
71
this.table.setHeaderVisible(true);
72
this.table.addListener (SWT.MouseDoubleClick, new Listener() {
73
public void handleEvent (Event event) {
74
new MarkerNavigator().goToSelectedMarker(getSelectedMarker());
75
TuxGuitar.instance().updateCache(true);
78
this.measureColumn = new TableColumn(this.table, SWT.LEFT);
79
this.measureColumn.setWidth(70);
81
this.titleColumn = new TableColumn(this.table, SWT.LEFT);
82
this.titleColumn.setWidth(180);
84
this.loadTableItems(false);
86
// ------------------BUTTONS--------------------------
87
this.compositeButtons = new Composite(this.dialog, SWT.NONE);
88
this.compositeButtons.setLayout(new GridLayout(1,false));
89
this.compositeButtons.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
91
this.buttonAdd = new Button(this.compositeButtons, SWT.PUSH);
92
this.buttonAdd.setLayoutData(makeGridData(SWT.FILL, SWT.TOP,false));
93
this.buttonAdd.addSelectionListener(new SelectionAdapter() {
94
public void widgetSelected(SelectionEvent e) {
95
if(!ActionLock.isLocked() && !TuxGuitar.instance().isLocked()){
97
Caret caret = TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
98
TGMarker marker = TuxGuitar.instance().getSongManager().getFactory().newMarker();
99
marker.setMeasure(caret.getMeasure().getNumber());
100
if(new MarkerEditor(marker,MarkerEditor.STATUS_NEW).open(MarkerList.this.dialog)){
101
TuxGuitar.instance().updateCache(true);
102
loadTableItems(true);
109
this.buttonEdit = new Button(this.compositeButtons, SWT.PUSH);
110
this.buttonEdit.setLayoutData(makeGridData(SWT.FILL, SWT.TOP,false));
111
this.buttonEdit.addSelectionListener(new SelectionAdapter() {
112
public void widgetSelected(SelectionEvent arg0) {
113
if(!ActionLock.isLocked() && !TuxGuitar.instance().isLocked()){
115
TGMarker marker = getSelectedMarker();
117
if(new MarkerEditor(marker,MarkerEditor.STATUS_EDIT).open(MarkerList.this.dialog)){
118
TuxGuitar.instance().updateCache(true);
119
loadTableItems(true);
127
this.buttonDelete = new Button(this.compositeButtons, SWT.PUSH);
128
this.buttonDelete.setLayoutData(makeGridData(SWT.FILL, SWT.TOP,false));
129
this.buttonDelete.addSelectionListener(new SelectionAdapter() {
130
public void widgetSelected(SelectionEvent arg0) {
131
if(!ActionLock.isLocked() && !TuxGuitar.instance().isLocked()){
133
TGMarker marker = getSelectedMarker();
134
// comienza el undoable
135
UndoableChangeMarker undoable = UndoableChangeMarker.startUndo(marker);
137
TuxGuitar.instance().getSongManager().removeMarker(marker);
139
// termia el undoable
140
TuxGuitar.instance().getUndoableManager().addEdit(undoable.endUndo(null));
141
TuxGuitar.instance().getFileHistory().setUnsavedFile();
142
TuxGuitar.instance().updateCache(true);
143
loadTableItems(true);
149
this.buttonGo = new Button(this.compositeButtons, SWT.PUSH);
150
this.buttonGo.setLayoutData(makeGridData(SWT.FILL, SWT.BOTTOM,true));
151
this.buttonGo.addSelectionListener(new SelectionAdapter() {
152
public void widgetSelected(SelectionEvent arg0) {
153
if(!ActionLock.isLocked() && !TuxGuitar.instance().isLocked()){
155
new MarkerNavigator().goToSelectedMarker(getSelectedMarker());
156
TuxGuitar.instance().updateCache(true);
162
this.buttonClose = new Button(this.compositeButtons, SWT.PUSH);
163
this.buttonClose.setLayoutData(makeGridData(SWT.FILL, SWT.BOTTOM,false));
164
this.buttonClose.addSelectionListener(new SelectionAdapter() {
165
public void widgetSelected(SelectionEvent arg0) {
166
MarkerList.this.dialog.dispose();
171
this.loadProperties(false);
173
this.dialog.setDefaultButton( this.buttonGo );
175
DialogUtils.openDialog(this.dialog,DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK);
178
public void dispose(){
180
this.dialog.dispose();
184
public void update(){
188
public void update(final boolean keepSelection){
190
new SyncThread(new Runnable() {
193
loadTableItems(keepSelection);
200
private GridData makeGridData(int horizontalAlignment,int verticalAlignment,boolean grabExcessVerticalSpace){
201
GridData data = new GridData();
202
data.horizontalAlignment = horizontalAlignment;
203
data.verticalAlignment = verticalAlignment;
204
data.grabExcessHorizontalSpace = true;
205
data.grabExcessVerticalSpace = grabExcessVerticalSpace;
206
data.minimumWidth = 80;
207
data.minimumHeight = 25;
212
protected void loadTableItems(boolean keepSelection){
213
int itemSelected = (keepSelection ? this.table.getSelectionIndex() : -1 );
215
this.table.removeAll();
216
this.markers = TuxGuitar.instance().getSongManager().getMarkers();
218
Iterator it = this.markers.iterator();
219
while (it.hasNext()) {
220
TGMarker marker = (TGMarker) it.next();
222
TableItem item = new TableItem(this.table, SWT.NONE);
223
item.setText(new String[] { Integer.toString(marker.getMeasure()),marker.getTitle() });
226
if(itemSelected >= 0 && itemSelected < this.markers.size()){
227
this.table.select(itemSelected);
231
protected TGMarker getSelectedMarker(){
232
int itemSelected = this.table.getSelectionIndex();
233
if(itemSelected >= 0 && itemSelected < this.markers.size()){
234
return (TGMarker)this.markers.get(itemSelected);
239
public boolean isDisposed(){
240
return (this.dialog == null || this.dialog.isDisposed());
243
public void loadIcons() {
245
this.dialog.setImage(TuxGuitar.instance().getIconManager().getAppIcon());
249
public void loadProperties() {
250
this.loadProperties(true);
253
public void loadProperties(boolean layout) {
255
this.dialog.setText(TuxGuitar.getProperty("marker.list"));
256
this.measureColumn.setText(TuxGuitar.getProperty("measure"));
257
this.titleColumn.setText(TuxGuitar.getProperty("title"));
258
this.buttonAdd.setText(TuxGuitar.getProperty("add"));
259
this.buttonEdit.setText(TuxGuitar.getProperty("edit"));
260
this.buttonDelete.setText(TuxGuitar.getProperty("remove"));
261
this.buttonGo.setText(TuxGuitar.getProperty("go"));
262
this.buttonClose.setText(TuxGuitar.getProperty("close"));
266
this.compositeTable.layout();
267
this.compositeButtons.layout();
268
this.dialog.pack(true);