1
package org.herac.tuxguitar.gui.tools.browser.dialog;
3
import java.io.InputStream;
4
import java.util.Collections;
5
import java.util.Iterator;
8
import org.eclipse.swt.SWT;
9
import org.eclipse.swt.events.DisposeEvent;
10
import org.eclipse.swt.events.DisposeListener;
11
import org.eclipse.swt.layout.GridData;
12
import org.eclipse.swt.layout.GridLayout;
13
import org.eclipse.swt.widgets.Composite;
14
import org.eclipse.swt.widgets.Event;
15
import org.eclipse.swt.widgets.Listener;
16
import org.eclipse.swt.widgets.Shell;
17
import org.eclipse.swt.widgets.Table;
18
import org.eclipse.swt.widgets.TableColumn;
19
import org.eclipse.swt.widgets.TableItem;
20
import org.herac.tuxguitar.gui.TuxGuitar;
21
import org.herac.tuxguitar.gui.actions.ActionLock;
22
import org.herac.tuxguitar.gui.actions.file.FileActionUtils;
23
import org.herac.tuxguitar.gui.helper.SyncThread;
24
import org.herac.tuxguitar.gui.system.config.TGConfigKeys;
25
import org.herac.tuxguitar.gui.system.icons.IconLoader;
26
import org.herac.tuxguitar.gui.system.language.LanguageLoader;
27
import org.herac.tuxguitar.gui.tools.browser.TGBrowserCollection;
28
import org.herac.tuxguitar.gui.tools.browser.TGBrowserConnection;
29
import org.herac.tuxguitar.gui.tools.browser.TGBrowserConnectionHandler;
30
import org.herac.tuxguitar.gui.tools.browser.TGBrowserFactoryHandler;
31
import org.herac.tuxguitar.gui.tools.browser.TGBrowserManager;
32
import org.herac.tuxguitar.gui.tools.browser.base.TGBrowserElement;
33
import org.herac.tuxguitar.gui.tools.browser.base.TGBrowserFactory;
34
import org.herac.tuxguitar.gui.util.ConfirmDialog;
35
import org.herac.tuxguitar.gui.util.DialogUtils;
36
import org.herac.tuxguitar.gui.util.MessageDialog;
37
import org.herac.tuxguitar.io.base.TGFileFormatException;
38
import org.herac.tuxguitar.io.base.TGFileFormatManager;
39
import org.herac.tuxguitar.song.models.TGSong;
41
public class TGBrowserDialog implements TGBrowserFactoryHandler,TGBrowserConnectionHandler,IconLoader,LanguageLoader{
42
private static final int SHELL_WIDTH = 500;
43
private static final int SHELL_HEIGHT = 350;
45
public static final int CALL_OPEN = 1;
46
public static final int CALL_CLOSE = 2;
47
public static final int CALL_CD_ROOT = 3;
48
public static final int CALL_CD_UP = 4;
49
public static final int CALL_LIST = 5;
50
public static final int CALL_ELEMENT = 6;
52
private TGBrowserCollection collection;
53
private TGBrowserConnection connection;
55
protected Table table;
56
protected TableColumn column;
57
protected List elements;
58
protected TGBrowserMenuBar menu;
59
protected TGBrowserToolBar toolBar;
61
public TGBrowserDialog(){
62
this.connection = new TGBrowserConnection(this);
63
this.menu = new TGBrowserMenuBar(this);
64
this.toolBar = new TGBrowserToolBar(this);
67
public TGBrowserConnection getConnection(){
68
return this.connection;
71
public TGBrowserCollection getCollection() {
72
return this.collection;
75
public void setCollection(TGBrowserCollection collection) {
76
this.collection = collection;
79
public Shell getShell(){
84
this.getConnection().release();
85
this.getConnection().close(CALL_CLOSE);
86
TGBrowserManager.instance().writeCollections();
87
TuxGuitar.instance().getIconManager().removeLoader(this);
91
this.dialog = DialogUtils.newDialog(TuxGuitar.instance().getShell(),SWT.DIALOG_TRIM | SWT.RESIZE);
92
this.dialog.setLayout(new GridLayout());
93
this.dialog.setImage(TuxGuitar.instance().getIconManager().getAppIcon());
95
this.menu.init(getShell());
96
this.toolBar.init(getShell());
97
this.initTable(this.dialog);
98
this.updateCollections(null);
100
this.dialog.setSize(SHELL_WIDTH,SHELL_HEIGHT);
101
this.dialog.addDisposeListener(new DisposeListener() {
102
public void widgetDisposed(DisposeEvent e) {
107
this.loadProperties();
110
TGBrowserManager.instance().setFactoryHandler(this);
111
TuxGuitar.instance().getIconManager().addLoader(this);
112
TuxGuitar.instance().getLanguageManager().addLoader(this);
113
DialogUtils.openDialog(this.dialog, DialogUtils.OPEN_STYLE_CENTER);
116
private void initTable(Composite parent){
117
this.table = new Table(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
118
this.table.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
119
this.table.setLinesVisible(TuxGuitar.instance().getConfig().getBooleanConfigValue(TGConfigKeys.BROWSER_LINES_VISIBLE));
120
this.table.setHeaderVisible(false);
122
this.column = new TableColumn(this.table, SWT.LEFT);
124
this.table.addListener (SWT.MouseDoubleClick, new Listener() {
125
public void handleEvent (Event event) {
131
public boolean isDisposed(){
132
return (this.dialog == null || this.dialog.isDisposed());
135
public void dispose(){
137
this.dialog.dispose();
141
private void updateTable(){
143
new SyncThread(new Runnable() {
146
TGBrowserDialog.this.table.removeAll();
147
if(TGBrowserDialog.this.elements != null){
148
Iterator it = TGBrowserDialog.this.elements.iterator();
150
TGBrowserElement element = (TGBrowserElement)it.next();
151
TableItem item = new TableItem(TGBrowserDialog.this.table, SWT.NONE);
152
item.setImage(element.isFolder()?TuxGuitar.instance().getIconManager().getBrowserFolder():TuxGuitar.instance().getIconManager().getBrowserFile());
153
item.setText(element.getName());
163
protected void updateColumn(){
169
public void updateBars(){
171
this.menu.updateItems();
172
this.toolBar.updateItems();
176
public void updateCollections(final TGBrowserCollection selection){
178
new SyncThread(new Runnable() {
181
TGBrowserDialog.this.menu.updateCollections(selection);
182
TGBrowserDialog.this.toolBar.updateCollections(selection);
189
public TGBrowserElement getSelection(int index){
190
if(!isDisposed() && getConnection().isOpen()){
191
if(this.elements != null && index >= 0 && index < this.elements.size()){
192
return (TGBrowserElement)this.elements.get(index);
198
protected void removeElements(){
199
this.elements = null;
202
protected void addElements(List elements){
203
if(elements != null){
204
Collections.sort(elements,new TGBrowserElementComparator());
206
this.elements = elements;
209
protected void openCollection(){
210
if(!isDisposed() && getCollection() != null){
211
TGBrowserFactory factory = TGBrowserManager.instance().getFactory(getCollection().getType());
212
getConnection().open(CALL_OPEN,factory.newTGBrowser(getCollection().getData()));
216
protected void closeCollection(){
217
if(!isDisposed() && getCollection() != null){
218
this.getConnection().close(CALL_CLOSE);
222
protected void removeCollection(TGBrowserCollection collection){
223
if(collection != null){
224
TGBrowserManager.instance().removeCollection(collection);
225
if( getCollection() != null && getCollection().equals( collection ) ){
226
this.getConnection().close(CALL_CLOSE);
228
this.updateCollections( getCollection() );
233
public void openElement(){
234
TGBrowserElement element = getSelection(this.table.getSelectionIndex());
236
this.getConnection().openStream(CALL_ELEMENT,element);
240
public void notifyLockStatusChanged(){
241
new SyncThread(new Runnable() {
245
TuxGuitar.instance().loadCursor(getShell(),( getConnection().isLocked() ? SWT.CURSOR_WAIT : SWT.CURSOR_ARROW ) );
251
public void notifyOpened(int callId) {
253
this.removeElements();
255
this.updateCollections(getCollection());
256
this.getConnection().release();
257
this.getConnection().listElements(CALL_LIST);
261
public void notifyClosed(int callId) {
262
if(callId != CALL_OPEN){
263
this.setCollection(null);
265
this.removeElements();
266
this.updateCollections(getCollection());
268
if(callId != CALL_OPEN){
269
this.getConnection().release();
273
public void notifyError(int callId,Throwable throwable){
276
this.getConnection().release();
277
MessageDialog.errorMessage(getShell(),throwable);
281
public void notifyCd(int callId) {
283
this.getConnection().release();
284
this.getConnection().listElements(CALL_LIST);
288
public void notifyElements(int callId,List elements) {
290
this.addElements(elements);
292
this.getConnection().release();
296
public void notifyStream(int callId,final InputStream stream,final TGBrowserElement element) {
299
new SyncThread(new Runnable() {
301
if(!TuxGuitar.isDisposed()){
302
TuxGuitar.instance().getPlayer().reset();
303
if(TuxGuitar.instance().getFileHistory().isUnsavedFile()){
304
ConfirmDialog confirm = new ConfirmDialog(TuxGuitar.getProperty("file.save-changes-question"));
305
confirm.setDefaultStatus( ConfirmDialog.STATUS_CANCEL );
306
int status = confirm.confirm(ConfirmDialog.BUTTON_YES | ConfirmDialog.BUTTON_NO | ConfirmDialog.BUTTON_CANCEL, ConfirmDialog.BUTTON_YES);
307
if(status == ConfirmDialog.STATUS_CANCEL){
308
getConnection().release();
312
if(status == ConfirmDialog.STATUS_YES){
313
final String fileName = FileActionUtils.getFileName();
314
if(fileName == null){
315
getConnection().release();
319
new Thread(new Runnable() {
321
if(!TuxGuitar.isDisposed()){
322
FileActionUtils.save(fileName);
323
new SyncThread(new Runnable() {
325
if(!TuxGuitar.isDisposed()){
326
openStream(stream,element);
336
openStream(stream,element);
343
protected void openStream(final InputStream stream,final TGBrowserElement element){
344
new Thread(new Runnable() {
346
if(!TuxGuitar.isDisposed()){
348
TGSong song = TGFileFormatManager.instance().getLoader().load(TuxGuitar.instance().getSongManager().getFactory(),stream);
349
TuxGuitar.instance().fireNewSong(song,null);
350
}catch (Throwable throwable) {
351
TuxGuitar.instance().newSong();
352
MessageDialog.errorMessage(getShell(),new TGFileFormatException(TuxGuitar.getProperty("file.open.error", new String[]{element.getName()}),throwable));
354
getConnection().release();
361
public void loadIcons() {
363
this.getShell().setImage(TuxGuitar.instance().getIconManager().getAppIcon());
368
public void loadProperties() {
370
this.dialog.setText(TuxGuitar.getProperty("browser.dialog"));
371
this.menu.loadProperties();
372
this.toolBar.loadProperties();
376
public void notifyAdded() {
380
public void notifyRemoved() {
381
if(getCollection() != null){
387
protected void reload(){
389
this.menu.reload(getShell());
390
this.toolBar.reload();
392
this.updateCollections(getCollection());
393
this.getShell().layout();