/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.design.pages;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import net.java.dev.genesis.annotation.ViewHandler;
import net.java.dev.genesis.ui.swt.SWTBinder;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.widgets.ColumnLayout;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import de.innovationgate.eclipse.utils.ui.GenesisBoundFormPage;
import de.innovationgate.eclipse.utils.ui.model.BeanListTableModelCellModifier;
import de.innovationgate.eclipse.utils.ui.model.BeanListTableModelContentProvider;
import de.innovationgate.eclipse.utils.ui.model.BeanListTableModelLabelProvider;
import de.innovationgate.eclipse.utils.ui.model.BeanListTableModelValueMapper;
import de.innovationgate.wga.common.beans.csconfig.v1.JobDefinition;
import de.innovationgate.wga.model.BeanListTableModelListener;
import de.innovationgate.wga.model.JobDefinitionsModel;
import de.innovationgate.wga.model.JobType;
import de.innovationgate.wga.model.Model;
import de.innovationgate.wga.model.WGADesignConfigurationModel;
@ViewHandler
public class JobDefinitionsConfigurationPage extends GenesisBoundFormPage {
public static final String ID = "de.innovationgate.eclipse.wgadesigner.pages.JobDefinitionsConfigurationPage";
public static final String PAGE_TITLE = "Job Definitions";
private WGADesignConfigurationModel _model;
private Table _tblJobDefinitions;
private TableColumn _jobNameColumn;
private TableColumn _jobTypeColumn;
private JobDefinitionsModel _jobDefinitionsModel;
private TableViewer _tblViewerJobDefinitions;
private Button _btnAddJobDefinition;
private TableColumn _jobResourceColumn;
private Button _btnRemoveJobDefinition;
private HashMap<Integer, JobType> _jobTypesByIndex;
private TableColumn _jobDescriptionColumn;
private TableColumn _jobScheduleColumn;
public JobDefinitionsConfigurationPage(FormEditor editor) {
super(editor, ID, PAGE_TITLE);
}
public void setModel(Model model) {
_model = (WGADesignConfigurationModel) model;
}
public Model getModel() {
return _model;
}
@Override
protected void createFormContent(IManagedForm managedForm) {
ScrolledForm form = managedForm.getForm();
FormToolkit toolkit = managedForm.getToolkit();
toolkit.decorateFormHeading(form.getForm());
form.setText(PAGE_TITLE);
ColumnLayout layout = new ColumnLayout();
layout.maxNumColumns = 2;
form.getBody().setLayout(layout);
// encoder section
Section section = toolkit.createSection(form.getBody(), Section.DESCRIPTION|Section.TITLE_BAR|Section.TWISTIE|Section.EXPANDED);
section.setText("Job Definitions");
Composite sectionClient = toolkit.createComposite(section);
section.setClient(sectionClient);
GridLayout sectionLayout = new GridLayout();
GridData fillHSpan = new GridData(GridData.FILL_HORIZONTAL);
fillHSpan.horizontalSpan = 4;
GridData prefSize = new GridData();
prefSize.widthHint = 50;
sectionLayout.numColumns = 2;
sectionClient.setLayout(sectionLayout);
// create Table for remote actions
_tblJobDefinitions = toolkit.createTable(sectionClient, SWT.BORDER|SWT.FULL_SELECTION);
_tblJobDefinitions.setHeaderVisible(true);
GridData tblLayoutData = new GridData(GridData.FILL_BOTH);
tblLayoutData.verticalSpan = 2;
tblLayoutData.minimumHeight = 200;
_tblJobDefinitions.setLayoutData(tblLayoutData);
registerField("jobDefinitions", _tblJobDefinitions);
int tableWidth = 700;
_jobNameColumn = new TableColumn(_tblJobDefinitions, SWT.NONE);
_jobNameColumn.setText("Name");
_jobNameColumn.setWidth((int)(tableWidth * 0.2));
_jobTypeColumn = new TableColumn(_tblJobDefinitions, SWT.NONE);
_jobTypeColumn.setText("Type");
_jobTypeColumn.setWidth((int)(tableWidth * 0.2));
_jobResourceColumn = new TableColumn(_tblJobDefinitions, SWT.NONE);
_jobResourceColumn.setText("Resource");
_jobResourceColumn.setWidth((int)(tableWidth * 0.2));
_jobDescriptionColumn = new TableColumn(_tblJobDefinitions, SWT.NONE);
_jobDescriptionColumn.setText("Description");
_jobDescriptionColumn.setWidth((int)(tableWidth * 0.2));
_jobScheduleColumn = new TableColumn(_tblJobDefinitions, SWT.NONE);
_jobScheduleColumn.setText("CRON schedule");
_jobScheduleColumn.setWidth((int)(tableWidth * 0.2));
_jobDefinitionsModel = new JobDefinitionsModel(_model.getJobDefinitions());
_jobDefinitionsModel.addListener(new BeanListTableModelListener() {
public void add(Object bean) {
_model.fireModelChanged();
}
public void remove(Object bean) {
_model.fireModelChanged();
}
public void update(Object bean) {
_model.fireModelChanged();
}
@SuppressWarnings("unchecked")
public void refresh(List beans) {
_model.fireModelChanged();
}
});
_tblViewerJobDefinitions = new TableViewer(_tblJobDefinitions);
List<String> jobTypes = new ArrayList<String>();
Iterator<JobType> it = WGADesignConfigurationModel.JOBTYPES.values().iterator();
_jobTypesByIndex = new HashMap<Integer, JobType>();
int i = 0;
while (it.hasNext()) {
JobType type = it.next();
jobTypes.add(type.getValue());
_jobTypesByIndex.put(i, type);
i++;
}
CellEditor[] editors = new CellEditor[5];
editors[0] = new TextCellEditor(_tblJobDefinitions);
editors[1] = new ComboBoxCellEditor(_tblJobDefinitions, jobTypes.toArray(new String[0]), SWT.READ_ONLY);
editors[2] = new TextCellEditor(_tblJobDefinitions);
editors[3] = new TextCellEditor(_tblJobDefinitions);
editors[4] = new TextCellEditor(_tblJobDefinitions);
_tblViewerJobDefinitions.setCellEditors(editors);
BeanListTableModelCellModifier modifier = new BeanListTableModelCellModifier(_tblViewerJobDefinitions, _jobDefinitionsModel);
modifier.setEditMode(1, BeanListTableModelCellModifier.EDIT_MODE_ON_SINGLE_CLICK);
_tblViewerJobDefinitions.setCellModifier(modifier);
modifier.setValueMapper(1, new BeanListTableModelValueMapper() {
public Object map(Object element, String property, Object value) {
return _jobTypesByIndex.get((Integer) value).getKey();
}
});
_tblViewerJobDefinitions.setContentProvider(new BeanListTableModelContentProvider());
_tblViewerJobDefinitions.setLabelProvider(new BeanListTableModelLabelProvider(_jobDefinitionsModel));
_tblViewerJobDefinitions.setInput(_jobDefinitionsModel);
GridData btnLayout = new GridData(GridData.HORIZONTAL_ALIGN_FILL, GridData.VERTICAL_ALIGN_FILL, false, false);
_btnAddJobDefinition = toolkit.createButton(sectionClient, "add", SWT.PUSH);
_btnAddJobDefinition.setLayoutData(btnLayout);
_btnAddJobDefinition.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleAddJobDefinition();
}
});
_btnRemoveJobDefinition = toolkit.createButton(sectionClient, "remove", SWT.PUSH);
_btnRemoveJobDefinition.setLayoutData(btnLayout);
_btnRemoveJobDefinition.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleRemoveJobDefinition();
}
});
bind(form, SWTBinder.BINDING_STRATEGY_AS_YOU_TYPE);
}
private void handleAddJobDefinition() {
JobDefinition jobDef = new JobDefinition();
jobDef.setName("<name>");
jobDef.setType(JobType.TYPE_TMLSCRIPTMODULE);
jobDef.setResource("<job resource>");
_jobDefinitionsModel.add(jobDef);
}
private void handleRemoveJobDefinition() {
if (_tblJobDefinitions.getSelectionCount() > 0) {
_jobDefinitionsModel.remove((JobDefinition) _tblJobDefinitions.getSelection()[0].getData());
}
}
@Override
public void refresh() throws IOException {
super.refresh();
if (_jobDefinitionsModel != null && _model != null && _model.getJobDefinitions() != null) {
_jobDefinitionsModel.refresh(_model.getJobDefinitions());
}
}
}