Source code: jmat/data/Text.java
1 package jmat.data;
2
3 import jmat.io.data.TextFile;
4 import jmat.io.data.fileTools.MatrixString;
5 import jmat.io.data.fileTools.TextString;
6
7 import jmat.io.gui.FrameView;
8 import jmat.io.gui.TextWindow;
9
10 import javax.swing.JPanel;
11
12
13 /**
14 <P>
15 The Text Class is just designed to provide easy-to-use string operations
16 like building log files, displaying text in a window, converting matrix to String format...
17
18 @author Yann RICHET
19 @version 2.0
20 */
21 public class Text implements java.io.Serializable
22 {
23 //~ Instance fields ////////////////////////////////////////////////////////
24
25 /* ------------------------
26 Class variables
27 * ------------------------ */
28
29 /** String for internal storage.
30 @serial internal string storage.
31 */
32 private String string = new String("");
33
34 //~ Constructors ///////////////////////////////////////////////////////////
35
36 /* ------------------------
37 Constructor
38 * ------------------------ */
39
40 /** Construct a text.
41 @param str String of the text.
42 */
43 public Text(String str)
44 {
45 setString(str);
46 }
47
48 /** Construct a text.
49 @param X Matrix to convert in text.
50 */
51 public Text(Matrix X)
52 {
53 setString(X);
54 }
55
56 //~ Methods ////////////////////////////////////////////////////////////////
57
58 /** Provides access to the string of the text.
59 @param str String of the text.
60 */
61 public void setString(String str)
62 {
63 string = str;
64 }
65
66 public void setString(Matrix X)
67 {
68 string = MatrixString.printMatrix(X);
69 }
70
71 /* ------------------------
72 Public Methods
73 * ------------------------ */
74
75 /** Provides access to the string of the text.
76 @return String of the text.
77 */
78 public String getString()
79 {
80 String s = new String(string);
81
82 return s;
83 }
84
85 /** Load the text from a file.
86 @param fileName fileName.
87 @return Text.
88 */
89 public static Text fromFile(String fileName)
90 {
91 TextFile mf = new TextFile(fileName);
92
93 return mf.getText();
94 }
95
96 /** Save the text from a file.
97 @param file file.
98 @return Text.
99 */
100 public static Text fromFile(java.io.File file)
101 {
102 TextFile mf = new TextFile(file);
103
104 return mf.getText();
105 }
106
107 /** Merge the two Texts.
108 @param text text to merge.
109 */
110 public void merge(Text text)
111 {
112 string = new String(string + text.getString());
113 }
114
115 /** Merge the Matrix.
116 @param X Matrix to merge.
117 */
118 public void merge(Matrix X)
119 {
120 Text text = new Text(X);
121 string = new String(string + text.getString());
122 }
123
124 /** Merge the string.
125 @param s String to merge.
126 */
127 public void merge(String s)
128 {
129 string = new String(string + s);
130 }
131
132 /** Print the Text in the Command Line.
133 */
134 public void toCommandLine()
135 {
136 System.out.println(string);
137 }
138
139 /** Save the Text in a file.
140 @param fileName fileName.
141 */
142 public void toFile(String fileName)
143 {
144 TextFile mf = new TextFile(fileName, this);
145 mf = null;
146 }
147
148 /** Save the text in a file.
149 @param file file.
150 */
151 public void toFile(java.io.File file)
152 {
153 TextFile mf = new TextFile(file, this);
154 mf = null;
155 }
156
157 /** Display the text in a Window in a Frame.
158 @param title Title of the JFrame.
159 */
160 public void toFrame(String title)
161 {
162 FrameView fv = new FrameView(title, toPanel());
163 fv = null;
164 }
165
166 /** Display the text in a Window.
167 @return JPanel.
168 */
169 public JPanel toPanel()
170 {
171 return new TextWindow(this);
172 }
173 }
174 ///////////////////////////////////////////////////////////////////////////////
175 // END OF FILE.
176 ///////////////////////////////////////////////////////////////////////////////