From: Erik V. <ev...@us...> - 2009-11-28 22:41:19
|
Update of /cvsroot/rails/18xx/rails/ui/swing In directory sfp-cvsdas-1.v30.ch3.sourceforge.com:/tmp/cvs-serv1818/rails/ui/swing Modified Files: ReportWindow.java Log Message: Added option to make Report Window editable, and to save from and load files into this window Index: ReportWindow.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/ReportWindow.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** ReportWindow.java 27 Nov 2009 20:32:18 -0000 1.12 --- ReportWindow.java 28 Nov 2009 22:41:04 -0000 1.13 *************** *** 4,14 **** --- 4,28 ---- import java.awt.*; import java.awt.event.*; + import java.io.BufferedReader; + import java.io.File; + import java.io.FileReader; + import java.io.FileWriter; + import java.io.IOException; + import java.io.PrintWriter; + import java.util.Date; import javax.swing.*; + import org.apache.log4j.Logger; + + import rails.game.DisplayBuffer; + import rails.game.Game; + import rails.game.GameManager; import rails.game.GameManagerI; import rails.game.ReportBuffer; + import rails.ui.swing.elements.ActionMenuItem; import rails.util.Config; import rails.util.LocalText; + import rails.util.Util; /** *************** *** 16,43 **** * during the rails.game. */ ! public class ReportWindow extends JFrame implements KeyListener { private static final long serialVersionUID = 1L; ! private JTextArea message; private JScrollPane messageScroller; private JScrollBar vbar; private JPanel messagePanel; private ReportWindow messageWindow; private GameManagerI gameManager; public ReportWindow(GameManagerI gameManager) { messageWindow = this; this.gameManager = gameManager; ! message = new JTextArea(); ! message.setEditable(false); ! message.setLineWrap(false); ! message.setBackground(Color.WHITE); ! message.setOpaque(true); ! message.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); messagePanel = new JPanel(new GridBagLayout()); messageScroller = ! new JScrollPane(message, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); --- 30,75 ---- * during the rails.game. */ ! public class ReportWindow extends JFrame implements ActionListener, KeyListener { private static final long serialVersionUID = 1L; ! private JTextArea reportText; private JScrollPane messageScroller; private JScrollBar vbar; private JPanel messagePanel; private ReportWindow messageWindow; + private JMenuBar menuBar; + private JMenu fileMenu, editMenu; + private JMenuItem saveItem, loadItem, printItem, findItem; + private GameManagerI gameManager; + + private String reportDirectory = Config.get("report.directory"); + private String reportFile; + + private boolean editable = "yes".equalsIgnoreCase(Config.get("report.window.editable")); + protected static final String SAVE_CMD = "Save"; + protected static final String LOAD_CMD = "Load"; + protected static final String PRINT_CMD = "Print"; + protected static final String FIND_CMD = "Find"; + + protected static Logger log = + Logger.getLogger(ReportWindow.class.getPackage().getName()); + + public ReportWindow(GameManagerI gameManager) { messageWindow = this; this.gameManager = gameManager; ! reportText = new JTextArea(); ! reportText.setEditable(editable); ! reportText.setLineWrap(false); ! reportText.setBackground(Color.WHITE); ! reportText.setOpaque(true); ! reportText.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); messagePanel = new JPanel(new GridBagLayout()); messageScroller = ! new JScrollPane(reportText, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); *************** *** 45,51 **** GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = gbc.gridy = 0; ! gbc.weightx = gbc.weighty = 0.5; gbc.fill = GridBagConstraints.BOTH; messagePanel.add(messageScroller, gbc); setContentPane(messagePanel); --- 77,112 ---- GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = gbc.gridy = 0; ! gbc.weightx = gbc.weighty = 1.0; gbc.fill = GridBagConstraints.BOTH; messagePanel.add(messageScroller, gbc); + + menuBar = new JMenuBar(); + fileMenu = new JMenu(LocalText.getText("FILE")); + fileMenu.setMnemonic(KeyEvent.VK_F); + editMenu = new JMenu(LocalText.getText("EDIT")); + editMenu.setMnemonic(KeyEvent.VK_E); + + loadItem = new ActionMenuItem(LocalText.getText("LOAD")); + loadItem.setActionCommand(LOAD_CMD); + loadItem.setMnemonic(KeyEvent.VK_L); + loadItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, + ActionEvent.ALT_MASK)); + loadItem.addActionListener(this); + loadItem.setEnabled(true); + fileMenu.add(loadItem); + + saveItem = new ActionMenuItem(LocalText.getText("SAVE")); + saveItem.setActionCommand(SAVE_CMD); + saveItem.setMnemonic(KeyEvent.VK_S); + saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, + ActionEvent.ALT_MASK)); + saveItem.addActionListener(this); + saveItem.setEnabled(true); + fileMenu.add(saveItem); + + menuBar.add(fileMenu); + + setJMenuBar(menuBar); + setContentPane(messagePanel); *************** *** 69,73 **** String newText = ReportBuffer.get(); if (newText.length() > 0) { ! message.append(newText); scrollDown(); } --- 130,134 ---- String newText = ReportBuffer.get(); if (newText.length() > 0) { ! reportText.append(newText); scrollDown(); } *************** *** 82,85 **** --- 143,213 ---- } + public void actionPerformed(ActionEvent actor) { + String command = actor.getActionCommand(); + if (LOAD_CMD.equalsIgnoreCase(command)) { + loadReportFile(); + } else if (SAVE_CMD.equalsIgnoreCase(command)) { + saveReportFile(); + } + } + + private void loadReportFile() { + + JFileChooser jfc = new JFileChooser(); + if (Util.hasValue(reportDirectory)) + jfc.setCurrentDirectory(new File(reportDirectory)); + File selectedFile; + + if (jfc.showOpenDialog(getContentPane()) == JFileChooser.APPROVE_OPTION) { + selectedFile = jfc.getSelectedFile(); + reportFile = selectedFile.getPath(); + reportDirectory = selectedFile.getParent(); + } else { + return; + } + + try { + BufferedReader in = new BufferedReader (new FileReader(selectedFile)); + String line; + StringBuffer b = new StringBuffer(); + while ((line = in.readLine()) != null) b.append(line).append("\n"); + in.close(); + reportText.setText(b.toString()); + } catch (IOException e) { + log.error ("Error whilst reading file "+reportFile, e); + JOptionPane.showMessageDialog(this, + e.getMessage(), "", JOptionPane.ERROR_MESSAGE); + } + } + + private void saveReportFile () { + + JFileChooser jfc = new JFileChooser(); + if (Util.hasValue(reportDirectory)) { + jfc.setCurrentDirectory(new File(reportDirectory)); + } + if (Util.hasValue(reportFile)) { + jfc.setSelectedFile(new File(reportFile)); + } + if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { + File selectedFile = jfc.getSelectedFile(); + String filepath = selectedFile.getPath(); + reportDirectory = selectedFile.getParent(); + if (!selectedFile.getName().equalsIgnoreCase(reportFile)) { + reportFile = filepath; + } + + try { + PrintWriter out = new PrintWriter (new FileWriter (new File (reportFile))); + out.print(reportText.getText()); + out.close(); + } catch (IOException e) { + log.error ("Error whilst writing file "+reportFile, e); + JOptionPane.showMessageDialog(this, + e.getMessage(), "", JOptionPane.ERROR_MESSAGE); + } + } + } + public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_F1) { |