Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.util / org.gvsig.tools.util.impl / src / main / java / org / gvsig / texteditor / DefaultJTextEditor.java @ 2285

History | View | Annotate | Download (6.39 KB)

1
package org.gvsig.texteditor;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Font;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.io.File;
8
import java.io.IOException;
9
import java.util.Objects;
10
import javax.swing.JFileChooser;
11
import javax.swing.JMenu;
12
import javax.swing.JMenuBar;
13
import javax.swing.JMenuItem;
14
import javax.swing.JOptionPane;
15
import javax.swing.JScrollPane;
16
import javax.swing.JTextArea;
17
import javax.swing.event.UndoableEditEvent;
18
import javax.swing.text.JTextComponent;
19
import javax.swing.undo.CannotUndoException;
20
import javax.swing.undo.UndoManager;
21
import org.apache.commons.io.FileUtils;
22

    
23
/**
24
 *
25
 * @author gvSIG Team
26
 */
27
public class DefaultJTextEditor extends AbstractJTextEditor {
28

    
29
    private JTextArea textArea;
30

    
31
    // Is File Saved/Opened
32
    private boolean opened = false;
33
    private boolean saved = false;
34

    
35
    private File openedFile;
36

    
37
    // Undo manager for managing the storage of the undos
38
    // so that the can be redone if requested
39
    private UndoManager undo;
40
    private String mimeType;
41

    
42

    
43
    public DefaultJTextEditor() {
44

    
45
        super();
46
        
47
        JMenuBar menuBar = new JMenuBar();
48
        JMenu fileMenu = new JMenu("File");
49
        addMenuItem(fileMenu, "Open...", (ActionEvent e) -> {
50
            doOpenFile();
51
        });
52
        addMenuItem(fileMenu, "Save", (ActionEvent e) -> {
53
            doSaveFile();
54
        });
55
        addMenuItem(fileMenu, "Save as...", (ActionEvent e) -> {
56
            doSaveAsFile();
57
        });
58
        addMenuItem(fileMenu, "Close", (ActionEvent e) -> {
59
            doClose();
60
        });
61

    
62
        JMenu editMenu = new JMenu("Edit");
63
        addMenuItem(editMenu, "Undo", (ActionEvent e) -> {
64
            doUndo();
65
        });
66
        addMenuItem(editMenu, "Redo", (ActionEvent e) -> {
67
            doRedo();
68
        });
69
        addMenuItem(editMenu, "Select all", (ActionEvent e) -> {
70
            doSelectAll();
71
        });
72
        addMenuItem(editMenu, "Cut", (ActionEvent e) -> {
73
            doCut();
74
        });
75
        addMenuItem(editMenu, "Copy", (ActionEvent e) -> {
76
            doCopy();
77
        });
78
        addMenuItem(editMenu, "Paste", (ActionEvent e) -> {
79
            doPaste();
80
        });
81
        menuBar.add(fileMenu);
82
        menuBar.add(editMenu);
83

    
84
        undo = new UndoManager();
85
        textArea.getDocument().addUndoableEditListener((UndoableEditEvent e) -> {
86
            undo.addEdit(e.getEdit());
87
        });
88

    
89
        this.setLayout(new BorderLayout());
90
        this.add(menuBar, BorderLayout.NORTH);
91

    
92
        JScrollPane scroll = new JScrollPane(
93
                getJTextComponent(), 
94
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
95
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
96
        );
97
        this.add(scroll, BorderLayout.CENTER);
98

    
99
    }
100

    
101
    private void addMenuItem(JMenu menu, String text, ActionListener listener) {
102
        JMenuItem item = new JMenuItem(text);
103
        item.addActionListener(listener);
104
        item.setEnabled(true);
105
        menu.add(item);
106
    }
107

    
108
    private void saveFile(File file) {
109
        try {
110
            FileUtils.write(file, textArea.getText());
111
            saved = true;
112
        } catch (IOException ex) {
113
            LOGGER.warn("Can't save file (" + Objects.toString(file) + ")", ex);
114
        }
115
    }
116

    
117
    private void doOpenFile() {
118
        JFileChooser open = new JFileChooser();
119
        open.showOpenDialog(null);
120
        File file = open.getSelectedFile();
121
        try {
122
            openedFile = file;
123
            String s = FileUtils.readFileToString(file);
124
            textArea.setText(s);
125
            opened = true;
126
        } catch (IOException ex) {
127
            LOGGER.warn("Can't open file (" + Objects.toString(file) + ")", ex);
128
        }
129
    }
130

    
131
    private void doSaveFile() {
132
        JFileChooser save = new JFileChooser();
133
        File file = save.getSelectedFile();
134
        if (opened == false && saved == false) {
135
            save.showSaveDialog(null);
136
            int confirmationResult;
137
            if (file.exists()) {
138
                confirmationResult = JOptionPane.showConfirmDialog(this, "Replace existing file?");
139
                if (confirmationResult == JOptionPane.YES_OPTION) {
140
                    saveFile(file);
141
                }
142
            } else {
143
                saveFile(file);
144
            }
145
        } else {
146
            try {
147
                FileUtils.write(openedFile, textArea.getText());
148
            } catch (IOException ex) {
149
                LOGGER.warn("Can't save file (" + Objects.toString(file) + ")", ex);
150
            }
151
        }
152
    }
153

    
154
    private void doSaveAsFile() {
155
        JFileChooser saveAs = new JFileChooser();
156
        saveAs.showSaveDialog(null);
157
        File filename = saveAs.getSelectedFile();
158
        int confirmationResult;
159
        if (filename.exists()) {
160
            confirmationResult = JOptionPane.showConfirmDialog(this, "Replace existing file?");
161
            if (confirmationResult == JOptionPane.YES_OPTION) {
162
                saveFile(filename);
163
            }
164
        } else {
165
            saveFile(filename);
166
        }
167
    }
168

    
169
    private void doClose() {
170
        this.setVisible(false);
171
    }
172

    
173
    private void doUndo() {
174
        try {
175
            undo.undo();
176
        } catch (CannotUndoException ex) {
177
            LOGGER.warn("Can't undo", ex);
178
        }
179
    }
180

    
181
    private void doRedo() {
182
        try {
183
            undo.redo();
184
        } catch (CannotUndoException ex) {
185
            LOGGER.warn("Can't redo", ex);
186
        }
187
    }
188

    
189
    private void doSelectAll() {
190
        textArea.selectAll();
191
    }
192

    
193
    private void doCopy() {
194
        textArea.copy();
195
    }
196

    
197
    private void doPaste() {
198
        textArea.paste();
199
    }
200

    
201
    private void doCut() {
202
        textArea.cut();
203
    }
204

    
205
    @Override
206
    public void setMimetype(String mimeType) {
207
        this.mimeType = mimeType;
208
    }
209

    
210
    @Override
211
    public String getMimetype() {
212
        return this.mimeType;
213
    }
214

    
215
    @Override
216
    public void setText(String text) {
217
        this.textArea.setText(text);
218
    }
219

    
220
    @Override
221
    public String getText() {
222
        return this.textArea.getText();
223
    }
224

    
225

    
226
    @Override
227
    public void clean() {
228
        this.setText("");
229
    }
230

    
231
    @Override
232
    public JTextComponent getJTextComponent() {
233
        if (this.textArea == null) {
234
            textArea = new JTextArea(30, 50);
235
            textArea.setEditable(true);
236

    
237
            Font textFont = new Font("Verdana", 0, 14);
238
            textArea.setFont(textFont);
239

    
240
        }
241
        return this.textArea;
242
    }
243

    
244
}