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