View Javadoc
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"; //$NON-NLS-1$
47      protected static final String DEFAULT_FILE_ENDING = ".xml"; //$NON-NLS-1$
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"); //$NON-NLS-1$
65          new MainWindow();
66      }
67  
68      public MainWindow() {
69          this.setTitle(Messages.getString("MainWindow.app.titlebar")); //$NON-NLS-1$
70          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
71          log("Creating actions"); //$NON-NLS-1$
72          createActions();
73          this.setJMenuBar(buildMenuBar());
74  
75          log("Configuring UI"); //$NON-NLS-1$
76          GUIHelperMethods.configureUI();
77  
78          JPanel cp = new JPanel(new BorderLayout());
79          cp.add(new HeaderPanel(), BorderLayout.NORTH);
80          //mainPanel = new JLabel("Laddar");
81          //cp.add(mainPanel, BorderLayout.CENTER);
82          cp.add(new StatusBar(), BorderLayout.SOUTH);
83          this.setContentPane(cp);
84          this.setSize(500, 500);
85          //GUIHelperMethods.centerOnScreen(this);
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          // Handle loading of XML-files
100         openFileAction = new AbstractAction(Messages.getString("MainWindow.menu.open")) { //$NON-NLS-1$
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; //$NON-NLS-1$
113                     }
114                 });
115 
116                 // Open file dialog.
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")) { //$NON-NLS-1$
124             public void actionPerformed(ActionEvent arg0) {
125                 JFileChooser fileChooser = new JFileChooser(new File(DEFAULT_FILE_LOC));
126 
127                 // Open file dialog.
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")) { //$NON-NLS-1$
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()); //$NON-NLS-1$
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(""); //$NON-NLS-1$
170                 if (first == null) {
171                     first = textarea;
172                 }
173                 textarea.setColumns(30);
174                 textarea.setWrapStyleWord(true);
175                 // TODO This line causes the whole thing to fail!
176                 // Not all lines are visible, but I would really like this...
177                 //textarea.setLineWrap(true);
178                 textarea.setBorder(BorderFactory.createLoweredBevelBorder());
179                 panel.add(textarea);
180 
181                 inputFields.put(msg.getId(), textarea);
182             }
183 
184             // panel, rows, cols, initialX, initialY, xPad, yPad
185             SpringUtilities.makeCompactGrid(panel, numberOfFields, 3, 3, 3, 3, 3);
186             log("Added " + numberOfFields + " items to InputFieldPanel"); //$NON-NLS-1$ //$NON-NLS-2$
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")); //$NON-NLS-1$
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          * menu.addSeparator(); menu.add(new JMenuItem("Print..."));
233          * 
234          * 
235          * menu = new JMenu("Edit"); menu.add(new JMenuItem("Cut")); menu.add(new
236          * JMenuItem("Copy")); menu.add(new JMenuItem("Paste")); menuBar.add(menu);
237          */
238 
239         return menuBar;
240     }
241 
242     // Helper Code ********************************************************
243     private static void log(String s) {
244         System.out.println(s);
245     }
246 }