You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextFileProperty.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import java.io.BufferedReader;
  6. import java.io.BufferedWriter;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.io.OutputStreamWriter;
  14. import java.nio.charset.Charset;
  15. /**
  16. * Property implementation for wrapping a text file.
  17. *
  18. * Supports reading and writing of a File from/to String.
  19. *
  20. * {@link ValueChangeListener}s are supported, but only fire when
  21. * setValue(Object) is explicitly called. {@link ReadOnlyStatusChangeListener}s
  22. * are supported but only fire when setReadOnly(boolean) is explicitly called.
  23. *
  24. */
  25. @SuppressWarnings("serial")
  26. public class TextFileProperty extends AbstractProperty<String> {
  27. private File file;
  28. private Charset charset = null;
  29. /**
  30. * Wrap given file with property interface.
  31. *
  32. * Setting the file to null works, but getValue() will return null.
  33. *
  34. * @param file
  35. * File to be wrapped.
  36. */
  37. public TextFileProperty(File file) {
  38. this.file = file;
  39. }
  40. /**
  41. * Wrap the given file with the property interface and specify character
  42. * set.
  43. *
  44. * Setting the file to null works, but getValue() will return null.
  45. *
  46. * @param file
  47. * File to be wrapped.
  48. * @param charset
  49. * Charset to be used for reading and writing the file.
  50. */
  51. public TextFileProperty(File file, Charset charset) {
  52. this.file = file;
  53. this.charset = charset;
  54. }
  55. /*
  56. * (non-Javadoc)
  57. *
  58. * @see com.vaadin.data.Property#getType()
  59. */
  60. @Override
  61. public Class<String> getType() {
  62. return String.class;
  63. }
  64. /*
  65. * (non-Javadoc)
  66. *
  67. * @see com.vaadin.data.Property#getValue()
  68. */
  69. @Override
  70. public String getValue() {
  71. if (file == null) {
  72. return null;
  73. }
  74. try {
  75. FileInputStream fis = new FileInputStream(file);
  76. InputStreamReader isr = charset == null ? new InputStreamReader(fis)
  77. : new InputStreamReader(fis, charset);
  78. BufferedReader r = new BufferedReader(isr);
  79. StringBuilder b = new StringBuilder();
  80. char buf[] = new char[8 * 1024];
  81. int len;
  82. while ((len = r.read(buf)) != -1) {
  83. b.append(buf, 0, len);
  84. }
  85. r.close();
  86. isr.close();
  87. fis.close();
  88. return b.toString();
  89. } catch (FileNotFoundException e) {
  90. return null;
  91. } catch (IOException e) {
  92. throw new RuntimeException(e);
  93. }
  94. }
  95. /*
  96. * (non-Javadoc)
  97. *
  98. * @see com.vaadin.data.Property#isReadOnly()
  99. */
  100. @Override
  101. public boolean isReadOnly() {
  102. return file == null || super.isReadOnly() || !file.canWrite();
  103. }
  104. /*
  105. * (non-Javadoc)
  106. *
  107. * @see com.vaadin.data.Property#setValue(java.lang.Object)
  108. */
  109. @Override
  110. public void setValue(Object newValue) throws ReadOnlyException {
  111. if (isReadOnly()) {
  112. throw new ReadOnlyException();
  113. }
  114. if (file == null) {
  115. return;
  116. }
  117. try {
  118. FileOutputStream fos = new FileOutputStream(file);
  119. OutputStreamWriter osw = charset == null ? new OutputStreamWriter(
  120. fos) : new OutputStreamWriter(fos, charset);
  121. BufferedWriter w = new BufferedWriter(osw);
  122. w.append(newValue.toString());
  123. w.flush();
  124. w.close();
  125. osw.close();
  126. fos.close();
  127. fireValueChange();
  128. } catch (IOException e) {
  129. throw new RuntimeException(e);
  130. }
  131. }
  132. }