Major cleanup of Utils class.
[trakem2.git] / ini / trakem2 / utils / DNDInsertImage.java
bloba398bca63633324dfc31fcdc4b84c3619f5ab357
1 /**
3 TrakEM2 plugin for ImageJ(C).
4 Copyright (C) 2005, 2006 Albert Cardona and Rodney Douglas.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation (http://www.gnu.org/licenses/gpl.txt )
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 You may contact Albert Cardona at acardona at ini.phys.ethz.ch
20 Institute of Neuroinformatics, University of Zurich / ETH, Switzerland.
21 **/
23 package ini.trakem2.utils;
25 import java.awt.Point;
26 import java.awt.dnd.*;
27 import java.awt.datatransfer.*;
28 import java.io.File;
29 import java.io.StringReader;
30 import java.io.BufferedReader;
31 import java.util.Arrays;
32 import java.util.Iterator;
33 import java.util.List;
34 import ij.gui.YesNoCancelDialog;
35 import ij.gui.GenericDialog;
36 import ij.io.*;
37 import ij.IJ;
38 import ij.ImagePlus;
39 import ij.ImageStack;
40 import ij.VirtualStack; // only after 1.38q
41 import ini.trakem2.display.*;
42 import ini.trakem2.*;
43 import ini.trakem2.persistence.*;
44 import ini.trakem2.io.ImageFileFilter;
47 public class DNDInsertImage implements DropTargetListener {
49 private Display display;
50 private DropTarget dt;
52 public DNDInsertImage(Display display) {
53 this.display = display;
54 this.dt = new DropTarget(display.getCanvas(), this);
57 public void destroy() {
58 // is there any way to really destroy it?
59 dt.setActive(false);
60 display.getCanvas().setDropTarget(null);
61 dt.setComponent(null);
64 public void dragEnter(DropTargetDragEvent e) {
65 e.acceptDrag(DnDConstants.ACTION_COPY);
67 public void dragExit(DropTargetEvent e) {}
68 public void dragOver(DropTargetDragEvent e) {}
69 public void dropActionChanged(DropTargetDragEvent e) {}
70 public void drop(final DropTargetDropEvent dtde) {
71 if (!display.getProject().isInputEnabled()) return;
73 try {
75 dtde.acceptDrop(DnDConstants.ACTION_COPY);
76 Point point = dtde.getLocation();
77 point.x = display.getCanvas().offScreenX(point.x);
78 point.y = display.getCanvas().offScreenY(point.y);
80 Transferable t = dtde.getTransferable();
81 DataFlavor[] flavors = t.getTransferDataFlavors();
82 int success = 0;
83 for (int i=0; i<flavors.length; i++) {
84 if (!flavors[i].getRepresentationClass().equals(String.class)) continue;
85 Object ob = t.getTransferData(flavors[i]);
86 if (!(ob instanceof String)) continue;
87 String s = ob.toString().trim();
89 BufferedReader br = new BufferedReader(new StringReader(s));
90 String tmp;
91 while (null != (tmp = br.readLine())) {
92 tmp = java.net.URLDecoder.decode(tmp, "UTF-8");
93 if (tmp.startsWith("file://")) {
94 tmp = tmp.substring(7);
95 if (IJ.isMacOSX()) {
96 if (tmp.startsWith("localhost")) {
97 tmp = tmp.substring(9);
102 File f = new File(tmp);
103 if (importImageFile(f, tmp, point)) success++;
105 break;
107 if (0 == success && t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
108 // from ij.plugin.DragAndDrop class by Wayne Rasband
109 Object data = t.getTransferData(DataFlavor.javaFileListFlavor);
110 Iterator iterator = ((List)data).iterator();
111 while(iterator.hasNext()) {
112 File f = (File)iterator.next();
113 String path = f.getCanonicalPath().replace('\\', '/');
114 importImageFile(f, path, point);
117 dtde.dropComplete(true);
119 } catch (Exception e) {
120 dtde.dropComplete(false);
124 private boolean importImageFile(File f, String path, Point point) throws Exception {
125 if (f.exists()) {
127 final Layer layer = display.getLayer();
128 Bureaucrat burro = null;
130 if (f.isDirectory()) {
131 // ask:
132 GenericDialog gd = new GenericDialog("Import directory");
133 String[] choice = new String[]{"Stack", "Grid", "Sequence as grid"};
134 gd.addChoice("Directory as: ", choice, choice[0]);
135 gd.showDialog();
136 if (gd.wasCanceled()) {
137 return true; // the user cancel it, so all is ok.
140 display.getLayerSet().addLayerContentStep(layer);
142 switch (gd.getNextChoiceIndex()) {
143 case 0: // as stack
144 // if importing image sequence as a stack:
145 String[] names = f.list(new ImageFileFilter()); // don't filter by name "^[^\\.].*[\\.][a-zA-Z1-9_]{3,4}$"
146 int max_len = 0;
147 // fake natural sorting: pre-pad short names with zeros
148 for (int i=0; i<names.length; i++) {
149 if (names[i].length() > max_len) max_len = names[i].length();
151 for (int i=0; i<names.length; i++) {
152 while (names[i].length() < max_len) names[i] = "0" + names[i];
154 Utils.log2("stack size: " + names.length);
155 for (int i=0; i<names.length; i++) {
156 Utils.log2(names[i]);
158 Arrays.sort(names);
159 VirtualStack stack = new VirtualStack(10, 10, null, f.getAbsolutePath().replace('\\', '/')); // I don't care about the dimensions
160 for (int k=0; k<names.length; k++) {
161 IJ.redirectErrorMessages();
162 if (names[k].toLowerCase().endsWith(".xml")) continue; // ignore trakem2 files
163 stack.addSlice(names[k]);
165 if (stack.getSize() > 0) {
166 burro = display.getProject().getLoader().importStack(layer, point.x, point.y, new ImagePlus("stack", stack), true, path);
168 break;
169 case 1: // as grid
170 burro = display.getProject().getLoader().importGrid(layer, path);
171 break;
172 case 2: // sequence as grid
173 burro = display.getProject().getLoader().importSequenceAsGrid(layer, path);
174 break;
176 } else {
177 layer.getParent().addLayerContentStep(layer);
179 // single image file (single image or a stack)
180 burro = display.getProject().getLoader().importImage(layer, point.x, point.y, path);
183 burro.addPostTask(new Runnable() { public void run() {
184 // The current state
185 layer.getParent().addLayerContentStep(layer);
186 }});
188 return true;
189 } else {
190 Utils.log("File not found: " + path);
191 return false;