Revision 45771 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.impl/src/main/java/org/gvsig/featureform/swing/impl/dynformfield/ImageByteArray/JDynFormFieldImageByteArray.java
JDynFormFieldImageByteArray.java | ||
---|---|---|
22 | 22 |
*/ |
23 | 23 |
package org.gvsig.featureform.swing.impl.dynformfield.ImageByteArray; |
24 | 24 |
|
25 |
import java.awt.event.ActionEvent; |
|
25 | 26 |
import java.awt.event.FocusListener; |
27 |
import java.io.File; |
|
28 |
import javax.swing.AbstractAction; |
|
29 |
import javax.swing.JFileChooser; |
|
30 |
import org.apache.commons.io.FileUtils; |
|
31 |
import org.apache.commons.lang3.ArrayUtils; |
|
26 | 32 |
import org.gvsig.featureform.swing.impl.dynformfield.ImageFile.AbstractJDynFormFieldImage; |
33 |
import org.gvsig.tools.ToolsLocator; |
|
27 | 34 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
28 | 35 |
import org.gvsig.tools.dynform.JDynFormField; |
29 | 36 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
30 | 37 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
38 |
import org.gvsig.tools.folders.FoldersManager; |
|
39 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
|
40 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
|
31 | 41 |
|
42 |
@SuppressWarnings("UseSpecificCatch") |
|
32 | 43 |
public class JDynFormFieldImageByteArray extends AbstractJDynFormFieldImage<byte[]> implements JDynFormField, FocusListener { |
33 | 44 |
|
34 | 45 |
public JDynFormFieldImageByteArray( |
... | ... | |
62 | 73 |
this.updateImage(this.currentValue); |
63 | 74 |
} |
64 | 75 |
|
76 |
|
|
77 |
private void doDownload() { |
|
78 |
File f = this.fileChooser("Save file", JFileChooser.SAVE_DIALOG); |
|
79 |
if( f ==null ) { |
|
80 |
return; |
|
81 |
} |
|
82 |
try { |
|
83 |
FileUtils.writeByteArrayToFile(f, this.currentValue); |
|
84 |
} catch(Exception ex) { |
|
85 |
LOGGER.warn("Can't save bytes from file '"+f.getAbsolutePath()+"'.",ex); |
|
86 |
} |
|
87 |
} |
|
88 |
|
|
89 |
private void doUpload() { |
|
90 |
File f = this.fileChooser("Load file", JFileChooser.OPEN_DIALOG); |
|
91 |
if( f == null ) { |
|
92 |
return; |
|
93 |
} |
|
94 |
try { |
|
95 |
byte[] bytes = FileUtils.readFileToByteArray(f); |
|
96 |
this.setValue(bytes); |
|
97 |
} catch (Exception ex) { |
|
98 |
LOGGER.warn("Can't load bytes from file '"+f.getAbsolutePath()+"'.",ex); |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
private File fileChooser(String title, int mode) { |
|
103 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
|
104 |
FoldersManager folderManager = ToolsLocator.getFoldersManager(); |
|
105 |
File[] f = dialogs.showChooserDialog( |
|
106 |
title, |
|
107 |
mode, |
|
108 |
JFileChooser.FILES_ONLY, |
|
109 |
true, |
|
110 |
folderManager.getLastPath("ByteArrayDynFormField",null), |
|
111 |
null, |
|
112 |
true |
|
113 |
); |
|
114 |
|
|
115 |
if( ArrayUtils.isEmpty(f) ) { |
|
116 |
return null; |
|
117 |
} |
|
118 |
return f[0]; |
|
119 |
} |
|
120 |
|
|
121 |
@Override |
|
122 |
protected void initImageViewer() { |
|
123 |
super.initComponent(); |
|
124 |
imageViewer.addAction(new AbstractAction(null, this.getIcon("picker-bytearray-download")) { |
|
125 |
@Override |
|
126 |
public void actionPerformed(ActionEvent e) { |
|
127 |
doDownload(); |
|
128 |
} |
|
129 |
}); |
|
130 |
imageViewer.addAction(new AbstractAction(null, this.getIcon("picker-bytearray-upload")) { |
|
131 |
@Override |
|
132 |
public void actionPerformed(ActionEvent e) { |
|
133 |
doUpload(); |
|
134 |
} |
|
135 |
}); |
|
136 |
imageViewer.addAction(new AbstractAction(null, this.getIcon("picker-bytearray-setnull")) { |
|
137 |
@Override |
|
138 |
public void actionPerformed(ActionEvent e) { |
|
139 |
clear(); |
|
140 |
} |
|
141 |
}); |
|
142 |
|
|
143 |
} |
|
144 |
|
|
65 | 145 |
} |
Also available in: Unified diff