/** *

Title: SVG editor

*

Description: Batik applicatie

*

Copyright: Copyright (c) 2003 Cha Suzanna - Haesen Mieke

* @version 1.0 */ package svgeditor; import java.io.*; import java.lang.Object; import java.io.File; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.filechooser.*; import org.apache.batik.i18n.LocalizableSupport; import org.apache.batik.transcoder.Transcoder; import org.apache.batik.transcoder.TranscoderInput; import org.apache.batik.transcoder.TranscoderOutput; import org.apache.batik.transcoder.TranscodingHints; import org.apache.batik.transcoder.svg2svg.SVGTranscoder; public class EditorFrame extends JFrame { private JPanel contentPane; private JMenuBar jMenuBar1 = new JMenuBar(); private JMenu jMenuFile = new JMenu(); private JMenuItem jMenuFileExit = new JMenuItem(); private JMenu jMenuHelp = new JMenu(); private JMenuItem jMenuHelpAbout = new JMenuItem(); private JMenuItem jMenuFileOpen = new JMenuItem(); private JMenuItem jMenuFileSave = new JMenuItem(); private JMenuItem jMenuFilePrettyPrint = new JMenuItem(); private JFileChooser jFileChooser = new JFileChooser(); //private ExampleFileFilter fileFilter1 = new ExampleFileFilter("svg","Scalable Vector Graphics");; private JEditorPane jEditorPane = new JEditorPane(); private BorderLayout borderLayout1 = new BorderLayout(); private int index; protected Transcoder transcoder = new SVGTranscoder(); //Construct the frame public EditorFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(EditorFrame.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); //jFileChooser.setFileFilter(fileFilter1); contentPane.setLayout(borderLayout1); this.setSize(new Dimension(800, 600)); this.setTitle("SVG Editor"); jMenuFile.setText("File"); jMenuFileExit.setText("Exit"); jMenuFileExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuFileExit_actionPerformed(e); } }); jMenuHelp.setText("Help"); jMenuHelpAbout.setText("About"); jMenuHelpAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuHelpAbout_actionPerformed(e); } }); jMenuFileOpen.setText("Open"); jMenuFileOpen.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jMenuFileOpen_actionPerformed(e); } }); jMenuFileSave.setText("Save"); jMenuFileSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jMenuFileSave_actionPerformed(e); } }); jMenuFilePrettyPrint.setText("Pretty Print"); jMenuFilePrettyPrint.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jMenuFilePrettyPrint_actionPerformed(e); } }); jFileChooser.setDialogTitle("Open/Save SVG document"); //jFileChooser.setFileFilter(fileFilter1); contentPane.setMinimumSize(new Dimension(800, 600)); jMenuFile.add(jMenuFileOpen); jMenuFile.add(jMenuFileSave); jMenuFile.add(jMenuFilePrettyPrint); jMenuFile.add(jMenuFileExit); jMenuHelp.add(jMenuHelpAbout); jMenuBar1.add(jMenuFile); jMenuBar1.add(jMenuHelp); contentPane.add(jEditorPane, BorderLayout.CENTER); this.setJMenuBar(jMenuBar1); JScrollPane jScrollPane = new JScrollPane(jEditorPane); getContentPane().add("Center",jScrollPane); } //File | Exit action performed public void jMenuFileExit_actionPerformed(ActionEvent e) { System.exit(0); } //Help | About action performed public void jMenuHelpAbout_actionPerformed(ActionEvent e) { EditorFrame_AboutBox dlg = new EditorFrame_AboutBox(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.pack(); dlg.show(); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { jMenuFileExit_actionPerformed(null); } } void jMenuFileOpen_actionPerformed(ActionEvent e) { int returnVal = jFileChooser.showOpenDialog(EditorFrame.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = jFileChooser.getSelectedFile(); try{ FileInputStream in = new FileInputStream(file); byte bt[] = new byte[(int)file.length()]; int i; i = in.read(bt); String s = new String(bt); jEditorPane.setText(s); }catch(java.io.IOException exc){ System.out.println(exc.toString()); } } } void jMenuFileSave_actionPerformed(ActionEvent e) { int returnVal = jFileChooser.showSaveDialog(EditorFrame.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = jFileChooser.getSelectedFile(); try{ String text = jEditorPane.getText(); byte b[] = text.getBytes(); FileOutputStream out = new FileOutputStream(file); out.write(b); out.close(); }catch (java.io.IOException exc){ System.out.println(exc.toString()); } } } //Pretty print void jMenuFilePrettyPrint_actionPerformed(ActionEvent e) { try{ //Tekst uit de editor halen String tekst = jEditorPane.getText(); TranscoderInput in; in = new TranscoderInput(tekst); TranscoderOutput out; out = new TranscoderOutput(tekst); //Pretty print zelf uitvoeren transcoder.transcode(in, out); //Aangepaste tekst opnieuw in editor weergeven jEditorPane.setText(tekst); } catch (Exception exc) { jEditorPane.setText("Pretty print mislukt"); } } }