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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
package net.java.sip.communicator.plugin.guicustomization;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.table.*;
import net.java.sip.communicator.util.*;
public class ImagesPanel
extends JScrollPane
{
private Logger logger = Logger.getLogger(ImagesPanel.class);
private CustomTableModel imagesTableModel = new CustomTableModel();
private JTable imagesTable = new JTable();
private JFrame parentWindow;
public ImagesPanel(JFrame parentWindow)
{
this.parentWindow = parentWindow;
this.getViewport().add(imagesTable);
imagesTableModel.addColumn("File path");
imagesTableModel.addColumn("Image size");
imagesTableModel.addColumn("Image");
imagesTableModel.addColumn("Change image");
imagesTable.setModel(imagesTableModel);
TableColumn sizeColumn
= imagesTable.getColumnModel().getColumn(1);
sizeColumn.setCellRenderer(
new LabelTableCellRenderer());
sizeColumn.setMaxWidth(100);
imagesTable.getColumnModel().getColumn(2).setCellRenderer(
new LabelTableCellRenderer());
TableColumn buttonColumn
= imagesTable.getColumnModel().getColumn(3);
buttonColumn.setCellRenderer(new ButtonTableCellRenderer());
buttonColumn.setCellEditor(new ButtonTableEditor());
this.initImageTable();
}
private void initImageTable()
{
Iterator imageKeys
= GuiCustomizationActivator.getResources()
.getCurrentImages();
// we set an initial row height, to fit the button.
int rowHeight = 40;
while (imageKeys.hasNext())
{
String key = (String) imageKeys.next();
ImageIcon image = getImage(key);
final JLabel imageLabel = new JLabel();
JLabel imageSizeLabel = new JLabel();
int currentImageWidth = 0;
int currentImageHeight = 0;
if (image != null)
{
imageLabel.setIcon(image);
currentImageWidth = image.getImage().getWidth(null);
currentImageHeight = image.getImage().getHeight(null);
imageSizeLabel
.setText(currentImageWidth + "x" + currentImageHeight);
images.put(key, getImageBytes(key));
}
JButton fileChooserButton = new JButton();
fileChooserButton.setAction(
new ChangeImageAction( key,
imageLabel,
imageSizeLabel,
currentImageWidth,
currentImageHeight));
imagesTableModel.addRow(new Object[]{ key,
imageSizeLabel,
imageLabel,
fileChooserButton});
fileChooserButton.setText("Change image");
if (image != null && rowHeight < image.getIconHeight())
{
imagesTable.setRowHeight( imagesTableModel.getRowCount() - 1,
image.getIconHeight() );
}
else
{
imagesTable.setRowHeight( imagesTableModel.getRowCount() - 1,
rowHeight );
}
}
}
/**
* Loads an image from a given image identifier.
*
* @param imageID The identifier of the image.
* @return The image for the given identifier.
*/
private ImageIcon getImage(String imageID)
{
BufferedImage image = null;
InputStream in =
GuiCustomizationActivator.getResources()
.getImageInputStream(imageID);
if(in == null)
return null;
try
{
image = ImageIO.read(in);
}
catch (IOException e)
{
logger.error("Failed to load image:" + imageID, e);
}
return new ImageIcon(image);
}
/**
* Loads an image from a given image identifier.
*
* @param imageID The identifier of the image.
* @return The image for the given identifier.
*/
private byte[] getImageBytes(String imageID)
{
InputStream in =
GuiCustomizationActivator.getResources()
.getImageInputStream(imageID);
if(in == null)
return null;
try
{
byte[] bs = new byte[in.available()];
in.read(bs);
return bs;
}
catch (IOException e)
{
logger.error("Failed to load image:" + imageID, e);
}
return null;
}
private class ChangeImageAction extends AbstractAction
{
private JLabel imageLabel;
private JLabel imageSizeLabel;
private int imageWidth;
private int imageHeight;
private String key;
public ChangeImageAction( String key,
JLabel imageLabel,
JLabel imageSizeLabel,
int imageWidth,
int imageHeight)
{
this.key = key;
this.imageLabel = imageLabel;
this.imageSizeLabel = imageSizeLabel;
this.imageWidth = imageWidth;
this.imageHeight = imageHeight;
}
public void actionPerformed(ActionEvent evt)
{
JFileChooser fileChooser
= new JFileChooser();
int result
= fileChooser.showOpenDialog(parentWindow);
if (result == JFileChooser.APPROVE_OPTION)
{
File newImageFile = fileChooser.getSelectedFile();
ImageIcon newImageIcon = new ImageIcon(newImageFile.getPath());
try
{
FileInputStream in = new FileInputStream(newImageFile);
byte[] bs = new byte[in.available()];
in.read(bs);
in.close();
images.put(key, bs);
}
catch (Exception e)
{
e.printStackTrace();
}
imageLabel.setIcon(newImageIcon);
if (newImageIcon.getIconWidth() > imageWidth
|| newImageIcon.getIconHeight() > imageHeight)
{
imageSizeLabel.setBackground(Color.RED);
}
}
}
}
// public static byte[] convertImage(Image img)
// {
// try
// {
// int[] pix = new int[img.getWidth(null) * img.getHeight(null)];
// PixelGrabber pg =
// new PixelGrabber(img, 0, 0, img.getWidth(null),
// img.getHeight(null), pix, 0, img.getWidth(null));
// pg.grabPixels();
//
// byte[] pixels = new byte[img.getWidth(null) * img.getHeight(null)];
//
// for (int j = 0; j < pix.length; j++)
// pixels[j] = new Integer(pix[j]).byteValue();
//
// return pixels;
// }
// catch (InterruptedException e)
// {
// e.printStackTrace();
// }
// return null;
// }
Hashtable<String, byte[]> images = new Hashtable<String, byte[]>();
Hashtable<String, byte[]> getImages()
{
return images;
// Hashtable res = new Hashtable();
// int rows = imagesTableModel.getRowCount();
// for (int i = 0; i < rows; i++)
// {
// String key = (String)imagesTableModel.getValueAt(i, 0);
// JLabel imageLabel = (JLabel)imagesTableModel.getValueAt(i, 2);
//
// Icon icon = imageLabel.getIcon();
// if(icon != null && icon instanceof ImageIcon)
// res.put(
// key,
// convertImage(((ImageIcon)icon).getImage()));
// else
// res.put(key,new byte[0]);
// }
//
// return res;
}
}
|