java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/Startér.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 15 Dec 2023 20:41:41 +0100
branchv_0
changeset 34 c2ce9d916103
parent 31 1ab5ce94a146
permissions -rw-r--r--
při zavření okna nedělat tvrdý EXIT, ale jen DISPOSE a počkat, až se vše ukončí
     1 /**
     2  * Rozšířené atributy – program na správu rozšířených atributů souborů
     3  * Copyright © 2012 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package cz.frantovo.rozsireneatributy;
    18 
    19 import cz.frantovo.rozsireneatributy.gui.Model;
    20 import cz.frantovo.rozsireneatributy.gui.Panel;
    21 import java.awt.BorderLayout;
    22 import java.awt.event.ActionEvent;
    23 import java.awt.event.ActionListener;
    24 import java.awt.event.KeyEvent;
    25 import java.io.File;
    26 import java.text.MessageFormat;
    27 import java.util.ResourceBundle;
    28 import java.util.logging.Level;
    29 import java.util.logging.Logger;
    30 import javax.swing.JComponent;
    31 import javax.swing.JFrame;
    32 import javax.swing.JOptionPane;
    33 import javax.swing.KeyStroke;
    34 
    35 /**
    36  * Spouštěč programu
    37  *
    38  * http://freedesktop.org/wiki/CommonExtendedAttributes
    39  * http://download.oracle.com/javase/tutorial/essential/io/fileAttr.html#user
    40  * http://today.java.net/pub/a/today/2008/07/03/jsr-203-new-file-apis.html
    41  * #so-what-is-a-path-really
    42  *
    43  * $ setfattr -n "user.franta.pozdrav" -v 'Dobrý den!' pokus.txt (v javě pak
    44  * pracujeme s klíči bez předpony „user.“)
    45  *
    46  * @author Ing. František Kučera (frantovo.cz)
    47  */
    48 public class Startér {
    49 
    50 	private static final Logger log = Logger
    51 		.getLogger(Startér.class.getSimpleName());
    52 	private static final ResourceBundle překlady = ResourceBundle
    53 		.getBundle(Atribut.class.getPackageName() + ".Překlady");
    54 
    55 	/**
    56 	 * @param args název souboru
    57 	 */
    58 	public static void main(String[] args) {
    59 
    60 		try {
    61 			// TODO: načítat konfiguraci i z XML souboru
    62 			CLIParser parser = new CLIParser();
    63 			Konfigurace konfigurace = parser.parsujParametry(args);
    64 
    65 			File soubor = konfigurace.getSoubor();
    66 
    67 			if (soubor.exists()) {
    68 				log.log(Level.INFO, "Pracuji se souborem: {0}", soubor);
    69 
    70 				Model model = new Model(konfigurace);
    71 
    72 				final JFrame f = new JFrame();
    73 				Panel p = new Panel(model);
    74 
    75 				f.setLayout(new BorderLayout());
    76 				f.add(p, BorderLayout.CENTER);
    77 
    78 				// Ukončení programu klávesou Escape:
    79 				f.getRootPane().registerKeyboardAction(new ActionListener() {
    80 
    81 					@Override
    82 					public void actionPerformed(ActionEvent ae) {
    83 						f.dispose();
    84 					}
    85 				},
    86 					KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
    87 					JComponent.WHEN_IN_FOCUSED_WINDOW);
    88 
    89 				f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    90 				f.setTitle(MessageFormat
    91 					.format(překlady.getString("titulek"), soubor));
    92 				f.setSize(640, 240);
    93 				f.setLocationRelativeTo(null);
    94 				f.setVisible(true);
    95 			} else {
    96 				ukončiChybou(MessageFormat.format(překlady
    97 					.getString("chyba.souborNeexistuje"), soubor));
    98 			}
    99 		} catch (Exception e) {
   100 			log.log(Level.SEVERE, překlady.getString("chyba"), e);
   101 			ukončiChybou(e.getLocalizedMessage());
   102 		}
   103 	}
   104 
   105 	private static void ukončiChybou(String hláška) {
   106 		log.log(Level.SEVERE, hláška);
   107 		JOptionPane.showMessageDialog(null, hláška, překlady
   108 			.getString("chyba.titulek"), JOptionPane.ERROR_MESSAGE);
   109 		System.exit(1);
   110 	}
   111 }