PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/_deactivated/javafx-filtered-tableview/src/main/java/org/rapidpm/modul/javafx/tableview/filtered/contextmenue/FilteredTableContextMenu.java

https://gitlab.com/jforge/Modules
Java | 375 lines | 310 code | 41 blank | 24 comment | 32 complexity | f5873edd88597ce2fb759ec002f0ae0f MD5 | raw file
  1. /*
  2. * Copyright [2013] [www.rapidpm.org / Sven Ruppert (sven.ruppert@rapidpm.org)]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.rapidpm.modul.javafx.tableview.filtered.contextmenue;
  17. import java.awt.Desktop;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24. import javax.inject.Inject;
  25. import javafx.application.Platform;
  26. import javafx.collections.ObservableList;
  27. import javafx.event.ActionEvent;
  28. import javafx.event.EventHandler;
  29. import javafx.scene.SnapshotParameters;
  30. import javafx.scene.control.ContextMenu;
  31. import javafx.scene.control.MenuItem;
  32. import javafx.scene.control.TableColumn;
  33. import javafx.scene.image.WritableImage;
  34. import javafx.scene.input.Clipboard;
  35. import javafx.scene.input.ClipboardContent;
  36. import javafx.stage.FileChooser;
  37. import org.apache.poi.xssf.usermodel.XSSFCell;
  38. import org.apache.poi.xssf.usermodel.XSSFRow;
  39. import org.apache.poi.xssf.usermodel.XSSFSheet;
  40. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  41. import org.rapidpm.commons.cdi.format.CDISimpleDateFormatter;
  42. import org.rapidpm.commons.cdi.logger.CDILogger;
  43. import org.rapidpm.commons.cdi.registry.property.CDIPropertyRegistryService;
  44. import org.rapidpm.commons.cdi.registry.property.PropertyRegistryService;
  45. import org.rapidpm.modul.javafx.tableview.filtered.FilteredTableDataRow;
  46. import org.rapidpm.modul.javafx.tableview.filtered.FilteredTableKeyMapper;
  47. import org.rapidpm.modul.javafx.tableview.filtered.FilteredTableView;
  48. import org.rapidpm.module.se.commons.logger.Logger;
  49. /**
  50. * User: Sven Ruppert Date: 14.08.13 Time: 16:30
  51. */
  52. public class FilteredTableContextMenu extends ContextMenu {
  53. private @Inject @CDILogger Logger logger;
  54. @Inject @CDIPropertyRegistryService PropertyRegistryService propertyRegistryService;
  55. @Inject FilteredTableKeyMapper keyMapper;
  56. private @Inject @CDISimpleDateFormatter(value = "date.yyyyMMdd") SimpleDateFormat sdf;
  57. private boolean openCSV = true;
  58. private boolean saveCSV = true;
  59. private boolean openXLSX = true;
  60. private boolean saveXLSX = true;
  61. private boolean copyClippboardCSV = true;
  62. private boolean copyClippboardImage = true;
  63. public FilteredTableContextMenu() {
  64. }
  65. private FilteredTableView filteredTableView;
  66. public void seti18n() {
  67. final ObservableList<MenuItem> items = getItems();
  68. for (final MenuItem item : items) {
  69. item.setText(map(item.getText()));
  70. }
  71. }
  72. private String map(final String key) {
  73. return propertyRegistryService.getRessourceForKey(keyMapper.map(key));
  74. }
  75. // @PostConstruct
  76. public void init() {
  77. if (openCSV) {
  78. MenuItem openCSV = new MenuItem("contextmenu.opencsv");
  79. openCSV.setOnAction(new EventHandler<ActionEvent>() {
  80. public void handle(ActionEvent e) {
  81. Platform.runLater(new Runnable() {
  82. @Override public void run() {
  83. try {
  84. final StringBuilder stringBuilder = convertTable2CSV();
  85. final File temp = File.createTempFile("xls-temp-" + System.nanoTime(), ".csv");
  86. final FileOutputStream fos = new FileOutputStream(temp);
  87. fos.write(stringBuilder.toString().getBytes());
  88. fos.flush();
  89. fos.close();
  90. Desktop desktop = Desktop.getDesktop();
  91. desktop.open(temp);
  92. } catch (IOException e1) {
  93. logger.error(e1);
  94. }
  95. }
  96. });
  97. }
  98. });
  99. getItems().add(openCSV);
  100. } else {
  101. if (logger.isDebugEnabled()) {
  102. logger.debug("open csv not active ");
  103. }
  104. }
  105. if (saveCSV) {
  106. MenuItem saveCSV = new MenuItem("contextmenu.savecsv");
  107. saveCSV.setOnAction(new EventHandler<ActionEvent>() {
  108. public void handle(ActionEvent e) {
  109. Platform.runLater(new Runnable() {
  110. @Override public void run() {
  111. try {
  112. final StringBuilder stringBuilder = convertTable2CSV();
  113. FileChooser fileChooser = new FileChooser();
  114. fileChooser.setTitle("contextmenu.savecsv");
  115. fileChooser.setInitialDirectory(
  116. new File(System.getProperty("contextmenu.defaultdir"))
  117. );
  118. final File targetFile = fileChooser.showSaveDialog(filteredTableView.getScene().getWindow());
  119. if (targetFile != null) {
  120. final FileOutputStream fos = new FileOutputStream(targetFile);
  121. fos.write(stringBuilder.toString().getBytes());
  122. fos.flush();
  123. fos.close();
  124. } else {
  125. if (logger.isDebugEnabled()) {
  126. logger.debug("keine Ziel ausgewaehlt..");
  127. }
  128. }
  129. } catch (IOException e1) {
  130. logger.error(e1);
  131. }
  132. }
  133. });
  134. }
  135. });
  136. getItems().add(saveCSV);
  137. } else {
  138. if (logger.isDebugEnabled()) {
  139. logger.debug("save csv not active ");
  140. }
  141. }
  142. if (openXLSX) {
  143. final MenuItem openXlS = new MenuItem("contextmenu.openxls");
  144. openXlS.setOnAction(new EventHandler<ActionEvent>() {
  145. public void handle(ActionEvent e) {
  146. if (logger.isDebugEnabled()) {
  147. logger.debug(openXlS.toString());
  148. }
  149. Platform.runLater(new Runnable() {
  150. @Override public void run() {
  151. try {
  152. final byte[] bytes = convertTable2Xls();
  153. final File temp = File.createTempFile("xls-temp-" + System.nanoTime(), ".xlsx");
  154. final FileOutputStream fos = new FileOutputStream(temp);
  155. fos.write(bytes);
  156. fos.flush();
  157. fos.close();
  158. Desktop desktop = Desktop.getDesktop();
  159. desktop.open(temp);
  160. } catch (IOException e1) {
  161. logger.error(e1);
  162. }
  163. }
  164. });
  165. }
  166. });
  167. getItems().add(openXlS);
  168. } else {
  169. if (logger.isDebugEnabled()) {
  170. logger.debug("openXLSXv not active ");
  171. }
  172. }
  173. if (saveXLSX) {
  174. final MenuItem saveXlS = new MenuItem("contextmenu.savexls");
  175. saveXlS.setOnAction(new EventHandler<ActionEvent>() {
  176. public void handle(ActionEvent e) {
  177. Platform.runLater(new Runnable() {
  178. @Override public void run() {
  179. try {
  180. final byte[] bytes = convertTable2Xls();
  181. FileChooser fileChooser = new FileChooser();
  182. fileChooser.setTitle("contextmenu.savexls");
  183. fileChooser.setInitialDirectory(
  184. new File(System.getProperty("contextmenu.defaultdir"))
  185. );
  186. final File targetFile = fileChooser.showSaveDialog(filteredTableView.getScene().getWindow());
  187. if (targetFile != null) {
  188. final FileOutputStream fos = new FileOutputStream(targetFile);
  189. fos.write(bytes);
  190. fos.flush();
  191. fos.close();
  192. } else {
  193. if (logger.isDebugEnabled()) {
  194. logger.debug("keine Ziel ausgew�hlt..");
  195. }
  196. }
  197. } catch (IOException e1) {
  198. logger.error(e1);
  199. }
  200. }
  201. });
  202. }
  203. });
  204. getItems().add(saveXlS);
  205. } else {
  206. if (logger.isDebugEnabled()) {
  207. logger.debug("saveXLSX not active ");
  208. }
  209. }
  210. if (copyClippboardCSV) {
  211. MenuItem copyTableCSV2Clipboard = new MenuItem("contextmenu.copyclipboard");
  212. copyTableCSV2Clipboard.setOnAction(new EventHandler<ActionEvent>() {
  213. public void handle(ActionEvent e) {
  214. Platform.runLater(new Runnable() {
  215. @Override public void run() {
  216. final StringBuilder clipboardString = convertTable2CSV();
  217. final ClipboardContent content = new ClipboardContent();
  218. content.putString(clipboardString.toString());
  219. Clipboard.getSystemClipboard().setContent(content);
  220. }
  221. });
  222. }
  223. });
  224. getItems().add(copyTableCSV2Clipboard);
  225. } else {
  226. if (logger.isDebugEnabled()) {
  227. logger.debug("copyClippboardCSV not active ");
  228. }
  229. }
  230. if (copyClippboardImage) {
  231. MenuItem copyTableImage2Clipboard = new MenuItem("contextmenu.copyimage");
  232. copyTableImage2Clipboard.setOnAction(new EventHandler<ActionEvent>() {
  233. public void handle(ActionEvent e) {
  234. Platform.runLater(new Runnable() {
  235. @Override public void run() {
  236. final WritableImage snapImage = filteredTableView.snapshot(new SnapshotParameters(), null);
  237. final ClipboardContent content = new ClipboardContent();
  238. content.putImage(snapImage);
  239. Clipboard.getSystemClipboard().setContent(content);
  240. }
  241. });
  242. }
  243. });
  244. getItems().add(copyTableImage2Clipboard);
  245. } else {
  246. if (logger.isDebugEnabled()) {
  247. logger.debug("copyClippboardImage not active ");
  248. }
  249. }
  250. //send csv as mail
  251. //send xls as mail
  252. //save as pdf
  253. //zoom view
  254. seti18n();
  255. }
  256. private StringBuilder convertTable2CSV() {
  257. final StringBuilder clipboardString = new StringBuilder();
  258. final ObservableList<FilteredTableDataRow> items = filteredTableView.getItems();
  259. for (final FilteredTableDataRow item : items) {
  260. final String csvRow = item.convertToCSV();
  261. clipboardString.append(csvRow);
  262. clipboardString.append("\n");
  263. }
  264. return clipboardString;
  265. }
  266. private byte[] convertTable2Xls() throws IOException {
  267. //konvertiere
  268. final XSSFWorkbook workbook = new XSSFWorkbook();
  269. final XSSFSheet xssfSheet = workbook.createSheet("ExcelExport_" + sdf.format(new Date()));
  270. final XSSFRow xssfHeaderRow = xssfSheet.createRow(0);
  271. final ObservableList<TableColumn> columns = filteredTableView.getColumns();
  272. int colNr = 0;
  273. for (final TableColumn column : columns) {
  274. final String columnText = column.getText();
  275. final XSSFCell xssfCell = xssfHeaderRow.createCell(colNr);
  276. colNr = colNr + 1;
  277. xssfCell.setCellValue(columnText);
  278. xssfCell.setCellType(XSSFCell.CELL_TYPE_STRING);
  279. }
  280. final ObservableList<FilteredTableDataRow> rowList = filteredTableView.getItems();
  281. int rowNr = 0;
  282. for (final FilteredTableDataRow row : rowList) {
  283. final XSSFRow xssfRow = xssfSheet.createRow(rowNr);
  284. rowNr = rowNr + 1;
  285. final String csvRow = row.convertToCSV();
  286. final String[] split = csvRow.split(";");
  287. int cellNr = 0;
  288. for (final String s : split) {
  289. final XSSFCell xssfCell = xssfRow.createCell(cellNr);
  290. cellNr = cellNr + 1;
  291. xssfCell.setCellValue(s);
  292. xssfCell.setCellType(XSSFCell.CELL_TYPE_STRING);
  293. }
  294. }
  295. final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  296. try {
  297. workbook.write(outputStream);
  298. } catch (IOException e) {
  299. logger.error(e);
  300. }
  301. return outputStream.toByteArray();
  302. }
  303. public void setFilteredTableView(FilteredTableView filteredTableView) {
  304. this.filteredTableView = filteredTableView;
  305. }
  306. public FilteredTableContextMenu openCSV(final boolean openCSV) {
  307. this.openCSV = openCSV;
  308. return this;
  309. }
  310. public FilteredTableContextMenu saveCSV(final boolean saveCSV) {
  311. this.saveCSV = saveCSV;
  312. return this;
  313. }
  314. public FilteredTableContextMenu openXLSX(final boolean openXLSX) {
  315. this.openXLSX = openXLSX;
  316. return this;
  317. }
  318. public FilteredTableContextMenu saveXLSX(final boolean saveXLSX) {
  319. this.saveXLSX = saveXLSX;
  320. return this;
  321. }
  322. public FilteredTableContextMenu copyClippboardCSV(final boolean copyClippboardCSV) {
  323. this.copyClippboardCSV = copyClippboardCSV;
  324. return this;
  325. }
  326. public FilteredTableContextMenu copyClippboardImage(final boolean copyClippboardImage) {
  327. this.copyClippboardImage = copyClippboardImage;
  328. return this;
  329. }
  330. public FilteredTableContextMenu filteredTableView(final FilteredTableView filteredTableView) {
  331. this.filteredTableView = filteredTableView;
  332. return this;
  333. }
  334. }