ancient-projects

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

OptionFrame.java (5369B)


      1 /*
      2 Copyright 2009-2010 Sebastiano Tronto <sebastiano@luganega.org>
      3 
      4 This file is part of JBriscola.
      5 
      6     JBriscola 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     JBriscola 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 JBriscola; 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 java.util.Formatter;
     23 import javax.swing.*;
     24 import java.awt.*;
     25 import java.awt.event.*;
     26 
     27 public class OptionFrame extends JFrame {
     28 	
     29 	public static Main main = new Main();
     30 	public static Formatter formatter;
     31 	public static JButton ok, annulla, scegliColore;
     32 	public static JLabel lingua, sfondo;
     33 	public static JComboBox selezionaLingua;
     34 	public static JCheckBox suono, tastiRapidi;
     35 	public static JPanel[] pannello = new JPanel[6];
     36 	
     37 	public OptionFrame() {
     38 		
     39 		super( "JBriscola - " + main.stringaModificaOpzioni );
     40 		
     41 		ok = new JButton( "OK" );
     42 		ok.addActionListener( new ActionListener() {
     43 			
     44 			/* Quando le modifiche vengono confermate premendo ok, viene scritto
     45 			 * il file di configurazione (o aggiornato) se possibile, e modificate
     46 			 * le impostazioni attuali. */
     47 			public void actionPerformed( ActionEvent e ) {
     48 				
     49 				try {
     50 					formatter = new Formatter( "JBriscolaConfig.txt" );
     51 				} catch ( Exception ex ) {
     52 					JOptionPane.showMessageDialog( OptionFrame.this, main.stringaImpossibileConfigurare + ex, "JBriscola - " + main.version, JOptionPane.ERROR_MESSAGE );
     53 				}
     54 				
     55 				String lang;
     56 				
     57 				switch ( selezionaLingua.getSelectedIndex() ) {
     58 					case 0:
     59 						lang = "it";
     60 						break;
     61 					case 1:
     62 						lang = "la";
     63 						break;
     64 					case 2:
     65 						lang = "bl";
     66 						break;
     67 					case 3:
     68 						lang = "bv";
     69 						break;
     70 					case 4:
     71 						lang = "en";
     72 						break;
     73 					default:
     74 						lang = "it";
     75 						break;
     76 				}
     77 				
     78 				String suonosiono, tastisiono;
     79 				
     80 				if ( suono.isSelected() )
     81 					suonosiono = "suono=si";
     82 				else
     83 					suonosiono = "suono=no";
     84 				if ( tastiRapidi.isSelected() )
     85 					tastisiono = "tasti=si";
     86 				else
     87 					tastisiono = "tasti=no";
     88 					
     89 				formatter.format( lang );
     90 				formatter.format( "\n" );
     91 				formatter.format( suonosiono );
     92 				formatter.format( "\n" );
     93 				formatter.format( tastisiono );
     94 				formatter.format( "\n" );
     95 				formatter.format( main.color.getRed() + " " + main.color.getGreen() + " " + main.color.getBlue() );
     96 				formatter.close();
     97 				
     98 				main.settaOpzioni();
     99 				
    100 				OptionFrame.this.dispose();
    101 			}
    102 		});
    103 		
    104 		annulla = new JButton( main.stringaAnnulla );
    105 		annulla.addActionListener( new ActionListener() {
    106 			/* Se l'utente annulla viene chiusa la finestra senza fare nulla. */
    107 			public void actionPerformed( ActionEvent e ) {
    108 				OptionFrame.this.dispose();
    109 			}
    110 		});
    111 		
    112 		scegliColore = new JButton( " " );
    113 		scegliColore.setBackground( main.color );
    114 		scegliColore.addActionListener( new ActionListener() {
    115 			/* Permette di scegliere il colore dello sfondo. */
    116 			public void actionPerformed( ActionEvent e ) {
    117 				Color colore = JColorChooser.showDialog( OptionFrame.this, "JBriscola " + main.version, main.color );
    118 				if ( colore != null )
    119 					main.color = colore;
    120 				OptionFrame.scegliColore.setBackground( main.color );
    121 			}
    122 		} );
    123 		
    124 		/* Vengono impostati e disegnati i vari componenti. */
    125 		lingua = new JLabel( main.stringaLingua );
    126 		sfondo = new JLabel( main.stringaSfondo );
    127 		String[] lingueDaSelezionare = { main.stringaIt, main.stringaLa, main.stringaBL, main.stringaBV, main.stringaEN };
    128 		selezionaLingua = new JComboBox( lingueDaSelezionare );
    129 		int linguaPreselezionata = 0;
    130 		
    131 		if ( main.lang.equals( "it" ) )
    132 			linguaPreselezionata = 0;
    133 		else if ( main.lang.equals( "la" ) )
    134 			linguaPreselezionata = 1;
    135 		else if ( main.lang.equals( "bl" ) )
    136 			linguaPreselezionata = 2;
    137 		else if ( main.lang.equals( "bv" ) )
    138 			linguaPreselezionata = 3;
    139 		else if ( main.lang.equals( "en" ) )
    140 			linguaPreselezionata = 4;
    141 		
    142 		selezionaLingua.setSelectedIndex( linguaPreselezionata );
    143 		suono = new JCheckBox( main.stringaSuono );
    144 		suono.setSelected( main.sound );
    145 		tastiRapidi = new JCheckBox( main.stringaTasti );
    146 		tastiRapidi.setSelected( main.keys );
    147 
    148 		pannello[0] = new JPanel( new GridLayout( 4, 1 ) );
    149 		pannello[1] = new JPanel( new FlowLayout() );
    150 		pannello[2] = new JPanel( new FlowLayout() );
    151 		pannello[3] = new JPanel( new FlowLayout() );
    152 		pannello[4] = new JPanel( new FlowLayout() );
    153 		pannello[5] = new JPanel( new FlowLayout() );
    154 		pannello[5].add( ok );
    155 		pannello[5].add( annulla );
    156 		pannello[4].add( sfondo );
    157 		pannello[4].add( scegliColore );
    158 		pannello[3].add( tastiRapidi );
    159 		pannello[2].add( suono );
    160 		pannello[1].add( lingua );
    161 		pannello[1].add( selezionaLingua );
    162 		pannello[0].add( pannello[1] );
    163 		pannello[0].add( pannello[2] );
    164 		pannello[0].add( pannello[3] );
    165 		pannello[0].add( pannello[4] );
    166 		add( pannello[0], BorderLayout.CENTER );
    167 		add( pannello[5], BorderLayout.SOUTH );
    168 	}
    169 }