1 |
1 |
/**
|
2 |
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
3 |
*
|
4 |
|
* Copyright (C) 2007-2013 gvSIG Association.
|
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
5 |
*
|
6 |
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
7 |
* the terms of the GNU General Public License as published by the Free Software
|
... | ... | |
24 |
24 |
|
25 |
25 |
import java.io.File;
|
26 |
26 |
import java.io.FileInputStream;
|
27 |
|
import java.io.FileNotFoundException;
|
28 |
27 |
import java.util.ArrayList;
|
29 |
28 |
import java.util.Collections;
|
30 |
|
import java.util.Comparator;
|
31 |
29 |
import java.util.HashMap;
|
32 |
30 |
import java.util.Iterator;
|
33 |
31 |
import java.util.List;
|
34 |
32 |
import java.util.Map;
|
35 |
33 |
import java.util.Properties;
|
36 |
|
import java.util.logging.Level;
|
37 |
|
import javax.imageio.stream.FileImageInputStream;
|
38 |
34 |
import org.apache.commons.io.IOUtils;
|
39 |
35 |
import org.apache.commons.lang3.BooleanUtils;
|
40 |
36 |
import org.apache.commons.lang3.StringUtils;
|
... | ... | |
48 |
44 |
import org.slf4j.Logger;
|
49 |
45 |
import org.slf4j.LoggerFactory;
|
50 |
46 |
|
|
47 |
@SuppressWarnings("UseSpecificCatch")
|
51 |
48 |
public class DefaultActionInfoManager implements ActionInfoManager {
|
52 |
49 |
|
53 |
|
private static final Logger logger = LoggerFactory.getLogger(DefaultActionInfoManager.class);
|
|
50 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultActionInfoManager.class);
|
54 |
51 |
|
55 |
52 |
private final Map<String, ActionInfo> actions = new HashMap<>();
|
56 |
53 |
private int anonymousCounter = 1;
|
... | ... | |
58 |
55 |
|
59 |
56 |
@Override
|
60 |
57 |
public ActionInfo createAction(Class<? extends IExtension> extension, String name, String text, String command, String icon, String accelerator, long position, String tip) {
|
61 |
|
//name = StringUtils.stripToNull(name);
|
62 |
|
name = emptyToNull(name);
|
63 |
58 |
String actionName = name;
|
64 |
|
if ( StringUtils.isEmpty(actionName) ) {
|
|
59 |
if ( StringUtils.isBlank(actionName) ) {
|
65 |
60 |
actionName = String.format("anonymous__%04d", this.anonymousCounter++);
|
66 |
61 |
}
|
67 |
62 |
ActionInfo action = new DefaultActionInfo(extension, actionName, text, command, icon, accelerator, position, tip);
|
... | ... | |
70 |
65 |
((DefaultActionInfo) action).merge(previous);
|
71 |
66 |
}
|
72 |
67 |
if (name == null) {
|
73 |
|
logger.info("createAction: name of action is null/empty, rename to '" + actionName + "' (" + action.toString() + ").");
|
|
68 |
LOGGER.info("createAction: name of action is null/empty, rename to '" + actionName + "' (" + action.toString() + ").");
|
74 |
69 |
}
|
75 |
70 |
if (action.getLabel() == null && action.getIconName() == null) {
|
76 |
|
logger.info("createAction(name='" + name + "'): text and icon of action is null");
|
|
71 |
LOGGER.info("createAction(name='" + name + "'): text and icon of action is null");
|
77 |
72 |
}
|
78 |
73 |
return action;
|
79 |
74 |
}
|
80 |
75 |
|
81 |
76 |
@Override
|
82 |
77 |
public ActionInfo createAction(IExtension extension, String name, String text, String command, String icon, String accelerator, long position, String tip) {
|
83 |
|
name = emptyToNull(name);
|
84 |
78 |
String actionName = name;
|
85 |
|
if ( StringUtils.isEmpty(actionName) ) {
|
|
79 |
if ( StringUtils.isBlank(actionName) ) {
|
86 |
80 |
actionName = String.format("anonymous__%04d", this.anonymousCounter++);
|
87 |
81 |
}
|
88 |
82 |
ActionInfo action = new DefaultActionInfo(extension, actionName, text, command, icon, accelerator, position, tip);
|
... | ... | |
91 |
85 |
((DefaultActionInfo) action).merge(previous);
|
92 |
86 |
}
|
93 |
87 |
if (name == null) {
|
94 |
|
logger.info("createAction: name of action is null/empty, rename to '" + actionName + "' (" + action.toString() + ").");
|
|
88 |
LOGGER.info("createAction: name of action is null/empty, rename to '" + actionName + "' (" + action.toString() + ").");
|
95 |
89 |
}
|
96 |
90 |
if (action.getLabel() == null && action.getIconName() == null) {
|
97 |
|
logger.info("createAction(name='" + name + "'): text and icon of action is null");
|
|
91 |
LOGGER.info("createAction(name='" + name + "'): text and icon of action is null");
|
98 |
92 |
}
|
99 |
93 |
return action;
|
100 |
94 |
}
|
101 |
95 |
|
102 |
|
private String emptyToNull(String s) {
|
103 |
|
if (s == null) {
|
104 |
|
return null;
|
105 |
|
}
|
106 |
|
return "".equals(s.trim()) ? null : s;
|
|
96 |
@Override
|
|
97 |
public ActionInfo registerAction(ActionInfo action) {
|
|
98 |
return this.registerAction(action, false);
|
107 |
99 |
}
|
108 |
100 |
|
109 |
101 |
@Override
|
110 |
|
public ActionInfo registerAction(ActionInfo action) {
|
|
102 |
public ActionInfo registerAction(ActionInfo action, boolean override) {
|
111 |
103 |
if (action == null) {
|
112 |
|
// Avisamos en el log de que se intenta registrar una accion null, intentado
|
|
104 |
// Avisamos en el log de que se intenta registrar una accion null, intentado
|
113 |
105 |
// sacar el stack para que se pueda ver quien lo esta haciendo, pero no
|
114 |
106 |
// provocamos un error, solo retornamos null.
|
115 |
107 |
try {
|
116 |
108 |
throw new IllegalArgumentException();
|
117 |
109 |
} catch (IllegalArgumentException e) {
|
118 |
|
logger.info("registerAction(null).", e);
|
|
110 |
LOGGER.info("registerAction(null).", e);
|
119 |
111 |
}
|
120 |
112 |
return null;
|
121 |
113 |
}
|
122 |
114 |
ActionInfo previous = this.getAction(action.getName());
|
123 |
115 |
if (previous != null) {
|
124 |
|
((DefaultActionInfo) previous).merge(action);
|
|
116 |
if( override ) {
|
|
117 |
this.actions.put(action.getName(), action);
|
|
118 |
} else {
|
|
119 |
((DefaultActionInfo) previous).merge(action);
|
|
120 |
}
|
125 |
121 |
action = previous;
|
126 |
122 |
} else {
|
127 |
123 |
this.actions.put(action.getName(), action);
|
... | ... | |
143 |
139 |
fis = new FileInputStream(states);
|
144 |
140 |
this.activeStates.load(fis);
|
145 |
141 |
} catch (Exception ex) {
|
146 |
|
logger.warn("Can't load actions states from '"+states.getAbsolutePath()+"'.",ex);
|
|
142 |
LOGGER.warn("Can't load actions states from '"+states.getAbsolutePath()+"'.",ex);
|
147 |
143 |
} finally {
|
148 |
144 |
IOUtils.closeQuietly(fis);
|
149 |
145 |
}
|
... | ... | |
159 |
155 |
try {
|
160 |
156 |
throw new IllegalArgumentException();
|
161 |
157 |
} catch (IllegalArgumentException e) {
|
162 |
|
logger.info("getAction(null/empty) return null.", e);
|
|
158 |
LOGGER.info("getAction(null/empty) return null.", e);
|
163 |
159 |
return null;
|
164 |
160 |
}
|
165 |
161 |
}
|
... | ... | |
168 |
164 |
|
169 |
165 |
@Override
|
170 |
166 |
public Iterator<ActionInfo> getActions() {
|
171 |
|
List<ActionInfo> actions = new ArrayList<>();
|
172 |
|
actions.addAll(this.actions.values());
|
173 |
|
Collections.sort(actions, new Comparator<ActionInfo>() {
|
174 |
|
@Override
|
175 |
|
public int compare(ActionInfo arg0, ActionInfo arg1) {
|
176 |
|
String s0 = String.format("%s/%012d", arg0.getPluginName(), arg0.getPosition());
|
177 |
|
String s1 = String.format("%s/%012d", arg1.getPluginName(), arg1.getPosition());
|
178 |
|
return s0.compareTo(s1);
|
179 |
|
}
|
180 |
|
;
|
|
167 |
List<ActionInfo> theActions = new ArrayList<>();
|
|
168 |
theActions.addAll(this.actions.values());
|
|
169 |
Collections.sort(theActions, (ActionInfo arg0, ActionInfo arg1) -> {
|
|
170 |
String s0 = String.format("%s/%012d", arg0.getPluginName(), arg0.getPosition());
|
|
171 |
String s1 = String.format("%s/%012d", arg1.getPluginName(), arg1.getPosition());
|
|
172 |
return s0.compareTo(s1);
|
181 |
173 |
});
|
182 |
|
return actions.iterator();
|
|
174 |
return theActions.iterator();
|
183 |
175 |
}
|
184 |
176 |
|
185 |
177 |
@Override
|