r341 - trunk/nuiton-matrix-gui/src/main/java/org/nuiton/math/matrix/gui
Author: echatellier Date: 2011-03-17 11:03:00 +0100 (Thu, 17 Mar 2011) New Revision: 341 Url: http://nuiton.org/repositories/revision/nuiton-matrix/341 Log: Auto add csv extion if not set. Fix javadoc. Fix e.printStackTrace Modified: trunk/nuiton-matrix-gui/src/main/java/org/nuiton/math/matrix/gui/MatrixPopupMenu.java Modified: trunk/nuiton-matrix-gui/src/main/java/org/nuiton/math/matrix/gui/MatrixPopupMenu.java =================================================================== --- trunk/nuiton-matrix-gui/src/main/java/org/nuiton/math/matrix/gui/MatrixPopupMenu.java 2011-03-12 14:21:41 UTC (rev 340) +++ trunk/nuiton-matrix-gui/src/main/java/org/nuiton/math/matrix/gui/MatrixPopupMenu.java 2011-03-17 10:03:00 UTC (rev 341) @@ -5,7 +5,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2004 - 2010 CodeLutin + * Copyright (C) 2004 - 2011 CodeLutin, Chatellier Eric * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -32,6 +32,7 @@ import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.ActionEvent; import java.io.File; import java.io.IOException; @@ -51,6 +52,8 @@ import javax.swing.JSeparator; import javax.swing.filechooser.FileFilter; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.nuiton.math.matrix.MatrixND; import org.nuiton.util.FileUtil; @@ -70,26 +73,54 @@ /** serialVersionUID. */ private static final long serialVersionUID = 3349189688987885915L; - private MatrixEditor matrixEditor; - private JFileChooser fileChooser; + /** Logger for this class. */ + private static final Log log = LogFactory.getLog(MatrixPopupMenu.class); - private JMenu sendToClipBoard; - private JMenu sendToFile; + /** Matrix editor where this menu is attached. */ + protected MatrixEditor matrixEditor; + + /** File chooser. */ + protected JFileChooser fileChooser; - private JCheckBoxMenuItem withSemantics; + /** Send to clip board menu item. */ + protected JMenu sendToClipBoard; - private Action sendToClipBoardAllCopyAction; - private Action sendToClipBoardAllPasteAction; - private Action sendToClipBoardSelectionCopyAction; - private Action sendToClipBoardCurrentPasteAction; + /** Send to file menu item. */ + protected JMenu sendToFile; - private Action sendToFileAllCopyAction; - private Action sendToFileAllPasteAction; - private Action sendToFileSelectionCopyAction; - private Action sendToFileCurrentPasteAction; + /** Export with semantics checkbox. */ + protected JCheckBoxMenuItem withSemantics; + /** Copy all matrix action. */ + protected Action sendToClipBoardAllCopyAction; + + /** Paste all matrix action. */ + protected Action sendToClipBoardAllPasteAction; + + /** Copy selection matrix action. */ + protected Action sendToClipBoardSelectionCopyAction; + + /** Paste selection matrix action. */ + protected Action sendToClipBoardCurrentPasteAction; + + /** Export all matrix to file action. */ + protected Action sendToFileAllCopyAction; + + /** Import all matrix from file. */ + protected Action sendToFileAllPasteAction; + + /** Export selection to file. */ + protected Action sendToFileSelectionCopyAction; + + /** Import selection from file. */ + protected Action sendToFileCurrentPasteAction; + + /** + * Init popop menu. + * + * @param matrixEditor matrix editor where menu is attached + */ public MatrixPopupMenu(MatrixEditor matrixEditor) { - super(); this.matrixEditor = matrixEditor; sendToClipBoard = getSendToClipBoard(); @@ -105,6 +136,8 @@ } /** + * Init send to clip board action. + * * @return retourne le menu d'action pour le bloc note */ public JMenu getSendToClipBoard() { @@ -139,6 +172,8 @@ } /** + * Init export to file action. + * * @return retourne le menu d'action pour les fichiers CSV */ public JMenu getSendToFile() { @@ -171,30 +206,41 @@ } /** + * Init export to file writer. + * * @return retourne un writer du fichier choisi dans le selecteur de fichier * @throws IOException */ - private Writer getFileChooserWriter() throws IOException { + protected Writer getFileChooserWriter() throws IOException { int returnVal = getFileChooser().showOpenDialog(matrixEditor); if (returnVal == JFileChooser.APPROVE_OPTION) { File selectedFile = getFileChooser().getSelectedFile(); + + // add csv extension if not already set in selected file + if (selectedFile.getName().endsWith(".csv")) { + selectedFile = new File(selectedFile.getName() + ".csv"); + } return FileUtil.getWriter(selectedFile); } return null; } /** + * Init export to clip board writer. + * * @return retourne un writer pour le bloc note */ - private Writer getClipBoardWriter() { + protected Writer getClipBoardWriter() { return new StringWriter(); } /** + * Init import from file reader. + * * @return retourne un reader du fichier choisi dans le selecteur de fichier * @throws IOException */ - private Reader getFileChooserReader() throws IOException { + protected Reader getFileChooserReader() throws IOException { int returnVal = getFileChooser().showOpenDialog(matrixEditor); if (returnVal == JFileChooser.APPROVE_OPTION) { File selectedFile = getFileChooser().getSelectedFile(); @@ -204,28 +250,25 @@ } /** + * Init import from clip board reader. + * * @return retourne le contenu du bloc note sous la forme d'un reader + * @throws IOException + * @throws UnsupportedFlavorException */ - private Reader getClipBoardReader() { + protected Reader getClipBoardReader() throws UnsupportedFlavorException, IOException { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable contents = clipboard.getContents(clipboard); if (contents != null) { - try { - String data = (String) contents - .getTransferData(DataFlavor.stringFlavor); - return new StringReader(data); - } catch (Exception e) { - JOptionPane.showMessageDialog(matrixEditor, - "Impossible de coller les données", "Warning", - JOptionPane.WARNING_MESSAGE); - e.printStackTrace(); - } + String data = (String) contents + .getTransferData(DataFlavor.stringFlavor); + return new StringReader(data); } return null; } /** - * Desactive le menu si la matrice ne supporte pas le mode CSV + * Desactive le menu si la matrice ne supporte pas le mode CSV. */ @Override protected void firePopupMenuWillBecomeVisible() { @@ -240,17 +283,21 @@ } /** - * @return Matrice en cours de saisie dans l'editeur + * Get matrix in matrix editor. + * + * @return matrice en cours de saisie dans l'editeur */ - private MatrixND getMatrix() { + protected MatrixND getMatrix() { return matrixEditor.getMatrix(); } /** + * Get selected matrix in editor. + * * @return la sous matrice en cours de saisie dans l'editeur c'est a dire la * partie selectionnee */ - private MatrixND getSelectedMatrix() { + protected MatrixND getSelectedMatrix() { int beginSelectedColumn = matrixEditor.getTable().getSelectedColumn(); int nbSelectedColumn = matrixEditor.getTable().getSelectedColumnCount(); @@ -278,9 +325,11 @@ } /** + * Get matrix first selection coordinates. + * * @return retourne les coordonnees de la première cellule selectionnee */ - private int[] getCoordinatesFirstCellSelectedMatrix() { + protected int[] getCoordinatesFirstCellSelectedMatrix() { int selectedColumn = matrixEditor.getTable().getSelectedColumn(); /* Prend en compte le décalage des lignes par rapport aux dimenssions */ @@ -296,9 +345,11 @@ } /** - * @return Selecteur de fichier CSV + * Get file chooser to csv file (import/export). + * + * @return selecteur de fichier CSV */ - private JFileChooser getFileChooser() { + protected JFileChooser getFileChooser() { if (fileChooser == null) { fileChooser = new JFileChooser(); FileFilter filter = new FileFilter() { @@ -331,10 +382,12 @@ } /** - * @return retourne l'action du bloc note permettant la copie entere de la + * Init emport to clip board action. + * + * @return retourne l'action du bloc note permettant la copie entiere de la * matrice */ - public Action getSendToClipBoardAllCopyAction() { + protected Action getSendToClipBoardAllCopyAction() { if (sendToClipBoardAllCopyAction == null) { sendToClipBoardAllCopyAction = new AbstractAction() { private static final long serialVersionUID=1L; @@ -348,13 +401,15 @@ } /** + * Init import from clip board action. + * * @return retourne l'action du bloc note permettant la recopie entere de la * matrice depuis le bloc note */ - public Action getSendToClipBoardAllPasteAction() { + protected Action getSendToClipBoardAllPasteAction() { if (sendToClipBoardAllPasteAction == null) { sendToClipBoardAllPasteAction = new AbstractAction() { - private static final long serialVersionUID=1L; + private static final long serialVersionUID = 1L; @Override public void actionPerformed(ActionEvent e) { sendToClipBoardAllPastePerformed(); @@ -365,13 +420,15 @@ } /** + * Init emport selection to clip board action. + * * @return retourne l'action du bloc note permettant la copie de la partie * selectionnee */ - public Action getSendToClipBoardSelectionCopyAction() { + protected Action getSendToClipBoardSelectionCopyAction() { if (sendToClipBoardSelectionCopyAction == null) { sendToClipBoardSelectionCopyAction = new AbstractAction() { - private static final long serialVersionUID=1L; + private static final long serialVersionUID = 1L; @Override public void actionPerformed(ActionEvent e) { sendToClipBoardSelectionCopyPerformed(); @@ -382,6 +439,8 @@ } /** + * Init import selection from clip board action. + * * @return retourne l'action du bloc note permettant la recopie de la partie * selectionnee de la matrice depuis le bloc note */ @@ -398,6 +457,9 @@ return sendToClipBoardCurrentPasteAction; } + /** + * Export matrix to clip board. + */ protected void sendToClipBoardAllCopyPerformed() { try { Writer writer = getClipBoardWriter(); @@ -408,14 +470,19 @@ clipboard.setContents(contents, contents); writer.close(); matrixEditor.repaint(); - } catch (Exception e) { + } catch (Exception ex) { JOptionPane.showMessageDialog(matrixEditor, _("nuitonmatrix.error.clipboard.write"), _("nuitonmatrix.error"), JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); + if (log.isErrorEnabled()) { + log.error(_("nuitonmatrix.error.clipboard.write"), ex); + } } } + /** + * Import matrix from clip board. + */ protected void sendToClipBoardAllPastePerformed() { try { Reader reader = getClipBoardReader(); @@ -423,14 +490,19 @@ reader.close(); matrixEditor.fireEvent(); matrixEditor.repaint(); - } catch (Exception e) { + } catch (Exception ex) { JOptionPane.showMessageDialog(matrixEditor, _("nuitonmatrix.error.clipboard.read"), _("nuitonmatrix.error"), JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); + if (log.isErrorEnabled()) { + log.error(_("nuitonmatrix.error.clipboard.read"), ex); + } } } + /** + * Export selection to clip board. + */ protected void sendToClipBoardSelectionCopyPerformed() { try { Writer writer = getClipBoardWriter(); @@ -441,14 +513,19 @@ clipboard.setContents(contents, contents); writer.close(); matrixEditor.repaint(); - } catch (Exception e) { + } catch (Exception ex) { JOptionPane.showMessageDialog(matrixEditor, _("nuitonmatrix.error.clipboard.write"), _("nuitonmatrix.error"), JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); + if (log.isErrorEnabled()) { + log.error(_("nuitonmatrix.error.clipboard.write"), ex); + } } } + /** + * Import from clip board. + */ protected void sendToClipBoardCurrentPastePerformed() { try { Reader reader = getClipBoardReader(); @@ -457,19 +534,23 @@ reader.close(); matrixEditor.fireEvent(); matrixEditor.repaint(); - } catch (Exception e) { + } catch (Exception ex) { JOptionPane.showMessageDialog(matrixEditor, _("nuitonmatrix.error.clipboard.read"), _("nuitonmatrix.error"), JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); + if (log.isErrorEnabled()) { + log.error(_("nuitonmatrix.error.clipboard.read"), ex); + } } } /** - * @return retourne l'action du fichier permettant la copie entere de la + * Init export to file action. + * + * @return retourne l'action du fichier permettant la copie entiere de la * matrice */ - public Action getSendToFileAllCopyAction() { + protected Action getSendToFileAllCopyAction() { if (sendToFileAllCopyAction == null) { sendToFileAllCopyAction = new AbstractAction() { private static final long serialVersionUID=1L; @@ -483,10 +564,12 @@ } /** + * Init import from file action. + * * @return retourne l'action du fichier permettant la recopie entere de la * matrice depuis le fichier */ - public Action getSendToFileAllPasteAction() { + protected Action getSendToFileAllPasteAction() { if (sendToFileAllPasteAction == null) { sendToFileAllPasteAction = new AbstractAction() { private static final long serialVersionUID=1L; @@ -500,10 +583,12 @@ } /** + * Init export selection to file action. + * * @return retourne l'action du fichier permettant la copie de la partie * selectionnee */ - public Action getSendToFileSelectionCopyAction() { + protected Action getSendToFileSelectionCopyAction() { if (sendToFileSelectionCopyAction == null) { sendToFileSelectionCopyAction = new AbstractAction() { private static final long serialVersionUID=1L; @@ -517,10 +602,12 @@ } /** + * Init import selection from file action. + * * @return retourne l'action du fichier permettant la recopie de la partie * selectionnee de la matrice depuis le fichier */ - public Action getSendToFileCurrentPasteAction() { + protected Action getSendToFileCurrentPasteAction() { if (sendToFileCurrentPasteAction == null) { sendToFileCurrentPasteAction = new AbstractAction() { private static final long serialVersionUID=1L; @@ -533,6 +620,9 @@ return sendToFileCurrentPasteAction; } + /** + * Export to file. + */ protected void sendToFileAllCopyPerformed() { try { Writer writer = getFileChooserWriter(); @@ -541,14 +631,19 @@ writer.close(); matrixEditor.repaint(); } - } catch (Exception e) { + } catch (Exception ex) { JOptionPane.showMessageDialog(matrixEditor, _("nuitonmatrix.error.file.write"), _("nuitonmatrix.error"), JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); + if (log.isErrorEnabled()) { + log.error(_("nuitonmatrix.error.file.write"), ex); + } } } + /** + * Import from file. + */ protected void sendToFileAllPastePerformed() { try { Reader reader = getFileChooserReader(); @@ -558,14 +653,19 @@ matrixEditor.fireEvent(); matrixEditor.repaint(); } - } catch (Exception e) { + } catch (Exception ex) { JOptionPane.showMessageDialog(matrixEditor, _("nuitonmatrix.error.file.read"), _("nuitonmatrix.error"), JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); + if (log.isErrorEnabled()) { + log.error(_("nuitonmatrix.error.file.read"), ex); + } } } + /** + * Export selection to file. + */ protected void sendToFileSelectionCopyPerformed() { try { Writer writer = getFileChooserWriter(); @@ -574,14 +674,19 @@ writer.close(); matrixEditor.repaint(); } - } catch (Exception e) { + } catch (Exception ex) { JOptionPane.showMessageDialog(matrixEditor, _("nuitonmatrix.error.file.write"), _("nuitonmatrix.error"), JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); + if (log.isErrorEnabled()) { + log.error(_("nuitonmatrix.error.file.write"), ex); + } } } + /** + * Import selection from file. + */ protected void sendToFileCurrentPastePerformed() { try { Reader reader = getFileChooserReader(); @@ -592,11 +697,13 @@ matrixEditor.fireEvent(); matrixEditor.repaint(); } - } catch (Exception e) { + } catch (Exception ex) { JOptionPane.showMessageDialog(matrixEditor, _("nuitonmatrix.error.file.read"), _("nuitonmatrix.error"), JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); + if (log.isErrorEnabled()) { + log.error(_("nuitonmatrix.error.file.read"), ex); + } } } }
participants (1)
-
echatellier@users.nuiton.org