This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 206233
Collapse All | Expand All

(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BlobFieldTableCellEditor.java (-24 / +31 lines)
Lines 51-56 Link Here
51
import java.io.FileOutputStream;
51
import java.io.FileOutputStream;
52
import java.io.IOException;
52
import java.io.IOException;
53
import java.io.InputStream;
53
import java.io.InputStream;
54
import java.io.OutputStream;
54
import java.sql.Blob;
55
import java.sql.Blob;
55
import java.sql.SQLException;
56
import java.sql.SQLException;
56
import java.util.EventObject;
57
import java.util.EventObject;
Lines 62-69 Link Here
62
import javax.swing.JTable;
63
import javax.swing.JTable;
63
import javax.swing.SwingConstants;
64
import javax.swing.SwingConstants;
64
import javax.swing.table.TableCellEditor;
65
import javax.swing.table.TableCellEditor;
66
import org.netbeans.api.progress.ProgressUtils;
65
import org.netbeans.modules.db.dataview.util.FileBackedBlob;
67
import org.netbeans.modules.db.dataview.util.FileBackedBlob;
66
import org.openide.util.Exceptions;
67
import org.openide.util.NbBundle;
68
import org.openide.util.NbBundle;
68
69
69
public class BlobFieldTableCellEditor extends AbstractCellEditor
70
public class BlobFieldTableCellEditor extends AbstractCellEditor
Lines 181-210 Link Here
181
            try {
182
            try {
182
                is = b.getBinaryStream();
183
                is = b.getBinaryStream();
183
                fos = new FileOutputStream(f);
184
                fos = new FileOutputStream(f);
184
                int read = 0;
185
                if(! doTransfer(is, fos, (int) b.length(), "Saving to file: " + f.toString())) {
185
                byte[] buffer = new byte[1024];
186
                    f.delete();
186
                while((read = is.read(buffer)) > 0) {
187
                    fos.write(buffer, 0, read);
188
                }
187
                }
189
            } catch (IOException ex) {
188
            } catch (IOException ex) {
190
                throw new RuntimeException(ex);
189
                throw new RuntimeException(ex);
191
            } catch (SQLException ex) {
190
            } catch (SQLException ex) {
192
                throw new RuntimeException(ex);
191
                throw new RuntimeException(ex);
193
            } finally {
194
                try {
195
                    if(fos != null) fos.close();
196
                } catch (IOException ex) {
197
                    Exceptions.printStackTrace(ex);
198
                }
199
                try {
200
                    if(is != null) is.close();
201
                } catch (IOException ex) {
202
                    Exceptions.printStackTrace(ex);
203
                }
204
            }
192
            }
205
        }
193
        }
206
    }
194
    }
207
    
195
208
    private Blob loadLobFromFile() {
196
    private Blob loadLobFromFile() {
209
        JFileChooser c = new JFileChooser();
197
        JFileChooser c = new JFileChooser();
210
        Blob result = null;
198
        Blob result = null;
Lines 214-232 Link Here
214
            FileInputStream fis = null;
202
            FileInputStream fis = null;
215
            try {
203
            try {
216
                fis = new FileInputStream(f);
204
                fis = new FileInputStream(f);
217
                result = new FileBackedBlob(fis);
205
                result = new FileBackedBlob();
206
                if(! doTransfer(fis, result.setBinaryStream(1), (int) f.length(), "Loading file: " + f.toString())) {
207
                    result = null;
208
                }
218
            } catch (IOException ex) {
209
            } catch (IOException ex) {
219
                throw new RuntimeException(ex);
210
                throw new RuntimeException(ex);
220
            } catch (SQLException ex) {
211
            } catch (SQLException ex) {
221
                throw new RuntimeException(ex);
212
                throw new RuntimeException(ex);
222
            } finally {
223
                try {
224
                    if(fis != null) fis.close();
225
                } catch (IOException ex) {
226
                    Exceptions.printStackTrace(ex);
227
                }
228
            }
213
            }
229
        }
214
        }
230
        return result;
215
        return result;
231
    }
216
    }
217
218
    /**
219
     * @return true if transfer is complete and not iterrupted 
220
     */
221
    private boolean doTransfer(InputStream is, OutputStream os, Integer size, String title) throws IOException {
222
        MonitorableStreamTransfer ft = new MonitorableStreamTransfer(is, os, size);
223
        Throwable t = null;
224
        // Only show dialog, if the filesize is large enougth and has a use for the user
225
        if (size == null || size > (1024 * 1024)) {
226
            t = ProgressUtils.showProgressDialogAndRun(ft, title, false);
227
        } else {
228
            t = ft.run(null);
229
        }
230
        if (t != null && t instanceof RuntimeException) {
231
            throw (RuntimeException) t;
232
        } else if (t != null && t instanceof IOException) {
233
            throw (IOException) t;
234
        } else if (t != null) {
235
            throw new RuntimeException(t);
236
        }
237
        return !ft.isCancel();
238
    }
