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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2000-2016 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.v7.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. * @deprecated As of 8.0, no replacement available.
  37. */
  38. @Deprecated
  39. @SuppressWarnings("serial")
  40. public class TextFileProperty extends AbstractProperty<String> {
  41. private File file;
  42. private Charset charset = null;
  43. /**
  44. * Wrap given file with property interface.
  45. *
  46. * Setting the file to null works, but getValue() will return null.
  47. *
  48. * @param file
  49. * File to be wrapped.
  50. */
  51. public TextFileProperty(File file) {
  52. this.file = file;
  53. }
  54. /**
  55. * Wrap the given file with the property interface and specify character
  56. * set.
  57. *
  58. * Setting the file to null works, but getValue() will return null.
  59. *
  60. * @param file
  61. * File to be wrapped.
  62. * @param charset
  63. * Charset to be used for reading and writing the file.
  64. */
  65. public TextFileProperty(File file, Charset charset) {
  66. this.file = file;
  67. this.charset = charset;
  68. }
  69. /*
  70. * (non-Javadoc)
  71. *
  72. * @see com.vaadin.data.Property#getType()
  73. */
  74. @Override
  75. public Class<String> getType() {
  76. return String.class;
  77. }
  78. /*
  79. * (non-Javadoc)
  80. *
  81. * @see com.vaadin.data.Property#getValue()
  82. */
  83. @Override
  84. public String getValue() {
  85. if (file == null) {
  86. return null;
  87. }
  88. try {
  89. FileInputStream fis = new FileInputStream(file);
  90. InputStreamReader isr = charset == null ? new InputStreamReader(fis)
  91. : new InputStreamReader(fis, charset);
  92. BufferedReader r = new BufferedReader(isr);
  93. StringBuilder b = new StringBuilder();
  94. char[] buf = new char[8 * 1024];
  95. int len;
  96. while ((len = r.read(buf)) != -1) {
  97. b.append(buf, 0, len);
  98. }
  99. r.close();
  100. isr.close();
  101. fis.close();
  102. return b.toString();
  103. } catch (FileNotFoundException e) {
  104. return null;
  105. } catch (IOException e) {
  106. throw new RuntimeException(e);
  107. }
  108. }
  109. /*
  110. * (non-Javadoc)
  111. *
  112. * @see com.vaadin.data.Property#isReadOnly()
  113. */
  114. @Override
  115. public boolean isReadOnly() {
  116. return file == null || super.isReadOnly() || !file.canWrite();
  117. }
  118. /*
  119. * (non-Javadoc)
  120. *
  121. * @see com.vaadin.data.Property#setValue(java.lang.Object)
  122. */
  123. @Override
  124. public void setValue(String newValue) throws ReadOnlyException {
  125. if (isReadOnly()) {
  126. throw new ReadOnlyException();
  127. }
  128. if (file == null) {
  129. return;
  130. }
  131. try {
  132. FileOutputStream fos = new FileOutputStream(file);
  133. OutputStreamWriter osw = charset == null
  134. ? new OutputStreamWriter(fos)
  135. : new OutputStreamWriter(fos, charset);
  136. BufferedWriter w = new BufferedWriter(osw);
  137. w.append(newValue.toString());
  138. w.flush();
  139. w.close();
  140. osw.close();
  141. fos.close();
  142. fireValueChange();
  143. } catch (IOException e) {
  144. throw new RuntimeException(e);
  145. }
  146. }
  147. }