Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.about / org.gvsig.about.impl / src / main / java / org / gvsig / about / impl / AbstractParticipant.java @ 42024

History | View | Annotate | Download (7.66 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.about.impl;
23

    
24
import java.net.URL;
25
import java.text.SimpleDateFormat;
26
import java.util.ArrayList;
27
import java.util.Calendar;
28
import java.util.Collections;
29
import java.util.Comparator;
30
import java.util.Date;
31
import java.util.HashMap;
32
import java.util.Iterator;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.regex.Pattern;
36

    
37
import org.gvsig.about.AboutContribution;
38
import org.gvsig.about.AboutManager;
39
import org.gvsig.about.AboutParticipant;
40

    
41
/**
42
 * @author gvSIG Team
43
 * @version $Id$
44
 * 
45
 */
46
public class AbstractParticipant implements AboutParticipant {
47

    
48
    private String name;
49
    private URL description;
50
    private int priority;
51
    private URL icon;
52
    protected List<AboutContribution> contributions;
53
    private DefaultAboutManager manager;
54
    private Map<String, String> vars;
55

    
56
    public AbstractParticipant(AboutManager manager, String name,
57
        URL description, int priority, URL icon) {
58
        this.manager = (DefaultAboutManager) manager;
59
        this.name = name;
60
        this.description = description;
61
        this.priority = priority;
62
        this.icon = icon;
63
        this.contributions = new ArrayList<AboutContribution>();
64
        this.vars = new HashMap<String, String>();
65
    }
66

    
67
    public void addVariables(Map<String, String> vars) {
68
        this.vars.putAll(vars);
69
    }
70

    
71
    public void set(String name, String value) {
72
        this.vars.put(name, value);
73
    }
74

    
75
    public String getName() {
76
        return this.name;
77
    }
78

    
79
    public URL getDescription() {
80
        return this.description;
81
    }
82

    
83
    public int getPriority() {
84
        return this.priority;
85
    }
86

    
87
    @Override
88
    public String toString() {
89
        return this.name;
90
    }
91

    
92
    public URL getIcon() {
93
        return this.icon;
94
    }
95

    
96
    public AboutManager getManager() {
97
        return this.manager;
98
    }
99

    
100
    protected Date getDate(int year, int month, int day) {
101
        Calendar cal = Calendar.getInstance();
102
        cal.set(year, month, day);
103
        return cal.getTime();
104
    }
105

    
106
    public AboutContribution addContribution(String title, String description,
107
        int begin_year, int begin_month, int begin_day, int end_year,
108
        int end_month, int end_day) {
109
        return this.addContribution(title, description,
110
            getDate(begin_year, begin_month, begin_day),
111
            getDate(end_year, end_month, end_day));
112
    }
113

    
114
    public AboutContribution addContribution(String title, String description,
115
        Date begin, Date end) {
116
        AboutContribution contribution =
117
            new DefaultAboutContribution(this, title, description, begin, end);
118
        // AƱadimos a la lista de contribuciones
119
        this.contributions.add(contribution);
120
        // Ordenamos por fecha de inicio
121
        Collections.sort(this.contributions,
122
            new Comparator<AboutContribution>() {
123

    
124
                public int compare(AboutContribution p1, AboutContribution p2) {
125
                    if (p1.getBegin().before(p2.getBegin())) {
126
                        return -1;
127
                    } else
128
                        if (p1.getBegin().after(p2.getBegin())) {
129
                            return 1;
130
                        } else {
131
                            return 0;
132
                        }
133
                }
134
            });
135

    
136
        return contribution;
137
    }
138

    
139
    public List<AboutContribution> getContributions() {
140
        return this.contributions;
141
    }
142

    
143
    public String getInformationPage() {
144
        String description = null;
145
        String contributionsTable = this.getContributionsTable();
146

    
147
        if (this.description != null) {
148
            description = manager.getStringFromUrl(this.description, this.vars);
149
        }
150
        if (description == null) {
151
            return "<html>\n" // Begin html
152
                + "<body>\n" + contributionsTable + "</body>\n" // Body
153
                + "</html>"; // End html
154
        }
155

    
156
        String baseTag =
157
            "<base href=\"" + manager.getURLBase(getDescription()) + "\"/>";
158

    
159
        int index = description.indexOf("<head>");
160
        if (index > -1) {
161
            description =
162
                description.substring(0, index) // HTML init
163
                    + "<head>"
164
                    + baseTag
165
                    + description.substring(index + "<head>".length());
166
        } else {
167
            index = description.indexOf("<html>");
168
            if (index > -1) {
169
                description =
170
                    description.substring(0, index + "<html>".length())
171
                        + "<head>" + baseTag + "</head>" // Head
172
                        + description.substring(index + "<html>".length());
173
            } else {
174
                description = "<head>" + baseTag + "</head>" // Head
175
                    + description;
176
            }
177
        }
178

    
179
        if (description.toLowerCase().indexOf("</body>") < 0) {
180
            return description + "\n" + contributionsTable;
181
        }
182
        return Pattern.compile("</body>", Pattern.CASE_INSENSITIVE)
183
            .matcher(description)
184
            .replaceFirst(contributionsTable + "\n</body>");
185
    }
186

    
187
    public String getContributionsTable() {
188
        List<AboutContribution> contributions = this.getContributions();
189
        if (contributions.size() < 1) {
190
            return "";
191
        }
192
        String table =
193
            "\t<div style=\"padding-top:5px\">\n"
194
                + "\t\t<div style=\"background-color: white;padding-top: 5px\">\n"
195
                + "\t\t\t<center><h3>Contribuciones realizadas</h3></center>\n"
196
                + "\t\t\t<br>\n"
197
                + "\t\t\t<table width=\"100%\" border=\"1\">\n"
198
                + "\t\t\t\t<tr>\n"
199
                + "\t\t\t\t\t<td valign=\"top\" align=\"center\">Nombre</td>\n"
200
                + "\t\t\t\t\t<td valign=\"top\" align=\"center\">Descripcion</td>\n"
201
                + "\t\t\t\t\t<td valign=\"top\" colspan=\"2\" align=\"center\">Periodo</td>\n"
202
                + "\t\t\t\t</tr>\n";
203
        Iterator<AboutContribution> it = contributions.iterator();
204
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
205
        while (it.hasNext()) {
206
            AboutContribution ap = it.next();
207
            String cont = "";
208
            if (ap.getDescription() != null) {
209
                cont = ap.getDescription();
210
            }
211

    
212
            table =
213
                table + "\t\t\t\t<tr>\n" + "\t\t\t\t\t <td valign=\"top\">"
214
                    + ap.getTitle() + "</td>\n"
215
                    + "\t\t\t\t\t <td valign=\"top\">" + cont + "</td>\n"
216
                    + "\t\t\t\t\t  <td valign=\"top\">"
217
                    + formatter.format(ap.getBegin().getTime()) + "</td>\n"
218
                    + "\t\t\t\t\t  <td valign=\"top\">"
219
                    + formatter.format(ap.getEnd().getTime()) + "</td>\n"
220
                    + "\t\t\t\t</tr>\n";
221
        }
222
        table = table + "\t\t\t</table>\n\t\t</div>\n\t</div>\n";
223
        return table;
224
    }
225

    
226
}