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: import java.awt.event.KeyEvent;
017: import java.util.ArrayList;
018: import java.util.List;
019:
020: import javax.swing.Action;
021: import javax.swing.KeyStroke;
022:
023: /**
024: * Default ActionList implementation
025: *
026: * @author Ole.Matzura
027: */
028:
029: public class DefaultActionList implements ActionList {
030: private List<Action> actions = new ArrayList<Action>();
031: private Action defaultAction;
032: private final String label;
033:
034: public DefaultActionList() {
035: this (null);
036: }
037:
038: public DefaultActionList(String label) {
039: this .label = label;
040: }
041:
042: public String getLabel() {
043: return label;
044: }
045:
046: public int getActionCount() {
047: return actions.size();
048: }
049:
050: public Action getActionAt(int index) {
051: return actions.get(index);
052: }
053:
054: public Action getDefaultAction() {
055: return defaultAction;
056: }
057:
058: public void setDefaultAction(Action defaultAction) {
059: this .defaultAction = defaultAction;
060: }
061:
062: public void addAction(Action action) {
063: actions.add(action);
064: }
065:
066: public void addSeparator() {
067: actions.add(ActionSupport.SEPARATOR_ACTION);
068: }
069:
070: public void insertAction(Action action, int index) {
071: actions.add(index, action);
072: }
073:
074: public void insertSeparator(int index) {
075: actions.add(index, ActionSupport.SEPARATOR_ACTION);
076: }
077:
078: public boolean hasDefaultAction() {
079: return defaultAction != null;
080: }
081:
082: public void performDefaultAction(ActionEvent event) {
083: if (defaultAction != null)
084: defaultAction.actionPerformed(event);
085: }
086:
087: public void clear() {
088: actions.clear();
089: defaultAction = null;
090: }
091:
092: public void dispatchKeyEvent(KeyEvent e) {
093: if (e.getKeyChar() == KeyEvent.VK_ENTER) {
094: performDefaultAction(new ActionEvent(e.getSource(), 0, null));
095: } else {
096: for (int c = 0; c < actions.size(); c++) {
097: Action action = actions.get(c);
098: KeyStroke acc = (KeyStroke) action
099: .getValue(Action.ACCELERATOR_KEY);
100: if (acc == null)
101: continue;
102:
103: if (acc.equals(KeyStroke.getKeyStrokeForEvent(e))) {
104: action.actionPerformed(new ActionEvent(e
105: .getSource(), 0, null));
106: e.consume();
107: return;
108: }
109: }
110: }
111: }
112:
113: public void addActions(ActionList defaultActions) {
114: for (int c = 0; c < defaultActions.getActionCount(); c++)
115: addAction(defaultActions.getActionAt(c));
116: }
117:
118: public void setEnabled(boolean b) {
119: for (int c = 0; c < actions.size(); c++) {
120: Action action = actions.get(c);
121: action.setEnabled(b);
122: }
123: }
124:
125: public void removeAction(int index) {
126: actions.remove(index);
127: }
128: }
|