# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: /home/matthias/NetBeansProjects/core-main # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BlobFieldTableCellEditor.java --- db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BlobFieldTableCellEditor.java +++ db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BlobFieldTableCellEditor.java @@ -43,10 +43,14 @@ import java.awt.Component; import java.awt.Font; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.io.*; +import java.nio.charset.Charset; import java.sql.Blob; import java.sql.SQLException; import java.util.EventObject; @@ -59,6 +63,8 @@ import javax.swing.*; import javax.swing.table.TableCellEditor; import org.netbeans.api.progress.ProgressUtils; +import org.netbeans.modules.db.dataview.util.CharsetSelector; +import org.netbeans.modules.db.dataview.util.EncodingHelper; import org.netbeans.modules.db.dataview.util.FileBackedBlob; import org.netbeans.modules.db.dataview.util.LobHelper; import org.openide.DialogDisplayer; @@ -68,7 +74,9 @@ import org.openide.filesystems.FileSystem; import org.openide.filesystems.FileUtil; import org.openide.loaders.DataObject; +import org.openide.util.Exceptions; import org.openide.util.NbBundle; +import org.openide.windows.WindowManager; public class BlobFieldTableCellEditor extends AbstractCellEditor implements TableCellEditor, @@ -78,13 +86,19 @@ private static final String EDIT = "edit"; private static File lastFile; + private static Charset lastSelectedCharset = Charset.defaultCharset(); private Blob currentValue; private JButton button; private JPopupMenu popup; private JTable table; + private int currentRow; + private int currentColumn; + private int currentModelColumn; + private int currentModelRow; private JMenuItem saveContentMenuItem; private JMenuItem miOpenImageMenuItem; + private JMenuItem miOpenAsTextMenuItem; private JMenuItem miLobLoadAction; private JMenuItem miLobNullAction; @@ -102,28 +116,42 @@ button.setFont(new Font(button.getFont().getFamily(), Font.ITALIC, 9)); popup = new JPopupMenu(); - saveContentMenuItem = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "saveLob.title")); - saveContentMenuItem.addActionListener(new ActionListener() { + miOpenImageMenuItem = new JMenuItem("Open as Image"); + miOpenImageMenuItem.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { - saveLobToFile(currentValue); + openAsImage(currentValue); fireEditingCanceled(); } }); - popup.add(saveContentMenuItem); + popup.add(miOpenImageMenuItem); - miOpenImageMenuItem = new JMenuItem("Open as Image"); - miOpenImageMenuItem.addActionListener(new ActionListener() { + miOpenAsTextMenuItem = new JMenuItem("Open as Text"); + miOpenAsTextMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - openAsImage(currentValue); + openAsText(); fireEditingCanceled(); } }); - popup.add(miOpenImageMenuItem); + popup.add(miOpenAsTextMenuItem); + popup.add(new JSeparator()); + + saveContentMenuItem = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "saveLob.title")); + saveContentMenuItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + saveLobToFile(currentValue); + fireEditingCanceled(); + } + }); + popup.add(saveContentMenuItem); + miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title")); miLobLoadAction.addActionListener(new ActionListener() { @@ -175,6 +203,10 @@ miLobLoadAction.setEnabled(editable); miLobNullAction.setEnabled(editable); this.table = table; + this.currentColumn = column; + this.currentRow = row; + this.currentModelColumn = table.convertColumnIndexToModel(column); + this.currentModelRow = table.convertRowIndexToModel(row); return button; } @@ -370,4 +402,157 @@ dd.notifyLater(nd); } + + protected void openAsText() { + GridBagConstraints gbc; + + Charset detectedCharset = detectEncoding(); + + final CharsetSelector charsetSelector = new CharsetSelector(); + + charsetSelector.setSelectedItem(detectedCharset == null ? lastSelectedCharset : detectedCharset); + + JPanel panel = new JPanel(); + panel.setLayout(new GridBagLayout()); + + final JTextArea textArea = new JTextArea(20, 80); + // Work around: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7100524 + textArea.setDropTarget(null); + textArea.setText(getTextFromCurrentCell(charsetSelector.getSelectedItem())); + textArea.setCaretPosition(0); + textArea.setEditable(table.getModel().isCellEditable(currentModelRow, currentModelColumn)); + + JScrollPane pane = new JScrollPane(textArea); + + gbc = new GridBagConstraints(); + gbc.anchor = GridBagConstraints.BASELINE; + gbc.gridx = 0; + gbc.gridy = 0; + gbc.gridwidth = 1; + gbc.insets = new Insets(3, 3, 3, 3); + gbc.weightx = 0d; + gbc.weighty = 0d; + gbc.fill = GridBagConstraints.HORIZONTAL; + panel.add(new JLabel("Charset-Encoding: "), gbc); + + gbc = new GridBagConstraints(); + gbc.anchor = GridBagConstraints.BASELINE; + gbc.gridx = 1; + gbc.gridy = 0; + gbc.gridwidth = 1; + gbc.insets = new Insets(3, 3, 3, 3); + gbc.weightx = 1d; + gbc.weighty = 0d; + gbc.fill = GridBagConstraints.HORIZONTAL; + panel.add(charsetSelector, gbc); + + JButton reloadButton = new JButton("Reload"); + reloadButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + textArea.setText(getTextFromCurrentCell(charsetSelector.getSelectedItem())); + lastSelectedCharset = charsetSelector.getSelectedItem(); } + }); + + gbc = new GridBagConstraints(); + gbc.anchor = GridBagConstraints.BASELINE_LEADING; + gbc.gridx = 2; + gbc.gridy = 0; + gbc.gridwidth = 1; + gbc.insets = new Insets(3, 3, 3, 3); + gbc.weightx = 0d; + gbc.weighty = 0d; + gbc.fill = GridBagConstraints.NONE; + panel.add(reloadButton, gbc); + + gbc = new GridBagConstraints(); + gbc.anchor = GridBagConstraints.CENTER; + gbc.gridx = 0; + gbc.gridy = 1; + gbc.gridwidth = 3; + gbc.insets = new Insets(3, 3, 3, 3); + gbc.weightx = 1d; + gbc.weighty = 1d; + gbc.fill = GridBagConstraints.BOTH; + panel.add(pane, gbc); + + pane.addHierarchyListener( + new StringTableCellEditor.MakeResizableListener(panel)); + Component parent = WindowManager.getDefault().getMainWindow(); + + if (table.isCellEditable(currentRow, currentColumn)) { + int result = JOptionPane.showOptionDialog(parent, panel, table.getColumnName(currentColumn), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null); + if (result == JOptionPane.OK_OPTION) { + try { + table.setValueAt(new FileBackedBlob( + new ByteArrayInputStream( + textArea.getText().getBytes(charsetSelector.getSelectedItem()))), + currentRow, currentColumn); + lastSelectedCharset = charsetSelector.getSelectedItem(); + } catch (SQLException ex) { + Exceptions.printStackTrace(ex); + } + } + } else { + JOptionPane.showMessageDialog(parent, panel, table.getColumnName(currentColumn), JOptionPane.PLAIN_MESSAGE, null); + } + } + + private Charset detectEncoding() { + if(currentValue == null) { + return null; + } + InputStream blobStream = null; + try { + blobStream = currentValue.getBinaryStream(); + BufferedInputStream inputStream = new BufferedInputStream(blobStream); + String charsetName = EncodingHelper.detectEncoding(inputStream); + Charset result = null; + if(charsetName != null) { + result = Charset.forName(charsetName); + } + return result; + } catch (SQLException | IOException ex) { + LOG.log(Level.FINE, "Failed to read BLOB contents.", ex); + } finally { + if (blobStream != null) { + try { + blobStream.close(); + } catch (IOException ex) { + } + } + } + return null; + } + + private String getTextFromCurrentCell(Charset charset) { + if(currentValue == null) { + return ""; + } + InputStream blobStream = null; + try { + blobStream = currentValue.getBinaryStream(); + Reader reader = new BufferedReader( + new InputStreamReader(blobStream, charset) + ); + StringBuilder sb = new StringBuilder(); + char[] buffer = new char[1024]; + int read; + while((read = reader.read(buffer)) > 0) { + sb.append(buffer, 0, read); + } + return sb.toString(); + } catch (SQLException | IOException ex) { + LOG.log(Level.FINE, "Failed to read BLOB contents.", ex); + } finally { + if(blobStream != null) { + try { + blobStream.close(); + } catch (IOException ex) { + } + } + } + return ""; + } +} Index: db.dataview/src/org/netbeans/modules/db/dataview/util/Bundle.properties --- db.dataview/src/org/netbeans/modules/db/dataview/util/Bundle.properties +++ db.dataview/src/org/netbeans/modules/db/dataview/util/Bundle.properties @@ -81,3 +81,35 @@ Cause: {1} BinaryToStringConverter_InvalidBitFormat={0} found at character {1}; 0 or 1 expected. + +# Encoding display names taken from TAx library +NAME_ISO-8859-1=ISO-8859-1 (Latin 1) +NAME_IBM870=IBM870 +NAME_ISO-2022-JP=ISO-2022-JP +NAME_ISO-8859-3=ISO-8859-3 +NAME_IBM424=IBM424 +NAME_KOI8-R=KOI8-R (Russian) +NAME_IBM280=IBM280 +NAME_ISO-8859-4=ISO-8859-4 +NAME_IBM285=IBM285 +NAME_EUC-JP=EUC-JP +NAME_UTF-8=UTF-8 (recommended) +NAME_IBM297=IBM297 +NAME_ASCII=ASCII (7-bit) +NAME_ISO-8859-2=ISO-8859-2 +NAME_UTF-16=UTF-16 (recommended) +NAME_IBM500=IBM500 +NAME_IBM277=IBM277 +NAME_GB2312=GB2312 +NAME_ISO-8859-9=ISO-8859-9 +NAME_IBM871=IBM871 +NAME_ISO-8859-7=ISO-8859-7 +NAME_IBM037=IBM037 +NAME_IBM918=IBM918 +NAME_IBM284=IBM284 +NAME_ISO-2022-KR=ISO-2022-KR +NAME_EUC-KR=EUC-KR +NAME_ISO-8859-6=ISO-8859-6 +NAME_ISO-8859-8=ISO-8859-8 +NAME_BIG5=BIG5 +NAME_ISO-8859-5=ISO-8859-5 \ No newline at end of file Index: db.dataview/src/org/netbeans/modules/db/dataview/util/CharsetSelector.java --- db.dataview/src/org/netbeans/modules/db/dataview/util/CharsetSelector.java +++ db.dataview/src/org/netbeans/modules/db/dataview/util/CharsetSelector.java @@ -0,0 +1,78 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2014 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.db.dataview.util; + +import java.awt.Component; +import java.nio.charset.Charset; +import javax.swing.DefaultListCellRenderer; +import javax.swing.JComboBox; +import javax.swing.JList; + +public class CharsetSelector extends JComboBox{ + + public CharsetSelector() { + super( + Charset.availableCharsets().values().toArray(new Charset[]{}) + ); + this.setRenderer(new DefaultListCellRenderer(){ + + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + Object displayValue; + if(value instanceof Charset) { + displayValue = ((Charset) value).displayName(); + } else { + displayValue = value; + } + return super.getListCellRendererComponent(list, displayValue, index, isSelected, cellHasFocus); + } + + }); + } + + @Override + public Charset getSelectedItem() { + return (Charset) super.getSelectedItem(); + } + +} Index: db.dataview/src/org/netbeans/modules/db/dataview/util/EncodingHelper.java --- db.dataview/src/org/netbeans/modules/db/dataview/util/EncodingHelper.java +++ db.dataview/src/org/netbeans/modules/db/dataview/util/EncodingHelper.java @@ -0,0 +1,513 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package org.netbeans.modules.db.dataview.util; + +import java.io.*; +import java.util.Iterator; +import java.util.Map; +import java.util.TreeMap; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.text.*; +import org.openide.util.NbBundle; + +/** + * XML uses inband encoding detection - this class obtains it. + * + * Copied from xml.core to no introduce another dependency + * + * @author Petr Kuzel + * @version 1.0 + */ +public class EncodingHelper extends Object { + + // heuristic constant guessing max prolog length + private static final int EXPECTED_PROLOG_LENGTH = 1000; + private static final Logger logger = Logger.getLogger(EncodingHelper.class.getName()); + + /** + * Returns the Java encoding name for the specified IANA encoding name. + * + * @param ianaEncoding + * @return + */ + public static String getIANA2JavaMapping(String ianaEncoding) { + String java = (String) encodingIANA2JavaMap.get (ianaEncoding.toUpperCase ()); + return java == null ? ianaEncoding : java; + } + + /** + * Returns the IANA encoding name for the specified Java encoding name. + * + * @param ianaEncoding + * @return + */ + public static String getJava2IANAMapping(String javaEncoding) { + String iana = (String) encodingJava2IANAMap.get (javaEncoding); + return iana == null ? javaEncoding : iana; + } + + /** Detect input stream encoding. + * The stream stays intact. + * @return java encoding names ("UTF8", "ASCII", etc.) or null + * if the stream is not markable or enoding cannot be detected. + */ + public static String detectEncoding(InputStream in) throws IOException { + + if (! in.markSupported()) { + logger.log(Level.WARNING, "EncodingHelper got unmarkable stream: " + in.getClass()); // NOI18N + return null; + } + + try { + in.mark(EXPECTED_PROLOG_LENGTH); + + byte[] bytes = new byte[EXPECTED_PROLOG_LENGTH]; + for (int i = 0; inull for unrecognized + */ + public static String autoDetectEncoding(byte[] buf) throws IOException { + + + if (buf.length >= 4) { + switch (buf[0]) { + case 0: + // byte order mark of (1234-big endian) or (2143) USC-4 + // or '<' encoded as UCS-4 (1234, 2143, 3412) or UTF-16BE + if (buf[1] == (byte)0x3c && buf[2] == (byte)0x00 && buf[3] == (byte)0x3f) { + return "UnicodeBigUnmarked"; + } + // else it's probably UCS-4 + break; + + case 0x3c: + switch (buf[1]) { + // First character is '<'; could be XML without + // an XML directive such as "", "