/
SimpleEditor.java
188 lines (161 loc) · 5.71 KB
/
SimpleEditor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// SimpleEditor.java
// An example showing several DefaultEditorKit features. This class is designed
// to be easily extended for additional functionality.
//
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.util.Hashtable;
public class SimpleEditor extends JFrame {
private Action openAction = new OpenAction();
private Action saveAction = new SaveAction();
private JTextComponent textComp;
private Hashtable actionHash = new Hashtable();
public static void main(String[] args) {
SimpleEditor editor = new SimpleEditor();
editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editor.setVisible(true);
}
// Create an editor.
public SimpleEditor() {
super("Swing Editor");
textComp = createTextComponent();
makeActionsPretty();
Container content = getContentPane();
content.add(textComp, BorderLayout.CENTER);
content.add(createToolBar(), BorderLayout.NORTH);
setJMenuBar(createMenuBar());
setSize(320, 240);
}
// Create the JTextComponent subclass.
protected JTextComponent createTextComponent() {
JTextArea ta = new JTextArea();
ta.setLineWrap(true);
return ta;
}
// Add icons and friendly names to actions we care about.
protected void makeActionsPretty() {
Action a;
a = textComp.getActionMap().get(DefaultEditorKit.cutAction);
a.putValue(Action.SMALL_ICON, new ImageIcon("icons/cut.gif"));
a.putValue(Action.NAME, "Cut");
a = textComp.getActionMap().get(DefaultEditorKit.copyAction);
a.putValue(Action.SMALL_ICON, new ImageIcon("icons/copy.gif"));
a.putValue(Action.NAME, "Copy");
a = textComp.getActionMap().get(DefaultEditorKit.pasteAction);
a.putValue(Action.SMALL_ICON, new ImageIcon("icons/paste.gif"));
a.putValue(Action.NAME, "Paste");
a = textComp.getActionMap().get(DefaultEditorKit.selectAllAction);
a.putValue(Action.NAME, "Select All");
}
// Create a simple JToolBar with some buttons.
protected JToolBar createToolBar() {
JToolBar bar = new JToolBar();
// Add simple actions for opening & saving.
bar.add(getOpenAction()).setText("");
bar.add(getSaveAction()).setText("");
bar.addSeparator();
// Add cut/copy/paste buttons.
bar.add(textComp.getActionMap().get(DefaultEditorKit.cutAction)).setText("");
bar.add(textComp.getActionMap().get(
DefaultEditorKit.copyAction)).setText("");
bar.add(textComp.getActionMap().get(
DefaultEditorKit.pasteAction)).setText("");
return bar;
}
// Create a JMenuBar with file & edit menus.
protected JMenuBar createMenuBar() {
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
menubar.add(file);
menubar.add(edit);
file.add(getOpenAction());
file.add(getSaveAction());
file.add(new ExitAction());
edit.add(textComp.getActionMap().get(DefaultEditorKit.cutAction));
edit.add(textComp.getActionMap().get(DefaultEditorKit.copyAction));
edit.add(textComp.getActionMap().get(DefaultEditorKit.pasteAction));
edit.add(textComp.getActionMap().get(DefaultEditorKit.selectAllAction));
return menubar;
}
// Subclass can override to use a different open action.
protected Action getOpenAction() { return openAction; }
// Subclass can override to use a different save action.
protected Action getSaveAction() { return saveAction; }
protected JTextComponent getTextComponent() { return textComp; }
// ********** ACTION INNER CLASSES ********** //
// A very simple exit action
public class ExitAction extends AbstractAction {
public ExitAction() { super("Exit"); }
public void actionPerformed(ActionEvent ev) { System.exit(0); }
}
// An action that opens an existing file
class OpenAction extends AbstractAction {
public OpenAction() {
super("Open", new ImageIcon("icons/open.gif"));
}
// Query user for a filename and attempt to open and read the file into the
// text component.
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(SimpleEditor.this) !=
JFileChooser.APPROVE_OPTION)
return;
File file = chooser.getSelectedFile();
if (file == null)
return;
FileReader reader = null;
try {
reader = new FileReader(file);
textComp.read(reader, null);
}
catch (IOException ex) {
JOptionPane.showMessageDialog(SimpleEditor.this,
"File Not Found", "ERROR", JOptionPane.ERROR_MESSAGE);
}
finally {
if (reader != null) {
try {
reader.close();
} catch (IOException x) {}
}
}
}
}
// An action that saves the document to a file
class SaveAction extends AbstractAction {
public SaveAction() {
super("Save", new ImageIcon("icons/save.gif"));
}
// Query user for a filename and attempt to open and write the text
// component’s content to the file.
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(SimpleEditor.this) !=
JFileChooser.APPROVE_OPTION)
return;
File file = chooser.getSelectedFile();
if (file == null)
return;
FileWriter writer = null;
try {
writer = new FileWriter(file);
textComp.write(writer);
}
catch (IOException ex) {
JOptionPane.showMessageDialog(SimpleEditor.this,
"File Not Saved", "ERROR", JOptionPane.ERROR_MESSAGE);
}
finally {
if (writer != null) {
try {
writer.close();
} catch (IOException x) {}
}
}
}
}
}