Revision 2847
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.dynform/org.gvsig.tools.dynform.services/src/main/java/org/gvsig/tools/dynform/services/dynformfield/Bytearray/JDynFormFieldBytearray.java | ||
---|---|---|
49 | 49 |
protected JTextComponent jtext = null; |
50 | 50 |
protected JButton jbuttonUpload = null; |
51 | 51 |
protected JButton jbuttonDownload = null; |
52 |
protected JButton jbuttonEditAsText = null; |
|
52 | 53 |
private PickerController<byte[]> picker; |
53 | 54 |
|
54 | 55 |
public JDynFormFieldBytearray( |
... | ... | |
84 | 85 |
this.contents = new JPanel(); |
85 | 86 |
this.contents.setLayout(new BorderLayout()); |
86 | 87 |
|
87 |
DynFormSPIManager.ComponentsFactory components = this.getComponentsFactory(); |
|
88 |
DynFormSPIManager.ComponentsFactory componentsFactory = this.getComponentsFactory();
|
|
88 | 89 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
89 | 90 |
|
90 |
this.jtext = components.getJTextField(this.getDefinition(), null); |
|
91 |
this.jbuttonDownload = components.getJButton(this.getDefinition(), "Download"); |
|
92 |
this.jbuttonUpload = components.getJButton(this.getDefinition(), "Upload"); |
|
91 |
this.jtext = componentsFactory.getJTextField(this.getDefinition(), null); |
|
92 |
this.jbuttonDownload = componentsFactory.getJButton(this.getDefinition(), "Download"); |
|
93 |
this.jbuttonUpload = componentsFactory.getJButton(this.getDefinition(), "Upload"); |
|
94 |
this.jbuttonEditAsText = componentsFactory.getJButton(this.getDefinition(), "EditAsText"); |
|
93 | 95 |
|
94 | 96 |
this.picker = toolsSwingManager.createByteArrayPickerController( |
95 | 97 |
(JTextField) jtext, |
96 | 98 |
jbuttonUpload, |
97 | 99 |
jbuttonDownload, |
100 |
jbuttonEditAsText, |
|
98 | 101 |
this.getForm().getDefinition().getName() + ".RAWDATA", |
99 | 102 |
null |
100 | 103 |
); |
... | ... | |
104 | 107 |
fireFieldChangedEvent(); |
105 | 108 |
} |
106 | 109 |
}); |
107 |
if (!components.containsComponents(this.getDefinition())) { |
|
110 |
if (!componentsFactory.containsComponents(this.getDefinition())) {
|
|
108 | 111 |
toolsSwingManager.removeBorder(this.jbuttonDownload); |
109 | 112 |
toolsSwingManager.removeBorder(this.jbuttonUpload); |
113 |
toolsSwingManager.removeBorder(this.jbuttonEditAsText); |
|
110 | 114 |
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.TRAILING, 4, 2)); |
111 | 115 |
buttons.add(this.jbuttonUpload); |
112 | 116 |
buttons.add(this.jbuttonDownload); |
117 |
if( this.jbuttonEditAsText!=null ) { |
|
118 |
buttons.add(this.jbuttonEditAsText); |
|
119 |
} |
|
113 | 120 |
this.contents.add(jtext, BorderLayout.CENTER); |
114 | 121 |
this.contents.add(buttons, BorderLayout.LINE_END); |
115 | 122 |
this.contents.setVisible(true); |
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 | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.awt.Cursor; |
4 | 4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
|
6 | 5 |
import java.io.File; |
7 | 6 |
import java.net.URL; |
7 |
import java.nio.charset.StandardCharsets; |
|
8 | 8 |
import javax.swing.ImageIcon; |
9 | 9 |
import javax.swing.JButton; |
10 | 10 |
import javax.swing.JFileChooser; |
11 |
import javax.swing.JOptionPane; |
|
11 | 12 |
import javax.swing.text.JTextComponent; |
12 | 13 |
import org.apache.commons.io.FileUtils; |
13 | 14 |
import org.apache.commons.lang3.ArrayUtils; |
... | ... | |
19 | 20 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
20 | 21 |
import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
21 | 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; |
|
22 | 25 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
23 | 26 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
27 |
import org.gvsig.tools.swing.impl.DefaultZoomDialog; |
|
24 | 28 |
|
25 | 29 |
/** |
26 | 30 |
* |
... | ... | |
34 | 38 |
private final JTextComponent jtext; |
35 | 39 |
private final JButton btnUpload; |
36 | 40 |
private final JButton btnDownload; |
41 |
private final JButton btnEditAsText; |
|
37 | 42 |
|
38 | 43 |
private byte[] value; |
39 | 44 |
private final String fileChooserID; |
... | ... | |
43 | 48 |
JTextComponent jtext, |
44 | 49 |
JButton btnUpload, |
45 | 50 |
JButton btnDownload, |
51 |
JButton btnEdiAsText, |
|
46 | 52 |
String fileChooserID, |
47 | 53 |
File initialPath |
48 | 54 |
) { |
49 | 55 |
this.jtext = jtext; |
50 | 56 |
this.btnUpload = btnUpload; |
51 | 57 |
this.btnDownload = btnDownload; |
58 |
if( btnEdiAsText == null ) { |
|
59 |
this.btnEditAsText = new JButton(); |
|
60 |
} else { |
|
61 |
this.btnEditAsText = btnEdiAsText; |
|
62 |
} |
|
52 | 63 |
this.fileChooserID = fileChooserID; |
53 | 64 |
this.initialPath = initialPath; |
54 | 65 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
... | ... | |
71 | 82 |
this.btnUpload.addActionListener((ActionEvent e) -> { |
72 | 83 |
doUpload(); |
73 | 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 |
}); |
|
74 | 94 |
|
75 | 95 |
this.jtext.setEditable(false); |
76 | 96 |
ToolsSwingLocator.getToolsSwingManager().addClearButton(jtext, (ActionEvent e) -> { |
77 | 97 |
this.value = null; |
78 | 98 |
this.jtext.setText(""); |
79 | 99 |
this.btnDownload.setEnabled(false); |
100 |
this.btnEditAsText.setEnabled(true); |
|
80 | 101 |
}); |
81 | 102 |
} |
82 | 103 |
|
... | ... | |
88 | 109 |
@Override |
89 | 110 |
public void set(byte[] value) { |
90 | 111 |
this.value = value; |
91 |
int size = 0; |
|
92 | 112 |
if( this.value==null ) { |
93 | 113 |
this.btnDownload.setEnabled(false); |
114 |
this.btnEditAsText.setEnabled(true); |
|
115 |
this.jtext.setText(""); |
|
94 | 116 |
} else { |
95 | 117 |
this.btnDownload.setEnabled(true); |
96 |
size = this.value.length; |
|
118 |
this.btnEditAsText.setEnabled(true); |
|
119 |
int size = this.value.length; |
|
120 |
this.jtext.setText(String.format("%d bytes", size)); |
|
97 | 121 |
} |
98 |
this.jtext.setText(String.format("%d bytes", size));
|
|
122 |
this.fireChangeEvent();
|
|
99 | 123 |
} |
100 | 124 |
|
101 | 125 |
@Override |
... | ... | |
107 | 131 |
public void setEnabled(boolean enabled) { |
108 | 132 |
this.btnDownload.setEnabled(true); |
109 | 133 |
this.btnUpload.setEnabled(enabled); |
134 |
this.btnEditAsText.setEnabled(true); |
|
110 | 135 |
} |
111 | 136 |
|
112 | 137 |
@Override |
... | ... | |
137 | 162 |
} |
138 | 163 |
|
139 | 164 |
private void doDownload() { |
140 |
File f = this.fileChooser("Save file", JFileChooser.SAVE_DIALOG); |
|
165 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
166 |
String title = i18n.getTranslation("_Download_data"); |
|
167 |
File f = this.fileChooser(title, JFileChooser.SAVE_DIALOG); |
|
141 | 168 |
if( f ==null ) { |
142 | 169 |
return; |
143 | 170 |
} |
... | ... | |
149 | 176 |
} |
150 | 177 |
|
151 | 178 |
private void doUpload() { |
152 |
File f = this.fileChooser("Load file", JFileChooser.OPEN_DIALOG); |
|
179 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
180 |
String title = i18n.getTranslation("_Upload_data"); |
|
181 |
File f = this.fileChooser(title, JFileChooser.OPEN_DIALOG); |
|
153 | 182 |
if( f == null ) { |
154 | 183 |
return; |
155 | 184 |
} |
... | ... | |
162 | 191 |
} |
163 | 192 |
} |
164 | 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 |
|
|
165 | 240 |
public static void selfRegister() { |
166 | 241 |
URL imageResource = BytearrayPickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-bytearray-upload.png"); |
167 | 242 |
if (imageResource != null) { |
... | ... | |
177 | 252 |
ImageIcon icon = new ImageIcon(imageResource); |
178 | 253 |
theme.registerDefault("tools", "picker", "picker-bytearray-download", icon, imageResource); |
179 | 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 |
} |
|
180 | 262 |
imageResource = BytearrayPickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/picker/picker-bytearray-setnull.png"); |
181 | 263 |
if (imageResource != null) { |
182 | 264 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
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/DefaultToolsSwingManager.java | ||
---|---|---|
396 | 396 |
} |
397 | 397 |
|
398 | 398 |
@Override |
399 |
public PickerController<byte[]> createByteArrayPickerController(JTextComponent txtText, JButton btnUpload, JButton btnDownload, JButton btnEditAsText, String fileChooserID, File initialPath) { |
|
400 |
return new BytearrayPickerControllerImpl(txtText, btnUpload, btnDownload, btnEditAsText, fileChooserID, initialPath); |
|
401 |
} |
|
402 |
|
|
403 |
@Override |
|
399 | 404 |
public PickerController<byte[]> createByteArrayPickerController(JTextComponent txtText, JButton btnUpload, JButton btnDownload, String fileChooserID, File initialPath) { |
400 |
return new BytearrayPickerControllerImpl(txtText, btnUpload, btnDownload, fileChooserID, initialPath); |
|
405 |
return new BytearrayPickerControllerImpl(txtText, btnUpload, btnDownload, null, fileChooserID, initialPath);
|
|
401 | 406 |
} |
402 | 407 |
|
403 | 408 |
@Override |
404 | 409 |
public PickerController<byte[]> createByteArrayPickerController(JTextComponent txtText, JButton btnUpload, JButton btnDownload) { |
405 |
return new BytearrayPickerControllerImpl(txtText, btnUpload, btnDownload, null, null); |
|
410 |
return new BytearrayPickerControllerImpl(txtText, btnUpload, btnDownload, null, null, null);
|
|
406 | 411 |
} |
407 | 412 |
|
408 | 413 |
@Override |
... | ... | |
431 | 436 |
|
432 | 437 |
@Override |
433 | 438 |
public void removeBorder(JComponent component) { |
439 |
if( component == null ) { |
|
440 |
return; |
|
441 |
} |
|
434 | 442 |
try { |
435 | 443 |
component.setBorder(null); |
436 | 444 |
JScrollPane scrollPanel = this.getJScrollPane(component); |
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/DefaultZoomDialog.java | ||
---|---|---|
40 | 40 |
import javax.swing.JScrollPane; |
41 | 41 |
import javax.swing.JTextArea; |
42 | 42 |
import javax.swing.KeyStroke; |
43 |
import org.gvsig.tools.ToolsLocator; |
|
44 |
import org.gvsig.tools.i18n.I18nManager; |
|
43 | 45 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
44 | 46 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
47 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
|
45 | 48 |
|
46 | 49 |
public class DefaultZoomDialog extends JDialog { |
47 | 50 |
|
... | ... | |
52 | 55 |
|
53 | 56 |
private String value = null; |
54 | 57 |
private JTextArea text = null; |
58 |
private int action = WindowManager_v2.BUTTON_CANCEL; |
|
59 |
private JButton btnClose; |
|
55 | 60 |
|
56 | 61 |
public DefaultZoomDialog(Component parent, String title, WindowManager.MODE mode) { |
57 | 62 |
super(getParentWindow(parent), title, mode == WindowManager.MODE.WINDOW ? ModalityType.MODELESS : ModalityType.DOCUMENT_MODAL); |
... | ... | |
76 | 81 |
|
77 | 82 |
public void setEditable(boolean editable) { |
78 | 83 |
this.text.setEditable(editable); |
84 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
85 |
if( editable ) { |
|
86 |
this.btnClose.setText(i18n.getTranslation("_Accept")); |
|
87 |
} else { |
|
88 |
this.btnClose.setText(i18n.getTranslation("_Close")); |
|
89 |
} |
|
79 | 90 |
} |
80 | 91 |
|
81 | 92 |
private void initComponents() { |
... | ... | |
93 | 104 |
|
94 | 105 |
JPanel p = new JPanel(); |
95 | 106 |
|
96 |
JButton close = new JButton("Close"); |
|
97 |
close.addActionListener(new ActionListener() { |
|
107 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
108 |
this.btnClose = new JButton(i18n.getTranslation("_Accept")); |
|
109 |
this.btnClose.addActionListener(new ActionListener() { |
|
98 | 110 |
@Override |
99 | 111 |
public void actionPerformed(ActionEvent arg0) { |
100 |
value = text.getText(); |
|
112 |
if( text.isEditable() ) { |
|
113 |
value = text.getText(); |
|
114 |
setAction(WindowManager_v2.BUTTON_OK); |
|
115 |
} else { |
|
116 |
setAction(WindowManager_v2.BUTTON_CANCEL); |
|
117 |
} |
|
101 | 118 |
setVisible(false); |
102 | 119 |
} |
103 | 120 |
}); |
104 | 121 |
|
105 | 122 |
p.setLayout(new BorderLayout()); |
106 | 123 |
p.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); |
107 |
p.add(close, BorderLayout.LINE_END);
|
|
124 |
p.add(this.btnClose, BorderLayout.LINE_END);
|
|
108 | 125 |
|
109 | 126 |
panel.add(p, BorderLayout.PAGE_END); |
110 | 127 |
ContextMenuUtils.setDefaultContextMenu(text, false); |
... | ... | |
118 | 135 |
@Override |
119 | 136 |
public void actionPerformed(ActionEvent e) { |
120 | 137 |
setVisible(false); |
138 |
setAction(WindowManager_v2.BUTTON_CANCEL); |
|
121 | 139 |
} |
122 | 140 |
}, |
123 | 141 |
stroke, |
... | ... | |
145 | 163 |
public String getText() { |
146 | 164 |
return this.value; |
147 | 165 |
} |
166 |
|
|
167 |
protected void setAction(int action) { |
|
168 |
this.action = action; |
|
169 |
} |
|
170 |
|
|
171 |
public int getAction() { |
|
172 |
return this.action; |
|
173 |
} |
|
148 | 174 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.api/src/main/java/org/gvsig/tools/swing/api/ToolsSwingManager.java | ||
---|---|---|
443 | 443 |
|
444 | 444 |
public FolderPickerController createFolderPickerController(JTextComponent txtFile, JButton btnFile, String dialogTitle); |
445 | 445 |
|
446 |
public PickerController<byte[]> createByteArrayPickerController(JTextComponent txtText, JButton btnUpload, JButton btnDownload, JButton btnEditAsText, String fileChooserID, File initialPath); |
|
447 |
|
|
446 | 448 |
public PickerController<byte[]> createByteArrayPickerController(JTextComponent txtText, JButton btnUpload, JButton btnDownload, String fileChooserID, File initialPath); |
447 | 449 |
|
448 | 450 |
public PickerController<byte[]> createByteArrayPickerController(JTextComponent txtText, JButton btnUpload, JButton btnDownload); |
Also available in: Unified diff