package org.gcube.portlets.user.databasesmanager.client.toolbar; 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.SelectionListener; 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; private SubmitQueryData data; // toolbar private ToolBar toolBar; // the dialog that contains the form private Dialog dialog; private MessageBox sample; // RPC service private GWTdbManagerServiceAsync RPCservice = null; // GWT logger private static Logger rootLogger = Logger .getLogger("GxtToolbarFunctionality"); 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); 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); toolBar.add(btnSubmitQuery); // // Button to get information about a database // btnGetInfo = new Button(ConstantsPortlet.GETINFO); // btnGetInfo.setScale(ButtonScale.SMALL); // btnGetInfo.setArrowAlign(ButtonArrowAlign.BOTTOM); // toolBar.add(btnGetInfo); // Button to get the "show create table" btnShowCreateTable = new Button(ConstantsPortlet.TABLEDETAILS); btnShowCreateTable.setScale(ButtonScale.SMALL); btnShowCreateTable.setArrowAlign(ButtonArrowAlign.BOTTOM); toolBar.add(btnShowCreateTable); toolBar.add(new SeparatorToolItem()); // Button for Simple Sample btnSimpleSample = new Button(ConstantsPortlet.SAMPLING); btnSimpleSample.setScale(ButtonScale.SMALL); btnSimpleSample.setArrowAlign(ButtonArrowAlign.BOTTOM); toolBar.add(btnSimpleSample); // Button for Smart Sample btnSmartSample = new Button(ConstantsPortlet.SMARTSAMPLING); btnSmartSample.setScale(ButtonScale.SMALL); btnSmartSample.setArrowAlign(ButtonArrowAlign.BOTTOM); toolBar.add(btnSmartSample); // button for Random Sample btnRandomSample = new Button(ConstantsPortlet.RANDOMSAMPLING); btnRandomSample.setScale(ButtonScale.SMALL); btnRandomSample.setArrowAlign(ButtonArrowAlign.BOTTOM); toolBar.add(btnRandomSample); // add(toolBar, new FlowData(10)); 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) { // TODO Auto-generated method stub // enable button for table details and sampling // operation 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) { // clear variable data = null; // dialog to insert inputs in order to submit a query dialog = new Dialog(); 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.setHeading("Submit Query"); dialog.setSize(600, 500); // dialog.setWidth(290); // dialog.setHeight(250); // form to submit a query final GxtFormSubmitQuery form = new GxtFormSubmitQuery(); dialog.add(form); dialog.show(); dialog.getButtonById(Dialog.OK).addSelectionListener( new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { // recover info from dialog setInfoOnSubmitQuery(form); rootLogger.log(Level.SEVERE, "query submitted"); } }); dialog.getButtonById(Dialog.CANCEL) .addSelectionListener( new SelectionListener() { @Override public void componentSelected( ButtonEvent ce) { rootLogger.log(Level.INFO, "button Cancel event"); dialog.hide(); } }); } }); btnSimpleSample .addSelectionListener(new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { // TODO Auto-generated method stub // Listener l = new // Listener() { // // @Override // public void handleEvent(MessageBoxEvent be) { // // TODO Auto-generated method stub // // Button btn = ce.getButton(); // // if (btn.getText().equals("Yes")) // { // // //fire event // // eventBus.fireEvent(new SamplingEvent()); // // } // // // // } // }; // // // sample = new MessageBox(); // // sample.confirm("Confirm", // "Are you sure you want to perform the sampling", l); // // fire event eventBus.fireEvent(new SamplingEvent()); } }); btnSmartSample .addSelectionListener(new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { // TODO Auto-generated method stub // fire event eventBus.fireEvent(new SmartSamplingEvent()); } }); btnRandomSample .addSelectionListener(new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { // TODO Auto-generated method stub // fire event eventBus.fireEvent(new RandomSamplingEvent()); } }); btnShowCreateTable .addSelectionListener(new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { // TODO Auto-generated method stub // fire event eventBus.fireEvent(new ShowCreateTableEvent()); } }); btnTablesList .addSelectionListener(new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { // TODO Auto-generated method stub // fire event eventBus.fireEvent(new LoadTablesEvent()); } }); } //selection buttons 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) { data = form.getSubmitQueryData(); String query = data.getQuery(); if ((query == null) || (query.equals(""))) { MessageBox.alert("Warning", "Query field null", null); } else { dialog.mask("Loading", "x-mask-loading"); //fire event eventBus.fireEvent(new SubmitQueryEvent()); } } public SubmitQueryData getSubmitQueryData() { return data; } public Dialog getDialogForm() { return dialog; } }