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