Newer
Older
/* 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.6 2007-02-20 14:39:21 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 javax.swing.*;
/**
* This class is a JFileChooser that can read EMBL Entry objects.
*
* @author Kim Rutherford
**/
public class EntryFileDialog extends StickyFileChooser
{
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
/** 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 = (String)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 = (String)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 = (String)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 = (String)sequence_suffixes.elementAt(i);
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
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 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;
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;
// 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);
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
}
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();
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
}
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;
Box yBox = Box.createVerticalBox();
boolean useAccessory = false;
setDialogTitle("Save to ...");
setDialogType(JFileChooser.SAVE_DIALOG);
if( destination_type == DocumentEntryFactory.EMBL_FORMAT &&
(entry.getHeaderText() == null ||
!isHeaderEMBL(entry.getHeaderText())) )
{
yBox.add(emblHeader);
useAccessory = true;
}
if(((DocumentEntry)entry.getEMBLEntry()).getDocument()
instanceof RemoteFileDocument)
{
yBox.add(remoteSave);
useAccessory = true;
}
if(useAccessory)
setAccessory(yBox);
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(""))
{
String header = "ID "+idField.getText().trim();
if(entry.getFeatureCount() > 0)
header = header.concat("\nFH Key "+
"Location/Qualifiers\nFH\n");
entry.setHeaderText(header);
}
}
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
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?");
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
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;
}
// 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().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;
}