package ie.dcu.apps.ist.actions; import ie.dcu.apps.ist.*; import ie.dcu.apps.ist.views.SegmentationView; import ie.dcu.apps.ist.views.SegmentationView.Tool; import ie.dcu.segment.SegmentationContext; import ie.dcu.swt.layout.LayoutFactory; import java.io.*; import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; /** * Open an image or segmentation context. * * @author Kevin McGuinness */ public class OpenAction extends AppAction { private FileDialog dialog; /** * The file extension given to a this object when it is saved on a disk */ public static final String IMGMAP_EXTENSION = ".zip"; public OpenAction(ActionManager m) { super(m); } @Override public void run() { File file = getFile(); if (file != null) { open(file); } } public boolean open(File file) { String name = file.getName(); try { // clear the decks window.getView().resetView(); if (SegmentationContext.isContextFile(file)) { // JP - minority case (opening an zip file to re-annotate) // Load context window.setContext(SegmentationContext.load(file)); status("Opened segmentation context %s successfully", name); } else if (name.toLowerCase().endsWith(IMGMAP_EXTENSION)) { // Load Imagemap from Disk window.setContext(SegmentationContext.loadImageMap(file)); status("Opened segmentation context %s successfully", name); } else { // JP - majority of cases (opening an img file to annotate) // Create context window.setContext(SegmentationContext.create(file)); status("Opened image file %s successfully", name); } // Save history AppRecentFiles.getInstance().add(file); // Ok return true; } catch (IOException e) { handleError(file, e); status(Warning, "Problem opening file %s", name); return false; } } private void handleError(File file, IOException e) { // Get appropriate message String message = e.getCause() == null ? e.getLocalizedMessage() : e.getCause().getLocalizedMessage(); // Log warning log.warning(String.format( "Unable to open %s\n Problem: %s", file, message )); // Show warning dialog warning("Unable to open %s:\n%s", file.getName(), message); } private void createOpenDialog() { // Create dialog if necessary if (dialog == null) { dialog = new FileDialog(window.getShell(), SWT.OPEN | SWT.SHEET); dialog.setText(property("OpenAction.dialog.text")); dialog.setFilterExtensions(getFilters()); dialog.setFilterNames(getFilterNames()); } } private String[] getFilters() { return new String[] { property("OpenAction.dialog.filter.exts") }; } private String[] getFilterNames() { return new String[] { property("OpenAction.dialog.filter.text") }; } private File getFile() { createOpenDialog(); String path = dialog.open(); if (path != null) { return new File(path); } return null; } }