import java.awt.print.PageFormat;
import java.awt.print.PrinterJob;
import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JEditorPane;
+import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
* @version $Revision$
*/
public class AlignFrame extends GAlignFrame implements DropTargetListener,
- IProgressIndicator, AlignViewControllerGuiI, ColourChangeListener
+ IProgressIndicator, AlignViewControllerGuiI, ColourChangeListener,
+ PropertyChangeListener
{
public static final int DEFAULT_WIDTH = 700;
init();
}
+ @Override
+ public void propertyChange(PropertyChangeEvent evt)
+ {
+ Desktop.getDesktop().propertyChange(evt);
+ }
+
/**
* initalise the alignframe from the underlying viewport data and the
* configurations
int value = chooser.showSaveDialog(this);
+
if (value == JalviewFileChooser.APPROVE_OPTION)
{
currentFileFormat = chooser.getSelectedFormat();
chooser.setToolTipText(
MessageManager.getString("label.load_jalview_annotations"));
- int value = chooser.showOpenDialog(null);
+ Desktop.getDesktop().dialogData = new Object[] { "SelectedFile",
+ new Runnable()
+ {
+
+ @Override
+ public void run()
+ {
+ Object[] data = Desktop.getDesktop().dialogData;
+ int value = ((Integer) data[0]).intValue();
+
+ if (value == JFileChooser.APPROVE_OPTION)
+ {
+ JalviewFileChooser chooser = (JalviewFileChooser) data[2];
+ String choice = chooser.getSelectedFile().getPath();
+ jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);
+ loadJalviewDataFile(choice, null, null, null);
+ }
+ }
+
+ }, chooser };
+
+ chooser.showOpenDialog(null);
+
- if (value == JalviewFileChooser.APPROVE_OPTION)
- {
- String choice = chooser.getSelectedFile().getPath();
- jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);
- loadJalviewDataFile(choice, null, null, null);
- }
}
}
}
+
}
class PrintThread extends Thread
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
+import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
public static MyDesktopPane desktop;
+ public static MyDesktopPane getDesktop()
+ {
+ // BH 2018 could use currentThread() here as a reference to a
+ // Hashtable<Thread, MyDesktopPane> in JavaScript
+ return desktop;
+ }
+
static int openFrameCount = 0;
static final int xOffset = 30;
public void inputLocalFileMenuItem_actionPerformed(AlignViewport viewport)
{
String fileFormat = Cache.getProperty("DEFAULT_FILE_FORMAT");
- JalviewFileChooser chooser = JalviewFileChooser
+ final JalviewFileChooser chooser = JalviewFileChooser
.forRead(Cache.getProperty("LAST_DIRECTORY"), fileFormat);
chooser.setFileView(new JalviewFileView());
MessageManager.getString("label.open_local_file"));
chooser.setToolTipText(MessageManager.getString("action.open"));
- int value = chooser.showOpenDialog(this);
-
- if (value == JalviewFileChooser.APPROVE_OPTION)
+ chooser.setCallback(new Runnable()
{
- String choice = chooser.getSelectedFile().getPath();
- Cache.setProperty("LAST_DIRECTORY",
- chooser.getSelectedFile().getParent());
-
- FileFormatI format = chooser.getSelectedFormat();
- /*
- * Call IdentifyFile to verify the file contains what its extension implies.
- * Skip this step for dynamically added file formats, because
- * IdentifyFile does not know how to recognise them.
- */
- if (FileFormats.getInstance().isIdentifiable(format))
+ @Override
+ public void run()
{
- try
- {
- format = new IdentifyFile().identify(choice, DataSourceType.FILE);
- } catch (FileFormatException e)
+ File selectedFile = chooser.getSelectedFile();
+ Cache.setProperty("LAST_DIRECTORY", selectedFile.getParent());
+
+ FileFormatI format = chooser.getSelectedFormat();
+
+ /*
+ * Call IdentifyFile to verify the file contains what its extension implies.
+ * Skip this step for dynamically added file formats, because
+ * IdentifyFile does not know how to recognise them.
+ */
+ if (FileFormats.getInstance().isIdentifiable(format))
{
- // format = null; //??
+ try
+ {
+ format = new IdentifyFile().identify(selectedFile,
+ DataSourceType.FILE);
+ } catch (FileFormatException e)
+ {
+ // format = null; //??
+ }
}
- }
- if (viewport != null)
- {
- new FileLoader().LoadFile(viewport, choice, DataSourceType.FILE,
- format);
- }
- else
- {
- new FileLoader().LoadFile(choice, DataSourceType.FILE, format);
+ new FileLoader().LoadFile(viewport, selectedFile,
+ DataSourceType.FILE, format);
}
+ });
+
+ int value = chooser.showOpenDialog(this);
+ if (value == JFileChooser.APPROVE_OPTION)
+ {
+ chooser.getCallback().run();
}
+
}
/**
switch (name)
{
case "SelectedFile":
- File file = (File) val;
- byte[] array = (val == null ? null
- : /** @j2sNative file._bytes || */
- null);
- onDialogReturn("fileName is '" + file.getName() + "'\n\n"
- + new String(array));
+ // in JavaScript, this File object will have a _bytes property,
+ // because the file data has already been loaded
+ onDialogReturn(new Object[] { (File) val });
return;
}
break;
}
// JSCOmponent.DialogCaller interface
- private void onDialogReturn(Object value)
+ void onDialogReturn(Object value)
{
- System.out.println("not implemented");
+ switch ((String) dialogData[0])
+ {
+ case "SelectedFile":
+ case "runnable":
+ dialogData[0] = value;
+ ((Runnable) dialogData[1]).run();
+ break;
+ case "label.select_feature_colour":
+ ((FeatureRenderer) dialogData[1]).processColorDialog((Color) value);
+ break;
+ }
}
// JSCOmponent.DialogCaller interface
switch ((String) dialogData[0])
{
+ case "runnable":
+ dialogData[0] = Integer.valueOf(value);
+ ((Runnable) dialogData[1]).run();
+ break;
case "label.input_alignment_from_url":
// reconstruct the parameter data
int reply = value;
private AlignExportSettingI exportSettings;
+ private File selectedFile;
+
public static String INVALID_CHARACTERS = "Contains invalid characters";
/**
public AlignmentI readFile(String file, DataSourceType sourceType,
FileFormatI fileFormat) throws IOException
{
+ return readFile(null, file, sourceType, fileFormat);
+ }
+
+ public AlignmentI readFile(File selectedFile, String file, DataSourceType sourceType,
+ FileFormatI fileFormat) throws IOException
+ {
+
+ this.selectedFile = selectedFile;
+ if (selectedFile != null)
+ this.inFile = selectedFile.getPath();
this.inFile = file;
try
{
localSecondaryStruct, serviceSecondaryStruct);
if (isParseWithJMOL)
{
+ // needs a File option
alignFile = new JmolParser(inFile, sourceType);
}
else
((StructureFile) alignFile).setDbRefType(
FileFormat.PDB.equals(fileFormat) ? Type.PDB : Type.MMCIF);
}
- else
+ else if (selectedFile != null) {
+ alignFile = fileFormat.getReader(new FileParse(selectedFile, sourceType));
+ } else
{
// alignFile = fileFormat.getAlignmentFile(inFile, sourceType);
alignFile = fileFormat.getReader(new FileParse(inFile, sourceType));
boolean raiseGUI = true;
+ private File selectedFile;
+
/**
* default constructor always raised errors in GUI dialog boxes
*/
LoadFile(file, protocol, format);
}
+ public void LoadFile(AlignViewport viewport, File selectedFile,
+ DataSourceType protocol, FileFormatI format)
+ {
+ this.viewport = viewport;
+ this.selectedFile = selectedFile;
+ LoadFile(selectedFile.getPath(), protocol, format);
+ }
+
public void LoadFile(String file, DataSourceType protocol,
FileFormatI format)
{
format = new IdentifyFile().identify(source, false);
// identify stream and rewind rather than close
}
+ else if (selectedFile != null) {
+ format = new IdentifyFile().identify(selectedFile, protocol);
+ }
else
{
format = new IdentifyFile().identify(file, protocol);
}
else
{
- al = fa.readFile(file, protocol, format);
+ if (selectedFile == null) {
+ al = fa.readFile(file, protocol, format);
+
+ } else {
+ al = fa.readFile(selectedFile, protocol, format);
+ }
source = fa.getAlignFile(); // keep reference for later if
+
// necessary.
}
}
public File inFile = null;
+ public byte[] bytes; // from JavaScript
+
/**
* a viewport associated with the current file operation. May be null. May
* move to different object.
public FileParse(String fileStr, DataSourceType sourceType)
throws MalformedURLException, IOException
{
+
+ this(null, fileStr, sourceType, false);
+ }
+
+ public FileParse(File file, DataSourceType sourceType)
+ throws MalformedURLException, IOException
+ {
+
+ this(file, file.getPath(), sourceType, true);
+ }
+
+ private FileParse(File file, String fileStr, DataSourceType sourceType,
+ boolean isFileObject) throws MalformedURLException, IOException
+ {
+
+ /**
+ * @j2sNative
+ *
+ * this.bytes = file && file._bytes;
+ *
+ */
this.dataSourceType = sourceType;
error = false;
if (sourceType == DataSourceType.FILE)
{
- if (checkFileSource(fileStr))
+
+ if (bytes != null)
+ {
+ // this will be from JavaScript
+ inFile = file;
+ dataIn = new BufferedReader(new StringReader(new String(bytes)));
+ dataName = fileStr;
+ }
+ else if (checkFileSource(fileStr))
{
String suffixLess = extractSuffix(fileStr);
if (suffixLess != null)
{
// pass up the reason why we have no source to read from
throw new IOException(MessageManager.formatMessage(
- "exception.failed_to_read_data_from_source", new String[]
+ "exception.failed_to_read_data_from_source",
+ new String[]
{ errormessage }));
}
error = false;
import jalview.datamodel.SequenceI;
import jalview.util.Comparison;
+import java.io.File;
import java.io.IOException;
/**
return al;
}
+ public AlignmentI readFile(File file, DataSourceType sourceType,
+ FileFormatI fileFormat) throws IOException
+ {
+ AlignmentI al = super.readFile(file, null, sourceType, fileFormat);
+ return al;
+ }
+
@Override
public AlignmentI readFromFile(FileParse source, FileFormatI format)
throws IOException
*/
package jalview.io;
+import java.io.File;
import java.io.IOException;
/**
*/
public class IdentifyFile
{
+ public FileFormatI identify(File file, DataSourceType sourceType)
+ throws FileFormatException
+ {
+ String emessage = "UNIDENTIFIED FILE PARSING ERROR";
+ FileParse parser = null;
+ try
+ {
+ parser = new FileParse(file, sourceType);
+ if (parser.isValid())
+ {
+ return identify(parser);
+ }
+ } catch (Exception e)
+ {
+ System.err.println("Error whilst identifying " + file);
+ e.printStackTrace(System.err);
+ emessage = e.getMessage();
+ }
+ if (parser != null)
+ {
+ throw new FileFormatException(parser.errormessage);
+ }
+ throw new FileFormatException(emessage);
+ }
+
/**
* Identify a datasource's file content.
*
System.err.println("Usage: <Filename> [<Filename> ...]");
}
}
+
}
import java.awt.HeadlessException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
*
*/
public class JalviewFileChooser extends JFileChooser
+ implements PropertyChangeListener
{
/**
* Factory method to return a file chooser that offers readable alignment file
public static JalviewFileChooser forRead(String directory,
String selected)
{
- List<String> extensions = new ArrayList<String>();
- List<String> descs = new ArrayList<String>();
+ List<String> extensions = new ArrayList<>();
+ List<String> descs = new ArrayList<>();
for (FileFormatI format : FileFormats.getInstance().getFormats())
{
if (format.isReadable())
{
// TODO in Java 8, forRead and forWrite can be a single method
// with a lambda expression parameter for isReadable/isWritable
- List<String> extensions = new ArrayList<String>();
- List<String> descs = new ArrayList<String>();
+ List<String> extensions = new ArrayList<>();
+ List<String> descs = new ArrayList<>();
for (FileFormatI format : FileFormats.getInstance().getFormats())
{
if (format.isWritable())
super(safePath(dir));
if (extensions.length == descs.length)
{
- List<String[]> formats = new ArrayList<String[]>();
+ List<String[]> formats = new ArrayList<>();
for (int i = 0; i < extensions.length; i++)
{
formats.add(new String[] { extensions[i], descs[i] });
}
}
+ private Runnable theCallback;
+
+ public void setCallback(Runnable callback)
+ {
+ this.theCallback = callback;
+ }
+
+ public Runnable getCallback()
+ {
+ return theCallback;
+ }
+
+ @Override
+ public void propertyChange(PropertyChangeEvent evt)
+ {
+ switch (evt.getPropertyName())
+ {
+ case "SelectedFile":
+ theCallback.run();
+ break;
+ }
+ }
+
private static File safePath(String dir)
{
if (dir == null)
int ret = showDialog(parent, MessageManager.getString("action.save"));
+
if (getFileFilter() instanceof JalviewFileFilter)
{
JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
}
}
+
}