232
}
239
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/ClobFieldTableCellEditor.java (-26 / +33 lines)
Lines 73-78 Link Here
73
import javax.swing.JTable;
73
import javax.swing.JTable;
74
import javax.swing.SwingConstants;
74
import javax.swing.SwingConstants;
75
import javax.swing.table.TableCellEditor;
75
import javax.swing.table.TableCellEditor;
76
import org.netbeans.api.progress.ProgressUtils;
76
import org.netbeans.modules.db.dataview.util.FileBackedClob;
77
import org.netbeans.modules.db.dataview.util.FileBackedClob;
77
import org.openide.util.Exceptions;
78
import org.openide.util.Exceptions;
78
import org.openide.util.NbBundle;
79
import org.openide.util.NbBundle;
Lines 83-89 Link Here
83
84
84
    private class CharsetSelector extends JPanel {
85
    private class CharsetSelector extends JPanel {
85
        private JComboBox charsetSelect;
86
        private JComboBox charsetSelect;
86
        
87
87
        CharsetSelector() {
88
        CharsetSelector() {
88
            List<Charset> charset = new ArrayList<Charset>(Charset.availableCharsets().values());
89
            List<Charset> charset = new ArrayList<Charset>(Charset.availableCharsets().values());
89
            Collections.sort(charset, new Comparator<Charset>() {
90
            Collections.sort(charset, new Comparator<Charset>() {
Lines 106-112 Link Here
106
            charsetSelect.setSelectedItem(selectedCharset);
107
            charsetSelect.setSelectedItem(selectedCharset);
107
        }
108
        }
108
    }
109
    }
109
    
110
    protected static final String EDIT = "edit";
110
    protected static final String EDIT = "edit";
111
    protected Clob currentValue;
111
    protected Clob currentValue;
112
    protected JButton button;
112
    protected JButton button;
Lines 207-213 Link Here
207
        }
207
        }
208
        return super.isCellEditable(anEvent);
208
        return super.isCellEditable(anEvent);
209
    }
209
    }
210
    
210
211
    private void saveLobToFile(Clob b) {
211
    private void saveLobToFile(Clob b) {
212
        CharsetSelector charset = new CharsetSelector();
212
        CharsetSelector charset = new CharsetSelector();
213
        JFileChooser c = new JFileChooser();
213
        JFileChooser c = new JFileChooser();
Lines 220-249 Link Here
220
            try {
220
            try {
221
                r = b.getCharacterStream();
221
                r = b.getCharacterStream();
222
                w = new OutputStreamWriter(new FileOutputStream(f), charset.getSelectedCharset());
222
                w = new OutputStreamWriter(new FileOutputStream(f), charset.getSelectedCharset());
223
                int read = 0;
223
                if(! doTransfer(r, w, (int) b.length(), "Save to file: " + f.toString(), false)) {
224
                char[] buffer = new char[1024];
224
                    f.delete();
225
                while((read = r.read(buffer)) > 0) {
226
                    w.write(buffer, 0, read);
227
                }
225
                }
228
            } catch (IOException ex) {
226
            } catch (IOException ex) {
229
                throw new RuntimeException(ex);
227
                throw new RuntimeException(ex);
230
            } catch (SQLException ex) {
228
            } catch (SQLException ex) {
231
                throw new RuntimeException(ex);
229
                throw new RuntimeException(ex);
232
            } finally {
233
                try {
234
                    if(w != null) w.close();
235
                } catch (IOException ex) {
236
                    Exceptions.printStackTrace(ex);
237
                }
238
                try {
239
                    if(r != null) r.close();
240
                } catch (IOException ex) {
241
                    Exceptions.printStackTrace(ex);
242
                }
243
            }
230
            }
244
        }
231
        }
245
    }
232
    }
246
    
233
247
    private Clob loadLobFromFile() {
234
    private Clob loadLobFromFile() {
248
        CharsetSelector charset = new CharsetSelector();
235
        CharsetSelector charset = new CharsetSelector();
249
        JFileChooser c = new JFileChooser();
236
        JFileChooser c = new JFileChooser();
Lines 254-273 Link Here
254
            File f = c.getSelectedFile();
241
            File f = c.getSelectedFile();
255
            Reader r = null;
242
            Reader r = null;
256
            try {
243
            try {
244
                result = new FileBackedClob();
257
                r = new InputStreamReader(new FileInputStream(f), charset.getSelectedCharset());
245
                r = new InputStreamReader(new FileInputStream(f), charset.getSelectedCharset());
258
                result = new FileBackedClob(r);
246
                if(! doTransfer(r, result.setCharacterStream(1), (int) f.length() / 2, "Load from file: " + f.toString(), true)) {
247
                    result = null;
248
                }
259
            } catch (IOException ex) {
249
            } catch (IOException ex) {
260
                throw new RuntimeException(ex);
250
                throw new RuntimeException(ex);
261
            } catch (SQLException ex) {
251
            } catch (SQLException ex) {
262
                throw new RuntimeException(ex);
252
                throw new RuntimeException(ex);
263
            } finally {
264
                try {
265
                    if(r != null) r.close();
266
                } catch (IOException ex) {
267
                    Exceptions.printStackTrace(ex);
268
                }
269
            }
253
            }
270
        }
254
        }
