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