java/rozsirene-atributy-jedit/src/cz/frantovo/rozsireneAtributy/jedit/DokovatelnyPanel.java
author František Kučera <franta-hg@frantovo.cz>
Sun Oct 07 15:11:42 2012 +0200 (2012-10-07)
changeset 22 bf06eb899671
parent 20 bf328b110881
child 23 69f49b79342e
permissions -rw-r--r--
nabídka standardizovaných atributů
http://www.freedesktop.org/wiki/CommonExtendedAttributes
     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, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.rozsireneAtributy.jedit;
    19 
    20 import cz.frantovo.rozsireneAtributy.gui.Model;
    21 import cz.frantovo.rozsireneAtributy.gui.Panel;
    22 import java.awt.BorderLayout;
    23 import java.io.File;
    24 import java.io.IOException;
    25 import java.util.logging.Level;
    26 import java.util.logging.Logger;
    27 import javax.swing.JPanel;
    28 import org.gjt.sp.jedit.Buffer;
    29 import org.gjt.sp.jedit.EBComponent;
    30 import org.gjt.sp.jedit.EBMessage;
    31 import org.gjt.sp.jedit.EditBus;
    32 import org.gjt.sp.jedit.View;
    33 import org.gjt.sp.jedit.msg.EditPaneUpdate;
    34 
    35 /**
    36  *
    37  * @author Ing. František Kučera (frantovo.cz)
    38  */
    39 public class DokovatelnyPanel extends JPanel implements EBComponent {
    40 
    41 	private static final Logger log = Logger.getLogger(DokovatelnyPanel.class.getName());
    42 	private View view;
    43 
    44 	public DokovatelnyPanel(final View view, final String position) {
    45 		this.view = view;
    46 		setLayout(new BorderLayout());
    47 		změňSoubor(view.getBuffer());
    48 	}
    49 
    50 	/**
    51 	 * Zaregistrujeme se, aby nám chodily události editoru.
    52 	 */
    53 	@Override
    54 	public void addNotify() {
    55 		super.addNotify();
    56 		EditBus.addToBus(this);
    57 	}
    58 
    59 	/**
    60 	 * @see #addNotify()
    61 	 */
    62 	@Override
    63 	public void removeNotify() {
    64 		super.removeNotify();
    65 		EditBus.removeFromBus(this);
    66 	}
    67 
    68 	/**
    69 	 * Zpracujeme události editoru.
    70 	 * Zajímá nás přepnutí na jiný soubor – abychom pro něj zobrazili atributy.
    71 	 *
    72 	 * @param událost událost editoru
    73 	 */
    74 	@Override
    75 	public void handleMessage(EBMessage událost) {
    76 		try {
    77 			if (událost instanceof EditPaneUpdate) {
    78 				EditPaneUpdate epu = (EditPaneUpdate) událost;
    79 				// Chodí nám všechny události – potřebujeme filtrovat jen ty pro naše okno.
    80 				if (view == epu.getEditPane().getView()) {
    81 					změňSoubor(view.getBuffer());
    82 				}
    83 			}
    84 			// událost instanceof BufferUpdate
    85 			// událost instanceof PropertiesChanged
    86 		} catch (Exception e) {
    87 			log.log(Level.WARNING, "Chyba při zpracování události: " + událost, e);
    88 		}
    89 
    90 	}
    91 
    92 	private void změňSoubor(Buffer b) {
    93 		try {
    94 			File s = new File(b.getPath());
    95 
    96 			if (s.isFile() && s.canRead()) {
    97 				Model m = new Model(s);
    98 				Panel p = new Panel(m);
    99 				removeAll();
   100 				add(p, BorderLayout.CENTER);
   101 			} else {
   102 				// TODO: zobrazit chybu
   103 				log.log(Level.WARNING, "Soubor neexistuje nebo nemáme práva na čtení: {0}", s);
   104 			}
   105 		} catch (IOException e) {
   106 			log.log(Level.WARNING, "Chyba při změně souboru.", e);
   107 		}
   108 	}
   109 }