KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > devgui > EditorPanel


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/devgui/EditorPanel.java,v 1.9 2005/02/19 21:26:29 hkollmann Exp $
3  * $Revision: 1.9 $
4  * $Date: 2005/02/19 21:26:29 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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       //System.out.println("this.unsavedChanges = "+unsavedChanges);
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 JavaDoc 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 JavaDoc readFile(File afile) {
164       StringBuffer JavaDoc result = new StringBuffer JavaDoc();
165
166       try {
167          BufferedReader reader = new BufferedReader(new FileReader(afile));
168
169          String JavaDoc 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 }
197
Popular Tags