package com.example.vaadin_test.views;
import java.util.List;
import java.util.Set;
import org.ofbiz.base.util.Debug;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.ModelParam;
import org.ofbiz.service.ModelService;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.data.util.filter.SimpleStringFilter;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.HeaderCell;
import com.vaadin.ui.Grid.HeaderRow;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.example.vaadin_test.OfbizComponent;
public class ServiceView extends OfbizComponent implements View {
/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
@AutoGenerated
private HorizontalLayout mainLayout;
@AutoGenerated
private VerticalLayout verticalLayout_1;
@AutoGenerated
private Table outParametersTable;
@AutoGenerated
private Table inParametersTable;
@AutoGenerated
private Label serviceNameLabel;
@AutoGenerated
private Grid serviceList;
/**
* The constructor should first build the main layout, set the
* composition root and then do any custom initialization.
*
* The constructor will not be automatically regenerated by the
* visual editor.
*/
public ServiceView() {
buildMainLayout();
setCompositionRoot(mainLayout);
// TODO add user code here
initTables();
initServiceList();
}
private void initTables() {
// inParametersTable
inParametersTable.setCaption("In Parameters");
inParametersTable.addContainerProperty("Parameter Name", String.class, null);
inParametersTable.addContainerProperty("Description", String.class, null);
inParametersTable.addContainerProperty("Optional", String.class, null);
inParametersTable.addContainerProperty("Type", String.class, null);
inParametersTable.addContainerProperty("Mode", String.class, null);
inParametersTable.addContainerProperty("Is Set Internally", String.class, null);
inParametersTable.addContainerProperty("Entity Name", String.class, null);
inParametersTable.addContainerProperty("Field Name", String.class, null);
// outParametersTable
outParametersTable.setHeight("-1px");
outParametersTable.setCaption("Out Parameters");
outParametersTable.addContainerProperty("Parameter Name", String.class, null);
outParametersTable.addContainerProperty("Description", String.class, null);
outParametersTable.addContainerProperty("Optional", String.class, null);
outParametersTable.addContainerProperty("Type", String.class, null);
outParametersTable.addContainerProperty("Mode", String.class, null);
outParametersTable.addContainerProperty("Is Set Internally", String.class, null);
outParametersTable.addContainerProperty("Entity Name", String.class, null);
outParametersTable.addContainerProperty("Field Name", String.class, null);
}
private void initServiceList() {
// get all ofbiz service names
Set<String> serviceNames = dispatcher.getDispatchContext().getAllServiceNames();
final IndexedContainer serviceContainer = new IndexedContainer();
serviceContainer.addContainerProperty("name", String.class, null);
for (String s : serviceNames) {
Item newItem = serviceContainer.getItem(serviceContainer.addItem());
newItem.getItemProperty("name").setValue(s);
}
// init grid component
serviceList.setCaption("Service List");
serviceList.setContainerDataSource(serviceContainer);
// add grid item click listener
serviceList.addItemClickListener(new ItemClickEvent.ItemClickListener() {
@Override
public void itemClick(ItemClickEvent itemClickEvent) {
String serviceName = itemClickEvent.getItem().getItemProperty("name").getValue().toString();
ModelService serviceModel = null;
try {
serviceModel = dispatcher.getDispatchContext().getModelService(serviceName);
}
catch (GenericServiceException e) {
Debug.log(e.getMessage());
}
if (serviceModel != null) {
List<ModelParam> serviceParams = serviceModel.getModelParamList();
serviceNameLabel.setValue(serviceName);
inParametersTable.removeAllItems();
outParametersTable.removeAllItems();
int inIndex = 0;
int outIndex = 0;
for (ModelParam param : serviceParams) {
if (param.isIn()) {
inParametersTable.addItem(
new Object[]{
param.name,
param.description,
new Boolean(param.optional).toString(),
param.type,
param.mode,
new Boolean(param.internal).toString(),
param.entityName,
param.fieldName
}, inIndex++
);
}
}
for (ModelParam param : serviceParams) {
if (param.isOut()) {
outParametersTable.addItem(
new Object[]{
param.name,
param.description,
new Boolean(param.optional).toString(),
param.type,
param.mode,
new Boolean(param.internal).toString(),
param.entityName,
param.fieldName
}, inIndex++
);
}
}
}
}
});
// add service list filter
HeaderRow filterRow = serviceList.appendHeaderRow();
HeaderCell cell = filterRow.getCell("name");
TextField filterField = new TextField();
filterField.addTextChangeListener(new TextChangeListener() {
@Override
public void textChange(TextChangeEvent event) {
serviceContainer.removeContainerFilters("name");
if (! event.getText().isEmpty()) {
serviceContainer.addContainerFilter(
new SimpleStringFilter("name", event.getText(), true, false)
);
}
}
});
cell.setComponent(filterField);
}
@AutoGenerated
private HorizontalLayout buildMainLayout() {
// common part: create layout
mainLayout = new HorizontalLayout();
mainLayout.setImmediate(false);
mainLayout.setWidth("-1px");
mainLayout.setHeight("-1px");
mainLayout.setMargin(false);
// top-level component properties
setWidth("-1px");
setHeight("-1px");
// serviceList
serviceList = new Grid();
serviceList.setImmediate(false);
serviceList.setWidth("200px");
serviceList.setHeight("-1px");
mainLayout.addComponent(serviceList);
// verticalLayout_1
verticalLayout_1 = buildVerticalLayout_1();
mainLayout.addComponent(verticalLayout_1);
return mainLayout;
}
@AutoGenerated
private VerticalLayout buildVerticalLayout_1() {
// common part: create layout
verticalLayout_1 = new VerticalLayout();
verticalLayout_1.setImmediate(false);
verticalLayout_1.setWidth("-1px");
verticalLayout_1.setHeight("-1px");
verticalLayout_1.setMargin(false);
// serviceNameLabel
serviceNameLabel = new Label();
serviceNameLabel.setImmediate(false);
serviceNameLabel.setWidth("-1px");
serviceNameLabel.setHeight("-1px");
serviceNameLabel.setValue("Label");
verticalLayout_1.addComponent(serviceNameLabel);
// inParametersTable
inParametersTable = new Table();
inParametersTable.setImmediate(false);
inParametersTable.setWidth("-1px");
inParametersTable.setHeight("-1px");
verticalLayout_1.addComponent(inParametersTable);
// outParametersTable
outParametersTable = new Table();
outParametersTable.setImmediate(false);
outParametersTable.setWidth("-1px");
verticalLayout_1.addComponent(outParametersTable);
return verticalLayout_1;
}
@Override
public void enter(ViewChangeEvent event) {
// TODO Auto-generated method stub
}
}