1 package se.perfectfools.localizer.gui;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.event.ActionEvent;
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.FileNotFoundException;
9 import java.io.FileOutputStream;
10 import java.util.Enumeration;
11 import java.util.Hashtable;
12 import java.util.Iterator;
13
14 import javax.swing.AbstractAction;
15 import javax.swing.Action;
16 import javax.swing.BorderFactory;
17 import javax.swing.JComponent;
18 import javax.swing.JFileChooser;
19 import javax.swing.JFrame;
20 import javax.swing.JLabel;
21 import javax.swing.JMenu;
22 import javax.swing.JMenuBar;
23 import javax.swing.JMenuItem;
24 import javax.swing.JPanel;
25 import javax.swing.JTextArea;
26 import javax.swing.SpringLayout;
27 import javax.swing.filechooser.FileFilter;
28
29 import se.perfectfools.localizer.LocalizationFile;
30 import se.perfectfools.localizer.Message;
31 import se.perfectfools.localizer.i18n.Messages;
32 import se.perfectfools.localizer.impl.LocalizationFileImpl;
33 import se.perfectfools.localizer.impl.MessageImpl;
34 import se.perfectfools.localizer.parser.XMLParser;
35 import se.perfectfools.localizer.parser.impl.XMLParserImpl;
36
37 import com.jgoodies.plaf.Options;
38
39 /***
40 * Main GUI-window for EZLoc. This is also the main entry point of the application.
41 *
42 * @author Pål Brattberg, pal@eminds.se
43 */
44 public class MainWindow extends JFrame {
45
46 protected static final String DEFAULT_FILE_LOC = File.separator + "tmp";
47 protected static final String DEFAULT_FILE_ENDING = ".xml";
48
49 /*** Keep track of the input fields. */
50 protected Hashtable inputFields;
51 /***
52 * This is a reference to the last component to be added as the main panel. Needed for the
53 * ability to remove it later.
54 */
55 protected Component mainPanel;
56
57 /*** These actions are available in the application. */
58 protected Action openFileAction, saveFileAction, newAction;
59
60 /***
61 * Configures the UI, then builds and opens the UI.
62 */
63 public static void main(String[] args) {
64 log("Initializing application");
65 new MainWindow();
66 }
67
68 public MainWindow() {
69 this.setTitle(Messages.getString("MainWindow.app.titlebar"));
70 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
71 log("Creating actions");
72 createActions();
73 this.setJMenuBar(buildMenuBar());
74
75 log("Configuring UI");
76 GUIHelperMethods.configureUI();
77
78 JPanel cp = new JPanel(new BorderLayout());
79 cp.add(new HeaderPanel(), BorderLayout.NORTH);
80
81
82 cp.add(new StatusBar(), BorderLayout.SOUTH);
83 this.setContentPane(cp);
84 this.setSize(500, 500);
85
86 this.show();
87 }
88
89 public void setMainPanel(final JComponent panel) {
90 if (mainPanel != null) {
91 getContentPane().remove(mainPanel);
92 }
93 getContentPane().add(panel, BorderLayout.CENTER);
94 mainPanel = panel;
95 this.validate();
96 }
97
98 private void createActions() {
99
100 openFileAction = new AbstractAction(Messages.getString("MainWindow.menu.open")) {
101 public void actionPerformed(ActionEvent arg0) {
102 JFileChooser fileChooser = new JFileChooser(new File(DEFAULT_FILE_LOC));
103 fileChooser.addChoosableFileFilter(new FileFilter() {
104 public boolean accept(File file) {
105 if (file.isDirectory())
106 return true;
107 String filename = file.getName();
108 return filename.toLowerCase().endsWith(DEFAULT_FILE_ENDING);
109 }
110
111 public String getDescription() {
112 return "*" + DEFAULT_FILE_ENDING;
113 }
114 });
115
116
117 fileChooser.showOpenDialog(MainWindow.this);
118 if (fileChooser.getSelectedFile() != null)
119 loadXMLFile(fileChooser.getSelectedFile());
120 }
121 };
122
123 saveFileAction = new AbstractAction(Messages.getString("MainWindow.menu.saveas")) {
124 public void actionPerformed(ActionEvent arg0) {
125 JFileChooser fileChooser = new JFileChooser(new File(DEFAULT_FILE_LOC));
126
127
128 fileChooser.showSaveDialog(MainWindow.this);
129 if (fileChooser.getSelectedFile() != null)
130 saveToXML(fileChooser.getSelectedFile());
131 }
132
133 };
134
135 newAction = new AbstractAction(Messages.getString("MainWindow.menu.new")) {
136 public void actionPerformed(ActionEvent arg0) {
137 setMainPanel(new JPanel());
138 inputFields = new Hashtable();
139 }
140 };
141 }
142
143 private void loadXMLFile(File file) {
144 log("Loading new file: " + file.getAbsolutePath());
145 XMLParser parser = new XMLParserImpl();
146 try {
147 LocalizationFile locFile = parser.parseFile(new FileInputStream(file));
148
149 JPanel panel = new JPanel(new SpringLayout());
150
151 Iterator iter = locFile.iterator();
152 int numberOfFields = locFile.getMessages().size();
153 inputFields = new Hashtable();
154 JTextArea first = null;
155
156 while (iter.hasNext()) {
157 Message msg = (Message) iter.next();
158
159 panel.add(new JLabel(msg.getId()));
160
161 JTextArea jta = new JTextArea(msg.getText());
162 jta.setBackground(new JLabel().getBackground());
163 jta.setLineWrap(true);
164 jta.setWrapStyleWord(true);
165 jta.setEditable(false);
166 jta.setAlignmentX(Component.RIGHT_ALIGNMENT);
167 panel.add(jta);
168
169 JTextArea textarea = new JTextArea("");
170 if (first == null) {
171 first = textarea;
172 }
173 textarea.setColumns(30);
174 textarea.setWrapStyleWord(true);
175
176
177
178 textarea.setBorder(BorderFactory.createLoweredBevelBorder());
179 panel.add(textarea);
180
181 inputFields.put(msg.getId(), textarea);
182 }
183
184
185 SpringUtilities.makeCompactGrid(panel, numberOfFields, 3, 3, 3, 3, 3);
186 log("Added " + numberOfFields + " items to InputFieldPanel");
187
188 setMainPanel(GUIHelperMethods.createStrippedScrollPane(panel));
189 first.grabFocus();
190 } catch (FileNotFoundException e) {
191 throw new RuntimeException(e);
192 }
193
194 }
195
196 private void saveToXML(File file) {
197 Enumeration e = inputFields.keys();
198 LocalizationFile locFile = new LocalizationFileImpl();
199 while (e.hasMoreElements()) {
200 String id = e.nextElement().toString();
201 String text = ((JTextArea) inputFields.get(id)).getText();
202 Message m = new MessageImpl();
203 m.setId(id);
204 m.setText(text);
205 locFile.addMessage(m);
206 }
207 XMLParser parser = new XMLParserImpl();
208 try {
209 parser.serializeToFile(locFile, new FileOutputStream(file));
210 } catch (FileNotFoundException e1) {
211 throw new RuntimeException(e1);
212 }
213 }
214
215 /***
216 * Builds and returns the menu bar.
217 */
218 private JMenuBar buildMenuBar() {
219 JMenu menu;
220 JMenuBar menuBar = new JMenuBar();
221 menuBar.putClientProperty(Options.HEADER_STYLE_KEY, Boolean.TRUE);
222
223 menu = new JMenu(Messages.getString("MainWindow.menu.file"));
224 menu.add(new JMenuItem(newAction));
225
226 menu.add(new JMenuItem(openFileAction));
227 menu.add(new JMenuItem(saveFileAction));
228
229 menuBar.add(menu);
230
231
232
233
234
235
236
237
238
239 return menuBar;
240 }
241
242
243 private static void log(String s) {
244 System.out.println(s);
245 }
246 }