package org.reldb.dbrowser.ui.content.rel.var;

import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.reldb.dbrowser.ui.IconLoader;
import org.reldb.dbrowser.ui.preferences.PreferenceChangeAdapter;
import org.reldb.dbrowser.ui.preferences.PreferenceChangeEvent;
import org.reldb.dbrowser.ui.preferences.PreferenceChangeListener;
import org.reldb.dbrowser.ui.preferences.PreferencePageGeneral;
import org.reldb.dbrowser.ui.preferences.Preferences;

public class RelvarEditorToolbar {
    
    private PreferenceChangeListener preferenceChangeListener;

	private ToolItem refreshBtn = null;
	private ToolItem goToInsertBtn = null;
	private ToolItem deleteBtn = null;

	private ToolBar toolBar;
	
	public RelvarEditorToolbar(Composite parent, RelvarEditor relvarEditor) {
		toolBar = new ToolBar(parent, SWT.None);

		addAdditionalItems(toolBar);
		
		refreshBtn = new ToolItem(toolBar, SWT.PUSH);
		refreshBtn.setToolTipText("Refresh");
		refreshBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				relvarEditor.refresh();
			}
		});

		goToInsertBtn = new ToolItem(toolBar, SWT.PUSH);
		goToInsertBtn.setToolTipText("Go to INSERT row");
		goToInsertBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				relvarEditor.goToInsertRow();
			}
		});

		deleteBtn = new ToolItem(toolBar, SWT.PUSH);
		deleteBtn.setToolTipText("DELETE selected tuples");
		deleteBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				relvarEditor.askDeleteSelected();
			}
		});

		setupIcons();

		preferenceChangeListener = new PreferenceChangeAdapter("RelvarEditorToolbar") {
			@Override
			public void preferenceChange(PreferenceChangeEvent evt) {
				setupIcons();
			}
		};
		Preferences.addPreferenceChangeListener(PreferencePageGeneral.LARGE_ICONS, preferenceChangeListener);
	}

	private static class ToolbarItem {
		private ToolItem toolItem;
		private String iconName;
		public ToolbarItem(ToolItem toolItem, String iconName) {
			this.toolItem = toolItem;
			this.iconName = iconName;
		}
		ToolItem getToolItem() {return toolItem;}
		String getIconName() {return iconName;}
	}
	
	private Vector<ToolbarItem> additionalItems = new Vector<ToolbarItem>();
	
	/** Add an additional toolbar item. */
	protected void addAdditionalItem(ToolItem item, String iconName) {
		additionalItems.add(new ToolbarItem(item, iconName));
	}
	
	/** Override to add additional toolbar items before the default items. */
	protected void addAdditionalItems(ToolBar toolBar) {}

	public void dispose() {
		Preferences.removePreferenceChangeListener(PreferencePageGeneral.LARGE_ICONS, preferenceChangeListener);
	}

	private void setupIcons() {
		for (ToolbarItem tbi: additionalItems)
			tbi.getToolItem().setImage(IconLoader.loadIcon(tbi.getIconName()));
		refreshBtn.setImage(IconLoader.loadIcon("arrow_refresh"));
		goToInsertBtn.setImage(IconLoader.loadIcon("table_row_insert"));
		deleteBtn.setImage(IconLoader.loadIcon("table_row_delete"));
	}

	public ToolBar getToolBar() {
		return toolBar;
	}

}

@@ -45,34 +45,8 @@
 r;%0A%0A
-import java.util.Vector;%0A%0A
 impo
@@ -1010,40 +1010,8 @@
 ne);
-%0A%0A%09%09addAdditionalItems(toolBar);
 %0A%09%09%0A
@@ -2119,688 +2119,8 @@
 %09%7D%0A%0A
-%09private static class ToolbarItem %7B%0A%09%09private ToolItem toolItem;%0A%09%09private String iconName;%0A%09%09public ToolbarItem(ToolItem toolItem, String iconName) %7B%0A%09%09%09this.toolItem = toolItem;%0A%09%09%09this.iconName = iconName;%0A%09%09%7D%0A%09%09ToolItem getToolItem() %7Breturn toolItem;%7D%0A%09%09String getIconName() %7Breturn iconName;%7D%0A%09%7D%0A%09%0A%09private Vector%3CToolbarItem%3E additionalItems = new Vector%3CToolbarItem%3E();%0A%09%0A%09/** Add an additional toolbar item. */%0A%09protected void addAdditionalItem(ToolItem item, String iconName) %7B%0A%09%09additionalItems.add(new ToolbarItem(item, iconName));%0A%09%7D%0A%09%0A%09/** Override to add additional toolbar items before the default items. */%0A%09protected void addAdditionalItems(ToolBar toolBar) %7B%7D%0A%0A
 %09pub
@@ -2284,120 +2284,8 @@
 ) %7B%0A
-%09%09for (ToolbarItem tbi: additionalItems)%0A%09%09%09tbi.getToolItem().setImage(IconLoader.loadIcon(tbi.getIconName()));%0A
 %09%09re

Remove redundant code.