Skip to content
Snippets Groups Projects
EntryFileDialog.java 15.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    /* 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.1 2004-06-09 09:46:28 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.Entry;
    import uk.ac.sanger.artemis.io.DocumentEntryFactory;
    import uk.ac.sanger.artemis.io.ReadFormatException;
    import uk.ac.sanger.artemis.io.EntryInformation;
    import uk.ac.sanger.artemis.io.EntryInformationException;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    import javax.swing.*;
    
    /**
     *  This class is a JFileChooser that can read EMBL Entry objects.
     *
     *  @author Kim Rutherford
     *  @version $Id: EntryFileDialog.java,v 1.1 2004-06-09 09:46:28 tjc Exp $
     **/
    
    public class EntryFileDialog extends StickyFileChooser 
    {
    
      /** 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<sequence_suffixes.size(); ++i) 
              {
                final String this_suffix = sequence_suffixes.elementAt(i);
    
                if(file.getName().endsWith("." + this_suffix) ||
                    file.getName().endsWith("." + this_suffix + ".gz")) 
                  return true;
              }
    
              for(int i = 0; i<feature_suffixes.size(); ++i) 
              {
                final String this_suffix = feature_suffixes.elementAt(i);
    
                if(file.getName().endsWith("." + this_suffix) ||
                   file.getName().endsWith("." + this_suffix + ".gz")) 
                  return true;
              }
              return false;
            }
    
            public String getDescription() 
            {
              return "Artemis files";
            }
          };
    
        final javax.swing.filechooser.FileFilter feature_filter =
          new javax.swing.filechooser.FileFilter() 
        {
            public boolean accept(final File file) 
            {
              if(file.isDirectory()) 
                return true;
    
              for(int i = 0 ; i < feature_suffixes.size() ; ++i) 
              {
                final String this_suffix = feature_suffixes.elementAt(i);
    
                if(file.getName().endsWith("." + this_suffix) ||
                   file.getName().endsWith("." + this_suffix + ".gz")) 
                  return true;
              }
    
              return false;
            }
    
            public String getDescription() 
            {
              return "Feature files";
            }
          };
    
        final javax.swing.filechooser.FileFilter sequence_filter =
          new javax.swing.filechooser.FileFilter() 
          {
            public boolean accept(final File file) 
            {
              if(file.isDirectory()) 
                return true;
    
              for(int i = 0 ; i<sequence_suffixes.size() ; ++i) 
              {
                final String this_suffix = sequence_suffixes.elementAt(i);
    
                if(file.getName().endsWith("." + this_suffix) ||
                   file.getName().endsWith("." + this_suffix + ".gz")) 
                  return true;
              }
    
              return false;
            }
    
            public String getDescription() 
            {
              return "Sequence files";
            }
          };
    
        addChoosableFileFilter(artemis_filter);
        addChoosableFileFilter(feature_filter);
        addChoosableFileFilter(sequence_filter); 
    
        if(show_sequence_only) 
          setFileFilter(sequence_filter);
        else 
          setFileFilter(artemis_filter);
      }
    
      /**
       *  Return an uk.ac.sanger.artemis.io.Entry object representing the file
       *  the user has selected with the dialog or null if the read failed for any
       *  reason.
       *  @param entry_information The EntryInformation to use when reading.  This
       *    supplies the list of valid keys and qualifiers.  If a key or qualifier
       *    is read that is incompatible with this EntryInformation object the
       *    EntryInformation will be changed to cope.
       *  @param listener The object to which InputStreamProgressEvents will be
       *    send while reading.
       *  @exception EntryInformationException Thrown if an Entry using the given
       *    EntryInformation object cannot contain the Key, Qualifier or
       *    Key/Qualifier combination of one of the features in the Document.
       *  @param show_progress If true a InputStreamProgressDialog will be shown
       *    while loading.
       **/
      public Entry getEntry(final EntryInformation entry_information,
                            final InputStreamProgressListener listener,
                            final ProgressThread progress_thread,
                            final boolean show_progress) 
      {
        setDialogTitle("Select a file ...");
        setDialogType(JFileChooser.OPEN_DIALOG);
        SecurityManager sm = System.getSecurityManager();
        System.setSecurityManager(null);
    
        final int status = showOpenDialog(owner);
    
        System.setSecurityManager(sm);
    
        if(status != JFileChooser.APPROVE_OPTION ||
           getSelectedFile() == null) 
          return null;
    
        if(progress_thread != null)
          progress_thread.start();
    
        final File file = new File(getCurrentDirectory(),
                                   getSelectedFile().getName());
    
        return getEntryFromFile(owner, new FileDocument(file),
                                entry_information, show_progress); 
      }
    
      /**
       *  This exists only to get around a bug in the 1.1/1.2 code generator,
       *  which generates unverifiable code.
       *  @param frame Used when creating MessageDialog components.
       *  @param file_document The Document to read the Entry from(should be made
       *    from entry_file).
       *  @param entry_information The EntryInformation to use when reading.  This
       *    supplies the list of valid keys and qualifiers.  If a key or qualifier
       *    is read that is incompatible with this EntryInformation object the
       *    EntryInformation will be changed to cope.
       **/
      private static Entry getEntryFromFileHelper(final JFrame frame,
                                final Document file_document,
                                final EntryInformation entry_information)
          throws ReadFormatException, IOException 
      {
    
        final LogReadListener read_event_logger =
          new LogReadListener(file_document.getName());
    
        final Entry new_entry;
    
        try 
        {
          new_entry =
            DocumentEntryFactory.makeDocumentEntry(entry_information,
                                     file_document,read_event_logger);
        }
        catch(EntryInformationException e) 
        {
          throw new Error("internal error - unexpected exception: " + e);
        }
    
        if(read_event_logger.seenMessage())
        {
          final YesNoDialog yes_no_dialog = new YesNoDialog(frame,
                             "there were warnings while reading - view now?");
    
          if(yes_no_dialog.getResult()) 
            Splash.showLog();
        }
    
        return new_entry;
      }
    
      /**
       *  Read and return an Entry from the given File.
       *  @param frame Used when creating MessageDialog components.
       *  @param entry_file The file to read the Entry from.
       *  @param entry_information The EntryInformation to use when reading.  This
       *    supplies the list of valid keys and qualifiers.  If a key or qualifier
       *    is read that is incompatible with this EntryInformation object the
       *    EntryInformation will be changed to cope.
       *  @param listener The object to which InputStreamProgressEvents will be
       *    send while reading.
       *  @param show_progress If true a InputStreamProgressDialog will be shown
       *    while loading.
       **/
      public static Entry getEntryFromFile(final JFrame frame,
                         final Document entry_document,
                         final EntryInformation entry_information,
                         final boolean show_progress) 
      {
        InputStreamProgressDialog progress_dialog = null;
    
        if(show_progress) {
    // XXX FIXME
    
    // This doesn't work because getEntryFromFile() is called from the Swing
    // thread so the Dialog never gets updated
    
    //     progress_dialog =
    //       new InputStreamProgressDialog(frame, "Reading ...",
    //                                      "Reading from " +
    //                                      entry_document.getName(), false);
    //     final InputStreamProgressListener listener =
    //       progress_dialog.getInputStreamProgressListener();
    
    //     entry_document.addInputStreamProgressListener(listener);
        }
    
        try 
        {
          return getEntryFromFileHelper(frame, entry_document,
                                        entry_information);
        }
        catch(ReadFormatException e) 
        {
          final String message =
            "failed to open " + entry_document.getName() + ": " +
            e.getMessage() +(e.getLineNumber() > 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) 
      {
        try 
        {
          if(ask_for_name || entry.getName() == null) 
          {
            JCheckBox emblHeader = new JCheckBox("Add EMBL Header",
                                                 false);
            setDialogTitle("Save to ...");
            setDialogType(JFileChooser.SAVE_DIALOG);
    
    //      if(destination_type == DocumentEntryFactory.EMBL_FORMAT)
    //        setAccessory(emblHeader);
            int status = showSaveDialog(owner);
    
            if(status != JFileChooser.APPROVE_OPTION ||
               getSelectedFile() == null) 
              return;
    
            File 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 
            {
              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(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 
            {
              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;
        }
      }
    
    }