Permalink
Browse files

Sprite Subimage list DragNDrop and FileDrop support implemented. Some…

… other improvements to it. FileDrop somewhat modularized. Improved Util image reading. ErrorDialog changed to support Throwable instead of Exception, although if you're getting the other kind of Throwable, you probably have real problems.

git-svn-id: https://lateralgm.svn.sourceforge.net/svnroot/lateralgm/LateralGM/trunk@542 8f422083-7f27-0410-bc82-93e204be8cd2
  • Loading branch information...
1 parent d038ba5 commit f6e11156a611db111265a1ce59108a3be01fcd5a ismavatar committed Oct 18, 2011
@@ -41,9 +41,9 @@ private static JButton makeButton(String key, ActionListener listener)
return but;
}
- public ErrorDialog(Frame parent, String title, String message, Exception e)
+ public ErrorDialog(Frame parent, String title, String message, Throwable e)
{
- this(parent,title,message,exceptionToString(e));
+ this(parent,title,message,throwableToString(e));
}
public ErrorDialog(Frame parent, String title, String message, String debugInfo)
@@ -69,7 +69,7 @@ public ErrorDialog(Frame parent, String title, String message, String debugInfo)
setLocationRelativeTo(parent);
}
- protected static String exceptionToString(Exception e)
+ protected static String throwableToString(Throwable e)
{
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
@@ -127,28 +127,10 @@ public FileChooser()
addSaveFilters(new GmWriterFilter());
}
- public class DropHandler extends TransferHandler
+ public class LGMDropHandler extends FileDropHandler
{
private static final long serialVersionUID = 1L;
- static final String MIME_URI_LIST = "uri-list";
-
- public boolean canImport(TransferHandler.TransferSupport evt)
- {
- //Although I'd love to actually grab the list of files, Mac won't let me.
- return getSupportedFlavor(evt.getDataFlavors()) != null;
- }
-
- DataFlavor getSupportedFlavor(DataFlavor...dfs)
- {
- for (DataFlavor df : dfs)
- {
- if (df.isFlavorJavaFileListType()) return df;
- if (df.isRepresentationClassReader() && MIME_URI_LIST.equals(df.getSubType())) return df;
- }
- return null;
- }
-
public boolean importData(TransferHandler.TransferSupport evt)
{
List<?> files = getDropList(evt);
@@ -167,8 +149,35 @@ public boolean importData(TransferHandler.TransferSupport evt)
}
return false;
}
+ }
+
+ public static abstract class FileDropHandler extends TransferHandler
+ {
+ private static final long serialVersionUID = 1L;
- protected List<?> getDropList(TransferHandler.TransferSupport evt)
+ public static final String MIME_URI_LIST = "uri-list";
+
+ @SuppressWarnings("static-method")
+ public boolean isDataFlavorSupported(DataFlavor df)
+ {
+ return df.isFlavorJavaFileListType()
+ || (df.isRepresentationClassReader() && MIME_URI_LIST.equals(df.getSubType()));
+ }
+
+ public boolean canImport(TransferHandler.TransferSupport evt)
+ {
+ //Mac won't let us grab the transferable.
+ return getSupportedFlavor(evt.getDataFlavors()) != null;
+ }
+
+ protected DataFlavor getSupportedFlavor(DataFlavor...dfs)
+ {
+ for (DataFlavor df : dfs)
+ if (isDataFlavorSupported(df)) return df;
+ return null;
+ }
+
+ public List<?> getDropList(TransferHandler.TransferSupport evt)
{
Transferable tr = evt.getTransferable();
DataFlavor df = getSupportedFlavor(evt.getDataFlavors());
@@ -177,7 +186,11 @@ public boolean importData(TransferHandler.TransferSupport evt)
{
if (df.isFlavorJavaFileListType())
return (List<?>) tr.getTransferData(DataFlavor.javaFileListFlavor);
+
//Linux support (uri-list reader)
+ if (!df.isRepresentationClassReader() || !MIME_URI_LIST.equals(df.getSubType()))
+ return null; //Or not? Let implementation handle it.
+
BufferedReader br = new BufferedReader(df.getReaderForText(tr));
List<URI> uriList = new ArrayList<URI>();
String line;
@@ -462,7 +462,7 @@ public void run()
JPanel f = new JPanel(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(f);
- frame.setTransferHandler(listener.fc.new DropHandler());
+ frame.setTransferHandler(listener.fc.new LGMDropHandler());
f.add(BorderLayout.NORTH,toolbar);
f.add(BorderLayout.CENTER,split);
f.setOpaque(true);
@@ -25,6 +25,7 @@
import java.awt.image.RGBImageFilter;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
@@ -41,11 +42,11 @@
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
-import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.lateralgm.components.CustomFileChooser;
+import org.lateralgm.components.ErrorDialog;
import org.lateralgm.components.impl.CustomFileFilter;
import org.lateralgm.components.visual.FileChooserImagePreview;
import org.lateralgm.file.iconio.BitmapDescriptor;
@@ -202,14 +203,7 @@ public int filterRGB(int x, int y, int rgb)
*
* @return The selected image, or null if one is not chosen
*/
- public static BufferedImage getValidImage()
- {
- BufferedImage[] img = getValidImages();
- if (img == null || img.length == 0) return null;
- return img[0];
- }
-
- public static BufferedImage[] getValidImages()
+ public static File chooseImageFile()
{
if (imageFc == null)
{
@@ -230,31 +224,56 @@ public static BufferedImage getValidImage()
imageFc.setFileFilter(filt);
}
if (imageFc.showOpenDialog(LGM.frame) == JFileChooser.APPROVE_OPTION)
+ return imageFc.getSelectedFile();
+ return null;
+ }
+
+ public static BufferedImage getValidImage()
+ {
+ File f = chooseImageFile();
+ if (f == null) return null;
+ try
{
- try
- {
- ImageInputStream in = ImageIO.createImageInputStream(imageFc.getSelectedFile());
- Iterator<ImageReader> it = ImageIO.getImageReaders(in);
- ImageReader reader = it.next();
- reader.setInput(in);
- int count = reader.getNumImages(true);
- BufferedImage[] img = new BufferedImage[count];
- for (int i = 0; i < count; i++)
- img[i] = reader.read(i);
- //TODO: Gif overlay support (as GM already does)
- return img;
- }
- catch (Throwable t)
- {
- String msg = Messages.format("Util.ERROR_LOADING",imageFc.getSelectedFile()); //$NON-NLS-1$
- String title = Messages.getString("Util.ERROR_TITLE"); //$NON-NLS-1$
- JOptionPane.showMessageDialog(LGM.frame,msg,title,JOptionPane.ERROR_MESSAGE);
- t.printStackTrace();
- }
+ return ImageIO.read(f);
+ }
+ catch (IOException e)
+ {
+ new ErrorDialog(LGM.frame,Messages.getString("Util.ERROR_TITLE"), //$NON-NLS-1$
+ Messages.format("Util.ERROR_LOADING",f),e).setVisible(true); //$NON-NLS-1$
}
return null;
}
+ public static BufferedImage[] getValidImages()
+ {
+ File f = chooseImageFile();
+ if (f == null) return null;
+ try
+ {
+ return getValidImages(ImageIO.createImageInputStream(f));
+ }
+ catch (Exception e)
+ {
+ new ErrorDialog(LGM.frame,Messages.getString("Util.ERROR_TITLE"), //$NON-NLS-1$
+ Messages.format("Util.ERROR_LOADING",f),e).setVisible(true); //$NON-NLS-1$
+ }
+ return null;
+ }
+
+ public static BufferedImage[] getValidImages(ImageInputStream in) throws IOException,
+ IllegalArgumentException
+ {
+ Iterator<ImageReader> it = ImageIO.getImageReaders(in);
+ ImageReader reader = it.next();
+ reader.setInput(in);
+ int count = reader.getNumImages(true);
+ BufferedImage[] img = new BufferedImage[count];
+ for (int i = 0; i < count; i++)
+ img[i] = reader.read(i);
+ //TODO: Gif overlay support (as GM already does)
+ return img;
+ }
+
public static BufferedImage cloneImage(BufferedImage bi)
{
if (bi == null) return null;
Oops, something went wrong.

0 comments on commit f6e1115

Please sign in to comment.