package org.gcube.portlets.user.databasesmanager.client.toolbar; import java.util.LinkedHashMap; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import org.gcube.portlets.user.databasesmanager.client.GWTdbManagerServiceAsync; import org.gcube.portlets.user.databasesmanager.client.datamodel.SubmitQueryData; import org.gcube.portlets.user.databasesmanager.client.events.LoadTablesEvent; import org.gcube.portlets.user.databasesmanager.client.events.RandomSamplingEvent; import org.gcube.portlets.user.databasesmanager.client.events.SamplingEvent; import org.gcube.portlets.user.databasesmanager.client.events.SelectedTableEvent; import org.gcube.portlets.user.databasesmanager.client.events.ShowCreateTableEvent; import org.gcube.portlets.user.databasesmanager.client.events.SmartSamplingEvent; import org.gcube.portlets.user.databasesmanager.client.events.SubmitQueryEvent; import org.gcube.portlets.user.databasesmanager.client.events.interfaces.SelectedTableEventHandler; import org.gcube.portlets.user.databasesmanager.client.form.GxtFormSubmitQuery; import org.gcube.portlets.user.databasesmanager.client.utils.ConstantsPortlet; import com.extjs.gxt.ui.client.Style.ButtonArrowAlign; import com.extjs.gxt.ui.client.Style.ButtonScale; import com.extjs.gxt.ui.client.event.ButtonEvent; import com.extjs.gxt.ui.client.event.Events; import com.extjs.gxt.ui.client.event.Listener; import com.extjs.gxt.ui.client.event.SelectionListener; import com.extjs.gxt.ui.client.event.WindowEvent; import com.extjs.gxt.ui.client.widget.Dialog; import com.extjs.gxt.ui.client.widget.MessageBox; import com.extjs.gxt.ui.client.widget.button.Button; import com.extjs.gxt.ui.client.widget.layout.FitLayout; import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem; import com.extjs.gxt.ui.client.widget.toolbar.ToolBar; import com.google.gwt.event.shared.HandlerManager; //toolbar to contain buttons public class GxtToolbarFunctionality { // event bus private HandlerManager eventBus = null; // buttons private Button btnTablesList; private Button btnSubmitQuery; // private Button btnGetInfo; private Button btnShowCreateTable; private Button btnSimpleSample; private Button btnSmartSample; private Button btnRandomSample; // toolbar private ToolBar toolBar; // dialog list. Each dialog contains a form private LinkedHashMap dialogList = new LinkedHashMap(); private LinkedHashMap submitQueryDataList = new LinkedHashMap(); private static int ID = 0; // ID asscociated to a dialog form // GWT logger private static Logger rootLogger = Logger .getLogger("GxtToolbarFunctionality"); // RPC service private GWTdbManagerServiceAsync RPCservice = null; // Constructor // public GxtToolbarFunctionality(HandlerManager eBus) { // eventBus = eBus; // toolBar = new ToolBar(); // initToolBar(); // addHandler(); // addSelectionListenersOnToolBar(); // } // Constructor with GWTdbManagerServiceAsync service parameter public GxtToolbarFunctionality(HandlerManager eBus, GWTdbManagerServiceAsync service) { eventBus = eBus; RPCservice = service; toolBar = new ToolBar(); initToolBar(); addHandler(); addSelectionListenersOnToolBar(); } private void initToolBar() { // setLayout(new FlowLayout(10)); // Button for tables list btnTablesList = new Button(ConstantsPortlet.TABLESLIST); // btnSubmitQuery.setIcon(Resources.ICONS.text()); btnTablesList.setScale(ButtonScale.SMALL); btnTablesList.setArrowAlign(ButtonArrowAlign.BOTTOM); btnTablesList .setToolTip("returns the list of tables contained in the database schema"); toolBar.add(btnTablesList); // Button for Submit Query btnSubmitQuery = new Button(ConstantsPortlet.SUBMITQUERY); // btnSubmitQuery.setIcon(Resources.ICONS.text()); btnSubmitQuery.setScale(ButtonScale.SMALL); btnSubmitQuery.setArrowAlign(ButtonArrowAlign.BOTTOM); btnSubmitQuery .setToolTip("allows to submit a query to the selected database"); toolBar.add(btnSubmitQuery); // Button to get the "show create table" btnShowCreateTable = new Button(ConstantsPortlet.TABLEDETAILS); btnShowCreateTable.setScale(ButtonScale.SMALL); btnShowCreateTable.setArrowAlign(ButtonArrowAlign.BOTTOM); btnShowCreateTable .setToolTip("gets information about the selected table, e.g. create statement, number of rows, columns names"); toolBar.add(btnShowCreateTable); toolBar.add(new SeparatorToolItem()); // Button for Simple Sample btnSimpleSample = new Button(ConstantsPortlet.SAMPLING); btnSimpleSample.setScale(ButtonScale.SMALL); btnSimpleSample.setArrowAlign(ButtonArrowAlign.BOTTOM); btnSimpleSample.setToolTip("retrieves the first 100 rows of the table"); toolBar.add(btnSimpleSample); // Button for Smart Sample btnSmartSample = new Button(ConstantsPortlet.SMARTSAMPLING); btnSmartSample.setScale(ButtonScale.SMALL); btnSmartSample.setArrowAlign(ButtonArrowAlign.BOTTOM); btnSmartSample .setToolTip("retrieves the first 100 rows of the table, maximising the number of non empty columns"); toolBar.add(btnSmartSample); // button for Random Sample btnRandomSample = new Button(ConstantsPortlet.RANDOMSAMPLING); btnRandomSample.setScale(ButtonScale.SMALL); btnRandomSample.setArrowAlign(ButtonArrowAlign.BOTTOM); btnRandomSample .setToolTip("retrieves 100 randomly picked rows from the table"); toolBar.add(btnRandomSample); // add(toolBar, new FlowData(10)); // disable buttons btnTablesList.disable(); btnSubmitQuery.disable(); // btnGetInfo.disable(); btnShowCreateTable.disable(); btnSimpleSample.disable(); btnSmartSample.disable(); btnRandomSample.disable(); } private void addHandler() { eventBus.addHandler(SelectedTableEvent.TYPE, new SelectedTableEventHandler() { @Override public void onSelectedTable( SelectedTableEvent selectedTableEvent) { // enable button for table details and sampling // operations btnTablesList.enable(); btnSubmitQuery.enable(); btnShowCreateTable.enable(); btnSimpleSample.enable(); btnSmartSample.enable(); btnRandomSample.enable(); } }); } private void addSelectionListenersOnToolBar() { btnSubmitQuery .addSelectionListener(new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { // dialog to insert inputs in order to submit a query final Dialog dialog = new Dialog() { // override the maximize event modifying it with a // different behavior if the mimimize event occurs public void maximize() { if (isCollapsed()) { expand(); } else { super.maximize(); } } }; ID++; final int dialogID = ID; dialog.setLayout(new FitLayout()); // dialog.setModal(true); // dialog.setBlinkModal(true); dialog.setButtons(Dialog.OKCANCEL); // dialog.setPlain(true); // dialog.setCollapsible(false); dialog.setResizable(false); dialog.setMaximizable(true); dialog.setMinimizable(true); dialog.setHeading("Submit Query " + dialogID); dialog.setSize(600, 500); // dialog.setConstrain(false); // dialog.setTitleCollapse(true); // dialog.setWidth(290); // dialog.setHeight(250); // form to submit a query final GxtFormSubmitQuery form = new GxtFormSubmitQuery(); dialog.add(form); dialog.show(); // minimize event handled dialog.addListener(Events.Minimize, new Listener() { @Override public void handleEvent(WindowEvent be) { // collapse the dialog be.getWindow().collapse(); } }); // override maximize event dialog.addListener(Events.Maximize, new Listener() { @Override public void handleEvent(WindowEvent be) { // expand the dialog if (be.getWindow().isCollapsed()) { be.getWindow().expand(); } } }); // listener on the dialog "ok" button dialog.getButtonById(Dialog.OK).addSelectionListener( new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { //start computation and get id //generate a UUID for this request // UUID id = UUID.randomUUID(); // recover info from dialog setInfoOnSubmitQuery(form, dialog, dialogID); rootLogger.log(Level.SEVERE, "query submitted"); } }); // listener on the dialog "cancel" button dialog.getButtonById(Dialog.CANCEL) .addSelectionListener( new SelectionListener() { @Override public void componentSelected( ButtonEvent ce) { rootLogger.log(Level.INFO, "button Cancel event"); //remove computation dialog.hide(); } }); } }); btnSimpleSample .addSelectionListener(new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { // fire event eventBus.fireEvent(new SamplingEvent()); } }); btnSmartSample .addSelectionListener(new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { // fire event eventBus.fireEvent(new SmartSamplingEvent()); } }); btnRandomSample .addSelectionListener(new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { // fire event eventBus.fireEvent(new RandomSamplingEvent()); } }); btnShowCreateTable .addSelectionListener(new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { // fire event eventBus.fireEvent(new ShowCreateTableEvent()); } }); btnTablesList .addSelectionListener(new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { // fire event eventBus.fireEvent(new LoadTablesEvent()); } }); } // buttons enable/disable operation depending from the item selected in the // tree public void enableButtonOnToolbar(int infoTreeDepthSelectedItem, boolean infoSelectedItemIsSchema, String databaseType) { rootLogger.log(Level.INFO, "selectedItem depth: " + infoTreeDepthSelectedItem); switch (infoTreeDepthSelectedItem) { case 1: btnTablesList.disable(); btnSubmitQuery.disable(); // btnGetInfo.disable(); btnShowCreateTable.disable(); btnSimpleSample.disable(); btnSmartSample.disable(); btnRandomSample.disable(); break; case 2: btnTablesList.disable(); btnSubmitQuery.disable(); // btnGetInfo.enable(); btnShowCreateTable.disable(); btnSimpleSample.disable(); btnSmartSample.disable(); btnRandomSample.disable(); break; case 3: if ((databaseType != null) && (databaseType.equals(ConstantsPortlet.MYSQL))) { btnTablesList.enable(); btnSubmitQuery.enable(); // btnGetInfo.disable(); btnShowCreateTable.disable(); btnSimpleSample.disable(); btnSmartSample.disable(); btnRandomSample.disable(); } if ((databaseType != null) && (databaseType.equals(ConstantsPortlet.POSTGRES))) { btnTablesList.disable(); btnSubmitQuery.enable(); // btnGetInfo.disable(); btnShowCreateTable.disable(); btnSimpleSample.disable(); btnSmartSample.disable(); btnRandomSample.disable(); } break; case 4: // check to verify that this level refers to schema or table // and manage it differently if (infoSelectedItemIsSchema == true) { // this tree level is a schema btnTablesList.enable(); btnSubmitQuery.enable(); // btnShowCreateTable.enable(); btnShowCreateTable.disable(); btnSimpleSample.disable(); btnSmartSample.disable(); btnRandomSample.disable(); } else { // this tree level is a table btnTablesList.enable(); btnSubmitQuery.enable(); btnShowCreateTable.enable(); btnSimpleSample.enable(); btnSmartSample.enable(); btnRandomSample.enable(); } break; case 5: // if there is the schema this level refers to table btnTablesList.enable(); btnSubmitQuery.enable(); btnShowCreateTable.enable(); btnSimpleSample.enable(); btnSmartSample.enable(); btnRandomSample.enable(); break; } } public void disableButtonsOperationsOnTable() { btnShowCreateTable.disable(); btnSimpleSample.disable(); btnSmartSample.disable(); btnRandomSample.disable(); } public ToolBar getToolBar() { return this.toolBar; } private void setInfoOnSubmitQuery(GxtFormSubmitQuery form, Dialog SubmtQueryDialog, int dialogID) { SubmitQueryData data = form.getSubmitQueryData(); // data = form.getSubmitQueryData(); String query = data.getQuery(); if ((query == null) || (query.equals(""))) { MessageBox.alert("Warning", "Query field null", null); } else { dialogList.put(new Integer(dialogID), SubmtQueryDialog); submitQueryDataList.put(new Integer(dialogID), data); SubmtQueryDialog.getBody().mask("Loading", "x-mask-loading"); // fire event eventBus.fireEvent(new SubmitQueryEvent(dialogID)); } } // public SubmitQueryData getSubmitQueryData() { // return data; // } // public LinkedHashMap> getDialogForm() { // // return dialog; // return dialogList; // } public LinkedHashMap getDialogFormList() { return dialogList; } public LinkedHashMap getSubmitQueryDataList() { return submitQueryDataList; } // public Integer getDialogID() { // return new Integer(ID); // } }