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 @ 2541
History | View | Annotate | Download (6.27 KB)
1 |
package org.gvsig.tools.swing.impl.pickercontroller; |
---|---|
2 |
|
3 |
import java.awt.Cursor; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.io.File; |
7 |
import java.net.URL; |
8 |
import javax.swing.ImageIcon; |
9 |
import javax.swing.JButton; |
10 |
import javax.swing.JFileChooser; |
11 |
import javax.swing.text.JTextComponent; |
12 |
import org.apache.commons.io.FileUtils; |
13 |
import org.apache.commons.lang3.ArrayUtils; |
14 |
import org.apache.commons.lang3.StringUtils; |
15 |
import org.gvsig.tools.ToolsLocator; |
16 |
import org.gvsig.tools.dataTypes.DataTypes; |
17 |
import org.gvsig.tools.folders.FoldersManager; |
18 |
import org.gvsig.tools.i18n.I18nManager; |
19 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
20 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
21 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
22 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
23 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
24 |
|
25 |
/**
|
26 |
*
|
27 |
* @author jjdelcerro
|
28 |
*/
|
29 |
@SuppressWarnings("UseSpecificCatch") |
30 |
public class BytearrayPickerControllerImpl |
31 |
extends AbstractPickerController<byte[]> |
32 |
{ |
33 |
|
34 |
private final JTextComponent jtext; |
35 |
private final JButton btnUpload; |
36 |
private final JButton btnDownload; |
37 |
|
38 |
private byte[] value; |
39 |
private final String fileChooserID; |
40 |
private final File initialPath; |
41 |
|
42 |
public BytearrayPickerControllerImpl(
|
43 |
JTextComponent jtext,
|
44 |
JButton btnUpload,
|
45 |
JButton btnDownload,
|
46 |
String fileChooserID,
|
47 |
File initialPath
|
48 |
) { |
49 |
this.jtext = jtext;
|
50 |
this.btnUpload = btnUpload;
|
51 |
this.btnDownload = btnDownload;
|
52 |
this.fileChooserID = fileChooserID;
|
53 |
this.initialPath = initialPath;
|
54 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
55 |
|
56 |
this.btnDownload.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
57 |
this.btnDownload.setText(""); |
58 |
if( StringUtils.isBlank(this.btnDownload.getToolTipText()) ) { |
59 |
this.btnDownload.setToolTipText(i18n.getTranslation("_Download_data")); |
60 |
} |
61 |
this.btnDownload.setIcon(this.getIcon("picker-bytearray-download")); |
62 |
this.btnDownload.addActionListener((ActionEvent e) -> { |
63 |
doDownload(); |
64 |
}); |
65 |
this.btnUpload.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
66 |
this.btnUpload.setText(""); |
67 |
if( StringUtils.isBlank(this.btnUpload.getToolTipText()) ) { |
68 |
this.btnUpload.setToolTipText(i18n.getTranslation("_Upload_data")); |
69 |
} |
70 |
this.btnUpload.setIcon(this.getIcon("picker-bytearray-upload")); |
71 |
this.btnUpload.addActionListener((ActionEvent e) -> { |
72 |
doUpload(); |
73 |
}); |
74 |
|
75 |
this.jtext.setEditable(false); |
76 |
ToolsSwingLocator.getToolsSwingManager().addClearButton(jtext, (ActionEvent e) -> {
|
77 |
this.value = null; |
78 |
this.jtext.setText(""); |
79 |
this.btnDownload.setEnabled(false); |
80 |
}); |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public byte[] get() { |
85 |
return this.value; |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public void set(byte[] value) { |
90 |
this.value = value;
|
91 |
int size = 0; |
92 |
if( this.value==null ) { |
93 |
this.btnDownload.setEnabled(false); |
94 |
} else {
|
95 |
this.btnDownload.setEnabled(this.btnUpload.isEnabled()); |
96 |
size = this.value.length;
|
97 |
} |
98 |
this.jtext.setText(String.format("%d bytes", size)); |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public void coerceAndSet(Object value) { |
103 |
this.set((byte[]) this.coerce(DataTypes.BYTEARRAY, value, null)); |
104 |
} |
105 |
|
106 |
@Override
|
107 |
public void setEnabled(boolean enabled) { |
108 |
this.btnDownload.setEnabled(true); |
109 |
this.btnUpload.setEnabled(enabled);
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public boolean isEnabled() { |
114 |
return this.btnUpload.isEnabled(); |
115 |
} |
116 |
|
117 |
private File fileChooser(String title, int mode) { |
118 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
119 |
FoldersManager folderManager = ToolsLocator.getFoldersManager(); |
120 |
File[] f = dialogs.showChooserDialog( |
121 |
title, |
122 |
mode, |
123 |
JFileChooser.FILES_ONLY,
|
124 |
true,
|
125 |
folderManager.getLastPath( |
126 |
this.fileChooserID,
|
127 |
this.initialPath
|
128 |
), |
129 |
null,
|
130 |
true
|
131 |
); |
132 |
|
133 |
if( ArrayUtils.isEmpty(f) ) {
|
134 |
return null; |
135 |
} |
136 |
return f[0]; |
137 |
} |
138 |
|
139 |
private void doDownload() { |
140 |
File f = this.fileChooser("Save file", JFileChooser.SAVE_DIALOG); |
141 |
if( f ==null ) { |
142 |
return;
|
143 |
} |
144 |
try {
|
145 |
FileUtils.writeByteArrayToFile(f, value); |
146 |
} catch(Exception ex) { |
147 |
LOG.warn("Can't save bytes from file '"+f.getAbsolutePath()+"'.",ex); |
148 |
} |
149 |
} |
150 |
|
151 |
private void doUpload() { |
152 |
File f = this.fileChooser("Load file", JFileChooser.OPEN_DIALOG); |
153 |
if( f == null ) { |
154 |
return;
|
155 |
} |
156 |
try {
|
157 |
byte[] bytes = FileUtils.readFileToByteArray(f); |
158 |
this.set(bytes);
|
159 |
this.fireChangeEvent();
|
160 |
} catch (Exception ex) { |
161 |
LOG.warn("Can't load bytes from file '"+f.getAbsolutePath()+"'.",ex); |
162 |
} |
163 |
} |
164 |
|
165 |
public static void selfRegister() { |
166 |
URL imageResource = BytearrayPickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-bytearray-upload.png"); |
167 |
if (imageResource != null) { |
168 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
169 |
IconTheme theme = iconThemeManager.getCurrent(); |
170 |
ImageIcon icon = new ImageIcon(imageResource); |
171 |
theme.registerDefault("tools", "picker", "picker-bytearray-upload", icon, imageResource); |
172 |
} |
173 |
imageResource = BytearrayPickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-bytearray-download.png");
|
174 |
if (imageResource != null) { |
175 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
176 |
IconTheme theme = iconThemeManager.getCurrent(); |
177 |
ImageIcon icon = new ImageIcon(imageResource); |
178 |
theme.registerDefault("tools", "picker", "picker-bytearray-download", icon, imageResource); |
179 |
} |
180 |
} |
181 |
} |