package jsetool.gui; import java.awt.*; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import javax.swing.*; import javax.swing.border.BevelBorder; /** * @author Mike Lawson * * @since Oct 29, 2004 * * The FSMEditingPane class allows the user to enter a FSM graphically. It * includes a toolbar and a pane on which to draw the FSM. * * */ public class FSMEditingPane extends JPanel { protected FSMDrawingPane drawingPane; protected JScrollPane scroll = null; /** * Constructs a new finite state machine editing pane. */ public FSMEditingPane() { configureDisplay(); } /** * Returns the labeled state the user has selected to be the initial state * * @return LabeledState */ public LabeledState getInitialState() { return drawingPane.getInitialState(); } /** * Reads the drawing pane from the given file name. * * @param file * @throws Exception */ public void readDrawingPane(File file) throws Exception { File saveFile = file; FileInputStream fis = new FileInputStream(saveFile); ObjectInputStream ois = new ObjectInputStream(fis); drawingPane = (FSMDrawingPane) ois.readObject(); scroll.setViewportView(drawingPane); } /** * Clears all the objects out of the drawing. */ public void resetObjects() { drawingPane.resetObjects(); } /** * Saves the drawing pane to the given file name. * * @param file * @throws Exception */ public void saveDrawingPane(File file) throws Exception { File saveFile = file; FileOutputStream fos = new FileOutputStream(saveFile); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(drawingPane); } /* * (non-Javadoc) * * @see jsetool.gui.DrawingPane#configureDisplay() */ protected void configureDisplay() { setLayout(new BorderLayout()); JPanel buttonPanel = createButtonPanel(); // put the button panel on the left hand side of the panel JPanel tempPanel = new JPanel(); tempPanel.setLayout(new BorderLayout()); tempPanel.add(buttonPanel, BorderLayout.WEST); tempPanel.setBorder(new BevelBorder(0)); add(tempPanel, BorderLayout.NORTH); drawingPane = new FSMDrawingPane(); scroll = new JScrollPane(drawingPane); scroll.setPreferredSize(new Dimension(200, 200)); scroll.setAutoscrolls(true); scroll.setBorder(new BevelBorder(0)); add(scroll, BorderLayout.CENTER); } /** * Clears the current diagram. * */ public void clearDiagram() { drawingPane = new FSMDrawingPane(); scroll.setViewportView(drawingPane); } /** * Prompts the user for a file to read and reads it. * */ protected void promptAndReadDrawing() { try { JFileChooser fc = new JFileChooser(); int retval = fc.showOpenDialog(this); if (retval == JFileChooser.APPROVE_OPTION) { readDrawingPane(fc.getSelectedFile()); } } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(this, "Failed to load drawing", "Error Reading", JOptionPane.ERROR_MESSAGE); } } /** * Prompts the user for a file to write to and writes it. * */ protected void promptAndSaveDrawing() { try { JFileChooser fc = new JFileChooser(); int retval = fc.showSaveDialog(this); if (retval == JFileChooser.APPROVE_OPTION) { saveDrawingPane(fc.getSelectedFile()); } } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(this, "Failed to save drawing", "Error Saving", JOptionPane.ERROR_MESSAGE); } } /** * Creates the panel that shows the drawing tools. The panel is assumed to be * drawn top to bottom. * * @return JPanel */ private JPanel createButtonPanel() { JPanel buttonPanel = new JPanel(); JButton newStateButton = makeStateButton(); buttonPanel.add(newStateButton); JButton drawEdgeButton = makeEdgeButton(); buttonPanel.add(drawEdgeButton); // Normal Operations Button JButton normalOpsButton = makeNormalOpsButton(); buttonPanel.add(normalOpsButton); JButton saveButton = makeSaveButton(); buttonPanel.add(saveButton); JButton loadButton = makeLoadButton(); buttonPanel.add(loadButton); JButton clearButton = makeClearButton(); buttonPanel.add(clearButton); return buttonPanel; } /** * @return */ private JButton makeButton(String imagePath, String defaultText) { ImageIcon icon = getImageIcon(imagePath); JButton button = null; if (icon == null) { button = new JButton(defaultText); } else { Image img = icon.getImage(); icon.setImage(img.getScaledInstance(12, 12, Image.SCALE_SMOOTH)); button = new JButton(icon); button.setPreferredSize(new Dimension(24, 24)); button.setToolTipText(defaultText); } return button; } /** * @return */ private JButton makeClearButton() { JButton clearButton = makeButton("images/new.gif", "Clear"); clearButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { clearDiagram(); } }); return clearButton; } /** * @return */ private JButton makeLoadButton() { // Load Button JButton loadButton = makeButton("images/open.gif", "Load"); loadButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { promptAndReadDrawing(); } }); return loadButton; } /** * @return */ private JButton makeSaveButton() { // Save Button JButton saveButton = makeButton("images/save.gif", "Save"); saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { promptAndSaveDrawing(); } }); return saveButton; } /** * @return */ private JButton makeNormalOpsButton() { JButton normalOpsButton = makeButton("images/Select.gif", "Select"); normalOpsButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawingPane.drawingNormalOpsSelected(); } }); return normalOpsButton; } /** * @return */ private JButton makeEdgeButton() { JButton drawEdgeButton = makeButton("images/BlueArrow.gif", "Connect"); drawEdgeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawingPane.drawingEdgeToolSelected(); } }); return drawEdgeButton; } /** * @return */ private JButton makeStateButton() { // New State Button JButton newStateButton = makeButton("images/State.gif", "State"); newStateButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawingPane.drawingStateToolSelected(); } }); return newStateButton; } private ImageIcon getImageIcon(String path) { int MAX_IMAGE_SIZE = 2400; //Change this to the size of //your biggest image, in bytes. int count = 0; BufferedInputStream imgStream = new BufferedInputStream( this.getClass().getResourceAsStream(path)); if (imgStream != null) { byte buf[] = new byte[MAX_IMAGE_SIZE]; try { count = imgStream.read(buf); imgStream.close(); } catch (java.io.IOException ioe) { System.err.println("Couldn't read stream from file: " + path); ioe.printStackTrace(); return null; } if (count <= 0) { System.err.println("Empty file: " + path); return null; } return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buf)); } else { System.err.println("Couldn't find file: " + path); return null; } } }