1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.webphotos.util;
17
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.PrintStream;
21 import java.util.StringTokenizer;
22 import javax.swing.JOptionPane;
23 import javax.swing.JTable;
24 import javax.swing.JTextArea;
25 import javax.swing.table.DefaultTableColumnModel;
26 import javax.swing.table.TableColumn;
27 import javax.swing.table.TableColumnModel;
28 import org.apache.commons.configuration.CombinedConfiguration;
29 import org.apache.commons.configuration.Configuration;
30 import org.apache.commons.configuration.DefaultConfigurationBuilder;
31 import org.apache.commons.configuration.XMLConfiguration;
32 import org.apache.log4j.Logger;
33
34
35
36
37
38
39
40
41 @SuppressWarnings("StaticNonFinalUsedInInitialization")
42 public class Util {
43
44 private static final String WEBPHOTOS_USER_CONFIG = "webphotos.xml";
45
46
47
48 public static final String WEBPHOTOS_DEFAULT_CONFIG = "webphotos.dat";
49 private static Util instancia = new Util();
50
51
52
53 private static JTextArea saida;
54 private static File albunsRoot = null;
55 private static Configuration config;
56
57
58
59 public static PrintStream out;
60
61
62
63 public static PrintStream err;
64 private static final Logger log;
65
66 static {
67 out = System.out;
68 err = System.err;
69 log = Logger.getLogger(Util.class);
70
71 DefaultConfigurationBuilder configurationBuilder = new DefaultConfigurationBuilder();
72 String userHome = "";
73 try {
74 configurationBuilder.setFileName("Configuration.xml");
75 CombinedConfiguration auxConfig = configurationBuilder.getConfiguration(true);
76 config = auxConfig.interpolatedConfiguration();
77 userHome = config.getString("user.home");
78 } catch (Exception e) {
79 log.error("Can't load preferences");
80 log.debug("Stack Trace : ", e);
81 System.exit(-1);
82 }
83 configurationBuilder = new DefaultConfigurationBuilder();
84 try {
85 configurationBuilder.setFileName("SavedConfiguration.xml");
86 final CombinedConfiguration configuration = configurationBuilder.getConfiguration(true);
87 XMLConfiguration savedUserPrefs = new XMLConfiguration();
88 savedUserPrefs.append(configuration);
89 savedUserPrefs.setEncoding("ISO-8859-1");
90 savedUserPrefs.save(new FileOutputStream(userHome + File.separatorChar + WEBPHOTOS_USER_CONFIG));
91 } catch (Exception e) {
92 log.warn("Can't save preferences");
93 log.debug("Stack Trace : ", e);
94 }
95 }
96
97
98
99
100
101 public static Configuration getConfig() {
102 return config;
103 }
104
105 private Util() {
106 }
107
108
109
110
111
112
113 public static Util getInstance() {
114 return instancia;
115 }
116
117
118
119
120
121
122
123
124
125
126
127 public static File getAlbunsRoot() {
128 if (albunsRoot == null) {
129 albunsRoot = new File(getProperty("albunsRoot"));
130 if (!albunsRoot.isDirectory()) {
131 StringBuilder errMsg = new StringBuilder();
132 errMsg.append("O diretório fornecido no parâmetro albunsRoot (arquivo de configuração)\n");
133 errMsg.append("não pode ser utilizado, ou não existe.\n");
134 errMsg.append("O programa será encerrado.");
135 JOptionPane.showMessageDialog(null, errMsg.toString(), "Erro no arquivo de configuração", JOptionPane.ERROR_MESSAGE);
136
137 System.exit(-1);
138 }
139 }
140 return albunsRoot;
141 }
142
143
144
145
146
147
148
149
150
151
152
153 public static File getFolder(String param) {
154 File folder = new File(getProperty(param));
155 if (!folder.isDirectory()) {
156 StringBuilder errMsg = new StringBuilder();
157 errMsg.append("O diretório fornecido no parâmetro albunsRoot (arquivo de configuração)\n");
158 errMsg.append("não pode ser utilizado, ou não existe.\n");
159 errMsg.append("O programa será encerrado.");
160 JOptionPane.showMessageDialog(null, errMsg.toString(), "Erro no arquivo de configuração", JOptionPane.ERROR_MESSAGE);
161
162 System.exit(-1);
163 }
164 return folder;
165 }
166
167
168
169
170
171
172
173
174
175 public static void log(String texto) {
176 if (texto == null) {
177 saida = null;
178 }
179 if (saida == null) {
180 log.info("LOG: " + texto);
181 } else {
182 if (texto.startsWith("[")) {
183 Util.err.println(texto);
184 texto = texto.substring(texto.indexOf('/') + 1);
185 }
186 saida.append(texto);
187 saida.append("\n");
188 saida.setCaretPosition(saida.getText().length() - 1);
189 }
190 }
191
192
193
194
195
196
197
198
199 public static String stringToHtm(String valor) {
200 valor = valor.replaceAll("\n", "<br>");
201 valor = valor.replaceAll("\"", """);
202 valor = valor.replaceAll("\'", """);
203 return "\'" + valor + "\'";
204 }
205
206
207
208
209
210
211 public static void setLoggingTextArea(JTextArea saidaGUI) {
212 saida = saidaGUI;
213 }
214
215
216
217
218
219
220
221
222
223 public static String getProperty(String chave) {
224 try {
225 return (config.getString(chave) == null || config.getString(chave).isEmpty()) ? null : config.getString(chave);
226 } catch (Exception e) {
227 log.error("Error trying to get a property", e);
228 return null;
229 }
230 }
231
232
233
234
235
236
237
238 public static void ajustaLargura(JTable tabela, String parametros) {
239 int temR = -1;
240 TableColumnModel modeloColunas = tabela.getColumnModel();
241 if (parametros == null) {
242 return;
243 }
244 if (parametros.length() > 0) {
245 StringTokenizer tok = new StringTokenizer(parametros, ",");
246 int ct = 0;
247 String l;
248 while (tok.hasMoreTokens()) {
249 l = tok.nextToken();
250 try {
251 modeloColunas.getColumn(ct).setPreferredWidth(Integer.parseInt(l));
252 } catch (NumberFormatException nE) {
253 if (l.equals("*")) {
254 log.info("Packing column " + ct);
255 packColumn(tabela, ct, 1);
256 } else if (l.equals("R")) {
257 temR = ct;
258 }
259 } catch (Exception e) {
260 }
261 ct++;
262 }
263
264 if (temR > 0) {
265 modeloColunas.getColumn(temR).setPreferredWidth(modeloColunas.getColumn(temR).getPreferredWidth() + tabela.getWidth() - modeloColunas.getTotalColumnWidth());
266 log.debug("Tamanho da tabela: " + (modeloColunas.getColumn(temR).getPreferredWidth() + tabela.getWidth() - modeloColunas.getTotalColumnWidth()));
267 }
268
269
270 log.debug("Tamanho Total: " + modeloColunas.getTotalColumnWidth());
271 log.debug("Tamanho da tabela: " + tabela.getWidth());
272 }
273 }
274
275
276
277
278
279
280
281
282
283
284
285 public static void packColumn(JTable table, int vColIndex, int margin) {
286 DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
287 TableColumn col = colModel.getColumn(vColIndex);
288 int width;
289
290
291 javax.swing.table.TableCellRenderer renderer = col.getHeaderRenderer();
292 if (renderer == null) {
293 renderer = table.getTableHeader().getDefaultRenderer();
294 }
295 java.awt.Component comp = renderer.getTableCellRendererComponent(
296 table, col.getHeaderValue(), false, false, 0, 0);
297 width = comp.getPreferredSize().width;
298
299
300 for (int r = 0; r < table.getRowCount(); r++) {
301 renderer = table.getCellRenderer(r, vColIndex);
302 comp = renderer.getTableCellRendererComponent(
303 table, table.getValueAt(r, vColIndex), false, false, r, vColIndex);
304 width = Math.max(width, comp.getPreferredSize().width);
305 }
306
307
308 width += 2 * margin;
309
310
311 col.setPreferredWidth(width);
312 }
313
314 @Override
315 public Object clone() throws CloneNotSupportedException {
316 throw new CloneNotSupportedException("Singleton Object");
317 }
318
319
320
321
322 public static void loadSocksProxy() {
323 String socksHost = getConfig().getString("socks.host");
324 int socksPort = 0;
325 if (getConfig().containsKey("socks.port")) {
326 socksPort = getConfig().getInt("socks.port");
327 }
328
329 if (socksHost != null && !socksHost.isEmpty()) {
330 System.getProperties().put("socksProxyHost", socksHost);
331 if (socksPort > 0 && socksPort < 65534) {
332 System.getProperties().put("socksProxyPort", socksPort);
333 }
334 }
335 }
336 }