Created
December 28, 2011 18:49
-
-
Save TonyWhite/1529141 to your computer and use it in GitHub Desktop.
Java - Integrazione grafica
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Elenca i temi grafici disponibili nel sistema | |
| L'utente sceglie il tema che preferisce | |
| La classe salva il nome del tema nel file "options.txt" nella cartella del programma | |
| Un terzo programma può accedere al file ed applicare il tema senza dover effettuare alcun controllo. | |
| È utile per qualsiasi programma in cui non si vuole implementare una gestione del tema grafico. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Elenca i temi disponibili nel sistema | |
| * Permette di scegliere il tema preferito e salvarlo nelle opzioni | |
| * Se il tema settato nelle opzioni non è disponibile sceglie automaticamente Nimbus | |
| * Se si passa il nome del tema nei parametri di avvia si bypassa la scelta del tema settato nelle opzioni. | |
| */ | |
| import java.io.File; | |
| import java.io.FileWriter; | |
| import java.io.FileReader; | |
| import java.io.BufferedReader; | |
| import java.net.URL; | |
| import javax.swing.JFrame; | |
| import javax.swing.JOptionPane; | |
| import javax.swing.UIManager; | |
| import javax.swing.UIManager.LookAndFeelInfo; | |
| public class Integrazione | |
| { | |
| public static String ABS_PATH; | |
| final static String OPZIONI = "options.txt"; | |
| static String PATH_OPZIONI; | |
| private static String tema; | |
| public static void main(String args[]) | |
| { | |
| ABS_PATH = getAbsolutePath(); // Definisce il percorso assoluto dell'applicazione | |
| PATH_OPZIONI = ABS_PATH + OPZIONI; | |
| String lef = ""; | |
| int cont = 0; | |
| boolean argomento = false; | |
| // Decide se prendere il nome del tema dalle opzioni i dall'argomento di avvio | |
| if (args.length>0) | |
| { | |
| argomento = true; | |
| tema = args[0]; | |
| } | |
| else | |
| { | |
| tema = leggiOpzioni(); | |
| } | |
| // Applica il tema scelto | |
| try | |
| { | |
| for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) | |
| { | |
| lef += info.getName() + "\n"; | |
| cont++; | |
| if (tema.equals(info.getName())) | |
| { | |
| UIManager.setLookAndFeel(info.getClassName()); | |
| } | |
| } | |
| } | |
| catch (Exception e) | |
| { | |
| System.err.println(e.getMessage()); | |
| } | |
| // Visualizza il messaggio con i temi disponibili | |
| String messaggio = ""; | |
| if (cont==0) | |
| { | |
| //messaggio = "Ma su che ambiente ti trovi!? \n Devo saperlo!"; | |
| messaggio = "Impossibile ottenere la lista dei temi disponibili nel tuo sistema."; | |
| System.out.println(messaggio); | |
| JOptionPane.showMessageDialog(null, messaggio, "Integrazione", JOptionPane.WARNING_MESSAGE); | |
| } | |
| else | |
| { | |
| // Se esiste più di un tema disponibile, avvia una finestra di scelta | |
| String lefs[] = lef.split("\n"); // Esiste più di un tema disponibile nel sistema | |
| if (elencaTemi(lef.split("\n"))) | |
| { | |
| salvaOpzioni(tema); | |
| } | |
| /*if (cont==1) | |
| { | |
| messaggio = ""; | |
| System.out.println("È disponibile un solo LookAndFeel:\n\n" + lef); | |
| JOptionPane.showMessageDialog(null, "È disponibile un solo LookAndFeel:\n\n" + lef, "Integrazione", JOptionPane.INFORMATION_MESSAGE); | |
| } | |
| else | |
| { | |
| System.out.println("Sono disponibili " + cont + " LookAndFeel:\n\n" + lef); | |
| JOptionPane.showMessageDialog(null, "Sono disponibili " + cont + " LookAndFeel:\n\n" + lef, "Integrazione", JOptionPane.INFORMATION_MESSAGE); | |
| }*/ | |
| } | |
| System.exit(0); | |
| } | |
| /** | |
| * Elenca i temi e salva le motifiche | |
| * String temi è l'elenco dei temi separati da un "\n" | |
| */ | |
| private static boolean elencaTemi(String[] temi) | |
| { | |
| String scelta = (String)JOptionPane.showInputDialog(new JFrame(),"Temi disponibili:\n", "Integrazione grafica", JOptionPane.PLAIN_MESSAGE, null, temi, tema); | |
| if ((scelta != null) && (scelta.length() > 0)) | |
| { | |
| tema = scelta; | |
| return true; | |
| } | |
| // Non è stata effettuata alcuna scelta | |
| return false; | |
| } | |
| /** | |
| * Legge il tema salvato nel file delle opzioni | |
| */ | |
| private static String leggiOpzioni() | |
| { | |
| String tema = "Nimbus"; | |
| if (new File(PATH_OPZIONI).exists()) | |
| { | |
| // Il file esiste | |
| // Si sceglie il tema salvato | |
| String errore = ""; | |
| try | |
| { | |
| errore = "Impossibile leggere il file " + OPZIONI; | |
| FileReader streamOpzioni = new FileReader(PATH_OPZIONI); | |
| BufferedReader bufferOpzioni = new BufferedReader(streamOpzioni); | |
| errore = "Impossibile leggere il contenuto del file " + OPZIONI; | |
| tema = bufferOpzioni.readLine(); | |
| } | |
| catch(Exception e) | |
| { | |
| System.out.println("ERRORE: " + errore); | |
| } | |
| } | |
| else | |
| { | |
| // Il file non esiste | |
| // Si sceglie il tema di default: Nimbus | |
| System.out.println("Il file " + OPZIONI + " non esiste."); | |
| } | |
| return tema; | |
| } | |
| /** | |
| * Salva il tema scelto nel file delle opzioni | |
| */ | |
| private static void salvaOpzioni(String tema) | |
| { | |
| String errore = ""; | |
| try | |
| { | |
| errore = "Impossibile aprire il file " + OPZIONI; | |
| FileWriter fileOpzioni = new FileWriter(PATH_OPZIONI); | |
| char data[] = tema.toCharArray(); // Prepara i caratteri da scrivere sul file | |
| errore = "Impossibile scrivere sul file " + OPZIONI; | |
| fileOpzioni.write(data); | |
| errore = "Impossibile chiudere il file"; | |
| fileOpzioni.close(); // Chiude lo stream | |
| } | |
| catch(Exception e) | |
| { | |
| System.out.println("ERRORE: " + errore); | |
| } | |
| } | |
| /** | |
| * Ritorna il percorso assoluto dell'applicazione | |
| */ | |
| private static String getAbsolutePath() | |
| { | |
| URL fileURL = Integrazione.class.getResource("/"); | |
| if (fileURL == null) | |
| { | |
| return null; | |
| } | |
| else | |
| { | |
| return fileURL.getPath(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment