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 java.awt.event.ActionEvent;
016:
017: import javax.swing.AbstractAction;
018: import javax.swing.Action;
019: import javax.swing.JButton;
020: import javax.swing.JMenu;
021: import javax.swing.JPopupMenu;
022:
023: import com.eviware.soapui.support.actions.MarkerAction;
024: import com.eviware.soapui.support.components.JXToolBar;
025: import com.jgoodies.forms.builder.ButtonBarBuilder;
026:
027: /**
028: * ActionList-related utilities
029: *
030: * @author Ole.Matzura
031: */
032:
033: public class ActionSupport {
034: public static JPopupMenu buildPopup(ActionList actions) {
035: if (actions == null || actions.getActionCount() == 0)
036: return null;
037:
038: JPopupMenu popup = new JPopupMenu(actions.getLabel());
039:
040: return ActionSupport.addActions(actions, popup);
041: }
042:
043: public static JMenu buildMenu(ActionList actions) {
044: if (actions == null || actions.getActionCount() == 0)
045: return null;
046:
047: JMenu menu = new JMenu(actions.getLabel());
048:
049: return ActionSupport.addActions(actions, menu);
050: }
051:
052: public static JPopupMenu addActions(ActionList actions,
053: JPopupMenu popup) {
054: if (actions == null || actions.getActionCount() == 0)
055: return popup;
056:
057: for (int i = 0; i < actions.getActionCount(); i++) {
058: Action action = actions.getActionAt(i);
059: if (action instanceof MarkerAction)
060: continue;
061:
062: if (action == ActionSupport.SEPARATOR_ACTION)
063: popup.addSeparator();
064: else if (action instanceof ActionSupport.ActionListAction) {
065: ActionList actionList = ((ActionListAction) action)
066: .getActionList();
067: if (actionList == null
068: || actionList.getActionCount() == 0)
069: System.err
070: .println("null/empty ActionList in action "
071: + action.getValue(Action.NAME));
072: else
073: popup.add(buildMenu(actionList));
074: } else
075: popup.add(action);
076: }
077:
078: return popup;
079: }
080:
081: public static JMenu addActions(ActionList actions, JMenu menu) {
082: if (actions == null || menu == null)
083: return menu;
084:
085: for (int i = 0; i < actions.getActionCount(); i++) {
086: Action action = actions.getActionAt(i);
087:
088: if (action instanceof MarkerAction)
089: continue;
090:
091: if (action == ActionSupport.SEPARATOR_ACTION)
092: menu.addSeparator();
093: else if (action instanceof ActionSupport.ActionListAction)
094: menu.add(buildMenu(((ActionListAction) action)
095: .getActionList()));
096: else
097: menu.add(action);
098: }
099:
100: return menu;
101: }
102:
103: public final static Action SEPARATOR_ACTION = new AbstractAction() {
104: public void actionPerformed(ActionEvent e) {
105: }
106: };
107:
108: public static class ActionListAction extends AbstractAction {
109: private final ActionList actionList;
110:
111: public ActionListAction(ActionList actionList) {
112: this .actionList = actionList;
113: }
114:
115: public ActionList getActionList() {
116: return actionList;
117: }
118:
119: public void actionPerformed(ActionEvent e) {
120: Action defaultAction = actionList.getDefaultAction();
121: if (defaultAction != null)
122: defaultAction.actionPerformed(e);
123: }
124: };
125:
126: public static JPopupMenu insertActions(ActionList actions,
127: JPopupMenu popup, int index) {
128: for (int i = 0; i < actions.getActionCount(); i++) {
129: Action action = actions.getActionAt(i);
130: if (action instanceof MarkerAction)
131: continue;
132:
133: if (action == ActionSupport.SEPARATOR_ACTION)
134: popup.insert(new JPopupMenu.Separator(), index + i);
135: else if (action instanceof ActionSupport.ActionListAction)
136: popup
137: .insert(
138: buildMenu(((ActionSupport.ActionListAction) action)
139: .getActionList()), index + i);
140: else
141: popup.insert(action, index + i);
142: }
143:
144: return popup;
145: }
146:
147: public static void addActions(ActionList actionList,
148: ButtonBarBuilder builder) {
149: for (int c = 0; c < actionList.getActionCount(); c++) {
150: Action action = actionList.getActionAt(c);
151: if (action == SEPARATOR_ACTION) {
152: builder.addUnrelatedGap();
153: } else {
154: if (c > 0)
155: builder.addRelatedGap();
156:
157: builder.addFixed(new JButton(action));
158: }
159: }
160: }
161:
162: public static void addActions(ActionList actionList,
163: JXToolBar toolbar) {
164: for (int c = 0; c < actionList.getActionCount(); c++) {
165: Action action = actionList.getActionAt(c);
166: if (action == SEPARATOR_ACTION) {
167: toolbar.addUnrelatedGap();
168: } else {
169: if (c > 0)
170: toolbar.addRelatedGap();
171:
172: toolbar.addFixed(new JButton(action));
173: }
174: }
175: }
176: }
|