extra/rozsirene-atributy-jedit/src/cz/frantovo/rozsireneAtributy/jedit/DokovatelnyPanel.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 16 Dec 2023 19:09:35 +0100
branchv_0
changeset 38 41e91ea94acb
parent 29 java/rozsirene-atributy-jedit/src/cz/frantovo/rozsireneAtributy/jedit/DokovatelnyPanel.java@8d42303538ed
permissions -rw-r--r--
přesun pluginu pro jEdit do složky extra
     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.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package cz.frantovo.rozsireneAtributy.jedit;
    18 
    19 import cz.frantovo.rozsireneAtributy.gui.Model;
    20 import cz.frantovo.rozsireneAtributy.gui.Panel;
    21 import java.awt.BorderLayout;
    22 import java.io.File;
    23 import java.io.IOException;
    24 import java.util.logging.Level;
    25 import java.util.logging.Logger;
    26 import javax.swing.JOptionPane;
    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
    42 		.getLogger(DokovatelnyPanel.class.getName());
    43 	private View view;
    44 	private Panel panel;
    45 
    46 	public DokovatelnyPanel(final View view, final String position) {
    47 		this.view = view;
    48 		setLayout(new BorderLayout());
    49 		změňSoubor(view.getBuffer(), false);
    50 	}
    51 
    52 	/**
    53 	 * Zaregistrujeme se, aby nám chodily události editoru.
    54 	 */
    55 	@Override
    56 	public void addNotify() {
    57 		super.addNotify();
    58 		EditBus.addToBus(this);
    59 	}
    60 
    61 	/**
    62 	 * @see #addNotify()
    63 	 */
    64 	@Override
    65 	public void removeNotify() {
    66 		super.removeNotify();
    67 		EditBus.removeFromBus(this);
    68 	}
    69 
    70 	/**
    71 	 * Zpracujeme události editoru. Zajímá nás přepnutí na jiný soubor – abychom
    72 	 * pro něj zobrazili atributy.
    73 	 *
    74 	 * @param událost událost editoru
    75 	 */
    76 	@Override
    77 	public void handleMessage(EBMessage událost) {
    78 		try {
    79 			if (událost instanceof EditPaneUpdate) {
    80 				EditPaneUpdate epu = (EditPaneUpdate) událost;
    81 				// Chodí nám všechny události
    82 				// – potřebujeme filtrovat jen ty pro naše okno.
    83 				if (view == epu.getEditPane().getView()) {
    84 					// zajímá nás jen přepnutí souboru
    85 					if (epu.getWhat() == EditPaneUpdate.BUFFER_CHANGED) {
    86 						/**
    87 						 * TODO: je soubor nově otevřený?
    88 						 */
    89 						změňSoubor(view.getBuffer(),
    90 							epu.getWhat() == EditPaneUpdate.CREATED);
    91 					}
    92 				}
    93 			}
    94 			// událost instanceof BufferUpdate
    95 			// událost instanceof PropertiesChanged
    96 		} catch (Exception e) {
    97 			log.log(Level.WARNING, "Chyba při zpracování: " + událost, e);
    98 		}
    99 
   100 	}
   101 
   102 	private void změňSoubor(Buffer b, boolean využijAtributy) {
   103 		try {
   104 			File s = new File(b.getPath());
   105 
   106 			if (s.isFile() && s.canRead()) {
   107 				Model m = new Model(s);
   108 
   109 				if (panel == null) {
   110 					panel = new Panel(m);
   111 					removeAll();
   112 					add(panel, BorderLayout.CENTER);
   113 				} else {
   114 					panel.setModel(m);
   115 				}
   116 
   117 				if (využijAtributy) {
   118 					využijAtributy(m, b);
   119 				}
   120 
   121 			} else {
   122 				// TODO: zobrazit chybu
   123 				log.log(Level.WARNING,
   124 					"Soubor neexistuje nebo nemáme práva na čtení: {0}", s);
   125 			}
   126 		} catch (IOException e) {
   127 			log.log(Level.WARNING, "Chyba při změně souboru.", e);
   128 		}
   129 	}
   130 
   131 	/**
   132 	 * Nastaví jEdit podle atributů daného souboru: - odsazování - kódování
   133 	 *
   134 	 * @param m model obsahující atributy
   135 	 * @param b soubor otevřený v editoru
   136 	 */
   137 	private void využijAtributy(Model m, Buffer b) {
   138 		JOptionPane.showMessageDialog(panel, "Nový soubor!");
   139 	}
   140 }