Revision 3089
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.374/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/resources/META-INF/services/org.gvsig.tools.library.Library | ||
---|---|---|
1 |
org.gvsig.tools.util.impl.ToolsUtilLibraryImpl |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.374/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/simplehttpserver/HttpHandler.java | ||
---|---|---|
1 |
package org.gvsig.simplehttpserver; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.io.InputStream; |
|
5 |
import java.net.URL; |
|
6 |
import java.net.URLDecoder; |
|
7 |
import java.nio.charset.Charset; |
|
8 |
import java.util.ArrayList; |
|
9 |
import java.util.Arrays; |
|
10 |
import java.util.List; |
|
11 |
import java.util.Locale; |
|
12 |
import org.apache.commons.lang3.StringUtils; |
|
13 |
import org.apache.http.HttpEntity; |
|
14 |
import org.apache.http.HttpEntityEnclosingRequest; |
|
15 |
import org.apache.http.HttpException; |
|
16 |
import org.apache.http.HttpRequest; |
|
17 |
import org.apache.http.HttpResponse; |
|
18 |
import org.apache.http.HttpStatus; |
|
19 |
import org.apache.http.MethodNotSupportedException; |
|
20 |
import org.apache.http.NameValuePair; |
|
21 |
import org.apache.http.client.utils.URLEncodedUtils; |
|
22 |
import org.apache.http.entity.ByteArrayEntity; |
|
23 |
import org.apache.http.entity.ContentType; |
|
24 |
import org.apache.http.entity.InputStreamEntity; |
|
25 |
import org.apache.http.nio.entity.NStringEntity; |
|
26 |
import org.apache.http.nio.protocol.BasicAsyncRequestConsumer; |
|
27 |
import org.apache.http.nio.protocol.BasicAsyncResponseProducer; |
|
28 |
import org.apache.http.nio.protocol.HttpAsyncExchange; |
|
29 |
import org.apache.http.nio.protocol.HttpAsyncRequestConsumer; |
|
30 |
import org.apache.http.nio.protocol.HttpAsyncRequestHandler; |
|
31 |
import org.apache.http.protocol.HttpContext; |
|
32 |
import static org.gvsig.simplehttpserver.SimpleServer.INFO; |
|
33 |
import static org.gvsig.simplehttpserver.SimpleServer.WARN; |
|
34 |
import org.slf4j.Logger; |
|
35 |
import org.slf4j.LoggerFactory; |
|
36 |
import org.gvsig.simplehttpserver.commands.Command; |
|
37 |
|
|
38 |
/** |
|
39 |
* |
|
40 |
* @author jjdelcerro |
|
41 |
*/ |
|
42 |
@SuppressWarnings("UseSpecificCatch") |
|
43 |
public class HttpHandler implements HttpAsyncRequestHandler<HttpRequest> { |
|
44 |
|
|
45 |
private static final Logger LOG = LoggerFactory.getLogger(HttpHandler.class); |
|
46 |
|
|
47 |
private final SimpleServer server; |
|
48 |
|
|
49 |
public HttpHandler(SimpleServer server) { |
|
50 |
super(); |
|
51 |
this.server = server; |
|
52 |
} |
|
53 |
|
|
54 |
@Override |
|
55 |
public HttpAsyncRequestConsumer<HttpRequest> processRequest( |
|
56 |
final HttpRequest request, |
|
57 |
final HttpContext context) { |
|
58 |
// Buffer request content in memory for simplicity |
|
59 |
return new BasicAsyncRequestConsumer(); |
|
60 |
} |
|
61 |
|
|
62 |
@Override |
|
63 |
public void handle( |
|
64 |
final HttpRequest request, |
|
65 |
final HttpAsyncExchange httpexchange, |
|
66 |
final HttpContext context) throws HttpException, IOException { |
|
67 |
final HttpResponse response = httpexchange.getResponse(); |
|
68 |
handleInternal(request, response, context); |
|
69 |
httpexchange.submitResponse(new BasicAsyncResponseProducer(response)); |
|
70 |
} |
|
71 |
|
|
72 |
private void handleInternal( |
|
73 |
final HttpRequest request, |
|
74 |
final HttpResponse response, |
|
75 |
final HttpContext context) throws HttpException, IOException { |
|
76 |
|
|
77 |
InputStream contents = null; |
|
78 |
final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH); |
|
79 |
if (method.equals("GET")) { |
|
80 |
// Do nothing |
|
81 |
} else if (method.equals("POST")) { |
|
82 |
if( request instanceof HttpEntityEnclosingRequest) { |
|
83 |
HttpEntity httpEntity = ((HttpEntityEnclosingRequest)request).getEntity(); |
|
84 |
contents = httpEntity.getContent(); |
|
85 |
} |
|
86 |
} else { |
|
87 |
this.server.log(WARN, "Method not supported, Only GET/POST supported."); |
|
88 |
throw new MethodNotSupportedException(method + " method not supported"); |
|
89 |
} |
|
90 |
|
|
91 |
|
|
92 |
String target_uri = request.getRequestLine().getUri(); |
|
93 |
String target_path = target_uri; |
|
94 |
List<NameValuePair> queryparams = null; |
|
95 |
try { |
|
96 |
URL url = this.server.getURL(target_path); |
|
97 |
target_path = url.getPath(); |
|
98 |
queryparams = URLEncodedUtils.parse(url.getQuery(), Charset.forName("UTF-8")); |
|
99 |
} catch(Exception ex) { |
|
100 |
// Do nothing, ignore exception |
|
101 |
} |
|
102 |
final String commandline = URLDecoder.decode(target_path, "UTF-8"); |
|
103 |
final List<String> args = new ArrayList<>(); |
|
104 |
args.addAll(Arrays.asList(StringUtils.split(commandline, '/'))); |
|
105 |
if( queryparams!=null ) { |
|
106 |
for (NameValuePair queryparam : queryparams) { |
|
107 |
args.add(queryparam.getName()); |
|
108 |
args.add(queryparam.getValue()); |
|
109 |
} |
|
110 |
} |
|
111 |
final int argc = args.size() - 1; |
|
112 |
|
|
113 |
|
|
114 |
Command command = this.server.getCommand(args.get(0)); |
|
115 |
if (command == null) { |
|
116 |
this.server.log(WARN, "Command '" + target_uri + "' not found."); |
|
117 |
response.setStatusCode(HttpStatus.SC_NOT_FOUND); |
|
118 |
final NStringEntity entity = new NStringEntity( |
|
119 |
"<html><body><h1>Command " + target_uri |
|
120 |
+ " not found</h1></body></html>", |
|
121 |
ContentType.create("text/html", "UTF-8")); |
|
122 |
response.setEntity(entity); |
|
123 |
return; |
|
124 |
} |
|
125 |
if (!command.getNumArgs().contains(argc)) { |
|
126 |
this.server.log(WARN, "Command '" + target_uri + "', invalid number of arguments."); |
|
127 |
response.setStatusCode(HttpStatus.SC_BAD_REQUEST); |
|
128 |
final NStringEntity entity = new NStringEntity( |
|
129 |
"<html><body><h1>Number of arguments invalid in " + target_uri |
|
130 |
+ "</h1></body></html>", |
|
131 |
ContentType.create("text/html", "UTF-8")); |
|
132 |
response.setEntity(entity); |
|
133 |
return; |
|
134 |
} |
|
135 |
Object ret; |
|
136 |
try { |
|
137 |
ret = command.call(argc, args, contents); |
|
138 |
} catch (Exception ex) { |
|
139 |
LOG.warn("Can't server command '" + target_uri + "'.", ex); |
|
140 |
this.server.log(WARN, "Command '" + target_uri + "', error " + ex.getMessage() + "."); |
|
141 |
response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); |
|
142 |
final NStringEntity entity = new NStringEntity( |
|
143 |
"<html><body><h1>Error processing " + target_uri |
|
144 |
+ "</h1><p>" + ex.toString() + "</p></body></html>", |
|
145 |
ContentType.create("text/html", "UTF-8") |
|
146 |
); |
|
147 |
response.setEntity(entity); |
|
148 |
return; |
|
149 |
} |
|
150 |
HttpEntity entity; |
|
151 |
response.setStatusCode(HttpStatus.SC_OK); |
|
152 |
if (ret == null) { |
|
153 |
this.server.log(WARN, "Command '" + target_uri + "' return null."); |
|
154 |
response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); |
|
155 |
entity = new NStringEntity( |
|
156 |
"<html><body><h1>Process " + target_uri + " return null</h1></body></html>", |
|
157 |
ContentType.create("text/html", "UTF-8") |
|
158 |
); |
|
159 |
} else if (ret instanceof InputStream) { |
|
160 |
entity = new InputStreamEntity( |
|
161 |
(InputStream)ret, |
|
162 |
ContentType.create(command.getMimeType(), "UTF-8") |
|
163 |
); |
|
164 |
} else if (ret instanceof byte[]) { |
|
165 |
entity = new ByteArrayEntity( |
|
166 |
(byte[]) ret, |
|
167 |
ContentType.create(command.getMimeType(), "UTF-8") |
|
168 |
); |
|
169 |
} else { |
|
170 |
entity = new NStringEntity( |
|
171 |
ret.toString(), |
|
172 |
ContentType.create(command.getMimeType(), "UTF-8") |
|
173 |
); |
|
174 |
} |
|
175 |
response.setEntity(entity); |
|
176 |
this.server.log(INFO, "Command '" + target_uri + "' ok."); |
|
177 |
} |
|
178 |
|
|
179 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.374/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/simplehttpserver/SimpleServerManagerImpl.java | ||
---|---|---|
1 |
package org.gvsig.simplehttpserver; |
|
2 |
|
|
3 |
/** |
|
4 |
* |
|
5 |
* @author gvSIG Team |
|
6 |
*/ |
|
7 |
public class SimpleServerManagerImpl implements SimpleServerManager { |
|
8 |
|
|
9 |
@Override |
|
10 |
public SimpleServerConfig createSimpleServerConfig() { |
|
11 |
return new SimpleServerConfigImpl(); |
|
12 |
} |
|
13 |
|
|
14 |
@Override |
|
15 |
public SimpleServer createSimpleServer(SimpleServerConfig config) { |
|
16 |
return new SimpleServerImpl(config); |
|
17 |
} |
|
18 |
|
|
19 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.374/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/simplehttpserver/SimpleServerImpl.java | ||
---|---|---|
1 |
package org.gvsig.simplehttpserver; |
|
2 |
|
|
3 |
import java.net.MalformedURLException; |
|
4 |
import java.net.URL; |
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.HashSet; |
|
7 |
import java.util.LinkedHashMap; |
|
8 |
import java.util.List; |
|
9 |
import java.util.Map; |
|
10 |
import java.util.Set; |
|
11 |
import java.util.concurrent.TimeUnit; |
|
12 |
import org.apache.commons.lang3.StringUtils; |
|
13 |
import org.apache.http.ExceptionLogger; |
|
14 |
import org.apache.http.impl.nio.bootstrap.HttpServer; |
|
15 |
import org.apache.http.impl.nio.bootstrap.ServerBootstrap; |
|
16 |
import org.apache.http.impl.nio.reactor.IOReactorConfig; |
|
17 |
import org.gvsig.simplehttpserver.commands.Command; |
|
18 |
import org.gvsig.simplehttpserver.commands.CommandFactory; |
|
19 |
import org.slf4j.Logger; |
|
20 |
import org.slf4j.LoggerFactory; |
|
21 |
|
|
22 |
@SuppressWarnings("UseSpecificCatch") |
|
23 |
public class SimpleServerImpl implements SimpleServer { |
|
24 |
|
|
25 |
private static final Logger LOG = LoggerFactory.getLogger(SimpleServerImpl.class); |
|
26 |
|
|
27 |
private static final List<HttpServer> SERVERS = new ArrayList<>(); |
|
28 |
|
|
29 |
static { |
|
30 |
|
|
31 |
Runtime.getRuntime().addShutdownHook(new Thread() { |
|
32 |
@Override |
|
33 |
public void run() { |
|
34 |
for (HttpServer server : SERVERS) { |
|
35 |
server.shutdown(5, TimeUnit.SECONDS); |
|
36 |
} |
|
37 |
} |
|
38 |
}); |
|
39 |
} |
|
40 |
|
|
41 |
private HttpServer server; |
|
42 |
private final Set<LoggerListener> loglisteners; |
|
43 |
private Map<String, Command> commands; |
|
44 |
private final SimpleServerConfig config; |
|
45 |
|
|
46 |
public SimpleServerImpl(SimpleServerConfig config) { |
|
47 |
this.config = config; |
|
48 |
this.loglisteners = new HashSet<>(); |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public Map<String, Command> getCommands() { |
|
53 |
if (this.commands == null) { |
|
54 |
Map<String, Command>theCommands = new LinkedHashMap<>(); |
|
55 |
for (CommandFactory factory : this.config.getCommandFactories()) { |
|
56 |
theCommands.put(factory.getName(), factory.create(this)); |
|
57 |
} |
|
58 |
this.commands = theCommands; |
|
59 |
} |
|
60 |
return this.commands; |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public Command getCommand(String commandName) { |
|
65 |
if (commandName == null) { |
|
66 |
return null; |
|
67 |
} |
|
68 |
Command command = this.getCommands().get(commandName.toLowerCase()); |
|
69 |
return command; |
|
70 |
} |
|
71 |
|
|
72 |
@Override |
|
73 |
public SimpleServerConfig getConfig() { |
|
74 |
return this.config; |
|
75 |
} |
|
76 |
|
|
77 |
@Override |
|
78 |
public void addLogListener(LoggerListener logger) { |
|
79 |
this.loglisteners.add(logger); |
|
80 |
} |
|
81 |
|
|
82 |
@Override |
|
83 |
public void removeLogListener(LoggerListener logger) { |
|
84 |
this.loglisteners.remove(logger); |
|
85 |
} |
|
86 |
|
|
87 |
@Override |
|
88 |
public URL getURL(String path) { |
|
89 |
try { |
|
90 |
URL url; |
|
91 |
if( StringUtils.isBlank(path) ) { |
|
92 |
url = new URL("http", "127.0.0.1", this.config.getPort(),"/"); |
|
93 |
} else { |
|
94 |
url = new URL("http", "127.0.0.1", this.config.getPort(),path); |
|
95 |
} |
|
96 |
return url; |
|
97 |
} catch (MalformedURLException ex) { |
|
98 |
return null; |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public String getHost() { |
|
104 |
String host = "http://127.0.0.1:" + this.config.getPort(); |
|
105 |
return host; |
|
106 |
} |
|
107 |
|
|
108 |
@Override |
|
109 |
public void log(int level, String message) { |
|
110 |
switch (level) { |
|
111 |
default: |
|
112 |
case INFO: |
|
113 |
LOG.info(message); |
|
114 |
break; |
|
115 |
case WARN: |
|
116 |
LOG.warn(message); |
|
117 |
break; |
|
118 |
case DEBUG: |
|
119 |
LOG.debug(message); |
|
120 |
break; |
|
121 |
} |
|
122 |
for (LoggerListener logger : loglisteners) { |
|
123 |
logger.log(level, message); |
|
124 |
} |
|
125 |
} |
|
126 |
|
|
127 |
@Override |
|
128 |
public synchronized void start() { |
|
129 |
if (this.server != null) { |
|
130 |
this.stop(); |
|
131 |
} |
|
132 |
|
|
133 |
final IOReactorConfig theConfig = IOReactorConfig.custom() |
|
134 |
.setSoTimeout(this.config.getTimeout()) |
|
135 |
.setTcpNoDelay(true) |
|
136 |
.build(); |
|
137 |
|
|
138 |
this.server = ServerBootstrap.bootstrap() |
|
139 |
.setListenerPort(this.config.getPort()) |
|
140 |
.setServerInfo(this.config.getServerInfo()) |
|
141 |
.setIOReactorConfig(theConfig) |
|
142 |
.setExceptionLogger(ExceptionLogger.STD_ERR) |
|
143 |
.registerHandler("*", new HttpHandler(this)) |
|
144 |
.create(); |
|
145 |
|
|
146 |
try { |
|
147 |
this.server.start(); |
|
148 |
//this.server.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); |
|
149 |
SERVERS.add(this.server); |
|
150 |
log(INFO, "Server started."); |
|
151 |
} catch (Exception ex) { |
|
152 |
log(WARN, "Can't start server ("+ex.getMessage()+")."); |
|
153 |
} |
|
154 |
|
|
155 |
} |
|
156 |
|
|
157 |
@Override |
|
158 |
public void reload() { |
|
159 |
|
|
160 |
} |
|
161 |
|
|
162 |
@Override |
|
163 |
public synchronized boolean isStarted() { |
|
164 |
return this.server != null; |
|
165 |
} |
|
166 |
|
|
167 |
@Override |
|
168 |
@SuppressWarnings("SleepWhileHoldingLock") |
|
169 |
public synchronized void stop() { |
|
170 |
if (this.server == null) { |
|
171 |
log(INFO, "Server already stoped."); |
|
172 |
return; |
|
173 |
} |
|
174 |
this.server.shutdown(5, TimeUnit.SECONDS); |
|
175 |
try { |
|
176 |
Thread.sleep(6000); |
|
177 |
} catch (InterruptedException ex) { |
|
178 |
} |
|
179 |
SERVERS.remove(this.server); |
|
180 |
this.server = null; |
|
181 |
log(INFO, "Server stoped."); |
|
182 |
} |
|
183 |
|
|
184 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.374/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/simplehttpserver/SimpleServerConfigImpl.java | ||
---|---|---|
1 |
package org.gvsig.simplehttpserver; |
|
2 |
|
|
3 |
import java.beans.PropertyChangeListener; |
|
4 |
import java.beans.PropertyChangeSupport; |
|
5 |
import java.util.Collection; |
|
6 |
import java.util.HashMap; |
|
7 |
import java.util.Map; |
|
8 |
import org.gvsig.simplehttpserver.commands.CommandFactory; |
|
9 |
import org.gvsig.simplehttpserver.commands.HelpFactory; |
|
10 |
import org.gvsig.tools.util.PropertiesSupportHelper; |
|
11 |
|
|
12 |
/** |
|
13 |
* |
|
14 |
* @author jjdelcerro |
|
15 |
*/ |
|
16 |
public class SimpleServerConfigImpl implements SimpleServerConfig { |
|
17 |
|
|
18 |
private int port; |
|
19 |
private int timeout; |
|
20 |
private String serverInfo; |
|
21 |
private boolean autostart; |
|
22 |
private final PropertyChangeSupport propertyChangeSupport; |
|
23 |
private final Map<String, CommandFactory> commandFactories; |
|
24 |
private final PropertiesSupportHelper propertiesHelper = new PropertiesSupportHelper(); |
|
25 |
|
|
26 |
public SimpleServerConfigImpl() { |
|
27 |
this.port = 9876; |
|
28 |
this.timeout = 15000; |
|
29 |
this.serverInfo = "Test/1.1"; |
|
30 |
this.propertyChangeSupport = new PropertyChangeSupport(this); |
|
31 |
this.commandFactories = new HashMap<>(); |
|
32 |
|
|
33 |
HelpFactory factory = new HelpFactory(); |
|
34 |
this.commandFactories.put(factory.getName().toLowerCase(), factory); |
|
35 |
} |
|
36 |
|
|
37 |
@Override |
|
38 |
public int getPort() { |
|
39 |
return this.port; |
|
40 |
} |
|
41 |
|
|
42 |
@Override |
|
43 |
public SimpleServerConfig setPort(int port) { |
|
44 |
int oldValue = this.port; |
|
45 |
this.port = port; |
|
46 |
this.propertyChangeSupport.firePropertyChange("port", oldValue, port); |
|
47 |
return this; |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public int getTimeout() { |
|
52 |
return this.timeout; |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public SimpleServerConfig setTimeout(int timeout) { |
|
57 |
int oldValue = this.timeout; |
|
58 |
this.timeout = timeout; |
|
59 |
this.propertyChangeSupport.firePropertyChange("timeout", oldValue, timeout); |
|
60 |
return this; |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public String getServerInfo() { |
|
65 |
return this.serverInfo; |
|
66 |
} |
|
67 |
|
|
68 |
@Override |
|
69 |
public SimpleServerConfig setServerInfo(String serverInfo) { |
|
70 |
String oldValue = this.serverInfo; |
|
71 |
this.serverInfo = serverInfo; |
|
72 |
this.propertyChangeSupport.firePropertyChange("serverInfo", oldValue, serverInfo); |
|
73 |
return this; |
|
74 |
} |
|
75 |
|
|
76 |
@Override |
|
77 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
|
78 |
this.propertyChangeSupport.addPropertyChangeListener(listener); |
|
79 |
} |
|
80 |
|
|
81 |
@Override |
|
82 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
|
83 |
this.propertyChangeSupport.removePropertyChangeListener(listener); |
|
84 |
} |
|
85 |
|
|
86 |
@Override |
|
87 |
public Collection<CommandFactory> getCommandFactories() { |
|
88 |
return this.commandFactories.values(); |
|
89 |
} |
|
90 |
|
|
91 |
@Override |
|
92 |
public SimpleServerConfig addCommandFactory(CommandFactory factory) { |
|
93 |
this.commandFactories.put(factory.getName().toLowerCase(), factory); |
|
94 |
return this; |
|
95 |
} |
|
96 |
|
|
97 |
@Override |
|
98 |
public void removeCommandFactory(CommandFactory factory) { |
|
99 |
this.commandFactories.remove(factory.getName()); |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public void removeCommandFactory(String name) { |
|
104 |
this.commandFactories.remove(name); |
|
105 |
} |
|
106 |
|
|
107 |
@Override |
|
108 |
public Object getProperty(String name) { |
|
109 |
return this.propertiesHelper.getProperty(name); |
|
110 |
} |
|
111 |
|
|
112 |
@Override |
|
113 |
public void setProperty(String name, Object value) { |
|
114 |
this.propertiesHelper.setProperty(name, value); |
|
115 |
} |
|
116 |
|
|
117 |
@Override |
|
118 |
public Map<String, Object> getProperties() { |
|
119 |
return this.propertiesHelper.getProperties(); |
|
120 |
} |
|
121 |
|
|
122 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.374/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/tools/util/impl/ToolsUtilLibraryImpl.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify it under |
|
7 |
* the terms of the GNU General Public License as published by the Free Software |
|
8 |
* Foundation; either version 2 of the License, or (at your option) any later |
|
9 |
* version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
14 |
* details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License along with |
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51 |
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
19 |
* |
|
20 |
* For any additional information, do not hesitate to contact us at info AT |
|
21 |
* gvsig.com, or visit our website www.gvsig.com. |
|
22 |
*/ |
|
23 |
package org.gvsig.tools.util.impl; |
|
24 |
|
|
25 |
import org.gvsig.configurableactions.DefaultConfigurableActionsMamager; |
|
26 |
import org.gvsig.euclidean.DefaultEuclideanManager; |
|
27 |
import org.gvsig.filedialogchooser.impl.DefaultFileDialogChooserManager; |
|
28 |
import org.gvsig.imageviewer.DefaultImageViewerManager; |
|
29 |
import org.gvsig.json.JsonManagerImpl; |
|
30 |
import org.gvsig.propertypage.impl.DefaultPropertiesPageManager; |
|
31 |
import org.gvsig.simplehttpserver.SimpleServerManagerImpl; |
|
32 |
import org.gvsig.svgsupport.DumbSVGSupportManager; |
|
33 |
import org.gvsig.tools.library.AbstractLibrary; |
|
34 |
import org.gvsig.tools.library.Library; |
|
35 |
import org.gvsig.tools.library.LibraryException; |
|
36 |
import org.gvsig.tools.util.ToolsUtilLibrary; |
|
37 |
import org.gvsig.tools.util.ToolsUtilLocator; |
|
38 |
import org.gvsig.webbrowser.cssbox.CSSBoxWebBrowserManager; |
|
39 |
import org.gvsig.webbrowser.impl.DefaultWebBrowserManager; |
|
40 |
import org.gvsig.webbrowser.impl.DefaultWebBrowserPanel; |
|
41 |
|
|
42 |
/** |
|
43 |
* {@link Library} for the default tools util implementation. |
|
44 |
* |
|
45 |
* @author gvSIG team |
|
46 |
*/ |
|
47 |
public class ToolsUtilLibraryImpl extends AbstractLibrary { |
|
48 |
|
|
49 |
@Override |
|
50 |
public void doRegistration() { |
|
51 |
super.doRegistration(); |
|
52 |
registerAsImplementationOf(ToolsUtilLibrary.class); |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
protected void doInitialize() throws LibraryException { |
|
57 |
ToolsUtilLocator.registerDefaultToolsUtilManager(DefaultToolsUtilManager.class); |
|
58 |
ToolsUtilLocator.registerDefaultFileDialogChooserManager(DefaultFileDialogChooserManager.class); |
|
59 |
ToolsUtilLocator.registerDefaultSVGSupportManager(DumbSVGSupportManager.class); |
|
60 |
ToolsUtilLocator.registerDefaultImageViewerManager(DefaultImageViewerManager.class); |
|
61 |
ToolsUtilLocator.registerDefaultConfigurableActionsMamager(DefaultConfigurableActionsMamager.class); |
|
62 |
ToolsUtilLocator.registerDefaultPropertiesPageManager(DefaultPropertiesPageManager.class); |
|
63 |
|
|
64 |
ToolsUtilLocator.registerDefaultWebBrowserManager(DefaultWebBrowserManager.class); |
|
65 |
ToolsUtilLocator.registerWebBrowserManager(CSSBoxWebBrowserManager.class); |
|
66 |
|
|
67 |
ToolsUtilLocator.registerEuclideanManager(DefaultEuclideanManager.class); |
|
68 |
|
|
69 |
ToolsUtilLocator.registerJsonManager(JsonManagerImpl.class); |
|
70 |
|
|
71 |
ToolsUtilLocator.registerSimpleServerManager(SimpleServerManagerImpl.class); |
|
72 |
|
|
73 |
DefaultWebBrowserPanel.selfRegister(); |
|
74 |
} |
|
75 |
|
|
76 |
@Override |
|
77 |
protected void doPostInitialize() throws LibraryException { |
|
78 |
|
|
79 |
} |
|
80 |
|
|
81 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.374/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/tools/util/impl/BaseInformationBuilder.java | ||
---|---|---|
1 |
package org.gvsig.tools.util.impl; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.List; |
|
5 |
|
|
6 |
import org.apache.commons.lang3.StringEscapeUtils; |
|
7 |
import org.apache.commons.lang3.StringUtils; |
|
8 |
|
|
9 |
import org.gvsig.tools.ToolsLocator; |
|
10 |
import org.gvsig.tools.util.InformationBuilder; |
|
11 |
|
|
12 |
|
|
13 |
public class BaseInformationBuilder implements InformationBuilder { |
|
14 |
|
|
15 |
private String backgroundColor = "\"#FFFFFF\""; |
|
16 |
private String backgroundColorTitle = "\"#FBFFE1\""; // light yellow |
|
17 |
private String backgroundColorPropertyLabel = "\"#D6D6D6\""; // Gris |
|
18 |
private String backgroundColorPropertyValue1 = "\"#FEEDD6\""; // light salmon |
|
19 |
private String backgroundColorPropertyValue2 = "\"#EAEAEA\""; // light grey |
|
20 |
|
|
21 |
protected int propertyCount = 0; |
|
22 |
protected final List<Element> elements; |
|
23 |
|
|
24 |
protected class BaseTitleElement implements TitleElement { |
|
25 |
|
|
26 |
protected String label; |
|
27 |
|
|
28 |
@Override |
|
29 |
public TitleElement value(String format, Object... args) { |
|
30 |
if (args == null) { |
|
31 |
this.label = format; |
|
32 |
} else { |
|
33 |
this.label = String.format(format, args); |
|
34 |
} |
|
35 |
return this; |
|
36 |
} |
|
37 |
|
|
38 |
@Override |
|
39 |
public TitleElement label(String label) { |
|
40 |
this.label = StringEscapeUtils.escapeHtml3(label); |
|
41 |
return this; |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
public TitleElement labelkey(String labelkey) { |
|
46 |
return label(ToolsLocator.getI18nManager().getTranslation(labelkey)); |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
public String build() { |
|
51 |
propertyCount = 0; |
|
52 |
return "<tr valign=\"middle\" >" |
|
53 |
+ "<td bgcolor=" + backgroundColorTitle + " align=\"center\" colspan=\"2\"><font face=\"Arial\" size=\"3\"><b> " + this.label + "</b></font></td>" |
|
54 |
+ "</tr>"; |
|
55 |
} |
|
56 |
} |
|
57 |
|
|
58 |
protected class BaseRawElement implements RawElement { |
|
59 |
|
|
60 |
protected String value; |
|
61 |
|
|
62 |
@Override |
|
63 |
public RawElement value(String format, Object... args) { |
|
64 |
if (args == null) { |
|
65 |
this.value = format; |
|
66 |
} else { |
|
67 |
this.value = String.format(format, args); |
|
68 |
} |
|
69 |
return this; |
|
70 |
} |
|
71 |
|
|
72 |
@Override |
|
73 |
public String build() { |
|
74 |
String color = ((propertyCount++ % 2) == 0) ? backgroundColorPropertyValue1 : backgroundColorPropertyValue2; |
|
75 |
String content = "<tr valign=\"top\">\n"; |
|
76 |
content += "<td bgcolor=" + color + "align=\"left\" colspan=\"2\"><font face=\"Arial\" size=\"3\"><div>" + value.replace("\\n", "<br>\n") + "</div></font></td>\n"; |
|
77 |
content += "</tr>\n"; |
|
78 |
return content; |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
protected class BaseTextElement implements TextElement { |
|
83 |
|
|
84 |
protected String value; |
|
85 |
|
|
86 |
@Override |
|
87 |
public TextElement value(String format, Object... args) { |
|
88 |
if (args == null) { |
|
89 |
this.value = StringEscapeUtils.escapeHtml3(format); |
|
90 |
} else { |
|
91 |
this.value = StringEscapeUtils.escapeHtml3(String.format(format, args)); |
|
92 |
} |
|
93 |
return this; |
|
94 |
} |
|
95 |
|
|
96 |
@Override |
|
97 |
public String build() { |
|
98 |
String color = ((propertyCount++ % 2) == 0) ? backgroundColorPropertyValue1 : backgroundColorPropertyValue2; |
|
99 |
String content = "<tr valign=\"top\">\n"; |
|
100 |
content += "<td bgcolor=" + color + "align=\"left\" colspan=\"2\"><font face=\"Arial\" size=\"3\">" + value.replace("\n", "<br>\n") + "</font></td>\n"; |
|
101 |
content += "</tr>\n"; |
|
102 |
return content; |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
protected class BasePropertyElement implements PropertyElement { |
|
107 |
|
|
108 |
protected String label; |
|
109 |
protected boolean monospace = false; |
|
110 |
protected String value; |
|
111 |
|
|
112 |
@Override |
|
113 |
public PropertyElement value(String format, Object... args) { |
|
114 |
if (args == null) { |
|
115 |
this.value = StringEscapeUtils.escapeHtml3(format); |
|
116 |
} else { |
|
117 |
this.value = StringEscapeUtils.escapeHtml3(String.format(format, args)); |
|
118 |
} |
|
119 |
return this; |
|
120 |
} |
|
121 |
|
|
122 |
@Override |
|
123 |
public PropertyElement label(String label) { |
|
124 |
this.label = StringEscapeUtils.escapeHtml3(label); |
|
125 |
return this; |
|
126 |
} |
|
127 |
|
|
128 |
@Override |
|
129 |
public PropertyElement labelkey(String labelkey) { |
|
130 |
return label(ToolsLocator.getI18nManager().getTranslation(labelkey)); |
|
131 |
} |
|
132 |
|
|
133 |
@Override |
|
134 |
public PropertyElement monospace() { |
|
135 |
this.monospace = true; |
|
136 |
return this; |
|
137 |
} |
|
138 |
|
|
139 |
@Override |
|
140 |
public String build() { |
|
141 |
String color = ((propertyCount++ % 2) == 0) ? backgroundColorPropertyValue1 : backgroundColorPropertyValue2; |
|
142 |
String content = "<tr valign=\"top\">\n"; |
|
143 |
if (!StringUtils.isEmpty(label)) { |
|
144 |
content += "<td nowrap bgcolor=" + backgroundColorPropertyLabel + "align=\"right\" width=\"140\"><font face=\"Arial\" size=\"3\">" + label + ": </font></td>\n"; |
|
145 |
} |
|
146 |
if( this.monospace ) { |
|
147 |
content += "<td bgcolor=" + color + "align=\"left\"><font face=\"Monospaced\" size=\"3\">" + value.replace("\\n", "<br>\n") + "</font></td>"; |
|
148 |
} else { |
|
149 |
content += "<td bgcolor=" + color + "align=\"left\"><font face=\"Arial\" size=\"3\">" + value.replace("\\n", "<br>\n") + "</font></td>"; |
|
150 |
} |
|
151 |
content += "</tr>\n"; |
|
152 |
return content; |
|
153 |
} |
|
154 |
|
|
155 |
} |
|
156 |
|
|
157 |
public BaseInformationBuilder() { |
|
158 |
this.elements = new ArrayList<>(); |
|
159 |
} |
|
160 |
|
|
161 |
@Override |
|
162 |
public InformationBuilder backgroundColor(String color) { |
|
163 |
this.backgroundColor = color; |
|
164 |
return this; |
|
165 |
} |
|
166 |
|
|
167 |
@Override |
|
168 |
public InformationBuilder backgroundColorTitle(String color) { |
|
169 |
this.backgroundColorTitle = color; |
|
170 |
return this; |
|
171 |
} |
|
172 |
|
|
173 |
@Override |
|
174 |
public InformationBuilder backgroundColorPropertyLabel(String color) { |
|
175 |
this.backgroundColorPropertyLabel = color; |
|
176 |
return this; |
|
177 |
} |
|
178 |
|
|
179 |
@Override |
|
180 |
public InformationBuilder backgroundColorPropertyValue1(String color) { |
|
181 |
this.backgroundColorPropertyValue1 = color; |
|
182 |
return this; |
|
183 |
} |
|
184 |
|
|
185 |
@Override |
|
186 |
public InformationBuilder backgroundColorPropertyValue2(String color) { |
|
187 |
this.backgroundColorPropertyValue2 = color; |
|
188 |
return this; |
|
189 |
} |
|
190 |
|
|
191 |
@Override |
|
192 |
public TitleElement title() { |
|
193 |
TitleElement element = new BaseTitleElement(); |
|
194 |
this.elements.add(element); |
|
195 |
return element; |
|
196 |
} |
|
197 |
|
|
198 |
@Override |
|
199 |
public PropertyElement property() { |
|
200 |
PropertyElement element = new BasePropertyElement(); |
|
201 |
this.elements.add(element); |
|
202 |
return element; |
|
203 |
} |
|
204 |
|
|
205 |
@Override |
|
206 |
public TextElement text() { |
|
207 |
TextElement element = new BaseTextElement(); |
|
208 |
this.elements.add(element); |
|
209 |
return element; |
|
210 |
} |
|
211 |
|
|
212 |
@Override |
|
213 |
public RawElement raw() { |
|
214 |
RawElement element = new BaseRawElement(); |
|
215 |
this.elements.add(element); |
|
216 |
return element; |
|
217 |
} |
|
218 |
|
|
219 |
@Override |
|
220 |
public String build() { |
|
221 |
StringBuilder html = new StringBuilder(); |
|
222 |
html.append("<html>\n"); |
|
223 |
html.append("<body bgcolor=").append(backgroundColor).append(" topmargin=\"0\" marginheight=\"0\">\n"); |
|
224 |
html.append("<table cellpadding=\"0\" cellspacing=\"0\" align=\"center\" width=\"100%\">"); |
|
225 |
boolean first = true; |
|
226 |
for (Element element : elements) { |
|
227 |
if( element instanceof TitleElement ) { |
|
228 |
if( first ) { |
|
229 |
first = false; |
|
230 |
} else { |
|
231 |
html.append("<tr>\n<td><br></td>\n<td></td></tr>\n"); |
|
232 |
} |
|
233 |
} |
|
234 |
html.append(element.build()); |
|
235 |
} |
|
236 |
html.append("</table>"); |
|
237 |
html.append("</body>"); |
|
238 |
html.append("</html>"); |
|
239 |
return html.toString(); |
|
240 |
} |
|
241 |
|
|
242 |
@Override |
|
243 |
public String toString() { |
|
244 |
return this.build(); |
|
245 |
} |
|
246 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.374/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/tools/util/impl/DefaultToolsUtilManager.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.tools.util.impl; |
|
3 |
|
|
4 |
import org.gvsig.desktopopen.DefaultDesktopOpen; |
|
5 |
import org.gvsig.desktopopen.DesktopOpen; |
|
6 |
import org.gvsig.htmlbuilder.HTMLBuilder; |
|
7 |
import org.gvsig.htmlbuilder.impl.DefaultHTMLBuilder; |
|
8 |
import org.gvsig.tools.util.InformationBuilder; |
|
9 |
import org.gvsig.tools.util.ToolsUtilManager; |
|
10 |
|
|
11 |
|
|
12 |
/** |
|
13 |
* @author fdiaz |
|
14 |
* |
|
15 |
*/ |
|
16 |
public class DefaultToolsUtilManager implements ToolsUtilManager { |
|
17 |
|
|
18 |
@Override |
|
19 |
public InformationBuilder createInformationBuilder() { |
|
20 |
return new BaseInformationBuilder(); |
|
21 |
} |
|
22 |
|
|
23 |
@Override |
|
24 |
public DesktopOpen createDesktopOpen() { |
|
25 |
return new DefaultDesktopOpen(); |
|
26 |
} |
|
27 |
|
|
28 |
public HTMLBuilder createHTMLBuilder() { |
|
29 |
DefaultHTMLBuilder x = new DefaultHTMLBuilder(); |
|
30 |
return x; |
|
31 |
} |
|
32 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.374/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/configurableactions/ActionsControllerImpl.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.configurableactions; |
|
3 |
|
|
4 |
import java.awt.event.ActionEvent; |
|
5 |
import java.awt.event.MouseAdapter; |
|
6 |
import java.awt.event.MouseEvent; |
|
7 |
import java.net.URL; |
|
8 |
import java.util.ArrayList; |
|
9 |
import java.util.Collection; |
|
10 |
import javax.swing.AbstractAction; |
|
11 |
import javax.swing.AbstractButton; |
|
12 |
import javax.swing.Action; |
|
13 |
import javax.swing.ImageIcon; |
|
14 |
import javax.swing.JComponent; |
|
15 |
import javax.swing.JLabel; |
|
16 |
import javax.swing.JMenuItem; |
|
17 |
import javax.swing.JPopupMenu; |
|
18 |
import org.apache.commons.collections4.CollectionUtils; |
|
19 |
import org.gvsig.configurableactions.ConfigurableActionsMamager.ActionsController; |
|
20 |
import org.gvsig.tools.ToolsLocator; |
|
21 |
import org.gvsig.tools.i18n.I18nManager; |
|
22 |
import org.gvsig.tools.swing.api.SupportIsEnable; |
|
23 |
import org.gvsig.tools.swing.api.SupportIsVisible; |
|
24 |
|
|
25 |
|
|
26 |
public class ActionsControllerImpl implements ActionsController { |
|
27 |
|
|
28 |
private final JComponent component; |
|
29 |
private final Object source; |
|
30 |
private Collection<Action> actions; |
|
31 |
|
|
32 |
|
|
33 |
private class ActionListenerAdapter extends AbstractAction { |
|
34 |
|
|
35 |
private final Action action; |
|
36 |
|
|
37 |
public ActionListenerAdapter(Action action) { |
|
38 |
this.action = action; |
|
39 |
} |
|
40 |
|
|
41 |
@Override |
|
42 |
public void actionPerformed(ActionEvent e) { |
|
43 |
e.setSource(source); |
|
44 |
action.actionPerformed(e); |
|
45 |
} |
|
46 |
|
|
47 |
@Override |
|
48 |
public Object getValue(String key) { |
|
49 |
return action.getValue(key); |
|
50 |
} |
|
51 |
|
|
52 |
@Override |
|
53 |
public void putValue(String key, Object newValue) { |
|
54 |
action.putValue(key, newValue); |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public boolean isEnabled() { |
|
59 |
return action.isEnabled(); |
|
60 |
} |
|
61 |
|
|
62 |
@Override |
|
63 |
public void setEnabled(boolean newValue) { |
|
64 |
action.setEnabled(newValue); |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
|
69 |
public ActionsControllerImpl(Collection<Action> actions, Object source, JComponent component) { |
|
70 |
this.component = component; |
|
71 |
this.source = source; |
|
72 |
this.update(actions); |
|
73 |
this.initComponent(); |
|
74 |
} |
|
75 |
|
|
76 |
private void initComponent() { |
|
77 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
78 |
this.component.setToolTipText(i18n.getTranslation("_Click_to_see_more_action_in_this_panel")); |
|
79 |
URL url = this.getClass().getResource("/org/gvsig/configurableactions/images/configurableactions.png"); |
|
80 |
if( component instanceof AbstractButton ) { |
|
81 |
AbstractButton comp = (AbstractButton) this.component; |
|
82 |
if( comp.getText().equals("...") ) { |
|
83 |
comp.setText(""); |
|
84 |
} |
|
85 |
comp.setIcon(new ImageIcon(url)); |
|
86 |
} else if( component instanceof JLabel ) { |
|
87 |
JLabel comp = (JLabel) this.component; |
|
88 |
if( comp.getText().equals("...") ) { |
|
89 |
comp.setText(""); |
|
90 |
} |
|
91 |
comp.setIcon(new ImageIcon(url)); |
|
92 |
} |
|
93 |
this.component.setBorder(null); |
|
94 |
this.component.addMouseListener(new MouseAdapter() { |
|
95 |
@Override |
|
96 |
public void mouseClicked(MouseEvent e) { |
|
97 |
if( e.getClickCount()==1 && e.getButton()==MouseEvent.BUTTON1 ) { |
|
98 |
doShowMenu(); |
|
99 |
} |
|
100 |
} |
|
101 |
}); |
|
102 |
// this.addActionListener(new ActionListener() { |
|
103 |
// @Override |
|
104 |
// public void actionPerformed(ActionEvent e) { |
|
105 |
// doShowMenu(); |
|
106 |
// } |
|
107 |
// }); |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public void update(Collection<Action> actions) { |
|
112 |
this.actions = actions; |
|
113 |
if( actions==null || actions.isEmpty() ) { |
|
114 |
this.component.setVisible(false); |
|
115 |
} else { |
|
116 |
this.component.setVisible(true); |
|
117 |
} |
|
118 |
} |
|
119 |
|
|
120 |
@Override |
|
121 |
public Collection<Action> getActions() { |
|
122 |
return this.actions; |
|
123 |
} |
|
124 |
|
|
125 |
@Override |
|
126 |
public JComponent getComponent() { |
|
127 |
return component; |
|
128 |
} |
|
129 |
|
|
130 |
@Override |
|
131 |
public Object getSource() { |
|
132 |
return source; |
|
133 |
} |
|
134 |
|
|
135 |
@Override |
|
136 |
public void addAction(Action action) { |
|
137 |
Collection<Action> theActions = new ArrayList<>(); |
|
138 |
if( CollectionUtils.isNotEmpty(this.actions) ) { |
|
139 |
theActions.addAll(this.actions); |
|
140 |
} |
|
141 |
theActions.add(action); |
|
142 |
this.actions = theActions; |
|
143 |
} |
|
144 |
|
|
145 |
private void doShowMenu() { |
|
146 |
JPopupMenu menu = new JPopupMenu(); |
|
147 |
SupportIsVisible visible = null; |
|
148 |
if( this.source instanceof SupportIsVisible ) { |
|
149 |
visible = (SupportIsVisible) this.source; |
|
150 |
} |
|
151 |
SupportIsEnable enabled = null; |
|
152 |
if( this.source instanceof SupportIsEnable ) { |
|
153 |
enabled = (SupportIsEnable) this.source; |
|
154 |
} |
|
155 |
for (Action action : actions) { |
|
156 |
if( visible==null || visible.isVisible(action) ) { |
|
157 |
JMenuItem item = new JMenuItem(new ActionListenerAdapter(action)); |
|
158 |
if( enabled!=null && !enabled.isEnabled(action)) { |
|
159 |
item.setEnabled(false); |
|
160 |
} |
|
161 |
menu.add(item); |
|
162 |
} |
|
163 |
} |
|
164 |
menu.show(this.component, 0,this.component.getHeight()); |
|
165 |
} |
|
166 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.374/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/configurableactions/DefaultConfigurableActionsMamager.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.configurableactions; |
|
3 |
|
|
4 |
import java.lang.ref.WeakReference; |
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.Collection; |
|
7 |
import java.util.HashMap; |
|
8 |
import java.util.List; |
|
9 |
import java.util.Map; |
|
10 |
import javax.swing.Action; |
|
11 |
import javax.swing.JComponent; |
|
12 |
import javax.swing.JLabel; |
|
13 |
import org.apache.commons.lang3.StringUtils; |
|
14 |
|
|
15 |
|
|
16 |
public class DefaultConfigurableActionsMamager implements ConfigurableActionsMamager { |
|
17 |
|
|
18 |
private class PanelActions { |
|
19 |
List<Action> actions; |
|
20 |
WeakReference<ActionsControllerImpl> controller; |
|
21 |
private final String panelid; |
|
22 |
|
|
23 |
public PanelActions(String panelid) { |
|
24 |
this.panelid = panelid; |
|
25 |
this.actions = new ArrayList<>(); |
|
26 |
} |
|
27 |
|
|
28 |
public void setController(ActionsControllerImpl controller) { |
|
29 |
this.controller = new WeakReference<>(controller); |
|
30 |
} |
|
31 |
|
|
32 |
public ActionsControllerImpl getController() { |
|
33 |
if( this.controller == null ) { |
|
34 |
return null; |
|
35 |
} |
|
36 |
ActionsControllerImpl theController = this.controller.get(); |
|
37 |
return theController; |
|
38 |
} |
|
39 |
|
|
40 |
public void updateComponent() { |
|
41 |
ActionsControllerImpl theController = this.getController(); |
|
42 |
if( theController == null ) { |
|
43 |
return; |
|
44 |
} |
|
45 |
theController.update(actions); |
|
46 |
} |
|
47 |
|
|
48 |
public int size() { |
|
49 |
return this.actions.size(); |
|
50 |
} |
|
51 |
|
|
52 |
public Action get(int i) { |
|
53 |
return this.actions.get(i); |
|
54 |
} |
|
55 |
|
|
56 |
public void set(int i, Action action) { |
|
57 |
this.actions.set(i, action); |
|
58 |
this.updateComponent(); |
|
59 |
} |
|
60 |
|
|
61 |
public void add(Action action) { |
|
62 |
this.actions.add(action); |
|
63 |
this.updateComponent(); |
|
64 |
} |
|
65 |
|
|
66 |
public void remove(int i) { |
|
67 |
this.actions.remove(i); |
|
68 |
this.updateComponent(); |
|
69 |
} |
|
70 |
|
|
71 |
public Collection<Action> getActions() { |
|
72 |
return this.actions; |
|
73 |
} |
|
74 |
|
|
75 |
private void removeAll() { |
|
76 |
this.actions.clear(); |
|
77 |
this.updateComponent(); |
|
78 |
} |
|
79 |
|
|
80 |
} |
|
81 |
|
|
82 |
private final Map<String,PanelActions> panelsActions; |
|
83 |
|
|
84 |
|
|
85 |
public DefaultConfigurableActionsMamager() { |
|
86 |
this.panelsActions = new HashMap<>(); |
|
87 |
} |
|
88 |
|
|
89 |
@Override |
|
90 |
public void addConfigurableAction(String panelid, Action action) { |
|
91 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
92 |
if( panelActions==null ) { |
|
93 |
panelActions = new PanelActions(panelid); |
|
94 |
this.panelsActions.put(panelid, panelActions); |
|
95 |
} |
|
96 |
for( int i=0; i<panelActions.size(); i++ ) { |
|
97 |
Action currentAction = panelActions.get(i); |
|
98 |
if( StringUtils.equalsIgnoreCase( |
|
99 |
(String)currentAction.getValue(Action.ACTION_COMMAND_KEY), |
|
100 |
(String)action.getValue(Action.ACTION_COMMAND_KEY) ) ) { |
|
101 |
panelActions.set(i, action); |
|
102 |
return; |
|
103 |
} |
|
104 |
} |
|
105 |
panelActions.add(action); |
|
106 |
} |
|
107 |
|
|
108 |
@Override |
|
109 |
public void removeConfigurableAction(String panelid, String actionName) { |
|
110 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
111 |
if( panelActions==null ) { |
|
112 |
return; |
|
113 |
} |
|
114 |
for( int i=0; i<panelActions.size(); i++ ) { |
|
115 |
Action currentAction = panelActions.get(i); |
|
116 |
if( StringUtils.equalsIgnoreCase( |
|
117 |
(String)currentAction.getValue(Action.ACTION_COMMAND_KEY), |
|
118 |
actionName ) ) { |
|
119 |
panelActions.remove(i); |
|
120 |
return; |
|
121 |
} |
|
122 |
} |
|
123 |
} |
|
124 |
|
|
125 |
@Override |
|
126 |
public void removeAllConfigurableActions(String panelid) { |
|
127 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
128 |
if( panelActions==null ) { |
|
129 |
return; |
|
130 |
} |
|
131 |
panelActions.removeAll(); |
|
132 |
} |
|
133 |
|
|
134 |
@Override |
|
135 |
public Collection<Action> getConfigurableActions(String panelid) { |
|
136 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
137 |
if( panelActions==null ) { |
|
138 |
panelActions = new PanelActions(panelid); |
|
139 |
this.panelsActions.put(panelid, panelActions); |
|
140 |
} |
|
141 |
return panelActions.getActions(); |
|
142 |
} |
|
143 |
|
|
144 |
@Override |
|
145 |
public JComponent getConfigurableActionsComponent(String panelid, Object source) { |
|
146 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
147 |
if( panelActions==null ) { |
|
148 |
panelActions = new PanelActions(panelid); |
|
149 |
this.panelsActions.put(panelid, panelActions); |
|
150 |
} |
|
151 |
JComponent component = new JLabel(); |
|
152 |
ActionsControllerImpl controller = new ActionsControllerImpl(panelActions.getActions(), source, component); |
|
153 |
panelActions.setController(controller); |
|
154 |
return component; |
|
155 |
} |
|
156 |
|
|
157 |
@Override |
|
158 |
public ActionsController getConfigurableActionsController(String panelid) { |
|
159 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
160 |
if( panelActions==null ) { |
|
161 |
return null; |
|
162 |
} |
|
163 |
return panelActions.getController(); |
|
164 |
} |
|
165 |
|
|
166 |
@Override |
|
167 |
public void addConfigurableActions(String panelid, Object source, JComponent component) { |
|
168 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
169 |
if( panelActions==null ) { |
|
170 |
panelActions = new PanelActions(panelid); |
|
171 |
this.panelsActions.put(panelid, panelActions); |
|
172 |
} |
|
173 |
ActionsControllerImpl controller = new ActionsControllerImpl(panelActions.getActions(), source, component); |
|
174 |
panelActions.setController(controller); |
|
175 |
} |
|
176 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.374/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/propertypage/impl/DefaultPropertiesPageManager.java | ||
---|---|---|
1 |
package org.gvsig.propertypage.impl; |
|
2 |
|
|
3 |
import java.awt.event.ActionEvent; |
|
4 |
import java.lang.reflect.InvocationTargetException; |
|
5 |
import java.lang.reflect.Method; |
|
6 |
import java.util.ArrayList; |
|
7 |
import java.util.Collection; |
|
8 |
import java.util.Collections; |
|
9 |
import java.util.Comparator; |
|
10 |
import java.util.HashMap; |
|
11 |
import java.util.List; |
|
12 |
import java.util.Map; |
|
13 |
import org.apache.commons.lang3.StringUtils; |
|
14 |
import org.gvsig.propertypage.PropertiesPage; |
|
15 |
import org.gvsig.propertypage.PropertiesPage.SetPageEnabledEvent; |
|
16 |
import org.gvsig.propertypage.PropertiesPageFactory; |
|
17 |
import org.gvsig.propertypage.PropertiesPageManager; |
|
18 |
import org.gvsig.tools.ToolsLocator; |
|
19 |
import org.gvsig.tools.identitymanagement.SimpleIdentity; |
|
20 |
import org.gvsig.tools.identitymanagement.SimpleIdentityManager; |
|
21 |
import org.gvsig.tools.util.Invocable; |
|
22 |
import org.slf4j.Logger; |
|
23 |
import org.slf4j.LoggerFactory; |
|
24 |
|
|
25 |
public class DefaultPropertiesPageManager implements PropertiesPageManager { |
|
26 |
|
|
27 |
private static class DefaultSetPageEnabledEvent extends ActionEvent { |
|
28 |
|
|
29 |
private Invocable filter; |
|
30 |
private boolean enabled; |
|
31 |
|
|
32 |
public DefaultSetPageEnabledEvent(Object source, Invocable filter, boolean enabled ) { |
|
33 |
super(source, SetPageEnabledEvent.ID, SetPageEnabledEvent.ACTION_COMMAND); |
|
34 |
this.filter = filter; |
|
35 |
this.enabled = enabled; |
|
36 |
} |
|
37 |
|
|
38 |
public Invocable getFilter() { |
|
39 |
return filter; |
|
40 |
} |
|
41 |
|
|
42 |
public boolean isEnabled() { |
|
43 |
return enabled; |
|
44 |
} |
|
45 |
|
|
46 |
} |
|
47 |
|
|
48 |
public static final String ACCESS_PAGE_AUTHORIZATION = "propertypage-access"; |
|
49 |
|
|
50 |
private static final Logger logger = LoggerFactory.getLogger(DefaultPropertiesPageManager.class); |
|
51 |
|
|
52 |
private final Map<String, Map<String, PropertiesPageFactory>> groups = new HashMap<>(); |
|
53 |
|
|
54 |
public DefaultPropertiesPageManager() { |
|
55 |
|
|
56 |
} |
|
57 |
|
|
58 |
private String getFactoryName(PropertiesPageFactory factory) { |
|
59 |
String factoryName = factory.getClass().getName(); |
|
60 |
try { |
|
61 |
factoryName = factory.getName(); |
|
62 |
} catch(Throwable ex) { |
|
63 |
|
|
64 |
} |
|
65 |
return factoryName; |
|
66 |
} |
|
67 |
|
|
68 |
@Override |
|
69 |
public void registerFactory(PropertiesPageFactory factory) { |
|
70 |
logger.info("Register PropertisPageFactory " + factory.getClass().getName() + " , groupID=" + factory.getGroupID() + " (" + factory.toString() + ")."); |
|
71 |
Map<String, PropertiesPageFactory> group = this.getFactories(factory.getGroupID()); |
|
72 |
group.put(getFactoryName(factory), factory); |
|
73 |
} |
|
74 |
|
|
75 |
private Map<String, PropertiesPageFactory> getFactories(String groupID) { |
|
76 |
logger.info("get propeties page factories for groupID '" + groupID + "'"); |
|
77 |
Map<String,PropertiesPageFactory> group = this.groups.get(groupID); |
|
78 |
if (group == null) { |
|
79 |
group = new HashMap<>(); |
|
80 |
this.groups.put(groupID, group); |
|
81 |
} |
|
82 |
return group; |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
public List<PropertiesPage> getPages(String groupID, Object container, Object data) { |
|
87 |
logger.info("get propeties page for groupID '" + groupID + "', and object '" + ((data==null)?"null":data.toString()) + "'."); |
|
88 |
List<PropertiesPage> pages = new ArrayList<>(); |
|
89 |
SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager(); |
|
90 |
SimpleIdentity currentUser = identityManager.getCurrentIdentity(); |
|
91 |
if (!currentUser.isAuthorized(ACCESS_PAGE_AUTHORIZATION,null,groupID ) ) { |
|
92 |
return pages; // Empty list, no access to pages authoriced |
|
93 |
} |
|
94 |
|
|
95 |
Collection<PropertiesPageFactory> factories = this.getFactories(groupID).values(); |
|
96 |
String authorizationId = ACCESS_PAGE_AUTHORIZATION+"-"+groupID; |
|
97 |
for (PropertiesPageFactory factory : factories) { |
|
98 |
String factoryName = getFactoryName(factory); |
|
99 |
try { |
|
100 |
if (factory.isVisible(data)) { |
|
101 |
PropertiesPage page = createPage(factory, container, data); |
|
102 |
if (currentUser.isAuthorized(authorizationId,page, page.getTitle()) ) { |
|
103 |
pages.add(page); |
|
104 |
} |
|
105 |
} |
|
106 |
} catch(Throwable ex) { |
|
107 |
logger.warn("Can't get properties page from factory '"+factoryName+"'.",ex); |
|
108 |
} |
|
109 |
} |
|
110 |
Collections.sort(pages, new Comparator<PropertiesPage>() { |
|
111 |
@Override |
|
112 |
public int compare(PropertiesPage f1, PropertiesPage f2) { |
|
113 |
int n = f2.getPriority() - f1.getPriority(); |
|
114 |
if (n != 0) { |
|
115 |
return n; |
|
116 |
} |
|
117 |
return StringUtils.compareIgnoreCase(f1.getTitle(), f2.getTitle()); |
|
118 |
} |
|
119 |
}); |
|
120 |
return pages; |
|
121 |
} |
|
122 |
|
|
123 |
private PropertiesPage createPage(PropertiesPageFactory factory, Object container, Object data) |
|
124 |
throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException |
|
125 |
{ |
|
126 |
PropertiesPage page; |
|
127 |
try { |
|
128 |
page = factory.create(container, data); |
|
129 |
return page; |
|
130 |
} catch(AbstractMethodError ex) { |
|
131 |
} |
|
132 |
// Antes factory.create llevaba solo un argumento, data. Probamos |
|
133 |
// invocando a ese create. |
|
134 |
Method method = factory.getClass().getMethod("create", Object.class); |
|
135 |
page = (PropertiesPage) method.invoke(factory, data); |
|
136 |
return page; |
|
137 |
} |
|
138 |
|
|
139 |
@Override |
|
140 |
public ActionEvent createSetPageEnableEvent(PropertiesPage source, Invocable filter, boolean enabled) { |
Also available in: Unified diff