1 /*
2 * $Id: ActionPool.java 3077 2011-12-24 14:31:29Z andrewinkler $
3 * ============================================================================
4 * Project swinger
5 * Copyright (c) 2004-2007 by Andre Winkler. All rights reserved.
6 * ============================================================================
7 * GNU LESSER GENERAL PUBLIC LICENSE
8 * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26 package de.gluehloch.swinger.action;
27
28 import java.awt.event.ActionEvent;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import javax.swing.Action;
35
36 /**
37 * Ein Pool für Swing <code>Action</code>s. Eine Aktion wird als Singleton
38 * betrachtet und kann als Instanz nur einmal in diesem Pool abgelegt werden.
39 *
40 * @author by Andre Winkler, $LastChangedBy: andrewinkler $
41 * @version $LastChangedRevision: 3077 $ $LastChangedDate: 2011-12-24 15:31:29 +0100 (Sa, 24 Dez 2011) $
42 */
43 public class ActionPool {
44
45 /** Eine Map mit allen Aktionen. */
46 private final Map<String, Action> actions = new HashMap<String, Action>();
47
48 /** Sollen Platzhalter generiert werden? */
49 private final boolean generatePlaceHolders;
50
51 /**
52 * Defaultkonstruktor.
53 */
54 public ActionPool() {
55 this(false);
56 }
57
58 /**
59 * Konstruktor.
60 *
61 * @param _generatePlaceHolders Sollen Platzhalter bei Bedarf angelegt
62 * werden, wenn die {@link #getAction(String)} Methode keine
63 * Aktion findet?
64 */
65 public ActionPool(final boolean _generatePlaceHolders) {
66 this(null, _generatePlaceHolders);
67 }
68
69 /**
70 * Konstruktor.
71 *
72 * @param _actions Mit diesen Aktionen wird der Pool initialisiert.
73 */
74 public ActionPool(final List<Action> _actions) {
75 this(_actions, false);
76 }
77
78 /**
79 * Konstruktor.
80 *
81 * @param _actions Mit diesen Aktionen wird der Pool initialisiert.
82 * @param _generatePlaceHolders Sollen Platzhalter bei Bedarf angelegt
83 * werden, wenn die {@link #getAction(String)} Methode keine
84 * Aktion findet?
85 */
86 public ActionPool(final List<Action> _actions,
87 final boolean _generatePlaceHolders) {
88
89 generatePlaceHolders = _generatePlaceHolders;
90 if (_actions != null) {
91 for (Action action : _actions) {
92 addAction(action);
93 }
94 }
95 }
96
97 /**
98 * Liefert ein Map mit allen bekannten Aktionen.
99 *
100 * @return Die bekannten Aktionen.
101 */
102 public final Map<String, Action> getActions() {
103 return (Collections.unmodifiableMap(actions));
104 }
105
106 /**
107 * Liefert die Instanz einer gesuchten <code>Action</code> Klasse. Falls
108 * das <code>Action</code> Objekt nicht gefunden werden kann, wirft die
109 * Methode eine <code>IllegalArgumentException</code>.
110 *
111 * @param _actionId Die ID der gesuchte <code>Action</code>.
112 * @return Eine <code>Action</code>.
113 */
114 public final Action getAction(final String _actionId) {
115 if (actions.containsKey(_actionId)) {
116 return (actions.get(_actionId));
117 } else if (generatePlaceHolders) {
118 return new PlaceHolderAction(_actionId);
119 } else {
120 throw new IllegalArgumentException(_actionId + " action not found!");
121 }
122 }
123
124 /**
125 * Fügt eine neue Aktion dem Pool hinzu. Ist diese Aktion bereits im Pool
126 * vorhanden, wird eine <code>IllegalArgumentException</code> geworfen.
127 * Als Schlüssel wird der Klassentyp der Aktion verwendet.
128 *
129 * @param _action Eine <code>Action</code>.
130 */
131 public final void addAction(final Action _action) {
132 if (actions.containsKey(_action.getValue(Action.ACTION_COMMAND_KEY))) {
133 StringBuilder sb = new StringBuilder("'");
134 sb.append(_action).append("' with commandKey '");
135 sb.append(_action.getValue(Action.ACTION_COMMAND_KEY)).append("' ");
136 sb.append(" already added!");
137 throw new IllegalArgumentException(sb.toString());
138 }
139
140 actions.put(_action.getValue(Action.ACTION_COMMAND_KEY).toString(),
141 _action);
142 }
143
144 // ------------------------------------------------------------------------
145
146 /**
147 * Definiert eine Platzhalter-Aktion. Diese wird instanziert und geliefert,
148 * wenn keine Aktion unter einer bestimmten ID gefunden werden konnte.
149 */
150 private static final class PlaceHolderAction extends AbstractSwingerAction {
151
152 /** serial version id */
153 private static final long serialVersionUID = -6195650183795728687L;
154
155 protected PlaceHolderAction(final String name) {
156 super("actionPoolPlaceHolderAction", "UNDEFINED: " + name);
157 }
158
159 @Override
160 public void actionPerformed(ActionEvent e) {
161 }
162
163 }
164
165 }