package org.herac.tuxguitar.gui.tools.browser.dialog;
import java.io.InputStream;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.herac.tuxguitar.gui.TuxGuitar;
import org.herac.tuxguitar.gui.actions.ActionLock;
import org.herac.tuxguitar.gui.actions.file.FileActionUtils;
import org.herac.tuxguitar.gui.helper.SyncThread;
import org.herac.tuxguitar.gui.system.config.TGConfigKeys;
import org.herac.tuxguitar.gui.system.icons.IconLoader;
import org.herac.tuxguitar.gui.system.language.LanguageLoader;
import org.herac.tuxguitar.gui.tools.browser.TGBrowserCollection;
import org.herac.tuxguitar.gui.tools.browser.TGBrowserConnection;
import org.herac.tuxguitar.gui.tools.browser.TGBrowserConnectionHandler;
import org.herac.tuxguitar.gui.tools.browser.TGBrowserFactoryHandler;
import org.herac.tuxguitar.gui.tools.browser.TGBrowserManager;
import org.herac.tuxguitar.gui.tools.browser.base.TGBrowserElement;
import org.herac.tuxguitar.gui.tools.browser.base.TGBrowserFactory;
import org.herac.tuxguitar.gui.util.ConfirmDialog;
import org.herac.tuxguitar.gui.util.DialogUtils;
import org.herac.tuxguitar.gui.util.MessageDialog;
import org.herac.tuxguitar.io.base.TGFileFormatException;
import org.herac.tuxguitar.io.base.TGFileFormatManager;
import org.herac.tuxguitar.song.models.TGSong;
public class TGBrowserDialog implements TGBrowserFactoryHandler,
TGBrowserConnectionHandler, IconLoader, LanguageLoader {
public static final int CALL_CD_ROOT = 3;
public static final int CALL_CD_UP = 4;
public static final int CALL_CLOSE = 2;
public static final int CALL_ELEMENT = 6;
public static final int CALL_LIST = 5;
public static final int CALL_OPEN = 1;
private static final int SHELL_HEIGHT = 350;
private static final int SHELL_WIDTH = 500;
private TGBrowserCollection collection;
protected TableColumn column;
private TGBrowserConnection connection;
private Shell dialog;
protected List<TGBrowserElement> elements;
protected TGBrowserMenuBar menu;
protected Table table;
protected TGBrowserToolBar toolBar;
public TGBrowserDialog() {
this.connection = new TGBrowserConnection(this);
this.menu = new TGBrowserMenuBar(this);
this.toolBar = new TGBrowserToolBar(this);
}
protected void addElements(List<TGBrowserElement> elements) {
this.elements = elements;
}
protected void closeCollection() {
if (!isDisposed() && getCollection() != null) {
this.getConnection().close(CALL_CLOSE);
}
}
public void dispose() {
if (!isDisposed()) {
this.dialog.dispose();
}
}
public void exit() {
this.getConnection().release();
this.getConnection().close(CALL_CLOSE);
TGBrowserManager.instance().writeCollections();
TuxGuitar.instance().getIconManager().removeLoader(this);
}
public TGBrowserCollection getCollection() {
return this.collection;
}
public TGBrowserConnection getConnection() {
return this.connection;
}
public TGBrowserElement getSelection(int index) {
if (!isDisposed() && getConnection().isOpen()) {
if (this.elements != null && index >= 0 && index < this.elements.size()) {
return (TGBrowserElement) this.elements.get(index);
}
}
return null;
}
public Shell getShell() {
return this.dialog;
}
private void initTable(Composite parent) {
this.table = new Table(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
this.table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
this.table.setLinesVisible(TuxGuitar.instance().getConfig()
.getBooleanConfigValue(TGConfigKeys.BROWSER_LINES_VISIBLE));
this.table.setHeaderVisible(false);
this.column = new TableColumn(this.table, SWT.LEFT);
this.table.addListener(SWT.MouseDoubleClick, new Listener() {
public void handleEvent(Event event) {
openElement();
}
});
}
public boolean isDisposed() {
return (this.dialog == null || this.dialog.isDisposed());
}
public void loadIcons() {
if (!isDisposed()) {
this.getShell().setImage(
TuxGuitar.instance().getIconManager().getAppIcon());
this.reload();
}
}
public void loadProperties() {
if (!isDisposed()) {
this.dialog.setText(TuxGuitar.getProperty("browser.dialog"));
this.menu.loadProperties();
this.toolBar.loadProperties();
}
}
public void notifyAdded() {
reload();
}
public void notifyCd(int callId) {
if (!isDisposed()) {
this.getConnection().release();
this.getConnection().listElements(CALL_LIST);
}
}
public void notifyClosed(int callId) {
if (callId != CALL_OPEN) {
this.setCollection(null);
}
this.removeElements();
this.updateCollections(getCollection());
this.updateTable();
if (callId != CALL_OPEN) {
this.getConnection().release();
}
}
public void notifyElements(int callId, List<TGBrowserElement> elements) {
if (!isDisposed()) {
this.addElements(elements);
this.updateTable();
this.getConnection().release();
}
}
public void notifyError(int callId, Throwable throwable) {
if (!isDisposed()) {
this.updateTable();
this.getConnection().release();
MessageDialog.errorMessage(getShell(), throwable);
}
}
public void notifyLockStatusChanged() {
new SyncThread(new Runnable() {
public void run() {
if (!isDisposed()) {
updateBars();
TuxGuitar.instance()
.loadCursor(
getShell(),
(getConnection().isLocked() ? SWT.CURSOR_WAIT
: SWT.CURSOR_ARROW));
}
}
}).start();
}
public void notifyOpened(int callId) {
if (!isDisposed()) {
this.removeElements();
this.updateTable();
this.updateCollections(getCollection());
this.getConnection().release();
this.getConnection().listElements(CALL_LIST);
}
}
public void notifyRemoved() {
if (getCollection() != null) {
closeCollection();
}
reload();
}
public void notifyStream(int callId, final InputStream stream,
final TGBrowserElement element) {
if (!isDisposed()) {
ActionLock.lock();
new SyncThread(new Runnable() {
public void run() {
if (!TuxGuitar.isDisposed()) {
TuxGuitar.instance().getPlayer().reset();
if (TuxGuitar.instance().getFileHistory().isUnsavedFile()) {
ConfirmDialog confirm = new ConfirmDialog(TuxGuitar
.getProperty("file.save-changes-question"));
confirm.setDefaultStatus(ConfirmDialog.STATUS_CANCEL);
int status = confirm.confirm(ConfirmDialog.BUTTON_YES
| ConfirmDialog.BUTTON_NO | ConfirmDialog.BUTTON_CANCEL,
ConfirmDialog.BUTTON_YES);
if (status == ConfirmDialog.STATUS_CANCEL) {
getConnection().release();
ActionLock.unlock();
return;
}
if (status == ConfirmDialog.STATUS_YES) {
final String fileName = FileActionUtils.getFileName();
if (fileName == null) {
getConnection().release();
ActionLock.unlock();
return;
}
new Thread(new Runnable() {
public void run() {
if (!TuxGuitar.isDisposed()) {
FileActionUtils.save(fileName);
new SyncThread(new Runnable() {
public void run() {
if (!TuxGuitar.isDisposed()) {
openStream(stream, element);
}
}
}).start();
}
}
}).start();
return;
}
}
openStream(stream, element);
}
}
}).start();
}
}
protected void openCollection() {
if (!isDisposed() && getCollection() != null) {
TGBrowserFactory factory = TGBrowserManager.instance().getFactory(
getCollection().getType());
getConnection().open(CALL_OPEN,
factory.newTGBrowser(getCollection().getData()));
}
}
public void openElement() {
TGBrowserElement element = getSelection(this.table.getSelectionIndex());
if (element != null) {
this.getConnection().openStream(CALL_ELEMENT, element);
}
}
protected void openStream(final InputStream stream,
final TGBrowserElement element) {
new Thread(new Runnable() {
public void run() {
if (!TuxGuitar.isDisposed()) {
try {
TGSong song = TGFileFormatManager.instance().getLoader().load(
stream);
TuxGuitar.instance().fireNewSong(song, null);
} catch (Throwable throwable) {
TuxGuitar.instance().newSong();
MessageDialog.errorMessage(getShell(), new TGFileFormatException(
TuxGuitar.getProperty("file.open.error", new String[] { element
.getName() }), throwable));
}
getConnection().release();
ActionLock.unlock();
}
}
}).start();
}
protected void reload() {
if (!isDisposed()) {
this.menu.reload(getShell());
this.toolBar.reload();
this.updateTable();
this.updateCollections(getCollection());
this.getShell().layout();
}
}
protected void removeCollection(TGBrowserCollection collection) {
if (collection != null) {
TGBrowserManager.instance().removeCollection(collection);
if (getCollection() != null && getCollection().equals(collection)) {
this.getConnection().close(CALL_CLOSE);
} else {
this.updateCollections(getCollection());
}
}
}
protected void removeElements() {
this.elements = null;
}
public void setCollection(TGBrowserCollection collection) {
this.collection = collection;
}
public void show() {
this.dialog = DialogUtils.newDialog(TuxGuitar.instance().getShell(),
SWT.DIALOG_TRIM | SWT.RESIZE);
this.dialog.setLayout(new GridLayout());
this.dialog.setImage(TuxGuitar.instance().getIconManager().getAppIcon());
this.menu.init(getShell());
this.toolBar.init(getShell());
this.initTable(this.dialog);
this.updateCollections(null);
this.updateTable();
this.dialog.setSize(SHELL_WIDTH, SHELL_HEIGHT);
this.dialog.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
exit();
}
});
this.loadProperties();
this.updateBars();
TGBrowserManager.instance().setFactoryHandler(this);
TuxGuitar.instance().getIconManager().addLoader(this);
TuxGuitar.instance().getLanguageManager().addLoader(this);
DialogUtils.openDialog(this.dialog, DialogUtils.OPEN_STYLE_CENTER);
}
public void updateBars() {
if (!isDisposed()) {
this.menu.updateItems();
this.toolBar.updateItems();
}
}
public void updateCollections(final TGBrowserCollection selection) {
if (!isDisposed()) {
new SyncThread(new Runnable() {
public void run() {
if (!isDisposed()) {
TGBrowserDialog.this.menu.updateCollections(selection);
TGBrowserDialog.this.toolBar.updateCollections(selection);
}
}
}).start();
}
}
protected void updateColumn() {
if (!isDisposed()) {
this.column.pack();
}
}
private void updateTable() {
if (!isDisposed()) {
new SyncThread(new Runnable() {
public void run() {
if (!isDisposed()) {
TGBrowserDialog.this.table.removeAll();
if (TGBrowserDialog.this.elements != null) {
for (final TGBrowserElement element : TGBrowserDialog.this.elements) {
TableItem item = new TableItem(TGBrowserDialog.this.table,
SWT.NONE);
item.setImage(element.isFolder() ? TuxGuitar.instance()
.getIconManager().getBrowserFolder() : TuxGuitar.instance()
.getIconManager().getBrowserFile());
item.setText(element.getName());
}
}
updateColumn();
}
}
}).start();
}
}
}