001: package com.opensymphony.workflow.designer.dialogs;
002:
003: import java.util.ArrayList;
004: import java.awt.*;
005: import java.awt.event.ItemListener;
006: import java.awt.event.ItemEvent;
007: import javax.swing.*;
008:
009: import com.opensymphony.workflow.designer.*;
010: import com.opensymphony.workflow.loader.*;
011: import com.jgoodies.forms.builder.DefaultFormBuilder;
012:
013: /**
014: * Date: Mar 17, 2004
015: * Time: 10:33:21 PM
016: *
017: * @author hani
018: */
019: public class ActionTypeDialog extends BaseDialog implements
020: ItemListener {
021: private JComboBox actionType = new JComboBox(new String[] {
022: ResourceManager.getString("result.conditional"),
023: ResourceManager.getString("result.unconditional") });
024: private JComboBox relatedAction;
025: private StepDescriptor source;
026: private WorkflowGraphModel model;
027:
028: public ActionTypeDialog(Frame owner, StepDescriptor source)
029: throws HeadlessException {
030: super (owner, ResourceManager.getString("result.select"), true);
031: this .source = source;
032: getBanner().setTitle(
033: ResourceManager.getString("result.create.title"));
034: getBanner().setSubtitle(
035: ResourceManager.getString("result.create.subtitle"));
036: DefaultFormBuilder builder = UIFactory.getDialogBuilder(null,
037: getContentPane());
038: builder.append(ResourceManager.getString("result.select.long"),
039: actionType);
040:
041: java.util.List relatedActions = new ArrayList(source
042: .getActions());
043: relatedActions.add(0, ResourceManager
044: .getString("result.create.newaction"));
045: relatedAction = new JComboBox(relatedActions.toArray());
046: relatedAction.setEditable(true);
047: relatedAction.setRenderer(new DefaultListCellRenderer() {
048: public Component getListCellRendererComponent(JList list,
049: Object value, int index, boolean isSelected,
050: boolean cellHasFocus) {
051: JLabel label = (JLabel) super
052: .getListCellRendererComponent(list, value,
053: index, isSelected, cellHasFocus);
054: if (value instanceof ActionDescriptor) {
055: ActionDescriptor action = (ActionDescriptor) value;
056: label.setText(action.getName());
057: }
058: return label;
059: }
060: });
061: relatedAction.addItemListener(this );
062: actionType.addItemListener(this );
063:
064: builder.append(ResourceManager
065: .getString("result.related.action"), relatedAction);
066: builder.appendRow(builder.getLineGapSpec());
067: builder.nextLine();
068: }
069:
070: public int getType() {
071: switch (actionType.getSelectedIndex()) {
072: case 0:
073: return ConnectHelper.CONDITIONAL;
074: default:
075: return ConnectHelper.UNCONDITIONAL;
076: }
077: }
078:
079: public ActionDescriptor getRelatedAction() {
080: int index = relatedAction.getSelectedIndex();
081: if (index < 1) {
082: ActionDescriptor sourceAction = DescriptorBuilder
083: .createAction(source, index == 0 ? source.getName()
084: : (String) relatedAction.getEditor()
085: .getItem(), Utils.getNextId(model
086: .getContext()));
087: Utils.checkId(model.getContext(), sourceAction);
088: return sourceAction;
089: }
090: return (ActionDescriptor) relatedAction.getSelectedItem();
091: }
092:
093: public WorkflowGraphModel getModel() {
094: return model;
095: }
096:
097: public void setModel(WorkflowGraphModel model) {
098: this .model = model;
099: }
100:
101: public void itemStateChanged(ItemEvent e) {
102: if (e.getStateChange() == ItemEvent.SELECTED) {
103: if (e.getSource() == relatedAction
104: || e.getSource() == actionType) {
105: //don't allow unconditional results if we already have one for the current action
106: if (actionType.getSelectedIndex() == 1
107: && relatedAction.getSelectedIndex() > 0) {
108: ActionDescriptor action = (ActionDescriptor) relatedAction
109: .getSelectedItem();
110: if (action.getUnconditionalResult() != null) {
111: JOptionPane
112: .showMessageDialog(
113: this ,
114: "Action already has an unconditional result.\nTo create a new one, delete the existing one first.",
115: "Duplicate unconditional result",
116: JOptionPane.ERROR_MESSAGE);
117: if (e.getSource() == relatedAction)
118: relatedAction.setSelectedIndex(0);
119: else
120: actionType.setSelectedIndex(1);
121: }
122: }
123: }
124: }
125: }
126: }
|