/* EntryFileDialog.java * * created: Mon Dec 7 1998 * * This file is part of Artemis * * Copyright(C) 1998-2003 Genome Research Limited * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or(at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/EntryFileDialog.java,v 1.15 2009-04-01 12:23:20 tjc Exp $ */ package uk.ac.sanger.artemis.components; import uk.ac.sanger.artemis.Options; import uk.ac.sanger.artemis.util.*; import uk.ac.sanger.artemis.io.DatabaseDocumentEntry; import uk.ac.sanger.artemis.io.Entry; import uk.ac.sanger.artemis.io.DocumentEntryFactory; import uk.ac.sanger.artemis.io.GFFDocumentEntry; import uk.ac.sanger.artemis.io.ReadFormatException; import uk.ac.sanger.artemis.io.EntryInformation; import uk.ac.sanger.artemis.io.EntryInformationException; import uk.ac.sanger.artemis.io.DocumentEntry; import uk.ac.sanger.artemis.io.ReadAndWriteEntry; import java.io.*; import javax.swing.*; /** * This class is a JFileChooser that can read EMBL Entry objects. * * @author Kim Rutherford * @version $Id: EntryFileDialog.java,v 1.15 2009-04-01 12:23:20 tjc Exp $ **/ public class EntryFileDialog extends StickyFileChooser { /** */ private static final long serialVersionUID = 1L; /** JFrame reference that was passed to the constructor. */ private JFrame owner = null; /** * Create a new EntryFileDialog component. * @param owner The component where this dialog was created. * @param sequence_only If true default to showing only file that have * suffixes that suggest that the files contain sequence(eg. .embl, * .seq, .dna). Of false show all files that can be read. **/ public EntryFileDialog(final JFrame owner, final boolean show_sequence_only) { super(); this.owner = owner; setFileSelectionMode(JFileChooser.FILES_ONLY); setMultiSelectionEnabled(false); final StringVector sequence_suffixes = Options.getOptions().getOptionValues("sequence_file_suffixes"); final StringVector feature_suffixes = Options.getOptions().getOptionValues("feature_file_suffixes"); final javax.swing.filechooser.FileFilter artemis_filter = new javax.swing.filechooser.FileFilter() { public boolean accept(final File file) { if(file.isDirectory()) return true; for(int i = 0; i 1 ? " at line: " + e.getLineNumber() : ""); System.out.println(message); new MessageDialog(frame, message); } catch(FileNotFoundException e) { final String message = "failed to open " + entry_document.getName() + ": file not found"; new MessageDialog(frame, message); } catch(IOException e) { final String message = "failed to open " + entry_document.getName() + ": " + e.getMessage(); new MessageDialog(frame, message); } finally { if(progress_dialog != null) progress_dialog.dispose(); } return null; } /** * Save the given entry, prompting for a file name if necessary. * @param include_diana_extensions If true then the any diana additions to * the embl file format will be included in the output, otherwise they * will be removed. Also possible problems that would cause an entry to * bounce from the EMBL submission process will be flagged if this is * true. * @param ask_for_name If true then always prompt for a new filename, * otherwise prompt only when the entry name is not set. * @param keep_new_name If ask_for_name is true a file will be written with * the new name the user selects - if keep_new_name is true as well, then * the entry will have it's name set to the new name, otherwise it will * be used for this save and then discarded. * @param destination_type Should be one of EMBL_FORMAT, GENBANK_FORMAT or * ANY_FORMAT. If ANY_FORMAT then the Entry will be saved in the * same format it was created, otherwise it will be saved in the given * format. **/ void saveEntry(final uk.ac.sanger.artemis.Entry entry, final boolean include_diana_extensions, final boolean ask_for_name, final boolean keep_new_name, final int destination_type) { JCheckBox remoteSave = new JCheckBox("Ssh/Remote Save", true); File file = null; try { if(ask_for_name || entry.getName() == null) { Box yBox = Box.createVerticalBox(); boolean useAccessory = false; JCheckBox emblHeader = new JCheckBox("Add EMBL Header", false); JCheckBox removeProductForPseudo = new JCheckBox( "Remove products from pseudogenes", false); setDialogTitle("Save to ..."); setDialogType(JFileChooser.SAVE_DIALOG); if( destination_type == DocumentEntryFactory.EMBL_FORMAT ) { if((entry.getHeaderText() == null || !isHeaderEMBL(entry.getHeaderText()))) yBox.add(emblHeader); if(!include_diana_extensions) yBox.add(removeProductForPseudo); useAccessory = true; } final JCheckBox flattenGeneModel = new JCheckBox("Flatten Gene Model", false); final JCheckBox ignoreObsoleteFeatures = new JCheckBox( "Ignore obsolete features", true); if(((DocumentEntry)entry.getEMBLEntry()).getDocument() instanceof RemoteFileDocument) { yBox.add(remoteSave); useAccessory = true; } else if(entry.getEMBLEntry() instanceof DatabaseDocumentEntry || entry.getEMBLEntry() instanceof GFFDocumentEntry) { yBox.add(flattenGeneModel); if(entry.getEMBLEntry() instanceof DatabaseDocumentEntry) yBox.add(ignoreObsoleteFeatures); useAccessory = true; } if(useAccessory) setAccessory(yBox); switch(destination_type) // provide a suffix { case DocumentEntryFactory.EMBL_FORMAT: super.setSelectedFile(new File(".embl")); flattenGeneModel.setSelected(true); break; case DocumentEntryFactory.GENBANK_FORMAT: super.setSelectedFile(new File(".gbk")); flattenGeneModel.setSelected(true); break; case DocumentEntryFactory.GFF_FORMAT: super.setSelectedFile(new File(".gff")); break; default: break; } int status = showSaveDialog(owner); if(status != JFileChooser.APPROVE_OPTION || getSelectedFile() == null) return; if(emblHeader.isSelected()) { Box bdown = Box.createVerticalBox(); JTextField idField = new JTextField(""); bdown.add(idField); int n = JOptionPane.showConfirmDialog(null, bdown, "Enter the entry ID", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if(n != JOptionPane.CANCEL_OPTION && !idField.getText().trim().equals("")) { int length = entry.getBases().getLength(); String header = "ID "+idField.getText().trim()+"; SV ; ; ; ; ; "+length+" BP."; //String header = "ID "+idField.getText().trim(); if(entry.getFeatureCount() > 0) header = header.concat("\nFH Key "+ "Location/Qualifiers\nFH\n"); entry.setHeaderText(header); } } file = new File(getCurrentDirectory(), getSelectedFile().getName()); if(file.exists()) { final YesNoDialog yes_no_dialog = new YesNoDialog(owner, "this file exists: " + file.getName() + " overwrite it?"); if(!yes_no_dialog.getResult()) return; } final MessageDialog message = new MessageDialog(owner, "saving to " + file.getName() + " ...", false); try { DocumentEntryFactory.REMOVE_PRODUCT_FROM_PSEUDOGENE = removeProductForPseudo.isSelected(); if(entry.getEMBLEntry() instanceof DatabaseDocumentEntry || entry.getEMBLEntry() instanceof GFFDocumentEntry) ReadAndWriteEntry.writeDatabaseEntryToFile(entry, file, flattenGeneModel.isSelected(), ignoreObsoleteFeatures.isSelected(), false, include_diana_extensions, destination_type, owner); else if(include_diana_extensions) entry.save(file, destination_type, false); else entry.saveStandardOnly(file, destination_type, true); } catch(EntryInformationException e) { final YesNoDialog yes_no_dialog = new YesNoDialog(owner, "destination format can't handle all " + "keys/qualifiers - continue?"); if(yes_no_dialog.getResult()) { try { if(entry.getEMBLEntry() instanceof DatabaseDocumentEntry) ReadAndWriteEntry.writeDatabaseEntryToFile(entry, file, flattenGeneModel.isSelected(), ignoreObsoleteFeatures.isSelected(), true, include_diana_extensions, destination_type, null); else if(include_diana_extensions) entry.save(file, destination_type, true); else entry.saveStandardOnly(file, destination_type, true); } catch(EntryInformationException e2) { throw new Error("internal error - unexpected exception: "+ e); } } else return; } finally { DocumentEntryFactory.REMOVE_PRODUCT_FROM_PSEUDOGENE = false; if(message != null) message.dispose(); } if(keep_new_name) entry.setName(file.getName()); } else { final MessageDialog message = new MessageDialog(owner, "saving to " + entry.getName() + " ...", false); try { if(include_diana_extensions) entry.save(destination_type); else entry.saveStandardOnly(destination_type); } finally { message.dispose(); } } } catch(ReadOnlyException e) { new MessageDialog(owner, "this entry is read only"); return; } catch(IOException e) { new MessageDialog(owner, "error while saving: " + e); return; } catch(EntryInformationException e) { new MessageDialog(owner, "error while saving: " + e); return; } // save it back to ssh server if(((DocumentEntry)entry.getEMBLEntry()).getDocument() instanceof RemoteFileDocument && remoteSave.isSelected()) { RemoteFileDocument node = (RemoteFileDocument)(((DocumentEntry)entry.getEMBLEntry()).getDocument()); if(file == null) file = new File( ((DocumentEntry)entry.getEMBLEntry()).getDocument().getLocation().toString() ); node.saveEntry(file); } } /** * * Test for that the header of an EMBL entry begins * with an ID line. * @param header embl header * */ private boolean isHeaderEMBL(String header) { StringReader reader = new StringReader(header); BufferedReader buff_reader = new BufferedReader(reader); try { if(!buff_reader.readLine().startsWith("ID")) return false; } catch(IOException ioe){} return true; } }