java/rozsirene-atributy-jedit/src/cz/frantovo/rozsireneAtributy/jedit/DokovatelnyPanel.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 11 Dec 2023 00:08:35 +0100
branchv_0
changeset 26 706d4c013bf1
parent 23 279604933063
child 27 dc5786f3482b
permissions -rw-r--r--
jedit: přidání dosud neuložených změn
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@26
    27
import javax.swing.JOptionPane;
franta-hg@18
    28
import javax.swing.JPanel;
franta-hg@18
    29
import org.gjt.sp.jedit.Buffer;
franta-hg@18
    30
import org.gjt.sp.jedit.EBComponent;
franta-hg@18
    31
import org.gjt.sp.jedit.EBMessage;
franta-hg@18
    32
import org.gjt.sp.jedit.EditBus;
franta-hg@18
    33
import org.gjt.sp.jedit.View;
franta-hg@18
    34
import org.gjt.sp.jedit.msg.EditPaneUpdate;
franta-hg@18
    35
franta-hg@18
    36
/**
franta-hg@18
    37
 *
franta-hg@18
    38
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@18
    39
 */
franta-hg@18
    40
public class DokovatelnyPanel extends JPanel implements EBComponent {
franta-hg@18
    41
franta-hg@18
    42
	private static final Logger log = Logger.getLogger(DokovatelnyPanel.class.getName());
franta-hg@18
    43
	private View view;
franta-hg@23
    44
	private Panel panel;
franta-hg@18
    45
franta-hg@18
    46
	public DokovatelnyPanel(final View view, final String position) {
franta-hg@18
    47
		this.view = view;
franta-hg@20
    48
		setLayout(new BorderLayout());
franta-hg@26
    49
		změňSoubor(view.getBuffer(), false);
franta-hg@18
    50
	}
franta-hg@18
    51
franta-hg@18
    52
	/**
franta-hg@18
    53
	 * Zaregistrujeme se, aby nám chodily události editoru.
franta-hg@18
    54
	 */
franta-hg@18
    55
	@Override
franta-hg@18
    56
	public void addNotify() {
franta-hg@18
    57
		super.addNotify();
franta-hg@18
    58
		EditBus.addToBus(this);
franta-hg@18
    59
	}
franta-hg@18
    60
franta-hg@18
    61
	/**
franta-hg@18
    62
	 * @see #addNotify()
franta-hg@18
    63
	 */
franta-hg@18
    64
	@Override
franta-hg@18
    65
	public void removeNotify() {
franta-hg@18
    66
		super.removeNotify();
franta-hg@18
    67
		EditBus.removeFromBus(this);
franta-hg@18
    68
	}
franta-hg@18
    69
franta-hg@18
    70
	/**
franta-hg@26
    71
	 * Zpracujeme události editoru. Zajímá nás přepnutí na jiný soubor – abychom
franta-hg@26
    72
	 * pro něj zobrazili atributy.
franta-hg@18
    73
	 *
franta-hg@18
    74
	 * @param událost událost editoru
franta-hg@18
    75
	 */
franta-hg@18
    76
	@Override
franta-hg@18
    77
	public void handleMessage(EBMessage událost) {
franta-hg@18
    78
		try {
franta-hg@18
    79
			if (událost instanceof EditPaneUpdate) {
franta-hg@18
    80
				EditPaneUpdate epu = (EditPaneUpdate) událost;
franta-hg@18
    81
				// Chodí nám všechny události – potřebujeme filtrovat jen ty pro naše okno.
franta-hg@18
    82
				if (view == epu.getEditPane().getView()) {
franta-hg@26
    83
					// zajímá nás jen přepnutí souboru
franta-hg@26
    84
					if (epu.getWhat() == EditPaneUpdate.BUFFER_CHANGED) {
franta-hg@26
    85
						/**
franta-hg@26
    86
						 * TODO: je soubor nově otevřený?
franta-hg@26
    87
						 */
franta-hg@26
    88
						změňSoubor(view.getBuffer(), epu.getWhat() == EditPaneUpdate.CREATED);
franta-hg@26
    89
					}
franta-hg@18
    90
				}
franta-hg@18
    91
			}
franta-hg@18
    92
			// událost instanceof BufferUpdate
franta-hg@18
    93
			// událost instanceof PropertiesChanged
franta-hg@18
    94
		} catch (Exception e) {
franta-hg@18
    95
			log.log(Level.WARNING, "Chyba při zpracování události: " + událost, e);
franta-hg@18
    96
		}
franta-hg@18
    97
franta-hg@18
    98
	}
franta-hg@18
    99
franta-hg@26
   100
	private void změňSoubor(Buffer b, boolean využijAtributy) {
franta-hg@20
   101
		try {
franta-hg@20
   102
			File s = new File(b.getPath());
franta-hg@20
   103
franta-hg@20
   104
			if (s.isFile() && s.canRead()) {
franta-hg@20
   105
				Model m = new Model(s);
franta-hg@23
   106
franta-hg@23
   107
				if (panel == null) {
franta-hg@23
   108
					panel = new Panel(m);
franta-hg@23
   109
					removeAll();
franta-hg@23
   110
					add(panel, BorderLayout.CENTER);
franta-hg@23
   111
				} else {
franta-hg@23
   112
					panel.setModel(m);
franta-hg@23
   113
				}
franta-hg@23
   114
franta-hg@26
   115
				if (využijAtributy) {
franta-hg@26
   116
					využijAtributy(m, b);
franta-hg@26
   117
				}
franta-hg@26
   118
franta-hg@20
   119
			} else {
franta-hg@20
   120
				// TODO: zobrazit chybu
franta-hg@22
   121
				log.log(Level.WARNING, "Soubor neexistuje nebo nemáme práva na čtení: {0}", s);
franta-hg@20
   122
			}
franta-hg@20
   123
		} catch (IOException e) {
franta-hg@20
   124
			log.log(Level.WARNING, "Chyba při změně souboru.", e);
franta-hg@20
   125
		}
franta-hg@18
   126
	}
franta-hg@26
   127
franta-hg@26
   128
	/**
franta-hg@26
   129
	 * Nastaví jEdit podle atributů daného souboru: - odsazování - kódování
franta-hg@26
   130
	 *
franta-hg@26
   131
	 * @param m model obsahující atributy
franta-hg@26
   132
	 * @param b soubor otevřený v editoru
franta-hg@26
   133
	 */
franta-hg@26
   134
	private void využijAtributy(Model m, Buffer b) {
franta-hg@26
   135
		JOptionPane.showMessageDialog(panel, "Nový soubor!");
franta-hg@26
   136
	}
franta-hg@18
   137
}