java/rozsirene-atributy-jedit/src/cz/frantovo/rozsireneAtributy/jedit/DokovatelnyPanel.java
author František Kučera <franta-hg@frantovo.cz>
Sun Oct 07 16:15:00 2012 +0200 (2012-10-07)
changeset 23 69f49b79342e
parent 22 bf06eb899671
child 26 1d345074992c
permissions -rw-r--r--
Při přepnutí souboru obnovíme jen model, ne celou JTable.
     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 	private Panel panel;
    44 
    45 	public DokovatelnyPanel(final View view, final String position) {
    46 		this.view = view;
    47 		setLayout(new BorderLayout());
    48 		změňSoubor(view.getBuffer());
    49 	}
    50 
    51 	/**
    52 	 * Zaregistrujeme se, aby nám chodily události editoru.
    53 	 */
    54 	@Override
    55 	public void addNotify() {
    56 		super.addNotify();
    57 		EditBus.addToBus(this);
    58 	}
    59 
    60 	/**
    61 	 * @see #addNotify()
    62 	 */
    63 	@Override
    64 	public void removeNotify() {
    65 		super.removeNotify();
    66 		EditBus.removeFromBus(this);
    67 	}
    68 
    69 	/**
    70 	 * Zpracujeme události editoru.
    71 	 * Zajímá nás přepnutí na jiný soubor – abychom pro něj zobrazili atributy.
    72 	 *
    73 	 * @param událost událost editoru
    74 	 */
    75 	@Override
    76 	public void handleMessage(EBMessage událost) {
    77 		try {
    78 			if (událost instanceof EditPaneUpdate) {
    79 				EditPaneUpdate epu = (EditPaneUpdate) událost;
    80 				// Chodí nám všechny události – potřebujeme filtrovat jen ty pro naše okno.
    81 				if (view == epu.getEditPane().getView()) {
    82 					změňSoubor(view.getBuffer());
    83 				}
    84 			}
    85 			// událost instanceof BufferUpdate
    86 			// událost instanceof PropertiesChanged
    87 		} catch (Exception e) {
    88 			log.log(Level.WARNING, "Chyba při zpracování události: " + událost, e);
    89 		}
    90 
    91 	}
    92 
    93 	private void změňSoubor(Buffer b) {
    94 		try {
    95 			File s = new File(b.getPath());
    96 
    97 			if (s.isFile() && s.canRead()) {
    98 				Model m = new Model(s);
    99 
   100 				if (panel == null) {
   101 					panel = new Panel(m);
   102 					removeAll();
   103 					add(panel, BorderLayout.CENTER);
   104 				} else {
   105 					panel.setModel(m);
   106 				}
   107 
   108 			} else {
   109 				// TODO: zobrazit chybu
   110 				log.log(Level.WARNING, "Soubor neexistuje nebo nemáme práva na čtení: {0}", s);
   111 			}
   112 		} catch (IOException e) {
   113 			log.log(Level.WARNING, "Chyba při změně souboru.", e);
   114 		}
   115 	}
   116 }