New Jide License
[indepmod/experimental.git] / IndependentModeler / src / cz / cvut / promod / services / actionService / ActionControlServiceImpl.java
blobe4f5f79efb3db51a7013cad40ead96cd48d51f33
1 package cz.cvut.promod.services.actionService;
3 import cz.cvut.promod.gui.ModelerModel;
4 import cz.cvut.promod.services.actionService.actionUtils.ProModAction;
5 import org.apache.log4j.Logger;
7 import javax.swing.*;
8 import java.util.HashMap;
9 import java.util.Map;
11 /**
12 * ProMod, master thesis project
13 * User: Petr Zverina, petr.zverina@gmail.com
14 * Date: 17:48:55, 10.10.2009
17 /**
18 * ActionControlService implementation.
20 public class ActionControlServiceImpl implements ActionControlService{
22 private final Logger LOG = Logger.getLogger(ActionControlServiceImpl.class);
24 private final Map<String, NotationSpecificActions> notationSpecificActionsMap;
26 public ActionControlServiceImpl(){
27 notationSpecificActionsMap = new HashMap<String, NotationSpecificActions>();
30 /** {@inheritDoc} */
31 public boolean check() {
32 return true;
35 /** {@inheritDoc} */
36 public ProModAction getAction(final String notationIdentifier,
37 final String moduleIdentifier,
38 final String actionIdentifier){
40 if(notationIdentifier == null || actionIdentifier == null){
41 return null;
44 if(notationSpecificActionsMap.containsKey(notationIdentifier)){
45 final NotationSpecificActions notationSpecificActions = notationSpecificActionsMap.get(notationIdentifier);
47 if(moduleIdentifier == null){
48 return notationSpecificActions.getAction(actionIdentifier);
49 } else {
50 return notationSpecificActions.getAction(moduleIdentifier, actionIdentifier);
54 return null;
57 public ProModAction getAction(final String notationIdentifier, final String actionIdentifier) {
58 return getAction(notationIdentifier, null, actionIdentifier);
61 /** {@inheritDoc} */
62 public ProModAction registerAction(final String notationIdentifier,
63 final String moduleIdentifier,
64 final String actionIdentifier,
65 final ProModAction action){
67 if(notationIdentifier == null || actionIdentifier == null){
68 LOG.error("Action registration failed due to nullary info," +
69 " actionIdentifier: " + actionIdentifier +
70 ", notationIdentifier: " + notationIdentifier +
71 ", moduleIdentifier: " + moduleIdentifier + ".");
73 return null;
76 final ProModAction proModAction = getAction(notationIdentifier, moduleIdentifier, actionIdentifier);
78 if(proModAction != null){
79 return proModAction;
81 } else {
82 if(action == null){
83 LOG.error("Nullary action is not possible to register.");
84 return null;
87 if(!isUniqueKeyAccelerator(notationIdentifier, (KeyStroke) action.getValue(AbstractAction.ACCELERATOR_KEY))){
88 LOG.error("Duplicity in action's 'ACCELERATOR KEY'," +
89 " action: " + actionIdentifier +
90 " notation: " + notationIdentifier +
91 " module: " + moduleIdentifier);
92 action.putValue(AbstractAction.ACCELERATOR_KEY, null);
95 action.initAction(
96 notationIdentifier,
97 moduleIdentifier,
98 actionIdentifier);
100 setAction(notationIdentifier, moduleIdentifier, actionIdentifier, action);
102 LOG.debug("New Action (" + actionIdentifier + ") has been added.");
104 return action;
109 * Tests whether there is no other already registered action with the same accelerator key definition. This
110 * test is done only by the 'modeler' action and actions of the same notation.
112 * @param notationIdentifier is the notation where to perform the test
113 * @param keyStroke is the accelerator key to be tested @return true if there is no such a action, false otherwise
114 * @return true if the accelerator key is unique, false otherwise
116 private boolean isUniqueKeyAccelerator(final String notationIdentifier, final KeyStroke keyStroke) {
118 NotationSpecificActions notationSpecificActions = notationSpecificActionsMap.get(notationIdentifier);
119 if(notationSpecificActions != null){
120 if(notationSpecificActions.existAcceleratorKey(keyStroke)){
121 return false;
125 notationSpecificActions = notationSpecificActionsMap.get(ModelerModel.MODELER_IDENTIFIER);
126 if(notationSpecificActions != null){
127 if(notationSpecificActions.existAcceleratorKey(keyStroke)){
128 return false;
133 return true;
137 * {@inheritDoc}
139 public ProModAction registerAction(final String notationIdentifier,
140 final String actionIdentifier,
141 final ProModAction action) {
143 return registerAction(
144 notationIdentifier,
145 null,
146 actionIdentifier,
147 action
152 * Sets the action.
154 * @param notationIdentifier is the notation identifier
155 * @param moduleIdentifier os the module identifier
156 * @param actionIdentifier is the action identifier
157 * @param newProModAction is the action
159 private void setAction(final String notationIdentifier,
160 final String moduleIdentifier,
161 final String actionIdentifier,
162 final ProModAction newProModAction){
163 if(notationIdentifier == null || actionIdentifier == null){
164 return;
167 if(notationSpecificActionsMap.containsKey(notationIdentifier)){
168 notationSpecificActionsMap.get(notationIdentifier).setAction(moduleIdentifier, actionIdentifier, newProModAction);
169 } else {
170 final NotationSpecificActions notationSpecificActions = new NotationSpecificActions();
171 notationSpecificActions.setAction(moduleIdentifier, actionIdentifier, newProModAction);
172 notationSpecificActionsMap.put(notationIdentifier, notationSpecificActions);
177 /** {@inheritDoc} */
178 public void updateActionsVisibility(final String newNotationIdentifier) {
179 for(final NotationSpecificActions notationSpecificActions : notationSpecificActionsMap.values()){
180 notationSpecificActions.updateActionVisibility(newNotationIdentifier);
184 /** {@inheritDoc} */
185 public boolean isRegisteredAction(final ProModAction action) {
186 for(final NotationSpecificActions notationSpecificActions : notationSpecificActionsMap.values()){
187 if(notationSpecificActions.existAction(action)){
188 return true;
192 return false;