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@27
|
7 |
* the Free Software Foundation, either version 3 of the License.
|
franta-hg@20
|
8 |
*
|
franta-hg@19
|
9 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@19
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@20
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@19
|
12 |
* GNU General Public License for more details.
|
franta-hg@20
|
13 |
*
|
franta-hg@19
|
14 |
* You should have received a copy of the GNU General Public License
|
franta-hg@20
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@19
|
16 |
*/
|
franta-hg@18
|
17 |
package cz.frantovo.rozsireneAtributy.jedit;
|
franta-hg@18
|
18 |
|
franta-hg@20
|
19 |
import cz.frantovo.rozsireneAtributy.gui.Model;
|
franta-hg@20
|
20 |
import cz.frantovo.rozsireneAtributy.gui.Panel;
|
franta-hg@20
|
21 |
import java.awt.BorderLayout;
|
franta-hg@20
|
22 |
import java.io.File;
|
franta-hg@20
|
23 |
import java.io.IOException;
|
franta-hg@18
|
24 |
import java.util.logging.Level;
|
franta-hg@18
|
25 |
import java.util.logging.Logger;
|
franta-hg@26
|
26 |
import javax.swing.JOptionPane;
|
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@29
|
41 |
private static final Logger log = Logger
|
franta-hg@29
|
42 |
.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@29
|
81 |
// Chodí nám všechny události
|
franta-hg@29
|
82 |
// – potřebujeme filtrovat jen ty pro naše okno.
|
franta-hg@18
|
83 |
if (view == epu.getEditPane().getView()) {
|
franta-hg@26
|
84 |
// zajímá nás jen přepnutí souboru
|
franta-hg@26
|
85 |
if (epu.getWhat() == EditPaneUpdate.BUFFER_CHANGED) {
|
franta-hg@26
|
86 |
/**
|
franta-hg@26
|
87 |
* TODO: je soubor nově otevřený?
|
franta-hg@26
|
88 |
*/
|
franta-hg@29
|
89 |
změňSoubor(view.getBuffer(),
|
franta-hg@29
|
90 |
epu.getWhat() == EditPaneUpdate.CREATED);
|
franta-hg@26
|
91 |
}
|
franta-hg@18
|
92 |
}
|
franta-hg@18
|
93 |
}
|
franta-hg@18
|
94 |
// událost instanceof BufferUpdate
|
franta-hg@18
|
95 |
// událost instanceof PropertiesChanged
|
franta-hg@18
|
96 |
} catch (Exception e) {
|
franta-hg@29
|
97 |
log.log(Level.WARNING, "Chyba při zpracování: " + událost, e);
|
franta-hg@18
|
98 |
}
|
franta-hg@18
|
99 |
|
franta-hg@18
|
100 |
}
|
franta-hg@18
|
101 |
|
franta-hg@26
|
102 |
private void změňSoubor(Buffer b, boolean využijAtributy) {
|
franta-hg@20
|
103 |
try {
|
franta-hg@20
|
104 |
File s = new File(b.getPath());
|
franta-hg@20
|
105 |
|
franta-hg@20
|
106 |
if (s.isFile() && s.canRead()) {
|
franta-hg@20
|
107 |
Model m = new Model(s);
|
franta-hg@23
|
108 |
|
franta-hg@23
|
109 |
if (panel == null) {
|
franta-hg@23
|
110 |
panel = new Panel(m);
|
franta-hg@23
|
111 |
removeAll();
|
franta-hg@23
|
112 |
add(panel, BorderLayout.CENTER);
|
franta-hg@23
|
113 |
} else {
|
franta-hg@23
|
114 |
panel.setModel(m);
|
franta-hg@23
|
115 |
}
|
franta-hg@23
|
116 |
|
franta-hg@26
|
117 |
if (využijAtributy) {
|
franta-hg@26
|
118 |
využijAtributy(m, b);
|
franta-hg@26
|
119 |
}
|
franta-hg@26
|
120 |
|
franta-hg@20
|
121 |
} else {
|
franta-hg@20
|
122 |
// TODO: zobrazit chybu
|
franta-hg@29
|
123 |
log.log(Level.WARNING,
|
franta-hg@29
|
124 |
"Soubor neexistuje nebo nemáme práva na čtení: {0}", s);
|
franta-hg@20
|
125 |
}
|
franta-hg@20
|
126 |
} catch (IOException e) {
|
franta-hg@20
|
127 |
log.log(Level.WARNING, "Chyba při změně souboru.", e);
|
franta-hg@20
|
128 |
}
|
franta-hg@18
|
129 |
}
|
franta-hg@26
|
130 |
|
franta-hg@26
|
131 |
/**
|
franta-hg@26
|
132 |
* Nastaví jEdit podle atributů daného souboru: - odsazování - kódování
|
franta-hg@26
|
133 |
*
|
franta-hg@26
|
134 |
* @param m model obsahující atributy
|
franta-hg@26
|
135 |
* @param b soubor otevřený v editoru
|
franta-hg@26
|
136 |
*/
|
franta-hg@26
|
137 |
private void využijAtributy(Model m, Buffer b) {
|
franta-hg@26
|
138 |
JOptionPane.showMessageDialog(panel, "Nový soubor!");
|
franta-hg@26
|
139 |
}
|
franta-hg@18
|
140 |
}
|