gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / pickercontroller / BytearrayPickerControllerImpl.java @ 2847
History | View | Annotate | Download (10.4 KB)
1 |
package org.gvsig.tools.swing.impl.pickercontroller; |
---|---|
2 |
|
3 |
import java.awt.Cursor; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.io.File; |
6 |
import java.net.URL; |
7 |
import java.nio.charset.StandardCharsets; |
8 |
import javax.swing.ImageIcon; |
9 |
import javax.swing.JButton; |
10 |
import javax.swing.JFileChooser; |
11 |
import javax.swing.JOptionPane; |
12 |
import javax.swing.text.JTextComponent; |
13 |
import org.apache.commons.io.FileUtils; |
14 |
import org.apache.commons.lang3.ArrayUtils; |
15 |
import org.apache.commons.lang3.StringUtils; |
16 |
import org.gvsig.tools.ToolsLocator; |
17 |
import org.gvsig.tools.dataTypes.DataTypes; |
18 |
import org.gvsig.tools.folders.FoldersManager; |
19 |
import org.gvsig.tools.i18n.I18nManager; |
20 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
21 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
22 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
23 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
24 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
25 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
26 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
27 |
import org.gvsig.tools.swing.impl.DefaultZoomDialog; |
28 |
|
29 |
/**
|
30 |
*
|
31 |
* @author jjdelcerro
|
32 |
*/
|
33 |
@SuppressWarnings("UseSpecificCatch") |
34 |
public class BytearrayPickerControllerImpl |
35 |
extends AbstractPickerController<byte[]> |
36 |
{ |
37 |
|
38 |
private final JTextComponent jtext; |
39 |
private final JButton btnUpload; |
40 |
private final JButton btnDownload; |
41 |
private final JButton btnEditAsText; |
42 |
|
43 |
private byte[] value; |
44 |
private final String fileChooserID; |
45 |
private final File initialPath; |
46 |
|
47 |
public BytearrayPickerControllerImpl(
|
48 |
JTextComponent jtext,
|
49 |
JButton btnUpload,
|
50 |
JButton btnDownload,
|
51 |
JButton btnEdiAsText,
|
52 |
String fileChooserID,
|
53 |
File initialPath
|
54 |
) { |
55 |
this.jtext = jtext;
|
56 |
this.btnUpload = btnUpload;
|
57 |
this.btnDownload = btnDownload;
|
58 |
if( btnEdiAsText == null ) { |
59 |
this.btnEditAsText = new JButton(); |
60 |
} else {
|
61 |
this.btnEditAsText = btnEdiAsText;
|
62 |
} |
63 |
this.fileChooserID = fileChooserID;
|
64 |
this.initialPath = initialPath;
|
65 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
66 |
|
67 |
this.btnDownload.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
68 |
this.btnDownload.setText(""); |
69 |
if( StringUtils.isBlank(this.btnDownload.getToolTipText()) ) { |
70 |
this.btnDownload.setToolTipText(i18n.getTranslation("_Download_data")); |
71 |
} |
72 |
this.btnDownload.setIcon(this.getIcon("picker-bytearray-download")); |
73 |
this.btnDownload.addActionListener((ActionEvent e) -> { |
74 |
doDownload(); |
75 |
}); |
76 |
this.btnUpload.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
77 |
this.btnUpload.setText(""); |
78 |
if( StringUtils.isBlank(this.btnUpload.getToolTipText()) ) { |
79 |
this.btnUpload.setToolTipText(i18n.getTranslation("_Upload_data")); |
80 |
} |
81 |
this.btnUpload.setIcon(this.getIcon("picker-bytearray-upload")); |
82 |
this.btnUpload.addActionListener((ActionEvent e) -> { |
83 |
doUpload(); |
84 |
}); |
85 |
this.btnEditAsText.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
86 |
this.btnEditAsText.setText(""); |
87 |
if( StringUtils.isBlank(this.btnEditAsText.getToolTipText()) ) { |
88 |
this.btnEditAsText.setToolTipText(i18n.getTranslation("_Edit_as_text")); |
89 |
} |
90 |
this.btnEditAsText.setIcon(this.getIcon("picker-bytearray-editastext")); |
91 |
this.btnEditAsText.addActionListener((ActionEvent e) -> { |
92 |
doEditAsText(); |
93 |
}); |
94 |
|
95 |
this.jtext.setEditable(false); |
96 |
ToolsSwingLocator.getToolsSwingManager().addClearButton(jtext, (ActionEvent e) -> {
|
97 |
this.value = null; |
98 |
this.jtext.setText(""); |
99 |
this.btnDownload.setEnabled(false); |
100 |
this.btnEditAsText.setEnabled(true); |
101 |
}); |
102 |
} |
103 |
|
104 |
@Override
|
105 |
public byte[] get() { |
106 |
return this.value; |
107 |
} |
108 |
|
109 |
@Override
|
110 |
public void set(byte[] value) { |
111 |
this.value = value;
|
112 |
if( this.value==null ) { |
113 |
this.btnDownload.setEnabled(false); |
114 |
this.btnEditAsText.setEnabled(true); |
115 |
this.jtext.setText(""); |
116 |
} else {
|
117 |
this.btnDownload.setEnabled(true); |
118 |
this.btnEditAsText.setEnabled(true); |
119 |
int size = this.value.length; |
120 |
this.jtext.setText(String.format("%d bytes", size)); |
121 |
} |
122 |
this.fireChangeEvent();
|
123 |
} |
124 |
|
125 |
@Override
|
126 |
public void coerceAndSet(Object value) { |
127 |
this.set((byte[]) this.coerce(DataTypes.BYTEARRAY, value, null)); |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public void setEnabled(boolean enabled) { |
132 |
this.btnDownload.setEnabled(true); |
133 |
this.btnUpload.setEnabled(enabled);
|
134 |
this.btnEditAsText.setEnabled(true); |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public boolean isEnabled() { |
139 |
return this.btnUpload.isEnabled(); |
140 |
} |
141 |
|
142 |
private File fileChooser(String title, int mode) { |
143 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
144 |
FoldersManager folderManager = ToolsLocator.getFoldersManager(); |
145 |
File[] f = dialogs.showChooserDialog( |
146 |
title, |
147 |
mode, |
148 |
JFileChooser.FILES_ONLY,
|
149 |
true,
|
150 |
folderManager.getLastPath( |
151 |
this.fileChooserID,
|
152 |
this.initialPath
|
153 |
), |
154 |
null,
|
155 |
true
|
156 |
); |
157 |
|
158 |
if( ArrayUtils.isEmpty(f) ) {
|
159 |
return null; |
160 |
} |
161 |
return f[0]; |
162 |
} |
163 |
|
164 |
private void doDownload() { |
165 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
166 |
String title = i18n.getTranslation("_Download_data"); |
167 |
File f = this.fileChooser(title, JFileChooser.SAVE_DIALOG); |
168 |
if( f ==null ) { |
169 |
return;
|
170 |
} |
171 |
try {
|
172 |
FileUtils.writeByteArrayToFile(f, value); |
173 |
} catch(Exception ex) { |
174 |
LOG.warn("Can't save bytes from file '"+f.getAbsolutePath()+"'.",ex); |
175 |
} |
176 |
} |
177 |
|
178 |
private void doUpload() { |
179 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
180 |
String title = i18n.getTranslation("_Upload_data"); |
181 |
File f = this.fileChooser(title, JFileChooser.OPEN_DIALOG); |
182 |
if( f == null ) { |
183 |
return;
|
184 |
} |
185 |
try {
|
186 |
byte[] bytes = FileUtils.readFileToByteArray(f); |
187 |
this.set(bytes);
|
188 |
this.fireChangeEvent();
|
189 |
} catch (Exception ex) { |
190 |
LOG.warn("Can't load bytes from file '"+f.getAbsolutePath()+"'.",ex); |
191 |
} |
192 |
} |
193 |
|
194 |
private void doEditAsText() { |
195 |
DefaultZoomDialog dialog; |
196 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
197 |
boolean isEditable = this.isEditable() && this.isEnabled(); |
198 |
String title = isEditable?i18n.getTranslation("_Text_editor"):i18n.getTranslation("_Text_viewer"); |
199 |
try {
|
200 |
String text = ""; |
201 |
if( value!=null ) { |
202 |
text = new String(value, StandardCharsets.UTF_8); |
203 |
} |
204 |
dialog = new DefaultZoomDialog(
|
205 |
this.jtext,
|
206 |
title, |
207 |
text, |
208 |
WindowManager.MODE.DIALOG |
209 |
); |
210 |
dialog.setEditable(isEditable); |
211 |
dialog.setAlwaysOnTop(true);
|
212 |
dialog.setVisible(true);
|
213 |
} catch(Exception ex) { |
214 |
LOG.warn("Can't show text editor.",ex);
|
215 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
216 |
dialogs.messageDialog( |
217 |
i18n.getTranslation("_Cant_show_text_editor"),
|
218 |
title, |
219 |
JOptionPane.WARNING_MESSAGE
|
220 |
); |
221 |
return;
|
222 |
} |
223 |
try {
|
224 |
if ( isEditable && dialog.getAction()==WindowManager_v2.BUTTON_OK) {
|
225 |
String text = dialog.getText();
|
226 |
this.set(text.getBytes(StandardCharsets.UTF_8));
|
227 |
} |
228 |
} catch(Exception ex) { |
229 |
LOG.warn("Can't convert text to raw bytes.",ex);
|
230 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
231 |
dialogs.messageDialog( |
232 |
i18n.getTranslation("_Cant_convert_text_to_raw_bytes"),
|
233 |
title, |
234 |
JOptionPane.WARNING_MESSAGE
|
235 |
); |
236 |
return;
|
237 |
} |
238 |
} |
239 |
|
240 |
public static void selfRegister() { |
241 |
URL imageResource = BytearrayPickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-bytearray-upload.png"); |
242 |
if (imageResource != null) { |
243 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
244 |
IconTheme theme = iconThemeManager.getCurrent(); |
245 |
ImageIcon icon = new ImageIcon(imageResource); |
246 |
theme.registerDefault("tools", "picker", "picker-bytearray-upload", icon, imageResource); |
247 |
} |
248 |
imageResource = BytearrayPickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-bytearray-download.png");
|
249 |
if (imageResource != null) { |
250 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
251 |
IconTheme theme = iconThemeManager.getCurrent(); |
252 |
ImageIcon icon = new ImageIcon(imageResource); |
253 |
theme.registerDefault("tools", "picker", "picker-bytearray-download", icon, imageResource); |
254 |
} |
255 |
imageResource = BytearrayPickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-bytearray-editastext.png");
|
256 |
if (imageResource != null) { |
257 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
258 |
IconTheme theme = iconThemeManager.getCurrent(); |
259 |
ImageIcon icon = new ImageIcon(imageResource); |
260 |
theme.registerDefault("tools", "picker", "picker-bytearray-editastext", icon, imageResource); |
261 |
} |
262 |
imageResource = BytearrayPickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-bytearray-setnull.png");
|
263 |
if (imageResource != null) { |
264 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
265 |
IconTheme theme = iconThemeManager.getCurrent(); |
266 |
ImageIcon icon = new ImageIcon(imageResource); |
267 |
theme.registerDefault("tools", "picker", "picker-bytearray-setnull", icon, imageResource); |
268 |
} |
269 |
} |
270 |
} |