1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.devgui;
25
26 import java.awt.*;
27 import java.awt.event.*;
28
29 import java.io.*;
30
31 import javax.swing.*;
32 import javax.swing.event.*;
33
34
35
36 /***
37 * DOCUMENT ME!
38 *
39 * @author $author$
40 * @version $Revision: 1.9 $
41 */
42 public class EditorPanel extends JPanel implements ActionListener,
43 DocumentListener {
44 File file;
45 JButton b_save;
46 JLabel l_fileName;
47 JTextArea ta_editor;
48 boolean unsavedChanges = false;
49
50 /***
51 * Creates a new EditorPanel object.
52 */
53 public EditorPanel() {
54 initComponents();
55 doLayout();
56 }
57
58 /***
59 * DOCUMENT ME!
60 *
61 * @param aFile DOCUMENT ME!
62 */
63 public void setFile(File aFile) {
64 if (unsavedChanges) {
65 int result = JOptionPane.showConfirmDialog(this,
66 this.file.getName()
67 + " is not saved. Want to save?",
68 "save file?",
69 JOptionPane.YES_NO_OPTION);
70
71 if (result == JOptionPane.OK_OPTION) {
72 saveFile(this.file);
73 this.unsavedChanges = false;
74 }
75 }
76
77 this.file = aFile;
78 this.l_fileName.setText(file.getAbsolutePath());
79 this.ta_editor.setText(readFile(file));
80 this.ta_editor.setCaretPosition(0);
81 this.ta_editor.getDocument()
82 .addDocumentListener(this);
83 this.unsavedChanges = false;
84
85
86 }
87
88
89 /***
90 * DOCUMENT ME!
91 *
92 * @param e DOCUMENT ME!
93 */
94 public void actionPerformed(ActionEvent e) {
95 if (e.getSource() == b_save) {
96 saveFile(this.file);
97 this.unsavedChanges = false;
98 }
99 }
100
101
102 /***
103 * DOCUMENT ME!
104 *
105 * @param evt DOCUMENT ME!
106 */
107 public void changedUpdate(DocumentEvent evt) {
108 }
109
110
111 /***
112 * DOCUMENT ME!
113 *
114 * @param evt DOCUMENT ME!
115 */
116 public void insertUpdate(DocumentEvent evt) {
117 unsavedChanges = true;
118 }
119
120
121 /***
122 * DOCUMENT ME!
123 *
124 * @param evt DOCUMENT ME!
125 */
126 public void removeUpdate(DocumentEvent evt) {
127 unsavedChanges = true;
128 }
129
130
131 /***
132 * DOCUMENT ME!
133 *
134 * @param e DOCUMENT ME!
135 */
136 protected void showExceptionDialog(Exception e) {
137 JOptionPane.showMessageDialog(this,
138 "An exception occurred:\n\n" + e.toString()
139 + "\n", "Exception",
140 JOptionPane.ERROR_MESSAGE);
141 }
142
143
144 private void initComponents() {
145 this.setLayout(new BorderLayout());
146 l_fileName = new JLabel();
147
148 add(BorderLayout.NORTH, l_fileName);
149
150 ta_editor = new JTextArea();
151 add(BorderLayout.CENTER, new JScrollPane(ta_editor));
152
153 JPanel panel_buttons = new JPanel();
154 panel_buttons.setLayout(new BorderLayout());
155 b_save = new JButton("Save File");
156 b_save.addActionListener(this);
157 panel_buttons.add(BorderLayout.CENTER, b_save);
158
159 add(BorderLayout.SOUTH, panel_buttons);
160 }
161
162
163 private String readFile(File afile) {
164 StringBuffer result = new StringBuffer();
165
166 try {
167 BufferedReader reader = new BufferedReader(new FileReader(afile));
168
169 String s = null;
170
171 while ((s = reader.readLine()) != null) {
172 result.append(s);
173 result.append("\n");
174 }
175
176 reader.close();
177 } catch (IOException ioe) {
178 showExceptionDialog(ioe);
179 }
180
181 return result.toString();
182 }
183
184
185 private void saveFile(File afile) {
186 try {
187 FileWriter fw = new FileWriter(afile);
188 fw.write(ta_editor.getText());
189 fw.flush();
190 fw.close();
191 System.out.println("saved.");
192 } catch (IOException ioe) {
193 showExceptionDialog(ioe);
194 }
195 }
196 }