java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/Startér.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 12 Dec 2023 21:19:11 +0100
branchv_0
changeset 30 d511e4bf7d8f
parent 29 8d42303538ed
child 31 1ab5ce94a146
permissions -rw-r--r--
volitelné zamykání souborů (POSIX) a zaslání notifikace ostatním aplikacím (inotify/CLOSE_WRITE)
     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.io.IOException;
    27 import java.text.MessageFormat;
    28 import java.util.ResourceBundle;
    29 import java.util.logging.Level;
    30 import java.util.logging.Logger;
    31 import javax.swing.JComponent;
    32 import javax.swing.JFrame;
    33 import javax.swing.JOptionPane;
    34 import javax.swing.KeyStroke;
    35 
    36 /**
    37  * Spouštěč programu
    38  *
    39  * http://freedesktop.org/wiki/CommonExtendedAttributes
    40  * http://download.oracle.com/javase/tutorial/essential/io/fileAttr.html#user
    41  * http://today.java.net/pub/a/today/2008/07/03/jsr-203-new-file-apis.html
    42  *   #so-what-is-a-path-really
    43  *
    44  * $ setfattr -n "user.franta.pozdrav" -v 'Dobrý den!' pokus.txt
    45  * (v javě pak pracujeme s klíči bez předpony „user.“)
    46  * 
    47  * @author Ing. František Kučera (frantovo.cz)
    48  */
    49 public class Startér {
    50 
    51 	private static final Logger log = Logger
    52 		.getLogger(Startér.class.getSimpleName());
    53 	private static final ResourceBundle překlady = ResourceBundle
    54 		.getBundle(Atribut.class.getPackageName() + ".Překlady");
    55 
    56 	/**
    57 	 * @param args název souboru
    58 	 * @throws IOException TODO: ošetřit výjimku
    59 	 */
    60 	public static void main(String[] args) throws IOException {
    61 
    62 
    63 		if (args.length == 1 && args[0].length() > 0) {
    64 			File soubor = new File(args[0]);
    65 
    66 			if (soubor.exists()) {
    67 				log.log(Level.INFO, "Pracuji se souborem: {0}", soubor);
    68 
    69 				Model model = new Model(soubor);
    70 
    71 				final JFrame f = new JFrame();
    72 				Panel p = new Panel(model);
    73 
    74 				f.setLayout(new BorderLayout());
    75 				f.add(p, BorderLayout.CENTER);
    76 
    77 				/** Ukončení programu klávesou Escape */
    78 				f.getRootPane().registerKeyboardAction(new ActionListener() {
    79 
    80 					public void actionPerformed(ActionEvent ae) {
    81 						f.dispose();
    82 					}
    83 				},
    84 					KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
    85 					JComponent.WHEN_IN_FOCUSED_WINDOW);
    86 
    87 				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    88 				f.setTitle(MessageFormat
    89 					.format(překlady.getString("titulek"), soubor));
    90 				f.setSize(640, 240);
    91 				f.setLocationRelativeTo(null);
    92 				f.setVisible(true);
    93 			} else {
    94 				ukončiChybou(MessageFormat.format(překlady
    95 					.getString("chyba.souborNeexistuje"), soubor));
    96 			}
    97 		} else {
    98 			ukončiChybou(překlady.getString("chyba.chybíParametr"));
    99 		}
   100 	}
   101 
   102 	private static void ukončiChybou(String hláška) {
   103 		log.log(Level.SEVERE, hláška);
   104 		JOptionPane.showMessageDialog(null, hláška, překlady
   105 			.getString("chyba.titulek"), JOptionPane.ERROR_MESSAGE);
   106 		System.exit(1);
   107 	}
   108 }