aboutsummaryrefslogtreecommitdiff
path: root/pbfi/pbfi-1.1/src/Frame.java
blob: 906a16b73b2ae6b2c412a461f9f79655a04c8ae1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Copyright 2009 Sebastiano Tronto <sebastiano@luganega.org>

This file is part of PBFI (Porky's Brain F*** Interpreter).

    PBFI is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    PBFI is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PBFI; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.io.*;

public class Frame extends JFrame {
public static int valoreChooser;
	public static boolean firstSave;
	public static String defaultSave;
	public static Main main;
	public static JTextArea textArea;
	public static JButton runButton;
	public static JMenuBar bar;
	public static JMenu menuFile, menuAiuto;
	public static JMenuItem esci, esegui, salva, salvaConNome, apri, info;
	public static JFileChooser chooser;
	public static FileInputStream in;
	public static FileOutputStream out;
		public Frame() {
		super( "Porky's BrainF** Interpreter - v1.1" );
		firstSave = true;
		main = new Main();
		defaultSave = "";
		chooser = new JFileChooser();
		textArea = new JTextArea( "Digitare qui il codice BrainF***", 15, 15 );
		bar = new JMenuBar();
		menuFile = new JMenu( "File" );
		menuAiuto = new JMenu( "?" );
		esci = new JMenuItem( "Esci" );
		esegui = new JMenuItem( "Esegui" );
		salva = new JMenuItem( "Salva" );
		salvaConNome = new JMenuItem( "Salva con nome" );
		apri = new JMenuItem( "Apri" );
		info = new JMenuItem( "Aiuto" );
		menuFile.setMnemonic( 'F' );
		menuAiuto.setMnemonic( '?' );
		esci.setMnemonic( 'E' );
		esegui.setMnemonic( 's' );
		salva.setMnemonic( 'a' );
		salvaConNome.setMnemonic( 'n' );
		apri.setMnemonic( 'p' );
		info.setMnemonic( 'i' );
		esci.addActionListener(
			new ActionListener() {
				public void actionPerformed ( ActionEvent e ) {
					System.exit( 0 );
				}
			}
		);
		esegui.addActionListener(
			new ActionListener() {
				public void actionPerformed ( ActionEvent e ) {
					main.run( textArea.getText() );
				}
			}
		);
		salva.addActionListener(
			new ActionListener() {
				public void actionPerformed ( ActionEvent e ) {
					if ( firstSave ) {
						valoreChooser = chooser.showSaveDialog( Frame.this );
						switch ( valoreChooser ) {
							case JFileChooser.APPROVE_OPTION:
								try {
									out = new FileOutputStream( chooser.getSelectedFile().getPath() );
									defaultSave = chooser.getSelectedFile().getPath();
									char caratteri[];
									byte bytes[];
									caratteri = textArea.getText().toCharArray();
									bytes = new byte[caratteri.length];
									for ( int i = 0; i <  caratteri.length; i++ )
										bytes[i] = ( byte ) caratteri[i];
									out.write( bytes );
								} catch ( Exception ex ) {
									JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + ex + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
								}
								break;
							case JFileChooser.ERROR_OPTION:
								JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + e + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
								break;
							//case JFileChooser.CANCEL_OPTION:
						}
						firstSave = false;
					} else {
						try {
							out = new FileOutputStream( defaultSave );
							char caratteri[];
							byte bytes[];
							caratteri = textArea.getText().toCharArray();
							bytes = new byte[caratteri.length];
							for ( int i = 0; i <  caratteri.length; i++ )
								bytes[i] = ( byte ) caratteri[i];
							out.write( bytes );
						} catch ( Exception ex ) {
							JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + ex + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
						}
					}
				}
			}
		);
		salvaConNome.addActionListener(
			new ActionListener() {
				public void actionPerformed ( ActionEvent e ) {
					valoreChooser = chooser.showSaveDialog( Frame.this );
					switch ( valoreChooser ) {
						case JFileChooser.APPROVE_OPTION:
							try {
								out = new FileOutputStream( chooser.getSelectedFile().getPath() );
								defaultSave = chooser.getSelectedFile().getPath();
								char caratteri[];
								byte bytes[];
								caratteri = textArea.getText().toCharArray();
								bytes = new byte[caratteri.length];
								for ( int i = 0; i <  caratteri.length; i++ )
									bytes[i] = ( byte ) caratteri[i];
								out.write( bytes );
							} catch ( Exception ex ) {
								JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + ex + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
							}
							break;
						case JFileChooser.ERROR_OPTION:
							JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + e + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
							break;
						//case JFileChooser.CANCEL_OPTION:
					}
					firstSave = false;
				}
			}
		);
		apri.addActionListener(
			new ActionListener() {
				public void actionPerformed ( ActionEvent e ) {
					valoreChooser = chooser.showOpenDialog( Frame.this );
					switch ( valoreChooser ) {
						case JFileChooser.APPROVE_OPTION:
							try {
								String stringa;
								byte bytes[];
								char caratteri[];
								in = new FileInputStream( chooser.getSelectedFile().getPath() );
								bytes = new byte[in.available()];
								caratteri = new char[in.available()];
								in.read( bytes );
								for ( int i = 0; i < bytes.length; i++ )
									caratteri[i] = ( char ) bytes[i];
								stringa = new String( caratteri );
								textArea.setText( stringa );
							} catch ( Exception ex ) {
								JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + ex + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
							}
							break;
						case JFileChooser.ERROR_OPTION:
							JOptionPane.showMessageDialog( Frame.this, "Errore sconosciuto:\n\n" + e + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Errore", JOptionPane.ERROR_MESSAGE );
							break;
						//case JFileChooser.CANCEL_OPTION:
					}
				}
			}
		);
		info.addActionListener(
			new ActionListener() {
				public void actionPerformed ( ActionEvent e ) {
					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 );
				}
			}
		);
		menuFile.add( esegui );
		menuFile.add( salva );
		menuFile.add( salvaConNome );
		menuFile.add( apri );
		menuFile.add( esci );
		menuAiuto.add( info );
		bar.add( menuFile );
		bar.add( menuAiuto );
		runButton = new JButton( "Esegui" );
		runButton.addActionListener(
			new ActionListener() {
				public void actionPerformed( ActionEvent e ) {
					main.run( textArea.getText() );
				}
			}
		);
		setLayout( new BorderLayout() );
		setJMenuBar( bar );
		add( new JScrollPane( textArea ), BorderLayout.CENTER );
		add( runButton, BorderLayout.SOUTH );
	}
}

Generated with cgit - Back to sebastiano.tronto.net