Frame.java (10013B)
1 /* 2 Copyright 2009 Sebastiano Tronto <sebastiano@luganega.org> 3 4 This file is part of SlackYouInterpreter. 5 6 SlackYouInterpreter 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 SlackYouInterpreter 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 SlackYouInterpreter; 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 SlackYouInterpreter - v1.1" ); 42 firstSave = true; 43 main = new Main(); 44 defaultSave = ""; 45 chooser = new JFileChooser(); 46 textArea = new JTextArea( "Digitare qui il codice SlackYou", 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", "SlackYou - 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", "SlackYou - 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", "SlackYou - 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", "SlackYou - 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", "SlackYou - 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", "SlackYou - 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", "SlackYou - 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, "SlackYou è un linguaggio di programmazione esoterico,\n ispirato al famoso BrainFuck, ma con molte funzionalità in più.\nInnanzitutto in SlackYou l'array di byte è bidimensionale.\nIl linguaggio è case-sensitive. Ogni carattere che non è valido\ncome comando può essere usato come commento,\ninclusi i caratteri-comando scritti in maiuscolo.\nTutti gli altri comandi sono spiegati nella lista seguente:\n\n. Incrementa di 1 il byte al puntatore. Corrisponde al + in BF.\n, Decrementa di 1 il byte al puntatore. Corrisponde al - in BF.\n> Incrementa il puntatore orizzontale, come in BF.\n< Decrementa il puntatore orizzontale, come in BF.\nv Incrementa il puntatore verticale.\n^ Decrementa il puntatore verticale.\n+ Somma il byte al puntatore con il byte alla sua destra. Il risultato viene memorizzato nel byte al puntatore.\n- Come sopra, ma sottrae.\n* Come sopra, ma moltiplica.\n/ Come sopra, ma esegue una divisione euclidea (senza virgola e senza resto).\n% Come sopra, ma esegue l'operatore modulo (restituisce il resto della divisione euclidea).\nj Copia il byte al puntatore nel byte a destra e incrementa il puntatore orizzontale.\ng Copia il byte al puntatore nel byte a sinistra e decrementa il puntatore orizzontale.\ny Copia il byte al puntatore nel byte sopra e decrementa il puntatore verticale.\nh Copia il byte al puntatore nel byte sotto e incrementa il puntatore verticale.\n=xy Se x e y sono cifre esadecimali (da '0' a '9' e da 'a' a 'f') il byte al puntatore assume il valore esadecimale xy.\nr Memorizza nel byte al puntatore il codice ASCII del carattere digitato in input.\nw Scrive in output il byte al puntatore tradotto in carattere ASCII.\n? Se il byte al puntatore è 0, salta avanti fino all'istruzione dopo il corrispondente comando ; o :.\n! Se il byte al puntatore non è 0, salta avanti fino all'istruzione dopo il corrispondente comando : o ;.\n; Se il byte al puntatore non è 0, salta indietro fino all'istruzione dopo il corrispondente comando ? o !.\n: Se il byte al puntatore è 0, salta indietro fino all'istruzione dopo il corrispondente comando ! o ?.\n\nNella cartella src trovate un esempio di Hello World in questo linguaggio.\n\nPer maggiori informazioni visitate la pagina:\nhttp://porkynator.altervista.org/SlackYou.html\no contattatemi al mio indirizzo e-mail:\nsebastiano@luganega.org", "SlackYou - 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 }