/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.kickstart.ui.panel;
import org.activiti.kickstart.KickstartApplication;
import org.activiti.kickstart.dto.KickstartTask;
import org.activiti.kickstart.dto.KickstartUserTask;
import org.activiti.kickstart.dto.KickstartWorkflow;
import org.activiti.kickstart.service.KickstartService;
import org.activiti.kickstart.service.KickstartServiceFactory;
import org.activiti.kickstart.ui.ViewManager;
import org.activiti.kickstart.ui.popup.ErrorPopupWindow;
import org.activiti.kickstart.ui.popup.ProcessImagePopupWindow;
import org.activiti.kickstart.ui.table.TaskTable;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.terminal.ClassResource;
import com.vaadin.terminal.Resource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;
/**
* @author Joram Barrez
*/
public class KickstartWorkflowPanel extends Panel {
protected static final long serialVersionUID = -2074647293591779784L;
protected static final String NEW_WORKFLOW_TITLE = "Create new adhoc workflow";
protected static final String EXISTING_WORKFLOW_TITLE = "Edit workflow";
protected static final String NAME_FIELD = "Name";
protected static final String DESCRIPTION_FIELD = "Description";
// ui
protected Label titleLabel;
protected TextField nameField;
protected TextField descriptionField;
protected TaskTable taskTable;
protected Resource saveImage;
protected Resource generateImageImage;
// dependencies
protected KickstartService kickStartService;
protected KickstartWorkflow existingWorkflow;
public KickstartWorkflowPanel(KickstartWorkflow existingWorkflow) {
this.existingWorkflow = existingWorkflow;
this.saveImage = new ClassResource("images/page_save.png", KickstartApplication.get());
this.generateImageImage = new ClassResource("images/image.png", KickstartApplication.get());
init();
}
public KickstartWorkflowPanel() {
this(null);
}
protected void init() {
setSizeFull();
setStyleName(Reindeer.PANEL_LIGHT);
this.kickStartService = KickstartApplication.get().getKickstartService();
initUi();
}
protected void initUi() {
initTitle();
GridLayout layout = new GridLayout(2, 7);
layout.setSizeFull();
layout.setColumnExpandRatio(0, 1.0f);
layout.setColumnExpandRatio(1, 9.0f);
layout.setSpacing(true);
addComponent(layout);
initNameField(layout);
initDescriptionField(layout);
initTaskTable(layout);
initButtons(layout);
}
protected void initTitle() {
VerticalLayout verticalLayout = new VerticalLayout();
if (existingWorkflow == null) {
titleLabel = new Label(NEW_WORKFLOW_TITLE);
} else {
titleLabel = new Label(EXISTING_WORKFLOW_TITLE + " '" + existingWorkflow.getName() + "'");
}
titleLabel.setStyleName(Reindeer.LABEL_H1);
verticalLayout.addComponent(titleLabel);
// add some empty space
Label emptyLabel = new Label("");
emptyLabel.setHeight("1.5em");
verticalLayout.addComponent(emptyLabel);
addComponent(verticalLayout);
}
protected void initNameField(GridLayout layout) {
nameField = new TextField();
nameField.setWriteThrough(true);
nameField.setImmediate(true);
if (existingWorkflow != null) {
nameField.setValue(existingWorkflow.getName());
}
layout.addComponent(new Label(NAME_FIELD));
layout.addComponent(nameField);
}
protected void initDescriptionField(GridLayout layout) {
descriptionField = new TextField();
descriptionField.setRows(4);
descriptionField.setColumns(35);
if (existingWorkflow != null) {
descriptionField.setValue(existingWorkflow.getDescription());
}
layout.addComponent(new Label(DESCRIPTION_FIELD));
layout.addComponent(descriptionField);
}
protected void initTaskTable(GridLayout layout) {
taskTable = new TaskTable();
if (existingWorkflow == null) {
taskTable.addDefaultTaskRow();
} else {
for (KickstartTask task : existingWorkflow.getTasks()) {
if (task instanceof KickstartUserTask) {
taskTable.addTaskRow((KickstartUserTask) task);
}
}
}
layout.addComponent(new Label("Tasks"));
layout.addComponent(taskTable);
}
protected void initButtons(GridLayout layout) {
final Button saveButton = new Button("Save");
saveButton.setEnabled(nameField.getValue() != null && !"".equals((String) nameField.getValue()));
saveButton.setIcon(saveImage);
saveButton.addListener(new Button.ClickListener() {
private static final long serialVersionUID = 1L;
public void buttonClick(ClickEvent event) {
try {
kickStartService.deployWorkflow(createWorkflow(), null);
Panel successPanel = new Panel();
successPanel.setStyleName(Reindeer.PANEL_LIGHT);
Label successLabel = new Label("Process successfully deployed");
successPanel.addComponent(successLabel);
KickstartApplication.get().getViewManager().showComponent(successPanel);
} catch (Exception e) {
e.printStackTrace();
KickstartApplication.get().getViewManager().showPopupWindow(new ErrorPopupWindow(e));
}
}
});
// Dependending on namefield value, save button is enabled
nameField.addListener(new ValueChangeListener() {
private static final long serialVersionUID = 1L;
public void valueChange(ValueChangeEvent event) {
if (nameField.getValue() != null && !"".equals((String) nameField.getValue())) {
saveButton.setEnabled(true);
} else {
saveButton.setEnabled(false);
}
}
});
Button generateImageButton = new Button("View image");
generateImageButton.setIcon(generateImageImage);
generateImageButton.addListener(new Button.ClickListener() {
private static final long serialVersionUID = 5671158538486627690L;
public void buttonClick(ClickEvent event) {
ViewManager viewManager = KickstartApplication.get().getViewManager();
viewManager.showPopupWindow(new ProcessImagePopupWindow(createWorkflow()));
}
});
HorizontalLayout footer = new HorizontalLayout();
footer.setSpacing(true);
footer.addComponent(saveButton);
footer.addComponent(generateImageButton);
layout.addComponent(new Label());
layout.addComponent(footer);
}
protected KickstartWorkflow createWorkflow() {
KickstartWorkflow workflow = new KickstartWorkflow();
workflow.setName((String) nameField.getValue());
workflow.setDescription((String) descriptionField.getValue());
for (KickstartUserTask task : taskTable.getTasks()) {
workflow.addTask(task);
}
return workflow;
}
}