PageRenderTime 220ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/dbmsgsrc/src/org/riotfamily/dbmsgsrc/riot/TranslationExportCommand.java

https://github.com/ybl/riot
Java | 146 lines | 108 code | 25 blank | 13 comment | 7 complexity | 91276f033365b16aee0df6399fe277e6 MD5 | raw file
  1. /* Licensed under the Apache License, Version 2.0 (the "License");
  2. * you may not use this file except in compliance with the License.
  3. * You may obtain a copy of the License at
  4. *
  5. * http://www.apache.org/licenses/LICENSE-2.0
  6. *
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13. package org.riotfamily.dbmsgsrc.riot;
  14. import java.io.IOException;
  15. import java.io.OutputStream;
  16. import java.util.Collection;
  17. import java.util.Locale;
  18. import org.apache.poi.hssf.usermodel.HSSFCell;
  19. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  20. import org.apache.poi.hssf.usermodel.HSSFFont;
  21. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  22. import org.apache.poi.hssf.usermodel.HSSFRow;
  23. import org.apache.poi.hssf.usermodel.HSSFSheet;
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  25. import org.riotfamily.core.screen.list.command.CommandContext;
  26. import org.riotfamily.core.screen.list.command.Selection;
  27. import org.riotfamily.core.screen.list.command.impl.export.AbstractExportCommand;
  28. import org.riotfamily.dbmsgsrc.model.Message;
  29. import org.riotfamily.pages.model.Site;
  30. public class TranslationExportCommand extends AbstractExportCommand {
  31. @Override
  32. public String getFileExtension() {
  33. return "xls";
  34. }
  35. @Override
  36. protected void export(CommandContext context, Selection selection, OutputStream out) throws IOException {
  37. Site site = (Site) context.getParent();
  38. Collection<?> items = getItems(context, selection);
  39. HSSFWorkbook wb = new WorkbookCreator().createWorkbook(items, site);
  40. wb.write(out);
  41. }
  42. private final static class WorkbookCreator {
  43. HSSFWorkbook wb;
  44. HSSFSheet sheet;
  45. HSSFCellStyle locked;
  46. HSSFCellStyle editable;
  47. HSSFCellStyle hidden;
  48. private HSSFWorkbook createWorkbook(Collection<?> items, Site site) {
  49. wb = new HSSFWorkbook();
  50. sheet = wb.createSheet("Translations");
  51. //sheet.protectSheet("");
  52. locked = wb.createCellStyle();
  53. locked.setLocked(true);
  54. locked.setWrapText(true);
  55. locked.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
  56. editable = wb.createCellStyle();
  57. editable.setLocked(false);
  58. editable.setWrapText(true);
  59. editable.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
  60. hidden = wb.createCellStyle();
  61. hidden.setLocked(true);
  62. createHeadings("Category", "Code", "Default Message", "Translation", "Comment");
  63. createRows(items, site.getLocale());
  64. return wb;
  65. }
  66. private void createHeadings(String... labels) {
  67. HSSFCellStyle style = wb.createCellStyle();
  68. HSSFFont font = wb.createFont();
  69. font.setFontName("Trebuchet MS");
  70. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  71. style.setFont(font);
  72. HSSFRow row = sheet.createRow(0);
  73. short col = 0;
  74. for (String label : labels) {
  75. HSSFCell cell = row.createCell(col++);
  76. cell.setCellStyle(style);
  77. cell.setCellValue(new HSSFRichTextString(label));
  78. }
  79. }
  80. private void createRows(Collection<?> items, Locale locale) {
  81. int i = 1;
  82. for (Object item : items) {
  83. Message message = (Message) item;
  84. String translation = null;
  85. if (message.getLocale().equals(locale)) {
  86. translation = message.getText();
  87. }
  88. HSSFRow row = sheet.createRow(i++);
  89. addCell(row, 0, getCategory(message), editable);
  90. addCell(row, 1, message.getEntry().getCode(), locked);
  91. addCell(row, 2, message.getEntry().getDefaultText(), locked);
  92. addCell(row, 3, translation, editable);
  93. addCell(row, 4, message.getEntry().getComment(), editable);
  94. addCell(row, 5, message.getText(), hidden);
  95. }
  96. sheet.autoSizeColumn((short) 0);
  97. sheet.setColumnWidth((short) 1, (short) (25 * 256));
  98. sheet.setColumnWidth((short) 2, (short) (50 * 256));
  99. sheet.setColumnWidth((short) 3, (short) (50 * 256));
  100. sheet.setColumnWidth((short) 4, (short) (50 * 256));
  101. sheet.setColumnHidden((short) 5, true);
  102. }
  103. private String getCategory(Message message) {
  104. String category = message.getEntry().getCode();
  105. int i = category.indexOf('.');
  106. if (i != -1) {
  107. category = category.substring(0, i);
  108. }
  109. return category;
  110. }
  111. private void addCell(HSSFRow row, int i, String text, HSSFCellStyle style) {
  112. HSSFCell cell = row.createCell((short) i);
  113. cell.setCellStyle(style);
  114. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  115. if (text == null) {
  116. text = "";
  117. }
  118. cell.setCellValue(new HSSFRichTextString(text));
  119. }
  120. }
  121. }