databases-manager-portlet/src/main/java/org/gcube/portlets/user/databasesmanager/client/toolbar/GxtToolbarFunctionality.java

559 lines
14 KiB
Java

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 org.gcube.portlets.user.dbmanager.client.GWTdbManagerServiceAsync;
//import org.gcube.portlets.user.dbmanager.client.datamodel.SubmitQueryData;
//import org.gcube.portlets.user.dbmanager.client.events.SubmitQueryEvent;
//import org.gcube.portlets.user.dbmanager.client.form.GxtFormSubmitQuery;
//import org.gcube.portlets.user.dbmanager.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.Style.Scroll;
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;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.Listener;
public class GxtToolbarFunctionality {
private HandlerManager eventBus = null;
private Button btnTablesList;
private Button btnSubmitQuery;
// private Button btnGetInfo;
private Button btnShowCreateTable;
private Button btnSimpleSample;
private Button btnSmartSample;
private Button btnRandomSample;
private SubmitQueryData data;
private ToolBar toolBar;
// the dialog that contains the form
// private Dialog dialog = new Dialog();
private Dialog dialog;
private MessageBox sample;
// RPC service
private GWTdbManagerServiceAsync RPCservice = null;
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 fro 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 tabele details and sampling operation
btnTablesList.enable();
btnSubmitQuery.enable();
btnShowCreateTable.enable();
btnSimpleSample.enable();
btnSmartSample.enable();
btnRandomSample.enable();
}
});
}
private void addSelectionListenersOnToolBar() {
btnSubmitQuery
.addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
data = null;
// dialog to insert inputs in order to submit a query
// final Dialog dialog = new Dialog();
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);
final GxtFormSubmitQuery form = new GxtFormSubmitQuery();
dialog.add(form);
dialog.show();
dialog.getButtonById(Dialog.OK).addSelectionListener(
new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
// System.out.println("button OK event");
// dialog.mask("Loading",
// "x-mask-loading");
// dialog.hide();
// recover info from dialog
setInfoOnSubmitQuery(form);
// System.out
// .println("GxtToolbarFunctionality-> query submitted");
rootLogger.log(Level.SEVERE,
"query submitted");
}
});
dialog.getButtonById(Dialog.CANCEL)
.addSelectionListener(
new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(
ButtonEvent ce) {
// System.out
// .println("button Cancel event");
rootLogger.log(Level.INFO,
"button Cancel event");
dialog.hide();
}
});
}
});
btnSimpleSample
.addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
// TODO Auto-generated method stub
// Listener<MessageBoxEvent> l = new
// Listener<MessageBoxEvent>() {
//
// @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());
// // TODO: insert the result of the sample operation
//
// // TODO: manage the exception case (returned from
// // algorithm "Simple Sample") with an alert
//
// // System.out.println("simple sample event");
//
// Dialog d = new Dialog();
// d.setHeading("Simple Sample");
//
// // d.setBodyStyle("fontWeight:bold;padding:13px;");
//
// d.setBodyStyle("padding:9px;");
// d.setSize(600, 400);
// d.setScrollMode(Scroll.AUTO);
// d.setHideOnButtonClick(true);
// d.setModal(true);
// d.setMaximizable(true);
// d.addText("Result Table 1");
// d.show();
}
});
btnSmartSample
.addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
// TODO Auto-generated method stub
// fire event
eventBus.fireEvent(new SmartSamplingEvent());
// TODO: insert the result of the sample operation
// TODO: manage the exception case (returned from
// algorithm "Smart Sample") with an alert
// System.out.println("simple sample event");
// Dialog d = new Dialog();
// d.setHeading("Smart Sample");
//
// // d.setBodyStyle("fontWeight:bold;padding:13px;");
//
// d.setBodyStyle("padding:9px;");
// d.setSize(600, 400);
// d.setScrollMode(Scroll.AUTO);
// d.setHideOnButtonClick(true);
// d.setModal(true);
// d.setMaximizable(true);
// d.addText("Result Table 2");
// d.show();
}
});
btnRandomSample
.addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
// TODO Auto-generated method stub
// fire event
eventBus.fireEvent(new RandomSamplingEvent());
// TODO: insert the result of the sample operation
// TODO: manage the exception case (returned from
// algorithm "Random Sample") with an alert
// System.out.println("simple sample event");
// Dialog d = new Dialog();
// d.setHeading("Random Sample");
//
// // d.setBodyStyle("fontWeight:bold;padding:13px;");
//
// d.setBodyStyle("padding:9px;");
// d.setSize(600, 400);
// d.setScrollMode(Scroll.AUTO);
// d.setHideOnButtonClick(true);
// d.setModal(true);
// d.setMaximizable(true);
// d.addText("Result Table 3");
// d.show();
}
});
btnShowCreateTable
.addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
// TODO Auto-generated method stub
// fire event
eventBus.fireEvent(new ShowCreateTableEvent());
}
});
btnTablesList
.addSelectionListener(new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
// TODO Auto-generated method stub
// fire event
eventBus.fireEvent(new LoadTablesEvent());
}
});
}
public void enableButtonOnToolbar(int infoTreeDepthSelectedItem,
boolean infoSelectedItemIsSchema, String databaseType) {
// System.out.println("GxtToolbarFunctionality-> depth: "
// + infoTreeDepthSelectedItem);
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");
eventBus.fireEvent(new SubmitQueryEvent());
}
}
public SubmitQueryData getSubmitQueryData() {
return data;
}
public Dialog getDialogForm() {
return dialog;
}
}