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 4.3KB

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