ancient-projects

My earliest programs (2006-2010)
git clone https://git.tronto.net/ancient-projects
Download | Log | Files | Refs | README

Frame.java (7987B)


      1 /*
      2 Copyright 2009 Sebastiano Tronto <sebastiano@luganega.org>
      3 
      4 This file is part of PBFI (Porky's Brain F*** Interpreter).
      5 
      6     PBFI is free software; you can redistribute it and/or modify
      7     it under the terms of the GNU General Public License as published by
      8     the Free Software Foundation; either version 2 of the License, or
      9     (at your option) any later version.
     10 
     11     PBFI is distributed in the hope that it will be useful,
     12     but WITHOUT ANY WARRANTY; without even the implied warranty of
     13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14     GNU General Public License for more details.
     15 
     16     You should have received a copy of the GNU General Public License
     17     along with PBFI; if not, write to the Free Software
     18     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     19 
     20 */
     21 
     22 import javax.swing.*;
     23 import java.awt.BorderLayout;
     24 import java.awt.event.*;
     25 import java.io.*;
     26 
     27 public class Frame extends JFrame {
     28 public static int valoreChooser;
     29 	public static boolean firstSave;
     30 	public static String defaultSave;
     31 	public static Main main;
     32 	public static JTextArea textArea;
     33 	public static JButton runButton;
     34 	public static JMenuBar bar;
     35 	public static JMenu menuFile, menuAiuto;
     36 	public static JMenuItem esci, esegui, salva, salvaConNome, apri, info;
     37 	public static JFileChooser chooser;
     38 	public static FileInputStream in;
     39 	public static FileOutputStream out;
     40 		public Frame() {
     41 		super( "Porky's BrainF** Interpreter - v1.1" );
     42 		firstSave = true;
     43 		main = new Main();
     44 		defaultSave = "";
     45 		chooser = new JFileChooser();
     46 		textArea = new JTextArea( "Digitare qui il codice BrainF***", 15, 15 );
     47 		bar = new JMenuBar();
     48 		menuFile = new JMenu( "File" );
     49 		menuAiuto = new JMenu( "?" );
     50 		esci = new JMenuItem( "Esci" );
     51 		esegui = new JMenuItem( "Esegui" );
     52 		salva = new JMenuItem( "Salva" );
     53 		salvaConNome = new JMenuItem( "Salva con nome" );
     54 		apri = new JMenuItem( "Apri" );
     55 		info = new JMenuItem( "Aiuto" );
     56 		menuFile.setMnemonic( 'F' );
     57 		menuAiuto.setMnemonic( '?' );
     58 		esci.setMnemonic( 'E' );
     59 		esegui.setMnemonic( 's' );
     60 		salva.setMnemonic( 'a' );
     61 		salvaConNome.setMnemonic( 'n' );
     62 		apri.setMnemonic( 'p' );
     63 		info.setMnemonic( 'i' );
     64 		esci.addActionListener(
     65 			new ActionListener() {
     66 				public void actionPerformed ( ActionEvent e ) {
     67 					System.exit( 0 );
     68 				}
     69 			}
     70 		);
     71 		esegui.addActionListener(
     72 			new ActionListener() {
     73 				public void actionPerformed ( ActionEvent e ) {
     74 					main.run( textArea.getText() );
     75 				}
     76 			}
     77 		);
     78 		salva.addActionListener(
     79 			new ActionListener() {
     80 				public void actionPerformed ( ActionEvent e ) {
     81 					if ( firstSave ) {
     82 						valoreChooser = chooser.showSaveDialog( Frame.this );
     83 						switch ( valoreChooser ) {
     84 							case JFileChooser.APPROVE_OPTION:
     85 								try {
     86 									out = new FileOutputStream( chooser.getSelectedFile().getPath() );
     87 									defaultSave = chooser.getSelectedFile().getPath();
     88 									char caratteri[];
     89 									byte bytes[];
     90 									caratteri = textArea.getText().toCharArray();
     91 									bytes = new byte[caratteri.length];
     92 									for ( int i = 0; i <  caratteri.length; i++ )
     93 										bytes[i] = ( byte ) caratteri[i];
     94 									out.write( bytes );
     95 								} catch ( Exception ex ) {
     96 									JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + ex + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
     97 								}
     98 								break;
     99 							case JFileChooser.ERROR_OPTION:
    100 								JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + e + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
    101 								break;
    102 							//case JFileChooser.CANCEL_OPTION:
    103 						}
    104 						firstSave = false;
    105 					} else {
    106 						try {
    107 							out = new FileOutputStream( defaultSave );
    108 							char caratteri[];
    109 							byte bytes[];
    110 							caratteri = textArea.getText().toCharArray();
    111 							bytes = new byte[caratteri.length];
    112 							for ( int i = 0; i <  caratteri.length; i++ )
    113 								bytes[i] = ( byte ) caratteri[i];
    114 							out.write( bytes );
    115 						} catch ( Exception ex ) {
    116 							JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + ex + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
    117 						}
    118 					}
    119 				}
    120 			}
    121 		);
    122 		salvaConNome.addActionListener(
    123 			new ActionListener() {
    124 				public void actionPerformed ( ActionEvent e ) {
    125 					valoreChooser = chooser.showSaveDialog( Frame.this );
    126 					switch ( valoreChooser ) {
    127 						case JFileChooser.APPROVE_OPTION:
    128 							try {
    129 								out = new FileOutputStream( chooser.getSelectedFile().getPath() );
    130 								defaultSave = chooser.getSelectedFile().getPath();
    131 								char caratteri[];
    132 								byte bytes[];
    133 								caratteri = textArea.getText().toCharArray();
    134 								bytes = new byte[caratteri.length];
    135 								for ( int i = 0; i <  caratteri.length; i++ )
    136 									bytes[i] = ( byte ) caratteri[i];
    137 								out.write( bytes );
    138 							} catch ( Exception ex ) {
    139 								JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + ex + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
    140 							}
    141 							break;
    142 						case JFileChooser.ERROR_OPTION:
    143 							JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + e + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
    144 							break;
    145 						//case JFileChooser.CANCEL_OPTION:
    146 					}
    147 					firstSave = false;
    148 				}
    149 			}
    150 		);
    151 		apri.addActionListener(
    152 			new ActionListener() {
    153 				public void actionPerformed ( ActionEvent e ) {
    154 					valoreChooser = chooser.showOpenDialog( Frame.this );
    155 					switch ( valoreChooser ) {
    156 						case JFileChooser.APPROVE_OPTION:
    157 							try {
    158 								String stringa;
    159 								byte bytes[];
    160 								char caratteri[];
    161 								in = new FileInputStream( chooser.getSelectedFile().getPath() );
    162 								bytes = new byte[in.available()];
    163 								caratteri = new char[in.available()];
    164 								in.read( bytes );
    165 								for ( int i = 0; i < bytes.length; i++ )
    166 									caratteri[i] = ( char ) bytes[i];
    167 								stringa = new String( caratteri );
    168 								textArea.setText( stringa );
    169 							} catch ( Exception ex ) {
    170 								JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + ex + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
    171 							}
    172 							break;
    173 						case JFileChooser.ERROR_OPTION:
    174 							JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + e + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
    175 							break;
    176 						//case JFileChooser.CANCEL_OPTION:
    177 					}
    178 				}
    179 			}
    180 		);
    181 		info.addActionListener(
    182 			new ActionListener() {
    183 				public void actionPerformed ( ActionEvent e ) {
    184 					JOptionPane.showMessageDialog( Frame.this, "Brainfuck è un linguaggio di programmazione esoterico per computer,\ncreato da Urban Müller intorno al 1993. Il linguaggio viene\nin taluni casi denominato Brainf*ck, Brainf***\no anche soltanto BF per evitare di offendere\nla sensibilità altrui.\nNella cartella src trovate alcuni esempi di programmi in questo linguaggio.\n\nPer maggiori informazioni visitate la pagina:\nhttp://it.wikipedia.org/wiki/Brainfuck", "BrainF*** - Informazioni", JOptionPane.INFORMATION_MESSAGE );
    185 				}
    186 			}
    187 		);
    188 		menuFile.add( esegui );
    189 		menuFile.add( salva );
    190 		menuFile.add( salvaConNome );
    191 		menuFile.add( apri );
    192 		menuFile.add( esci );
    193 		menuAiuto.add( info );
    194 		bar.add( menuFile );
    195 		bar.add( menuAiuto );
    196 		runButton = new JButton( "Esegui" );
    197 		runButton.addActionListener(
    198 			new ActionListener() {
    199 				public void actionPerformed( ActionEvent e ) {
    200 					main.run( textArea.getText() );
    201 				}
    202 			}
    203 		);
    204 		setLayout( new BorderLayout() );
    205 		setJMenuBar( bar );
    206 		add( new JScrollPane( textArea ), BorderLayout.CENTER );
    207 		add( runButton, BorderLayout.SOUTH );
    208 	}
    209 }