/jbpm-flow/src/main/java/org/jbpm/process/instance/impl/demo/UIWorkItemHandlerDialog.java

https://github.com/mariofusco/jbpm · Java · 175 lines · 137 code · 19 blank · 19 comment · 3 complexity · 1fe54f9b00cf1fb3eb7c68dd627b76ea MD5 · raw file

  1. /**
  2. * Copyright 2010 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jbpm.process.instance.impl.demo;
  17. import java.awt.BorderLayout;
  18. import java.awt.Dimension;
  19. import java.awt.GridBagConstraints;
  20. import java.awt.GridBagLayout;
  21. import java.awt.Insets;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.Map;
  27. import javax.swing.JButton;
  28. import javax.swing.JDialog;
  29. import javax.swing.JLabel;
  30. import javax.swing.JPanel;
  31. import javax.swing.JTextArea;
  32. import javax.swing.JTextField;
  33. import org.kie.api.runtime.process.WorkItem;
  34. /**
  35. *
  36. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  37. */
  38. public class UIWorkItemHandlerDialog extends JDialog {
  39. private static final long serialVersionUID = 510l;
  40. private Map<String, Object> results = new HashMap<String, Object>();
  41. private UIWorkItemHandler handler;
  42. private WorkItem workItem;
  43. private JTextField resultNameTextField;
  44. private JTextField resultValueTextField;
  45. private JButton addResultButton;
  46. private JButton completeButton;
  47. private JButton abortButton;
  48. public UIWorkItemHandlerDialog(UIWorkItemHandler handler, WorkItem workItem) {
  49. super(handler, "Execute Work Item", true);
  50. this.handler = handler;
  51. this.workItem = workItem;
  52. setSize(new Dimension(400, 300));
  53. initializeComponent();
  54. }
  55. private void initializeComponent() {
  56. JPanel panel = new JPanel();
  57. panel.setLayout(new GridBagLayout());
  58. getRootPane().setLayout(new BorderLayout());
  59. getRootPane().add(panel, BorderLayout.CENTER);
  60. JTextArea params = new JTextArea();
  61. params.setText(getParameters());
  62. params.setEditable(false);
  63. GridBagConstraints c = new GridBagConstraints();
  64. c.weightx = 1;
  65. c.weighty = 1;
  66. c.gridwidth = 5;
  67. c.fill = GridBagConstraints.BOTH;
  68. c.insets = new Insets(5, 5, 5, 5);
  69. panel.add(params, c);
  70. JLabel resultName = new JLabel("Result");
  71. c = new GridBagConstraints();
  72. c.gridy = 1;
  73. c.insets = new Insets(5, 5, 5, 5);
  74. panel.add(resultName, c);
  75. resultNameTextField = new JTextField();
  76. c = new GridBagConstraints();
  77. c.gridx = 1;
  78. c.gridy = 1;
  79. c.weightx = 0.3;
  80. c.fill = GridBagConstraints.HORIZONTAL;
  81. c.insets = new Insets(5, 5, 5, 5);
  82. panel.add(resultNameTextField, c);
  83. JLabel resultValue = new JLabel("Value");
  84. c = new GridBagConstraints();
  85. c.gridx = 2;
  86. c.gridy = 1;
  87. c.insets = new Insets(5, 5, 5, 5);
  88. panel.add(resultValue, c);
  89. resultValueTextField = new JTextField();
  90. c = new GridBagConstraints();
  91. c.gridx = 3;
  92. c.gridy = 1;
  93. c.weightx = 0.7;
  94. c.fill = GridBagConstraints.HORIZONTAL;
  95. c.insets = new Insets(5, 5, 5, 5);
  96. panel.add(resultValueTextField, c);
  97. addResultButton = new JButton("Add");
  98. addResultButton.addActionListener(new ActionListener() {
  99. public void actionPerformed(ActionEvent event) {
  100. addResult();
  101. }
  102. });
  103. c = new GridBagConstraints();
  104. c.gridx = 4;
  105. c.gridy = 1;
  106. c.insets = new Insets(5, 5, 5, 5);
  107. panel.add(addResultButton, c);
  108. completeButton = new JButton("Complete");
  109. completeButton.addActionListener(new ActionListener() {
  110. public void actionPerformed(ActionEvent event) {
  111. complete();
  112. }
  113. });
  114. c = new GridBagConstraints();
  115. c.gridy = 2;
  116. c.weightx = 1;
  117. c.gridwidth = 4;
  118. c.anchor = GridBagConstraints.EAST;
  119. c.insets = new Insets(5, 5, 5, 5);
  120. panel.add(completeButton, c);
  121. abortButton = new JButton("Abort");
  122. abortButton.addActionListener(new ActionListener() {
  123. public void actionPerformed(ActionEvent event) {
  124. abort();
  125. }
  126. });
  127. c = new GridBagConstraints();
  128. c.gridx = 4;
  129. c.gridy = 2;
  130. c.insets = new Insets(5, 5, 5, 5);
  131. panel.add(abortButton, c);
  132. }
  133. private String getParameters() {
  134. String result = "";
  135. if (workItem.getParameters() != null) {
  136. for (Iterator<Map.Entry<String, Object>> iterator = workItem.getParameters().entrySet().iterator(); iterator.hasNext(); ) {
  137. Map.Entry<String, Object> entry = iterator.next();
  138. result += entry.getKey() + " = " + entry.getValue() + "\n";
  139. }
  140. }
  141. return result;
  142. }
  143. private void addResult() {
  144. results.put(resultNameTextField.getText(), resultValueTextField.getText());
  145. resultNameTextField.setText("");
  146. resultValueTextField.setText("");
  147. }
  148. private void complete() {
  149. handler.complete(workItem, results);
  150. dispose();
  151. }
  152. private void abort() {
  153. handler.abort(workItem);
  154. dispose();
  155. }
  156. }