Revision 2611
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.296/org.gvsig.tools.util/pom.xml | ||
---|---|---|
1 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
2 |
<modelVersion>4.0.0</modelVersion> |
|
3 |
<parent> |
|
4 |
<groupId>org.gvsig</groupId> |
|
5 |
<artifactId>org.gvsig.tools</artifactId> |
|
6 |
<version>3.0.296</version> |
|
7 |
</parent> |
|
8 |
<artifactId>org.gvsig.tools.util</artifactId> |
|
9 |
<name>org.gvsig.tools.util</name> |
|
10 |
<packaging>pom</packaging> |
|
11 |
<modules> |
|
12 |
<module>org.gvsig.tools.util.api</module> |
|
13 |
<module>org.gvsig.tools.util.impl</module> |
|
14 |
</modules> |
|
15 |
</project> |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.296/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.296/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.296/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.296/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.296/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.296/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.texteditor.DefaultTextEditorManager; |
|
34 |
import org.gvsig.tools.library.AbstractLibrary; |
|
35 |
import org.gvsig.tools.library.Library; |
|
36 |
import org.gvsig.tools.library.LibraryException; |
|
37 |
import org.gvsig.tools.util.ToolsUtilLibrary; |
|
38 |
import org.gvsig.tools.util.ToolsUtilLocator; |
|
39 |
import org.gvsig.webbrowser2.cssbox.CSSBoxWebBrowserManager; |
|
40 |
import org.gvsig.webbrowser2.impl.DefaultWebBrowserManager; |
|
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 |
ToolsUtilLocator.registerDefaultTextEditorManager(DefaultTextEditorManager.class); |
|
64 |
|
|
65 |
ToolsUtilLocator.registerDefaultWebBrowserManager(DefaultWebBrowserManager.class); |
|
66 |
ToolsUtilLocator.registerWebBrowserManager(CSSBoxWebBrowserManager.class); |
|
67 |
|
|
68 |
ToolsUtilLocator.registerEuclideanManager(DefaultEuclideanManager.class); |
|
69 |
|
|
70 |
ToolsUtilLocator.registerJsonManager(JsonManagerImpl.class); |
|
71 |
|
|
72 |
ToolsUtilLocator.registerSimpleServerManager(SimpleServerManagerImpl.class); |
|
73 |
} |
|
74 |
|
|
75 |
@Override |
|
76 |
protected void doPostInitialize() throws LibraryException { |
|
77 |
|
|
78 |
} |
|
79 |
|
|
80 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.296/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.296/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 org.apache.commons.lang3.StringUtils; |
|
13 |
|
|
14 |
|
|
15 |
public class DefaultConfigurableActionsMamager implements ConfigurableActionsMamager { |
|
16 |
|
|
17 |
private class PanelActions { |
|
18 |
List<Action> actions; |
|
19 |
WeakReference<ActionsComponent> component; |
|
20 |
private final String panelid; |
|
21 |
|
|
22 |
public PanelActions(String panelid) { |
|
23 |
this.panelid = panelid; |
|
24 |
this.actions = new ArrayList<>(); |
|
25 |
} |
|
26 |
|
|
27 |
public void setComponent(ActionsComponent component) { |
|
28 |
this.component = new WeakReference<>(component); |
|
29 |
} |
|
30 |
|
|
31 |
public ActionsComponent getComponent() { |
|
32 |
if( this.component == null ) { |
|
33 |
return null; |
|
34 |
} |
|
35 |
ActionsComponent component = this.component.get(); |
|
36 |
return component; |
|
37 |
} |
|
38 |
|
|
39 |
public void updateComponent() { |
|
40 |
ActionsComponent component = this.getComponent(); |
|
41 |
if( component == null ) { |
|
42 |
return; |
|
43 |
} |
|
44 |
component.update(actions); |
|
45 |
} |
|
46 |
|
|
47 |
public int size() { |
|
48 |
return this.actions.size(); |
|
49 |
} |
|
50 |
|
|
51 |
public Action get(int i) { |
|
52 |
return this.actions.get(i); |
|
53 |
} |
|
54 |
|
|
55 |
public void set(int i, Action action) { |
|
56 |
this.actions.set(i, action); |
|
57 |
this.updateComponent(); |
|
58 |
} |
|
59 |
|
|
60 |
public void add(Action action) { |
|
61 |
this.actions.add(action); |
|
62 |
this.updateComponent(); |
|
63 |
} |
|
64 |
|
|
65 |
public void remove(int i) { |
|
66 |
this.actions.remove(i); |
|
67 |
this.updateComponent(); |
|
68 |
} |
|
69 |
|
|
70 |
public Collection<Action> getActions() { |
|
71 |
return this.actions; |
|
72 |
} |
|
73 |
|
|
74 |
private void removeAll() { |
|
75 |
this.actions.clear(); |
|
76 |
this.updateComponent(); |
|
77 |
} |
|
78 |
|
|
79 |
} |
|
80 |
|
|
81 |
private Map<String,PanelActions> panelsActions; |
|
82 |
|
|
83 |
|
|
84 |
public DefaultConfigurableActionsMamager() { |
|
85 |
this.panelsActions = new HashMap<>(); |
|
86 |
} |
|
87 |
|
|
88 |
@Override |
|
89 |
public void addConfigurableAction(String panelid, Action action) { |
|
90 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
91 |
if( panelActions==null ) { |
|
92 |
panelActions = new PanelActions(panelid); |
|
93 |
this.panelsActions.put(panelid, panelActions); |
|
94 |
} |
|
95 |
for( int i=0; i<panelActions.size(); i++ ) { |
|
96 |
Action currentAction = panelActions.get(i); |
|
97 |
if( StringUtils.equalsIgnoreCase( |
|
98 |
(String)currentAction.getValue(Action.ACTION_COMMAND_KEY), |
|
99 |
(String)action.getValue(Action.ACTION_COMMAND_KEY) ) ) { |
|
100 |
panelActions.set(i, action); |
|
101 |
return; |
|
102 |
} |
|
103 |
} |
|
104 |
panelActions.add(action); |
|
105 |
} |
|
106 |
|
|
107 |
@Override |
|
108 |
public void removeConfigurableAction(String panelid, String actionName) { |
|
109 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
110 |
if( panelActions==null ) { |
|
111 |
return; |
|
112 |
} |
|
113 |
for( int i=0; i<panelActions.size(); i++ ) { |
|
114 |
Action currentAction = panelActions.get(i); |
|
115 |
if( StringUtils.equalsIgnoreCase( |
|
116 |
(String)currentAction.getValue(Action.ACTION_COMMAND_KEY), |
|
117 |
actionName ) ) { |
|
118 |
panelActions.remove(i); |
|
119 |
return; |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
@Override |
|
125 |
public void removeAllConfigurableActions(String panelid) { |
|
126 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
127 |
if( panelActions==null ) { |
|
128 |
return; |
|
129 |
} |
|
130 |
panelActions.removeAll(); |
|
131 |
} |
|
132 |
|
|
133 |
@Override |
|
134 |
public Collection<Action> getConfigurableActions(String panelid) { |
|
135 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
136 |
if( panelActions==null ) { |
|
137 |
panelActions = new PanelActions(panelid); |
|
138 |
this.panelsActions.put(panelid, panelActions); |
|
139 |
} |
|
140 |
return panelActions.getActions(); |
|
141 |
} |
|
142 |
|
|
143 |
@Override |
|
144 |
public JComponent getConfigurableActionsComponent(String panelid, Object source) { |
|
145 |
PanelActions panelActions = this.panelsActions.get(panelid); |
|
146 |
if( panelActions==null ) { |
|
147 |
panelActions = new PanelActions(panelid); |
|
148 |
this.panelsActions.put(panelid, panelActions); |
|
149 |
} |
|
150 |
ActionsComponent component = new ActionsComponent(panelActions.getActions(), source); |
|
151 |
panelActions.setComponent(component); |
|
152 |
return component; |
|
153 |
} |
|
154 |
|
|
155 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.296/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/configurableactions/ActionsComponent.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.Collection; |
|
9 |
import javax.swing.AbstractAction; |
|
10 |
import javax.swing.Action; |
|
11 |
import javax.swing.ImageIcon; |
|
12 |
import javax.swing.JLabel; |
|
13 |
import javax.swing.JMenuItem; |
|
14 |
import javax.swing.JPopupMenu; |
|
15 |
import org.gvsig.tools.ToolsLocator; |
|
16 |
import org.gvsig.tools.i18n.I18nManager; |
|
17 |
import org.gvsig.tools.swing.api.SupportIsEnable; |
|
18 |
import org.gvsig.tools.swing.api.SupportIsVisible; |
|
19 |
|
|
20 |
|
|
21 |
public class ActionsComponent extends JLabel { |
|
22 |
|
|
23 |
private final Object source; |
|
24 |
private Collection<Action> actions; |
|
25 |
|
|
26 |
private class ActionListenerAdapter extends AbstractAction { |
|
27 |
|
|
28 |
private final Action action; |
|
29 |
|
|
30 |
public ActionListenerAdapter(Action action) { |
|
31 |
this.action = action; |
|
32 |
} |
|
33 |
|
|
34 |
@Override |
|
35 |
public void actionPerformed(ActionEvent e) { |
|
36 |
e.setSource(source); |
|
37 |
action.actionPerformed(e); |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public Object getValue(String key) { |
|
42 |
return action.getValue(key); |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
public void putValue(String key, Object newValue) { |
|
47 |
action.putValue(key, newValue); |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public boolean isEnabled() { |
|
52 |
return action.isEnabled(); |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public void setEnabled(boolean newValue) { |
|
57 |
action.setEnabled(newValue); |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
|
62 |
public ActionsComponent(Collection<Action> actions, Object source) { |
|
63 |
this.source = source; |
|
64 |
this.update(actions); |
|
65 |
this.initComponent(); |
|
66 |
} |
|
67 |
|
|
68 |
private void initComponent() { |
|
69 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
70 |
this.setToolTipText(i18n.getTranslation("_Click_to_see_more_action_in_this_panel")); |
|
71 |
URL url = this.getClass().getResource("/org/gvsig/configurableactions/images/configurableactions.png"); |
|
72 |
this.setIcon(new ImageIcon(url)); |
|
73 |
this.setBorder(null); |
|
74 |
this.addMouseListener(new MouseAdapter() { |
|
75 |
@Override |
|
76 |
public void mouseClicked(MouseEvent e) { |
|
77 |
if( e.getClickCount()==1 && e.getButton()==MouseEvent.BUTTON1 ) { |
|
78 |
doShowMenu(); |
|
79 |
} |
|
80 |
} |
|
81 |
}); |
|
82 |
// this.addActionListener(new ActionListener() { |
|
83 |
// @Override |
|
84 |
// public void actionPerformed(ActionEvent e) { |
|
85 |
// doShowMenu(); |
|
86 |
// } |
|
87 |
// }); |
|
88 |
} |
|
89 |
|
|
90 |
public void update(Collection<Action> actions) { |
|
91 |
this.actions = actions; |
|
92 |
if( actions==null || actions.isEmpty() ) { |
|
93 |
this.setVisible(false); |
|
94 |
} else { |
|
95 |
this.setVisible(true); |
|
96 |
} |
|
97 |
} |
|
98 |
|
|
99 |
private void doShowMenu() { |
|
100 |
JPopupMenu menu = new JPopupMenu(); |
|
101 |
SupportIsVisible visible = null; |
|
102 |
if( this.source instanceof SupportIsVisible ) { |
|
103 |
visible = (SupportIsVisible) this.source; |
|
104 |
} |
|
105 |
SupportIsEnable enabled = null; |
|
106 |
if( this.source instanceof SupportIsEnable ) { |
|
107 |
enabled = (SupportIsEnable) this.source; |
|
108 |
} |
|
109 |
for (Action action : actions) { |
|
110 |
if( visible==null || visible.isVisible(action) ) { |
|
111 |
JMenuItem item = new JMenuItem(new ActionListenerAdapter(action)); |
|
112 |
if( enabled!=null && !enabled.isEnabled(action)) { |
|
113 |
item.setEnabled(false); |
|
114 |
} |
|
115 |
menu.add(item); |
|
116 |
} |
|
117 |
} |
|
118 |
menu.show(this, 0,this.getHeight()); |
|
119 |
} |
|
120 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.296/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) { |
|
141 |
return new DefaultSetPageEnabledEvent(source, filter, enabled); |
|
142 |
} |
|
143 |
|
|
144 |
@Override |
|
145 |
public ActionEvent createSetPageEnableEvent(final PropertiesPage source, final Class filterClass, final boolean enabled) { |
|
146 |
return new DefaultSetPageEnabledEvent( |
|
147 |
source, |
|
148 |
new Invocable() { |
|
149 |
@Override |
|
150 |
public Object call(Object... propertiesPages) { |
|
151 |
return filterClass.isInstance(propertiesPages[0]); |
|
152 |
} |
|
153 |
}, |
|
154 |
enabled |
|
155 |
); |
|
156 |
} |
|
157 |
|
|
158 |
} |
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.296/org.gvsig.tools.util/org.gvsig.tools.util.impl/src/main/java/org/gvsig/filedialogchooser/impl/VFSJFileChooserBased.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.filedialogchooser.impl; |
|
7 |
|
|
8 |
import com.googlecode.vfsjfilechooser2.VFSJFileChooser; |
|
9 |
import com.googlecode.vfsjfilechooser2.filechooser.AbstractVFSFileFilter; |
|
10 |
import java.awt.Component; |
|
11 |
import java.beans.PropertyChangeListener; |
|
12 |
import java.io.File; |
|
13 |
import javax.swing.JComponent; |
|
14 |
import javax.swing.filechooser.FileFilter; |
|
15 |
import org.apache.commons.vfs2.FileObject; |
|
16 |
import org.apache.commons.vfs2.provider.local.LocalFile; |
|
17 |
import org.gvsig.filedialogchooser.FileDialogChooser; |
|
18 |
import org.gvsig.filedialogchooser.FileDialogChooserManager; |
|
19 |
import org.gvsig.tools.util.ToolsUtilLocator; |
|
20 |
|
|
21 |
|
|
22 |
public class VFSJFileChooserBased implements FileDialogChooser { |
|
23 |
|
|
24 |
private static class FileFilterAdapter extends AbstractVFSFileFilter { |
|
25 |
|
|
26 |
private final FileFilter filter; |
|
27 |
|
|
28 |
FileFilterAdapter(FileFilter filter) { |
|
29 |
this.filter = filter; |
|
30 |
} |
|
31 |
|
|
32 |
@Override |
|
33 |
public boolean accept(FileObject f) { |
|
34 |
if( f instanceof LocalFile ) { |
|
35 |
return this.filter.accept( new File(f.getName().getPath())); |
|
36 |
} |
|
37 |
return false; |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public String getDescription() { |
|
42 |
return this.filter.getDescription(); |
|
43 |
} |
|
44 |
|
|
45 |
public FileFilter getFilter() { |
|
46 |
return this.filter; |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
private final VFSJFileChooser jfc; |
|
51 |
private final String fileChooserID; |
|
52 |
|
|
53 |
public VFSJFileChooserBased(String fileChooserID) { |
|
54 |
super(); |
|
55 |
this.jfc = new VFSJFileChooser(); |
|
56 |
this.fileChooserID = fileChooserID; |
|
57 |
} |
|
58 |
|
|
59 |
@Override |
|
60 |
public void setCurrentDirectory(File dir) { |
|
61 |
if( dir == null ) { |
|
62 |
dir = new File(System.getProperty("user.home")); |
|
63 |
} |
|
64 |
this.jfc.setCurrentDirectory(dir); |
|
65 |
} |
|
66 |
|
|
67 |
@Override |
|
68 |
public File getCurrentDirectory() { |
|
69 |
return this.jfc.getCurrentDirectory(); |
|
70 |
} |
|
71 |
|
|
72 |
@Override |
|
73 |
public void setDialogTitle(String dialogTitle) { |
|
74 |
this.jfc.setDialogTitle(dialogTitle); |
|
75 |
} |
|
76 |
|
|
77 |
@Override |
|
78 |
public void setDialogType(int dialogType) { |
|
79 |
switch(dialogType) { |
|
80 |
default: |
|
81 |
case OPEN_DIALOG: |
|
82 |
this.jfc.setDialogType(VFSJFileChooser.DIALOG_TYPE.OPEN); |
|
83 |
break; |
|
84 |
case SAVE_DIALOG: |
|
85 |
this.jfc.setDialogType(VFSJFileChooser.DIALOG_TYPE.SAVE); |
|
86 |
break; |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public void setDragEnabled(boolean b) { |
|
92 |
this.jfc.setDragEnabled(b); |
|
93 |
} |
|
94 |
|
|
95 |
@Override |
|
96 |
public void setFileFilter(FileFilter filter) { |
|
97 |
if( filter == null ) { |
|
98 |
this.jfc.setFileFilter(null); |
|
99 |
} else { |
|
100 |
this.jfc.setFileFilter(new FileFilterAdapter(filter)); |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
@Override |
|
105 |
public FileFilter getFileFilter() { |
|
106 |
FileFilterAdapter f = (FileFilterAdapter) this.jfc.getFileFilter(); |
|
107 |
return f.getFilter(); |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public void setFileSelectionMode(int mode) { |
|
112 |
switch(mode) { |
|
113 |
case FILES_ONLY: |
|
114 |
this.jfc.setFileSelectionMode(VFSJFileChooser.SELECTION_MODE.FILES_ONLY); |
|
115 |
break; |
|
116 |
case DIRECTORIES_ONLY: |
|
117 |
this.jfc.setFileSelectionMode(VFSJFileChooser.SELECTION_MODE.DIRECTORIES_ONLY); |
|
118 |
break; |
|
119 |
default: |
|
120 |
case FILES_AND_DIRECTORIES: |
|
121 |
this.jfc.setFileSelectionMode(VFSJFileChooser.SELECTION_MODE.FILES_AND_DIRECTORIES); |
|
122 |
break; |
|
123 |
} |
|
124 |
} |
|
125 |
|
|
126 |
@Override |
|
127 |
public File getSelectedFile() { |
|
128 |
return this.jfc.getSelectedFile(); |
|
129 |
} |
|
130 |
|
Also available in: Unified diff