271
        return result;
255
        return result;
272
    }
256
    }
257
258
    /**
259
     * @return true if transfer is complete and not iterrupted 
260
     */
261
    private boolean doTransfer(Reader in, Writer out, Integer size, String title, boolean sizeEstimated) throws IOException {
262
        // Only pass size if it is _not_ estimated
263
        MonitorableCharacterStreamTransfer ft = new MonitorableCharacterStreamTransfer(in, out, sizeEstimated ? null : size);
264
        Throwable t = null;
265
        // Only show dialog, if the filesize is large enougth and has a use for the user
266
        if (size == null || size > (1024 * 1024)) {
267
            t = ProgressUtils.showProgressDialogAndRun(ft, title, false);
268
        } else {
269
            t = ft.run(null);
270
        }
271
        if (t != null && t instanceof RuntimeException) {
272
            throw (RuntimeException) t;
273
        } else if (t != null && t instanceof IOException) {
274
            throw (IOException) t;
275
        } else if (t != null) {
276
            throw new RuntimeException(t);
277
        }
278
        return ! ft.isCancel();
279
    }
273
}
280
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/MonitorableCharacterStreamTransfer.java (+110 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.table.celleditor;
43
44
import java.io.IOException;
45
import java.io.Reader;
46
import java.io.Writer;
47
import org.netbeans.api.progress.ProgressHandle;
48
import org.netbeans.api.progress.ProgressRunnable;
49
import org.openide.util.Cancellable;
50
51
class MonitorableCharacterStreamTransfer implements ProgressRunnable<Exception>, Cancellable {
52
53
    private Reader in;
54
    private Writer out;
55
    private int transfered;
56
    private Integer size;
57
    private boolean cancel;
58
59
    public MonitorableCharacterStreamTransfer(Reader in, Writer out, Integer knownsize) {
60
        this.in = in;
61
        this.out = out;
62
        size = knownsize;
63
    }
64
65
    @Override
66
    public Exception run(ProgressHandle handle) {
67
        Exception result = null;
68
        if (handle != null && size != null) {
69
            handle.switchToDeterminate(size);
70
        }
71
        try {
72
            int read = 0;
73
            char[] buffer = new char[128 * 1024];
74
            while ((read = in.read(buffer)) > 0 && !cancel) {
75
                out.write(buffer, 0, read);
76
                transfered += read;
77
                if (handle != null && size != null) {
78
                    handle.progress(transfered);
79
                }
80
            }
81
        } catch (IOException ex) {
82
            try {
83
                in.close();
84
            } catch (IOException ex2) {
85
            }
86
            try {
87
                out.close();
88
            } catch (IOException ex2) {
89
            }
90
            return ex;
91
        }
92
        if (handle != null) {
93
            handle.finish();
94
        }
95
        return result;
96
    }
97
98
    @Override
99
    public boolean cancel() {
100
        if (cancel) {
101
            return false;
102
        }
103
        this.cancel = true;
104
        return true;
105
    }
106
107
    public boolean isCancel() {
108
        return cancel;
109
    }
110
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/MonitorableStreamTransfer.java (+110 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.table.celleditor;
43
44
import java.io.IOException;
45
import java.io.InputStream;
46
import java.io.OutputStream;
47
import org.netbeans.api.progress.ProgressHandle;
48
import org.netbeans.api.progress.ProgressRunnable;
49
import org.openide.util.Cancellable;
50
51
class MonitorableStreamTransfer implements ProgressRunnable<Exception>, Cancellable {
52
53
    private InputStream is;
54
    private OutputStream os;
55
    private int transfered;
56
    private Integer size;
57
    private boolean cancel;
58
59
    public MonitorableStreamTransfer(InputStream is, OutputStream os, Integer knownsize) {
60
        this.is = is;
61
        this.os = os;
62
        size = knownsize;
63
    }
64
65
    @Override
66
    public Exception run(ProgressHandle handle) {
67
        Exception result = null;
68
        if (handle != null && size != null) {
69
            handle.switchToDeterminate(size);
70
        }
71
        try {
72
            int read = 0;
73
            byte[] buffer = new byte[256 * 1024];
74
            while ((read = is.read(buffer)) > 0 && !cancel) {
75
                os.write(buffer, 0, read);
76
                transfered += read;
77
                if (handle != null && size != null) {
78
                    handle.progress(transfered);
79
                }
80
            }
81
        } catch (IOException ex) {
82
            try {
83
                is.close();
84
            } catch (IOException ex2) {
85
            }
86
            try {
87
                os.close();
88
            } catch (IOException ex2) {
89
            }
90
            return ex;
91
        }
92
        if (handle != null) {
93
            handle.finish();
94
        }
95
        return result;
96
    }
97
98
    @Override
99
    public boolean cancel() {
100
        if (cancel) {
101
            return false;
102
        }
103
        this.cancel = true;
104
        return true;
105
    }
106
107
    public boolean isCancel() {
108
        return cancel;
109
    }
110
}

Return to bug 206233