Revision 2847 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

View differences:

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();

Also available in: Unified diff