1 32 33 package it.businesslogic.ireport.gui.locale; 34 import it.businesslogic.ireport.gui.*; 35 import it.businesslogic.ireport.*; 36 import javax.swing.tree.*; 37 import javax.swing.table.*; 38 import javax.swing.*; 39 import it.businesslogic.ireport.chart.*; 40 import java.util.*; 41 import java.io.*; 42 import javax.swing.event.*; 43 import it.businesslogic.ireport.util.I18n; 44 48 public class LocaleResourceFilesDialog extends javax.swing.JDialog { 49 50 private int dialogResult = javax.swing.JOptionPane.CANCEL_OPTION; 51 private JReportFrame jReportFrame = null; 52 53 54 public LocaleResourceFilesDialog(java.awt.Dialog parent, boolean modal) { 55 super(parent, modal); 56 initComponents(); 57 initFrame(); 58 } 59 60 public void initFrame() 61 { 62 javax.swing.DefaultListModel dlm = new javax.swing.DefaultListModel () ; 63 64 this.setSize(500,400); 65 it.businesslogic.ireport.util.Misc.centerFrame(this); 66 67 DefaultListSelectionModel dlsm = (DefaultListSelectionModel)this.jTable1.getSelectionModel(); 68 dlsm.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 69 public void valueChanged(ListSelectionEvent e) { 70 jTable1ListSelectionValueChanged(e); 71 } 72 }); 73 74 javax.swing.KeyStroke escape = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0, false); 75 javax.swing.Action escapeAction = new javax.swing.AbstractAction () { 76 public void actionPerformed(java.awt.event.ActionEvent e) { 77 jButtonCloseActionPerformed(e); 78 } 79 }; 80 81 getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE"); 82 getRootPane().getActionMap().put("ESCAPE", escapeAction); 83 84 applyI18n(); 85 this.getRootPane().setDefaultButton(this.jButtonClose); 87 } 88 89 public void jTable1ListSelectionValueChanged(javax.swing.event.ListSelectionEvent e) 90 { 91 if (this.jTable1.getSelectedRowCount() > 0) { 92 this.jButtonModifyFile.setEnabled(true); 93 this.jButtonDeleteFile.setEnabled(true); 94 } 95 else { 96 this.jButtonModifyFile.setEnabled(false); 97 this.jButtonDeleteFile.setEnabled(false); 98 } 99 } 100 101 public LocaleResourceFilesDialog(java.awt.Frame parent, boolean modal) { 102 super(parent, modal); 103 initComponents(); 104 initFrame(); 105 } 106 107 public void updateFileList() 108 { 109 DefaultTableModel dtm = (DefaultTableModel)jTable1.getModel(); 110 dtm.setRowCount(0); 111 if (getJReportFrame() == null) { jTable1.updateUI(); return; } 112 113 Report report = getJReportFrame().getReport(); 114 if (report.getFilename() == null || 116 report.getFilename().trim().equals("") ) 117 { 118 return; 119 } 120 121 if (report.getResourceBundleBaseName() == null || 122 report.getResourceBundleBaseName().trim().equals("")) 123 { 124 return; 125 } 126 else 127 { 128 String basename = report.getResourceBundleBaseName(); 129 File f = new File(report.getFilename()); 130 File dir = f.getParentFile(); 131 String [] files = dir.list(); 132 for (int i=0; i<files.length; ++i) 133 { 134 if (files[i].startsWith(basename) && 135 files[i].endsWith(".properties")) 136 { 137 dtm.addRow(new Object []{getLocaleName(basename, files[i]),files[i]}); 138 } 139 } 140 } 141 142 jTable1.updateUI(); 143 } 144 145 public JReportFrame getJReportFrame() { 146 147 return jReportFrame; 148 } 149 150 public void setJReportFrame(JReportFrame jReportFrame) { 151 152 this.jReportFrame = jReportFrame; 153 updateFileList(); 154 } 155 156 public int getDialogResult() { 157 return dialogResult; 158 } 159 160 public void setDialogResult(int dialogResult) { 161 this.dialogResult = dialogResult; 162 } 163 164 169 private void initComponents() { 171 java.awt.GridBagConstraints gridBagConstraints; 172 173 jPanelData = new javax.swing.JPanel (); 174 jScrollPane3 = new javax.swing.JScrollPane (); 175 jTable1 = new javax.swing.JTable (); 176 jPanel2 = new javax.swing.JPanel (); 177 jButtonCreateNewLocale = new javax.swing.JButton (); 178 jButtonModifyFile = new javax.swing.JButton (); 179 jButtonDeleteFile = new javax.swing.JButton (); 180 jPanel3 = new javax.swing.JPanel (); 181 jButtonClose = new javax.swing.JButton (); 182 183 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 184 setTitle("Report Imports"); 185 jPanelData.setLayout(new java.awt.GridBagLayout ()); 186 187 jTable1.setModel(new javax.swing.table.DefaultTableModel ( 188 new Object [][] { 189 190 }, 191 new String [] { 192 "Locale", "File name" 193 } 194 ) { 195 boolean[] canEdit = new boolean [] { 196 false, false 197 }; 198 199 public boolean isCellEditable(int rowIndex, int columnIndex) { 200 return canEdit [columnIndex]; 201 } 202 }); 203 jTable1.addMouseListener(new java.awt.event.MouseAdapter () { 204 public void mouseClicked(java.awt.event.MouseEvent evt) { 205 jTable1MouseClicked(evt); 206 } 207 }); 208 209 jScrollPane3.setViewportView(jTable1); 210 211 gridBagConstraints = new java.awt.GridBagConstraints (); 212 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 213 gridBagConstraints.weightx = 1.0; 214 gridBagConstraints.weighty = 1.0; 215 gridBagConstraints.insets = new java.awt.Insets (4, 4, 4, 4); 216 jPanelData.add(jScrollPane3, gridBagConstraints); 217 218 jPanel2.setLayout(new java.awt.GridBagLayout ()); 219 220 jPanel2.setMinimumSize(new java.awt.Dimension (140, 151)); 221 jPanel2.setPreferredSize(new java.awt.Dimension (180, 100)); 222 jButtonCreateNewLocale.setText("Create new locale"); 223 jButtonCreateNewLocale.addActionListener(new java.awt.event.ActionListener () { 224 public void actionPerformed(java.awt.event.ActionEvent evt) { 225 jButtonCreateNewLocaleActionPerformed(evt); 226 } 227 }); 228 229 gridBagConstraints = new java.awt.GridBagConstraints (); 230 gridBagConstraints.gridx = 0; 231 gridBagConstraints.gridy = 1; 232 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 233 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 234 gridBagConstraints.weightx = 1.0; 235 jPanel2.add(jButtonCreateNewLocale, gridBagConstraints); 236 237 jButtonModifyFile.setText(" Modify file"); 238 jButtonModifyFile.setActionCommand("Modify locale"); 239 jButtonModifyFile.setEnabled(false); 240 jButtonModifyFile.addActionListener(new java.awt.event.ActionListener () { 241 public void actionPerformed(java.awt.event.ActionEvent evt) { 242 jButtonModifyFileActionPerformed(evt); 243 } 244 }); 245 246 gridBagConstraints = new java.awt.GridBagConstraints (); 247 gridBagConstraints.gridx = 0; 248 gridBagConstraints.gridy = 4; 249 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 250 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 251 gridBagConstraints.weightx = 1.0; 252 gridBagConstraints.insets = new java.awt.Insets (4, 0, 0, 0); 253 jPanel2.add(jButtonModifyFile, gridBagConstraints); 254 255 jButtonDeleteFile.setText("Delete file"); 256 jButtonDeleteFile.setEnabled(false); 257 jButtonDeleteFile.addActionListener(new java.awt.event.ActionListener () { 258 public void actionPerformed(java.awt.event.ActionEvent evt) { 259 jButtonDeleteFileActionPerformed(evt); 260 } 261 }); 262 263 gridBagConstraints = new java.awt.GridBagConstraints (); 264 gridBagConstraints.gridx = 0; 265 gridBagConstraints.gridy = 5; 266 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 267 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 268 gridBagConstraints.weightx = 1.0; 269 gridBagConstraints.insets = new java.awt.Insets (4, 0, 0, 0); 270 jPanel2.add(jButtonDeleteFile, gridBagConstraints); 271 272 gridBagConstraints = new java.awt.GridBagConstraints (); 273 gridBagConstraints.gridx = 0; 274 gridBagConstraints.gridy = 10; 275 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 276 gridBagConstraints.weightx = 1.0; 277 gridBagConstraints.weighty = 1.0; 278 jPanel2.add(jPanel3, gridBagConstraints); 279 280 jButtonClose.setText("Close"); 281 jButtonClose.addActionListener(new java.awt.event.ActionListener () { 282 public void actionPerformed(java.awt.event.ActionEvent evt) { 283 jButtonCloseActionPerformed(evt); 284 } 285 }); 286 287 gridBagConstraints = new java.awt.GridBagConstraints (); 288 gridBagConstraints.gridx = 0; 289 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 290 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 291 gridBagConstraints.weightx = 1.0; 292 jPanel2.add(jButtonClose, gridBagConstraints); 293 294 gridBagConstraints = new java.awt.GridBagConstraints (); 295 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 296 gridBagConstraints.insets = new java.awt.Insets (4, 0, 4, 4); 297 jPanelData.add(jPanel2, gridBagConstraints); 298 299 getContentPane().add(jPanelData, java.awt.BorderLayout.CENTER); 300 301 pack(); 302 } 304 private void jTable1MouseClicked(java.awt.event.MouseEvent evt) { if (evt.getClickCount() == 2 && evt.getButton() == evt.BUTTON1) 306 { 307 jButtonModifyFileActionPerformed(null); 308 309 } 310 } 312 private void jButtonDeleteFileActionPerformed(java.awt.event.ActionEvent evt) { 314 if (jTable1.getSelectedRow() >= 0) 315 { 316 Report report = getJReportFrame().getReport(); 317 File f = new File(report.getFilename()); 318 File dir = f.getParentFile(); 319 File localeFile = new File(dir,""+jTable1.getValueAt( jTable1.getSelectedRow(), 1) ); 320 try { 321 localeFile.delete(); 322 } catch (Exception ex) {} 323 updateFileList(); 324 } 325 326 } 328 private void jButtonModifyFileActionPerformed(java.awt.event.ActionEvent evt) { 330 if (jTable1.getSelectedRow() >= 0) 331 { 332 Report report = getJReportFrame().getReport(); 333 File f = new File(report.getFilename()); 334 File dir = f.getParentFile(); 335 File localeFile = new File(dir,""+jTable1.getValueAt( jTable1.getSelectedRow(), 1) ); 336 LocaleEditorDialog led = new LocaleEditorDialog(this.getJReportFrame().getMainFrame(), false); 337 led.setFile( localeFile ); 338 led.setVisible(true); 339 try { 340 led.setTitle( localeFile.getName() ); 341 } catch (Exception ex) {} 342 } 343 } 345 private void jButtonCreateNewLocaleActionPerformed(java.awt.event.ActionEvent evt) { 347 String suffix = ""; 348 349 Report report = getJReportFrame().getReport(); 350 if (report.getFilename() == null || 351 report.getFilename().trim().equals("") ) 352 { 353 354 javax.swing.JOptionPane.showMessageDialog(this, 355 I18n.getString("messages.localeResourceFilesDialog.saveFile","To create a new locale file please save your report first.") 356 ); 357 358 return; 359 } 360 361 File f = new File(report.getFilename()); 362 363 364 NewLocaleFileDialog fd = new NewLocaleFileDialog(this, true); 365 fd.setVisible(true); 366 File localeFile = null; 367 if (fd.getDialogResult() == javax.swing.JOptionPane.OK_OPTION) 368 { 369 try { 370 suffix = fd.getText(); 371 372 if (report.getResourceBundleBaseName() == null || 373 report.getResourceBundleBaseName().trim().equals("") ) 374 { 375 String filename = f.getName(); 376 int index = filename.lastIndexOf("."); 377 if (index >= 0) { 378 filename = filename.substring(0,index); 379 } 380 report.setResourceBundleBaseName(filename); 381 } 382 383 File dir = f.getParentFile(); 384 localeFile = new File(dir, report.getResourceBundleBaseName() + ".properties"); 385 String str = "# Locale " + suffix + " for report " + f.getName() +"\n"; 387 try { 388 FileInputStream fis = new FileInputStream(localeFile); 389 byte[] buffer = new byte[1024]; 390 int read = 0; 391 while ((read = fis.read(buffer)) > 0) 392 { 393 str += new String (buffer,0,read); 394 } 395 fis.close(); 396 } catch (Exception ex){} 397 398 localeFile = new File(dir, report.getResourceBundleBaseName() + suffix +".properties"); 399 FileOutputStream fos = new FileOutputStream( localeFile ); 400 fos.write( str.getBytes()); 401 fos.close(); 402 403 updateFileList(); 404 405 } catch (Exception ex) { 406 407 javax.swing.JOptionPane.showMessageDialog(this, 408 I18n.getFormattedString("messages.localeResourceFilesDialog.errorCreatingNewLocale","Error creating new locale {0}:\n{1}", 409 new Object []{localeFile.getName(),ex.getMessage()}) 410 ); 411 } 412 } 413 } 415 private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) { 417 this.setDialogResult( javax.swing.JOptionPane.OK_OPTION); 418 this.setVisible(false); 419 } 421 422 423 private javax.swing.JButton jButtonClose; 425 private javax.swing.JButton jButtonCreateNewLocale; 426 private javax.swing.JButton jButtonDeleteFile; 427 private javax.swing.JButton jButtonModifyFile; 428 private javax.swing.JPanel jPanel2; 429 private javax.swing.JPanel jPanel3; 430 private javax.swing.JPanel jPanelData; 431 private javax.swing.JScrollPane jScrollPane3; 432 private javax.swing.JTable jTable1; 433 435 public static String getLocaleName(String baseName, String propertiesName) 436 { 437 String language = ""; 438 String country = ""; 439 String variant = ""; 440 Locale locale = Locale.getDefault(); 441 442 if (!propertiesName.endsWith(".properties")) return ""; 443 if (!propertiesName.startsWith(baseName)) return ""; 444 445 propertiesName = propertiesName.substring(baseName.length(), propertiesName.length() - ".properties".length()); 446 447 if (propertiesName.length() == 0) return "Default"; 448 if (propertiesName.startsWith("_")) propertiesName = propertiesName.substring(1); 449 if (propertiesName.indexOf("_") > 0) 450 { 451 language = propertiesName.substring(0,propertiesName.indexOf("_")); 452 propertiesName = propertiesName.substring(propertiesName.indexOf("_")+1); 453 454 if (propertiesName.indexOf("_") > 0) 455 { 456 country = propertiesName.substring(0,propertiesName.indexOf("_")); 457 propertiesName = propertiesName.substring(propertiesName.indexOf("_")+1); 458 459 if (propertiesName.indexOf("_") > 0) 460 { 461 variant = propertiesName.substring(0,propertiesName.indexOf("_")); 462 propertiesName = propertiesName.substring(propertiesName.indexOf("_")+1); 463 } 464 else 465 { 466 variant = propertiesName; 467 } 468 } 469 else 470 { 471 country = propertiesName; 472 } 473 } 474 else 475 { 476 language = propertiesName; 477 } 478 479 locale = new Locale(language,country,variant); 480 481 return locale.getDisplayName(); 482 483 } 484 485 public void applyI18n(){ 486 jButtonClose.setText(I18n.getString("localeResourceFilesDialog.buttonClose","Close")); 488 jButtonCreateNewLocale.setText(I18n.getString("localeResourceFilesDialog.buttonCreateNewLocale","Create new locale")); 489 jButtonDeleteFile.setText(I18n.getString("localeResourceFilesDialog.buttonDeleteFile","Delete file")); 490 jButtonModifyFile.setText(I18n.getString("localeResourceFilesDialog.buttonModifyFile"," Modify file")); 491 493 jTable1.getColumnModel().getColumn(0).setHeaderValue( I18n.getString("localeResourceFilesDialog.tablecolumn.locale","Locale") ); 494 jTable1.getColumnModel().getColumn(1).setHeaderValue( I18n.getString("localeResourceFilesDialog.tablecolumn.fileName","File name") ); 495 496 } 497 } 498 | Popular Tags |