001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support.action.swing;
014:
015: import com.eviware.soapui.SoapUI;
016: import com.eviware.soapui.model.ModelItem;
017: import com.eviware.soapui.support.action.SoapUIAction;
018: import com.eviware.soapui.support.action.SoapUIActionGroup;
019: import com.eviware.soapui.support.action.SoapUIActionMapping;
020: import com.eviware.soapui.support.action.SoapUIActionRegistry.SeperatorAction;
021: import com.eviware.soapui.support.action.SoapUIActionRegistry.SoapUIActionGroupAction;
022:
023: /**
024: * Builder for ActionLists for a variety of targets
025: *
026: * @author ole.matzura
027: */
028:
029: public class ActionListBuilder {
030: /**
031: * Creates an ActionList for the specified modelItem
032: */
033:
034: public static <T extends ModelItem> ActionList buildActions(
035: T modelItem) {
036: Class<?> clazz = modelItem.getClass();
037: ActionList actions = buildActions(clazz.getSimpleName()
038: + "Actions", modelItem);
039:
040: if (actions.getActionCount() == 0) {
041: clazz = clazz.getSuperclass();
042:
043: while (actions.getActionCount() == 0 && clazz != null
044: && ModelItem.class.isAssignableFrom(clazz)) {
045: actions = buildActions(clazz.getSimpleName()
046: + "Actions", modelItem);
047: clazz = clazz.getSuperclass();
048: }
049: }
050:
051: return actions;
052: }
053:
054: @SuppressWarnings("hiding")
055: public static <T extends ModelItem> ActionList buildActions(
056: String actionGroup, T modelItem) {
057: DefaultActionList actions = new DefaultActionList();
058:
059: SoapUIActionGroup<T> group = SoapUI.getActionRegistry()
060: .getActionGroup(actionGroup);
061: if (group != null) {
062: addActions(modelItem, actions, group);
063: }
064:
065: return actions;
066: }
067:
068: /**
069: * Adds the specified ActionMappings to the specified ActionList for the specified modelItem
070: */
071:
072: @SuppressWarnings({"hiding","unchecked"})
073: protected static <T extends ModelItem> void addActions(T modelItem,
074: ActionList actions, SoapUIActionGroup<T> actionGroup) {
075: boolean prevWasSeparator = false;
076: for (SoapUIActionMapping<? extends ModelItem> mapping : actionGroup
077: .getActionMappings(modelItem)) {
078: SoapUIActionMapping<T> actionMapping = (com.eviware.soapui.support.action.SoapUIActionMapping<T>) mapping;
079: SoapUIAction<T> action = (SoapUIAction<T>) mapping
080: .getAction();
081:
082: if (!action.applies(modelItem)) {
083: System.out.println(action + " does not apply to "
084: + modelItem);
085: } else if (action instanceof SeperatorAction) {
086: if (!prevWasSeparator) {
087: actions.addAction(ActionSupport.SEPARATOR_ACTION);
088: }
089: prevWasSeparator = true;
090: } else if (action instanceof SoapUIActionGroupAction) {
091: DefaultActionList subActions = new DefaultActionList(
092: mapping.getName());
093: SoapUIActionGroup<T> subGroup = ((SoapUIActionGroupAction<T>) action)
094: .getActionGroup();
095: addActions(modelItem, subActions, subGroup);
096: ActionSupport.ActionListAction actionListAction = new ActionSupport.ActionListAction(
097: subActions);
098: actions.addAction(actionListAction);
099: actionListAction.setEnabled(mapping.isEnabled());
100: prevWasSeparator = false;
101: } else if (action != null) {
102: SwingActionDelegate<T> actionDelegate = new SwingActionDelegate<T>(
103: actionMapping, modelItem);
104: actions.addAction(actionDelegate);
105: if (mapping.isDefault())
106: actions.setDefaultAction(actionDelegate);
107:
108: actionDelegate.setEnabled(mapping.isEnabled());
109: prevWasSeparator = false;
110: }
111: }
112: }
113: }
|