-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLPanel.java
More file actions
235 lines (210 loc) · 5.09 KB
/
HTMLPanel.java
File metadata and controls
235 lines (210 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/** @author Gergely Kota
This class deals with showing the text with html markups. Also, allows for some
basic functionality
*/
public class HTMLPanel extends JPanel implements ActionListener
{
private JPanel[] edges = new JPanel[4];
private JScrollPane jsp;
private JLabel info;
private JEditorPane pane;
private File currentFile;
private String color;
private PairComparison currentPair = null;
private int currentIndex = 0;
public HTMLPanel()
{
this(Config.getInt("edgesize"));
}
/** @param size the thickness of the border in the panel
@param c the Color of the border
*/
public HTMLPanel(int size)
{
Config.addActionListener(this);
setLayout(new BorderLayout());
pane = new JEditorPane();
info = new JLabel();
color = Config.read("htmlcolor");
if(color == null)
color = "FF0000"; // red by default
// set to read html pages
pane.setContentType("text/html; charset=EUC-JP");
pane.setEditable(false);
jsp = new JScrollPane();
jsp.getViewport().add(pane);
add(edges[0] = GUtil.filler(size), BorderLayout.NORTH);
add(edges[1] = GUtil.filler(size), BorderLayout.SOUTH);
add(edges[2] = GUtil.filler(size), BorderLayout.EAST);
add(edges[3] = GUtil.filler(size), BorderLayout.WEST);
JPanel interests = new JPanel(new BorderLayout());
JPanel bottom = new JPanel(new BorderLayout());
interests.add(jsp, BorderLayout.CENTER);
interests.add(bottom, BorderLayout.SOUTH);
bottom.add(saveButton(), BorderLayout.EAST);
bottom.add(info, BorderLayout.CENTER);
add(interests, BorderLayout.CENTER);
}
public void reload()
{
invalidate();
pane.invalidate();
resize();
setHighlightColor();
setColor();
pane.validate();
validate();
repaint();
}
public void actionPerformed(ActionEvent ae)
{
String s = (String) ae.getSource();
if(s.equals("edgesize"))
resize();
if(s.equals("edgecolor"))
setColor();
if(s.equals("htmlcolor"))
setHighlightColor();
}
public void setHighlightColor()
{
String s3 = Config.read("htmlcolor");
if(s3 != null)
color = s3;
reColor();
}
public void setHighlightColor(Color c)
{
color = Conversion.hexString(c);
reColor();
}
public void resize()
{
String s2 = Config.read("edgesize");
try
{
resize(Integer.parseInt(s2));
}
catch(Exception e) {}
}
/** Resizes the size of the border of the HTMLPanel
@param size the size to set the border to
*/
public void resize(int size)
{
invalidate();
for(int i = 0; i < 4; i++)
edges[i].setPreferredSize(new Dimension(size, size));
validate();
repaint();
}
public void setColor()
{
String s1 = Config.read("edgecolor");
if(s1 != null)
setColor(Conversion.colorConvert(s1));
}
/** Sets the Color of the border of the HTMLPanel
@param c the Color to set the border to
*/
public void setColor(Color c)
{
if(c == null)
return;
for(int i = 0; i < 4; i++)
edges[i].setBackground(c);
repaint();
}
/** Sets the content of the HTMLPanel to the given String associated with
the given File
@param s the String content of the window
@param f the File the String came from
*/
public void setString(PairComparison pc, int index)
{
if(pc == null)
return;
if(index == 1)
{
pane.setText(pc.getHTML1(color));
currentFile = pc.getFile1();;
info.setText(pc.getFile1().getAbsolutePath());
}
else
{
pane.setText(pc.getHTML2(color));
currentFile = pc.getFile2();
info.setText(pc.getFile2().getAbsolutePath());
}
currentPair = pc;
currentIndex = index;
repaint();
}
public void setString(File f, String content)
{
currentFile = f;
pane.setText(content);
info.setText(f.getName());
}
public void reColor()
{
setString(currentPair, currentIndex);
}
/** Gets the String content of the HTMLPanel
@return the String in this HTMLPanel
*/
public String getString()
{
return pane.getText();
}
/** Gets the current File from the HTMLPanel
@return the File associated with this HTMLPanel
*/
public File getCurrentFile()
{
return currentFile;
}
private boolean save()
{
if(currentFile == null)
return false;
try
{
PrintWriter pw = new PrintWriter(new FileWriter(FileFrame.getSaveFile()));
pw.print(pane.getText());
pw.close();
return true;
}
catch(Exception e) {return false;}
}
private JButton saveButton()
{
JButton save = new JButton("Save This Panel");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
save();
}
});
return save;
}
/* ---------------------------------------------------------------- */
/* ---------------------------------------------------------------- */
/* ---------------------------------------------------------------- */
public static void main(String[] args)
{
HTMLPanel hp = new HTMLPanel(3);
JFrame jf = new JFrame();
jf.setSize(400,400);
jf.getContentPane().add(hp);
String s = "This is an html page.";
for(int i = 0; i < 5; i++)
s = s + s;
s = s + "<font color = \"FF0000\">" + s + "</font>";
// hp.setString("<HTML>" + s + "</HTML>", new File("Yo"));
jf.show();
}
}