Debian Codegraph @

netx/net/sourceforge/jnlp/splashscreen/parts/InformationElement.java

raw
1 /* InformationElement.java
2 Copyright (C) 2012 Red Hat, Inc.
3
4 This file is part of IcedTea.
5
6 IcedTea is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 IcedTea is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with IcedTea; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37 /**
38 http://docs.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/syntax.html
39 */
40 package net.sourceforge.jnlp.splashscreen.parts;
41
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
45 import net.sourceforge.jnlp.InformationDesc;
46 import net.sourceforge.jnlp.JNLPFile;
47 import net.sourceforge.jnlp.runtime.Translator;
48 import net.sourceforge.jnlp.util.logging.OutputController;
49
50 /**
51 * This class is wrapper arround the *information* tag element which should
52 * javaws provide from source jnlp file
53 */
54 public class InformationElement {
55
56 private InfoItem title;
57 private InfoItem vendor;
58 private InfoItem homepage;
59 private List<DescriptionInfoItem> descriptions = new ArrayList<DescriptionInfoItem>(5);
60
61 public void setTitle(String title) {
62 if (title == null) {
63 return;
64 }
65 this.title = new InfoItem(InfoItem.title, title);
66 }
67
68 public void setvendor(String vendor) {
69 if (vendor == null) {
70 return;
71 }
72 this.vendor = new InfoItem(InfoItem.vendor, vendor);
73 }
74
75 public void setHomepage(String homepage) {
76 if (homepage == null) {
77 return;
78 }
79 this.homepage = new InfoItem(InfoItem.homepage, homepage);
80 }
81
82 public void addDescription(String description) {
83 addDescription(description, null);
84 }
85
86 /**
87 * Just one description of each kind (4 including null) are allowed in information element.
88 * This method should throw exception when trying to add second description of same kind
89 * But I do not consider it as good idea to force this behaviour for somesing like psalsh screen,
90 * so I jsut replace the previous one with new one. without any warning
91 */
92 public void addDescription(String description, String kind) {
93 if (description == null) {
94 return;
95 }
96 DescriptionInfoItem d = new DescriptionInfoItem(description, kind);
97 for (DescriptionInfoItem descriptionInfoItem : descriptions) {
98 if (descriptionInfoItem.isOfSameKind(d)) {
99 descriptions.remove(descriptionInfoItem);
100 descriptions.add(d);
101 return;
102 }
103 }
104 descriptions.add(d);
105
106 }
107
108 public InfoItem getBestMatchingDescriptionForSplash() {
109 for (DescriptionInfoItem d : descriptions) {
110 if (InfoItem.descriptionKindOneLine.equals(d.getKind())) {
111 return d;
112 }
113 }
114 for (DescriptionInfoItem d : descriptions) {
115 if (d.getKind() == null) {
116 return d;
117 }
118 }
119 return null;
120 }
121
122 public InfoItem getLongestDescriptionForSplash() {
123 for (DescriptionInfoItem d : descriptions) {
124 if (InfoItem.descriptionKindShort.equals(d.getKind())) {
125 return d;
126 }
127 }
128 for (DescriptionInfoItem d : descriptions) {
129 if (d.getKind() == null) {
130 return d;
131 }
132 }
133 for (DescriptionInfoItem d : descriptions) {
134 if (InfoItem.descriptionKindOneLine.equals(d.getKind())) {
135 return d;
136 }
137 }
138 for (DescriptionInfoItem d : descriptions) {
139 if (InfoItem.descriptionKindToolTip.equals(d.getKind())) {
140 return d;
141 }
142 }
143 return null;
144 }
145
146 public String getTitle() {
147 if (title == null) {
148 return null;
149 }
150 return title.toNiceString();
151 }
152
153 public String getVendor() {
154 if (vendor == null) {
155 return null;
156 }
157 return vendor.toNiceString();
158 }
159
160 public String getHomepage() {
161 if (homepage == null) {
162 return null;
163 }
164 return homepage.toNiceString();
165 }
166
167 List<DescriptionInfoItem> getDescriptions() {
168 return Collections.unmodifiableList(descriptions);
169 }
170
171 public String getDescription() {
172 InfoItem i = getBestMatchingDescriptionForSplash();
173 if (i == null) {
174 return null;
175 }
176 return i.toNiceString();
177 }
178
179 public List<String> getHeader() {
180 List<String> r = new ArrayList<String>(4);
181 String t = getTitle();
182 String v = getVendor();
183 String h = getHomepage();
184 if (t != null) {
185 r.add(t);
186 }
187 if (v != null) {
188 r.add(v);
189 }
190 if (h != null) {
191 r.add(h);
192 }
193
194 return r;
195 }
196
197 public static InformationElement createFromJNLP(JNLPFile file) {
198 try {
199 if (file == null) {
200 String message = Translator.R(InfoItem.SPLASH + "errorInInformation");
201 InformationElement ie = new InformationElement();
202 ie.setHomepage("");
203 ie.setTitle(message);
204 ie.setvendor("");
205 ie.addDescription(message);
206 return ie;
207 }
208 if (file.getInformation() == null) {
209 String message = Translator.R(InfoItem.SPLASH + "missingInformation");
210 InformationElement ie = new InformationElement();
211 ie.setHomepage("");
212 ie.setTitle(message);
213 ie.setvendor("");
214 ie.addDescription(message);
215 return ie;
216 }
217 InformationElement ie = new InformationElement();
218 String homePage = Translator.R(InfoItem.SPLASH + "defaultHomepage");
219 if (file.getInformation().getHomepage() != null) {
220 homePage = file.getInformation().getHomepage().toString();
221 }
222 ie.setHomepage(homePage);
223 ie.setTitle(file.getInformation().getTitle());
224 ie.setvendor(file.getInformation().getVendor());
225 ie.addDescription(file.getInformation().getDescriptionStrict((String) (InformationDesc.DEFAULT)));
226 ie.addDescription(file.getInformation().getDescriptionStrict(InfoItem.descriptionKindOneLine), InfoItem.descriptionKindOneLine);
227 ie.addDescription(file.getInformation().getDescriptionStrict(InfoItem.descriptionKindShort), InfoItem.descriptionKindShort);
228 ie.addDescription(file.getInformation().getDescriptionStrict(InfoItem.descriptionKindToolTip), InfoItem.descriptionKindToolTip);
229 return ie;
230 } catch (Exception ex) {
231 OutputController.getLogger().log(OutputController.Level.ERROR_ALL, ex);
232 String message = Translator.R(InfoItem.SPLASH + "errorInInformation");
233 InformationElement ie = new InformationElement();
234 ie.setHomepage("");
235 ie.setTitle(message);
236 ie.setvendor("");
237 ie.addDescription(ex.getMessage());
238 return ie;
239 }
240 }
241 }