java/rozsirene-atributy-jedit/src/cz/frantovo/rozsireneAtributy/jedit/DokovatelnyPanel.java
author František Kučera <franta-hg@frantovo.cz>
Sat Aug 18 13:38:58 2012 +0200 (2012-08-18)
changeset 20 bf328b110881
parent 19 c20edbed09c3
child 22 bf06eb899671
permissions -rw-r--r--
První fungující verze doplňku pro jEdit.
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@18
    43
franta-hg@18
    44
	public DokovatelnyPanel(final View view, final String position) {
franta-hg@18
    45
		this.view = view;
franta-hg@20
    46
		setLayout(new BorderLayout());
franta-hg@20
    47
		změňSoubor(view.getBuffer());
franta-hg@18
    48
	}
franta-hg@18
    49
franta-hg@18
    50
	/**
franta-hg@18
    51
	 * Zaregistrujeme se, aby nám chodily události editoru.
franta-hg@18
    52
	 */
franta-hg@18
    53
	@Override
franta-hg@18
    54
	public void addNotify() {
franta-hg@18
    55
		super.addNotify();
franta-hg@18
    56
		EditBus.addToBus(this);
franta-hg@18
    57
	}
franta-hg@18
    58
franta-hg@18
    59
	/**
franta-hg@18
    60
	 * @see #addNotify()
franta-hg@18
    61
	 */
franta-hg@18
    62
	@Override
franta-hg@18
    63
	public void removeNotify() {
franta-hg@18
    64
		super.removeNotify();
franta-hg@18
    65
		EditBus.removeFromBus(this);
franta-hg@18
    66
	}
franta-hg@18
    67
franta-hg@18
    68
	/**
franta-hg@18
    69
	 * Zpracujeme události editoru.
franta-hg@18
    70
	 * Zajímá nás přepnutí na jiný soubor – abychom pro něj zobrazili atributy.
franta-hg@18
    71
	 *
franta-hg@18
    72
	 * @param událost událost editoru
franta-hg@18
    73
	 */
franta-hg@18
    74
	@Override
franta-hg@18
    75
	public void handleMessage(EBMessage událost) {
franta-hg@18
    76
		try {
franta-hg@18
    77
			if (událost instanceof EditPaneUpdate) {
franta-hg@18
    78
				EditPaneUpdate epu = (EditPaneUpdate) událost;
franta-hg@18
    79
				// Chodí nám všechny události – potřebujeme filtrovat jen ty pro naše okno.
franta-hg@18
    80
				if (view == epu.getEditPane().getView()) {
franta-hg@18
    81
					změňSoubor(view.getBuffer());
franta-hg@18
    82
				}
franta-hg@18
    83
			}
franta-hg@18
    84
			// událost instanceof BufferUpdate
franta-hg@18
    85
			// událost instanceof PropertiesChanged
franta-hg@18
    86
		} catch (Exception e) {
franta-hg@18
    87
			log.log(Level.WARNING, "Chyba při zpracování události: " + událost, e);
franta-hg@18
    88
		}
franta-hg@18
    89
franta-hg@18
    90
	}
franta-hg@18
    91
franta-hg@18
    92
	private void změňSoubor(Buffer b) {
franta-hg@20
    93
		try {
franta-hg@20
    94
			File s = new File(b.getPath());
franta-hg@20
    95
franta-hg@20
    96
			if (s.isFile() && s.canRead()) {
franta-hg@20
    97
				Model m = new Model(s);
franta-hg@20
    98
				Panel p = new Panel(m);
franta-hg@20
    99
				removeAll();
franta-hg@20
   100
				add(p, BorderLayout.CENTER);
franta-hg@20
   101
			} else {
franta-hg@20
   102
				// TODO: zobrazit chybu
franta-hg@20
   103
			}
franta-hg@20
   104
		} catch (IOException e) {
franta-hg@20
   105
			log.log(Level.WARNING, "Chyba při změně souboru.", e);
franta-hg@20
   106
		}
franta-hg@18
   107
	}
franta-hg@18
   108
}