Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable batch image export filename #3447

Merged
merged 1 commit into from Feb 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2,7 +2,7 @@
* org.openmicroscopy.shoola.agents.metadata.editor.Editor
*
*------------------------------------------------------------------------------
* Copyright (C) 2006-2014 University of Dundee. All rights reserved.
* Copyright (C) 2006-2015 University of Dundee. All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -483,14 +483,19 @@ public void setLoadedFile(FileAnnotationData data, File file,
void analysisResultsLoaded(AnalysisResultsItem analysis);

/**
* Saves the selected images as <code>JPEG</code>, <code>PNG</code>
* or <code>TIFF</code>.
* Saves the selected images as <code>JPEG</code>, <code>PNG</code> or
* <code>TIFF</code>.
*
* @param folder The folder to save.
* @param format The format to use.
* @param folder
* The folder to save.
* @param format
* The format to use.
* @param filename
* The filename to use for the batch export file (without
* extension)
* @see org.openmicroscopy.shoola.env.data.model.FigureParam
*/
public void saveAs(File folder, int format);
public void saveAs(File folder, int format, String filename);

/**
* Invokes when the user has switched group.
Expand Down
Expand Up @@ -2,7 +2,7 @@
* org.openmicroscopy.shoola.agents.metadata.editor.EditorComponent
*
*------------------------------------------------------------------------------
* Copyright (C) 2006-2014 University of Dundee. All rights reserved.
* Copyright (C) 2006-2015 University of Dundee. All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -1166,10 +1166,10 @@ public void propertyChange(PropertyChangeEvent evt) {
* Implemented as specified by the {@link Editor} interface.
* @see Editor#saveAs(File, int)
*/
public void saveAs(File folder, int format)
public void saveAs(File folder, int format, String fileName)
{
if (folder == null) folder = UIUtilities.getDefaultFolder();
model.saveAs(folder, format);
model.saveAs(folder, format, fileName);
}

/**
Expand Down
Expand Up @@ -54,7 +54,7 @@
//Third-party libraries
import org.jdesktop.swingx.JXTaskPane;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.ArrayUtils;

//Application-internal dependencies
import org.openmicroscopy.shoola.agents.events.iviewer.ViewImage;
Expand Down Expand Up @@ -97,6 +97,8 @@
import org.openmicroscopy.shoola.util.filter.file.TIFFFilter;
import org.openmicroscopy.shoola.util.filter.file.WordFilter;
import org.openmicroscopy.shoola.util.filter.file.XMLFilter;
import org.openmicroscopy.shoola.util.filter.file.ZipFilter;
import org.openmicroscopy.shoola.util.ui.MessageBox;
import org.openmicroscopy.shoola.util.ui.UIUtilities;
import org.openmicroscopy.shoola.util.ui.filechooser.FileChooser;
import org.openmicroscopy.shoola.util.ui.omeeditpane.OMEWikiComponent;
Expand Down Expand Up @@ -374,27 +376,19 @@ public void propertyChange(PropertyChangeEvent evt) {
void saveAs(final int format)
{
String v = FigureParam.FORMATS.get(format);
JFrame f = MetadataViewerAgent.getRegistry().getTaskBar().getFrame();
final JFrame f = MetadataViewerAgent.getRegistry().getTaskBar().getFrame();
List<FileFilter> filters = new ArrayList<FileFilter>();
switch (format) {
case FigureParam.JPEG:
filters.add(new JPEGFilter());
break;
case FigureParam.PNG:
filters.add(new PNGFilter());
break;
case FigureParam.TIFF:
filters.add(new TIFFFilter());
}
FileChooser chooser = new FileChooser(f, FileChooser.FOLDER_CHOOSER,
filters.add(new ZipFilter());
FileChooser chooser = new FileChooser(f, FileChooser.SAVE,
"Save As", "Select where to save locally the images as "+v,
filters);
try {
File file = UIUtilities.getDefaultFolder();
if (file != null) chooser.setCurrentDirectory(file);
if (file != null)
chooser.setCurrentDirectory(file);
chooser.setSelectedFile(UIUtilities.generateFileName(file,
"Batch_Image_Export", "zip"));
} catch (Exception ex) {}
String s = UIUtilities.removeFileExtension(view.getRefObjectName());
if (s != null && s.trim().length() > 0) chooser.setSelectedFile(s);
chooser.setApproveButtonText("Save");
IconManager icons = IconManager.getInstance();
chooser.setTitleIcon(icons.getIcon(IconManager.SAVE_AS_48));
Expand All @@ -403,23 +397,36 @@ void saveAs(final int format)
public void propertyChange(PropertyChangeEvent evt) {
String name = evt.getPropertyName();
if (FileChooser.APPROVE_SELECTION_PROPERTY.equals(name)) {
String value = (String) evt.getNewValue();
File folder = null;
if (StringUtils.isEmpty(value))
folder = UIUtilities.getDefaultFolder();
else folder = new File(value);
File[] files = (File[]) evt.getNewValue();
if (ArrayUtils.isEmpty(files))
return;
File file = files[0];
if (file == null)
file = UIUtilities.generateFileName(
UIUtilities.getDefaultFolder(),
"Batch_Image_Export", "zip");
if (file.exists()) {
MessageBox msg = new MessageBox(f, "File Exists",
"Do you want to overwrite the file?");
int option = msg.centerMsgBox();
if (option == MessageBox.NO_OPTION) {
return;
}
}
Object src = evt.getSource();
if (src instanceof FileChooser) {
((FileChooser) src).setVisible(false);
((FileChooser) src).dispose();
}
model.saveAs(folder, format);
model.saveAs(file.getParentFile(), format,
UIUtilities.removeFileExtension(file.getName()));
}
}
});
chooser.centerDialog();
}


/** Brings up the folder chooser. */
private void export()
{
Expand Down
Expand Up @@ -4141,7 +4141,7 @@ private DataObject saveAsObject(Object ho)
* @param folder The folder where to save the images.
* @param format The format to use.
*/
void saveAs(File folder, int format)
void saveAs(File folder, int format, String filename)
{
Collection l = parent.getRelatedNodes();
List<DataObject> objects = new ArrayList<DataObject>();
Expand All @@ -4163,6 +4163,7 @@ void saveAs(File folder, int format)
SaveAsParam p = new SaveAsParam(folder, objects);
p.setIndex(format);
p.setIcon(icons.getIcon(IconManager.SAVE_AS_22));
p.setBatchExportFilename(filename);
p.setDeleteWhenFinished(true);
UserNotifier un =
MetadataViewerAgent.getRegistry().getUserNotifier();
Expand Down
Expand Up @@ -6215,6 +6215,9 @@ ScriptCallback saveAs(SecurityContext ctx, long userID, SaveAsParam param)
map.put("IDs", omero.rtypes.rlist(ids));
map.put("Data_Type", omero.rtypes.rstring(type));
map.put("Format", omero.rtypes.rstring(param.getIndexAsString()));
if (!StringUtils.isEmpty(param.getBatchExportFilename()))
map.put("Folder_Name",
omero.rtypes.rstring(param.getBatchExportFilename()));
return runScript(ctx, id, map);
}

Expand Down
Expand Up @@ -94,6 +94,9 @@ public class DownloadActivityParam
/** FileAnnotation to delete after downloading */
private FileAnnotationData toDelete;

/** Overwrite local file, if it already exists */
private boolean overwrite = false;

/**
* Checks if the index is valid.
*
Expand Down Expand Up @@ -292,11 +295,32 @@ public FileAnnotationData getToDelete() {
/**
* Set the {@link FileAnnotationData} which should get
* deleted after download finished
*
* @param toDelete The {@link FileAnnotationData} to delete
*/
public void setToDelete(FileAnnotationData toDelete) {
this.toDelete = toDelete;
}

/**
* Returns <code>true</code> if the local file should be overwritten, if it
* already exists
*
* @return See above.
*/
public boolean isOverwrite() {
return overwrite;
}

/**
* Determines if the local file should be overwritten, if it already exists
*
* @param overwrite
* Set to <code>true</code> if local file should be overwritten
*/
public void setOverwrite(boolean overwrite) {
this.overwrite = overwrite;
}


}
Expand Up @@ -70,6 +70,9 @@ public class SaveAsParam
/** Flag to indicate to delete the file after downloading */
private boolean deleteWhenFinished = false;

/** Filename for the batch export */
private String batchExportFilename;

/**
* Creates a new instance.
*
Expand Down Expand Up @@ -161,6 +164,24 @@ public boolean isDeleteWhenFinished() {
public void setDeleteWhenFinished(boolean deleteWhenFinished) {
this.deleteWhenFinished = deleteWhenFinished;
}


/**
* Get the filename to use for the batch export file
*
* @return See above.
*/
public String getBatchExportFilename() {
return batchExportFilename;
}

/**
* Set the filename to use for the batch export file
*
* @param batchExportFilename
* The filename
*/
public void setBatchExportFilename(String batchExportFilename) {
this.batchExportFilename = batchExportFilename;
}

}
Expand Up @@ -619,6 +619,7 @@ void download(String text, Object object, File folder, final boolean deleteWhenF
activity.setUIRegister(true);
if (fa != null && deleteWhenFinished)
activity.setToDelete(fa);
activity.setOverwrite(true);
viewer.notifyActivity(ctx, activity);
return;
}
Expand Down
Expand Up @@ -84,6 +84,9 @@ public class DownloadActivity extends ActivityComponent {
/** The local name of the file. */
private String localFileName;

/** Overwrite if local file already exists */
private boolean overwrite = false;

/** The supported file filters. */
private static final List<CustomizedFileFilter> FILTERS;

Expand Down Expand Up @@ -190,6 +193,8 @@ public DownloadActivity(UserNotifier viewer, Registry registry,
else
localFileName = folder.toString();
messageLabel.setText(localFileName);

this.overwrite = parameters.isOverwrite();
}

/**
Expand All @@ -207,7 +212,7 @@ protected UserNotifierLoader createLoader() {
registry.getLogger().debug(this, file.getAbsolutePath());

boolean load = true;
if (file.exists())
if (file.exists() && !overwrite)
load = false;

switch (parameters.getIndex()) {
Expand Down
Expand Up @@ -123,8 +123,7 @@ public void endActivity(Object result) {
}

if(fa!=null && fa.isLoaded()) {
String name = getFileName(fa.getFileName());
download("", fa, new File(parameters.getFolder(), name), parameters.isDeleteWhenFinished());
download("", fa, new File(parameters.getFolder(), fa.getFileName()), parameters.isDeleteWhenFinished());
// call super method to stop busy label
super.endActivity(null);
}
Expand Down