Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Drag, Drop, Paste refactor

  • Loading branch information...
commit 94cd0101d379b734cac40a42719ba386828ee10d 1 parent 31831af
@ppeccin authored
View
2  src/pc/cartridge/ROMLoader.java
@@ -34,6 +34,8 @@ public static Cartridge load(File file) {
return load(file.toURI().toURL());
} catch (MalformedURLException ex) {
errorMessage(ex, file.getPath());
+ } catch (AccessControlException ex) {
+ errorMessage(ex, file.getPath());
}
return null;
}
View
108 src/pc/screen/ROMDropTransferHandler.java
@@ -1,108 +0,0 @@
-// Copyright 2011-2012 Paulo Augusto Peccin. See licence.txt distributed with this file.
-
-package pc.screen;
-
-import java.awt.datatransfer.DataFlavor;
-import java.awt.datatransfer.Transferable;
-import java.io.File;
-import java.util.List;
-
-import javax.swing.TransferHandler;
-
-import pc.cartridge.ROMLoader;
-import atari.cartridge.Cartridge;
-
-public class ROMDropTransferHandler extends TransferHandler {
-
- public ROMDropTransferHandler(Screen screen) {
- super();
- this.screen = screen;
- }
-
- @Override
- public boolean canImport(TransferSupport support) {
- if (!screen.isCartridgeChangeEnabled()) return false;
- Transferable transf = support.getTransferable();
- if (transf.isDataFlavorSupported(DATA_FLAVOR_TEXT) ||
- transf.isDataFlavorSupported(DATA_FLAVOR_FILE_LIST)) {
- if (support.isDrop() && support.getUserDropAction() != LINK) support.setDropAction(COPY);
- return true;
- }
- return false;
- }
-
- @Override
- public boolean importData(TransferSupport support) {
- if (!canImport(support)) return false;
-
- Transferable transf = support.getTransferable();
-
- // LINK Action means load Cartridge without auto power! :-)
- boolean autoPower = !support.isDrop() || support.getDropAction() != LINK;
-
- // First try String URL
- String url = getSingleURL(transf);
- if (url != null) {
- Cartridge cart = ROMLoader.load(url);
- if (cart != null)
- screen.cartridgeInsert(cart, autoPower);
- return true;
- }
-
- // Then try File
- File file = getSingleFile(transf);
- if (file != null) {
- Cartridge cart = ROMLoader.load(file);
- if (cart != null)
- screen.cartridgeInsert(cart, autoPower);
- return true;
- }
-
- // Fail if none worked
- return false;
- }
-
- private String getSingleURL(Transferable transf) {
- if (!transf.isDataFlavorSupported(DATA_FLAVOR_TEXT)) return null;
- try {
- String data = (String) transf.getTransferData(DATA_FLAVOR_TEXT);
- data = data.trim();
- if (!data.isEmpty())
- return data;
- } catch (Exception ex) {
- // Ignore
- }
- return null;
- }
-
- private File getSingleFile(Transferable transf) {
- if (!transf.isDataFlavorSupported(DATA_FLAVOR_FILE_LIST)) return null;
- try {
- @SuppressWarnings("unchecked")
- List<File> data = (List<File>) transf.getTransferData(DATA_FLAVOR_FILE_LIST);
- if (data.size() != 1) return null;
- return data.get(0);
- } catch (Exception ex) {
- // Ignore
- }
- return null;
- }
-
-
- private final Screen screen;
-
- private static DataFlavor DATA_FLAVOR_TEXT;
- private static DataFlavor DATA_FLAVOR_FILE_LIST;
-
- static {
- try {
- DATA_FLAVOR_TEXT = new DataFlavor("text/plain; class=java.lang.String");
- DATA_FLAVOR_FILE_LIST = new DataFlavor("application/x-java-file-list; class=java.util.List");
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- }
- }
-
- private static final long serialVersionUID = 1L;
-
-}
View
78 src/pc/screen/ROMTransferHandlerUtil.java
@@ -0,0 +1,78 @@
+// Copyright 2011-2012 Paulo Augusto Peccin. See licence.txt distributed with this file.
+
+package pc.screen;
+
+import java.awt.datatransfer.DataFlavor;
+import java.awt.datatransfer.Transferable;
+import java.io.File;
+import java.io.FilePermission;
+import java.util.List;
+
+import pc.cartridge.ROMLoader;
+import atari.cartridge.Cartridge;
+
+public class ROMTransferHandlerUtil {
+
+ public static boolean canAccept(Transferable transf) {
+ // General URLs
+ if (transf.isDataFlavorSupported(DATA_FLAVOR_TEXT)) return true;
+ // Files
+ if (!transf.isDataFlavorSupported(DATA_FLAVOR_FILE_LIST)) return false;
+ try { // Files Permission
+ new FilePermission(".", "read").checkGuard("Ignored");
+ return true;
+ } catch (SecurityException ex) {
+ return false;
+ }
+ }
+
+ public static Cartridge importCartridgeData(Transferable transf) {
+ // First try String URL
+ String url = getSingleURL(transf);
+ if (url != null) return ROMLoader.load(url);
+ // Then try File
+ File file = getSingleFile(transf);
+ if (file != null) return ROMLoader.load(file);
+ // Give up
+ return null;
+ }
+
+ private static String getSingleURL(Transferable transf) {
+ if (!transf.isDataFlavorSupported(DATA_FLAVOR_TEXT)) return null;
+ try {
+ String data = (String) transf.getTransferData(DATA_FLAVOR_TEXT);
+ data = data.trim();
+ if (!data.isEmpty())
+ return data;
+ } catch (Exception ex) {
+ // Ignore
+ }
+ return null;
+ }
+
+ private static File getSingleFile(Transferable transf) {
+ if (!transf.isDataFlavorSupported(DATA_FLAVOR_FILE_LIST)) return null;
+ try {
+ @SuppressWarnings("unchecked")
+ List<File> data = (List<File>) transf.getTransferData(DATA_FLAVOR_FILE_LIST);
+ if (data.isEmpty()) return null;
+ return data.get(0);
+ } catch (Exception ex) {
+ // Ignore
+ }
+ return null;
+ }
+
+ private static DataFlavor DATA_FLAVOR_TEXT;
+ private static DataFlavor DATA_FLAVOR_FILE_LIST;
+
+ static {
+ try {
+ DATA_FLAVOR_TEXT = new DataFlavor("text/plain; class=java.lang.String");
+ DATA_FLAVOR_FILE_LIST = new DataFlavor("application/x-java-file-list; class=java.util.List");
+ } catch (ClassNotFoundException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+}
View
37 src/pc/screen/Screen.java
@@ -19,6 +19,9 @@
import java.awt.GraphicsDevice;
import java.awt.Image;
import java.awt.RenderingHints;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.Raster;
@@ -501,6 +504,19 @@ private void loadCartridgeEmpty() {
cartridgeSocket.insert(null, false);
}
+ private void loadCartridgePaste() {
+ if (cartridgeChangeDisabledWarning()) return;
+ try {
+ Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
+ Transferable transf = clip.getContents("Ignored");
+ if (transf == null) return;
+ Cartridge cart = ROMTransferHandlerUtil.importCartridgeData(transf);
+ if (cart != null) cartridgeInsert(cart, true);
+ } catch (Exception ex) {
+ // Simply give up
+ }
+ }
+
private boolean cartridgeChangeDisabledWarning() {
if (!isCartridgeChangeEnabled()) {
showOSD("Cartridge change is disabled");
@@ -529,7 +545,7 @@ public void controlStateChanged(Control control, boolean state) {
// Toggles
if (!state) return;
switch(control) {
- case LOAD_CARTRIDGE_FILE:
+ case LOAD_CARTRIDGE_FILE:
loadCartridgeFromFile(true); break;
case LOAD_CARTRIDGE_FILE_NO_AUTO_POWER:
loadCartridgeFromFile(false); break;
@@ -539,6 +555,8 @@ public void controlStateChanged(Control control, boolean state) {
loadCartridgeFromURL(false); break;
case LOAD_CARTRIDGE_EMPTY:
loadCartridgeEmpty(); break;
+ case LOAD_CARTRIDGE_PASTE:
+ loadCartridgePaste(); break;
case FULL_SCREEN:
fullScreen(!fullScreen); break;
case QUALITY:
@@ -551,13 +569,6 @@ public void controlStateChanged(Control control, boolean state) {
debug++;
if (debug > 4) debug = 0;
break;
- case EXIT:
- if (fullScreen) {
- fullScreen(false);
- break;
- }
- // Close program
- Terminator.terminate();
case HELP:
window.consolePanelWindow.toggle(); break;
case ORIGIN_X_MINUS:
@@ -589,7 +600,14 @@ public void controlStateChanged(Control control, boolean state) {
case SIZE_PLUS:
setDisplayScaleDefaultAspect(displayScaleY + 1); break;
case SIZE_DEFAULT:
- setDisplayDefaultSize();
+ setDisplayDefaultSize(); break;
+ case EXIT:
+ if (fullScreen) {
+ fullScreen(false);
+ break;
+ }
+ // Close program
+ Terminator.terminate();
}
}
@@ -709,6 +727,7 @@ private static void arrayCopyWithStride(int[] src, int srcPos, int dest[], int d
LOAD_CARTRIDGE_FILE, LOAD_CARTRIDGE_FILE_NO_AUTO_POWER,
LOAD_CARTRIDGE_URL, LOAD_CARTRIDGE_URL_NO_AUTO_POWER,
LOAD_CARTRIDGE_EMPTY,
+ LOAD_CARTRIDGE_PASTE,
FULL_SCREEN, QUALITY, CRT_MODES,
HELP, DEBUG,
EXIT
View
32 src/pc/screen/ScreenControlsAdapter.java
@@ -19,10 +19,10 @@ public ScreenControlsAdapter(Screen screen, Component... components) {
private void init(Component... components) {
for (int i = 0; i < components.length; i++)
components[i].addKeyListener(this);
- keyCodeMap.put(KEY_EXIT, Screen.Control.EXIT);
- keyCodeMap.put(KEY_CART_FILE, Screen.Control.LOAD_CARTRIDGE_FILE);
- keyCodeMap.put(KEY_CART_URL, Screen.Control.LOAD_CARTRIDGE_URL);
- keyCodeMap.put(KEY_CART_EMPTY, Screen.Control.LOAD_CARTRIDGE_EMPTY);
+ keyCodeMap.put(KEY_EXIT, Screen.Control.EXIT);
+ keyCodeMap.put(KEY_CART_FILE, Screen.Control.LOAD_CARTRIDGE_FILE);
+ keyCodeMap.put(KEY_CART_URL, Screen.Control.LOAD_CARTRIDGE_URL);
+ keyCodeMap.put(KEY_CART_EMPTY, Screen.Control.LOAD_CARTRIDGE_EMPTY);
keyAltCodeMap.put(KEY_HELP, Screen.Control.HELP);
keyAltCodeMap.put(KEY_FULL_SCR, Screen.Control.FULL_SCREEN);
@@ -52,6 +52,9 @@ private void init(Component... components) {
keyShiftControlCodeMap.put(KEY_LEFT, Screen.Control.WIDTH_MINUS);
keyShiftControlCodeMap.put(KEY_RIGHT, Screen.Control.WIDTH_PLUS);
+ keyShiftCodeMap.put(KEY_CART_PASTE_INS, Screen.Control.LOAD_CARTRIDGE_PASTE);
+ keyControlCodeMap.put(KEY_CART_PASTE_V, Screen.Control.LOAD_CARTRIDGE_PASTE);
+
keyCodeMap.put(KEY_SIZE_DEFAULT, Screen.Control.SIZE_DEFAULT);
}
@@ -82,6 +85,8 @@ public void keyReleased(KeyEvent e) {
return keyAltCodeMap.get(e.getKeyCode());
case KeyEvent.SHIFT_DOWN_MASK:
return keyShiftCodeMap.get(e.getKeyCode());
+ case KeyEvent.CTRL_DOWN_MASK:
+ return keyControlCodeMap.get(e.getKeyCode());
case KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK:
return keyControlAltCodeMap.get(e.getKeyCode());
case KeyEvent.SHIFT_DOWN_MASK | KeyEvent.CTRL_DOWN_MASK:
@@ -99,6 +104,7 @@ public void keyReleased(KeyEvent e) {
private final Map<Integer, Screen.Control> keyAltCodeMap = new HashMap<Integer, Screen.Control>();
private final Map<Integer, Screen.Control> keyShiftControlCodeMap = new HashMap<Integer, Screen.Control>();
private final Map<Integer, Screen.Control> keyShiftAltCodeMap = new HashMap<Integer, Screen.Control>();
+ private final Map<Integer, Screen.Control> keyControlCodeMap = new HashMap<Integer, Screen.Control>();
private final Map<Integer, Screen.Control> keyControlAltCodeMap = new HashMap<Integer, Screen.Control>();
public static final int KEY_UP = KeyEvent.VK_UP;
@@ -108,14 +114,16 @@ public void keyReleased(KeyEvent e) {
public static final int KEY_SIZE_DEFAULT = KeyEvent.VK_BACK_SPACE;
- public static final int KEY_EXIT = KeyEvent.VK_ESCAPE;
- public static final int KEY_CART_FILE = KeyEvent.VK_F5;
- public static final int KEY_CART_URL = KeyEvent.VK_F6;
- public static final int KEY_CART_EMPTY = KeyEvent.VK_F7;
- public static final int KEY_FULL_SCR = KeyEvent.VK_ENTER;
- public static final int KEY_QUALITY = KeyEvent.VK_Q;
- public static final int KEY_CRT_MODES = KeyEvent.VK_R;
- public static final int KEY_VIDEO_STAND = KeyEvent.VK_V;
+ public static final int KEY_EXIT = KeyEvent.VK_ESCAPE;
+ public static final int KEY_CART_FILE = KeyEvent.VK_F5;
+ public static final int KEY_CART_URL = KeyEvent.VK_F6;
+ public static final int KEY_CART_PASTE_V = KeyEvent.VK_V;
+ public static final int KEY_CART_PASTE_INS = KeyEvent.VK_INSERT;
+ public static final int KEY_CART_EMPTY = KeyEvent.VK_F7;
+ public static final int KEY_FULL_SCR = KeyEvent.VK_ENTER;
+ public static final int KEY_QUALITY = KeyEvent.VK_Q;
+ public static final int KEY_CRT_MODES = KeyEvent.VK_R;
+ public static final int KEY_VIDEO_STAND = KeyEvent.VK_V;
public static final int KEY_DEBUG = KeyEvent.VK_D;
public static final int KEY_HELP = KeyEvent.VK_H;
View
49 src/pc/screen/ScreenWindow.java
@@ -16,10 +16,9 @@
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
-import java.awt.event.ActionEvent;
+import java.awt.datatransfer.Transferable;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
-import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
@@ -34,6 +33,7 @@
import parameters.Parameters;
import utils.GraphicsDeviceHelper;
import utils.SlickFrame;
+import atari.cartridge.Cartridge;
public class ScreenWindow extends SlickFrame implements DisplayCanvas {
@@ -41,7 +41,6 @@ public ScreenWindow(Screen screen) throws HeadlessException {
super();
this.screen = screen;
consolePanelWindow = new ConsolePanel(this, screen, screen.consoleControlsSocket);
- initTransferHandler();
}
@Override
@@ -61,6 +60,7 @@ protected void init() {
public void componentResized(ComponentEvent e) {
positionCanvas();
}});
+ getRootPane().setTransferHandler(new ROMDropTransferHandler());
super.init();
}
@@ -202,25 +202,6 @@ public void canvasMinimumSize(Dimension minSize) {
minimunResize(windowDimensionForCanvasDimension(minSize));
}
- private void initTransferHandler() {
- getRootPane().setTransferHandler(new ROMDropTransferHandler(screen));
- addKeyListener(new KeyListener() {
- @Override
- public void keyTyped(KeyEvent e) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_V && e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK)
- TransferHandler.getPasteAction().actionPerformed(
- new ActionEvent(getRootPane(), ActionEvent.ACTION_PERFORMED, null)
- );
- }
- });
- }
-
private Dimension windowDimensionForCanvasDimension(Dimension size) {
return new Dimension(
size.width + totalCanvasHorizPadding,
@@ -327,4 +308,28 @@ public void paint(Graphics origGraphics) {
public static final long serialVersionUID = 1L;
+
+ // To handle drag and drop of ROM files and links
+ class ROMDropTransferHandler extends TransferHandler {
+ @Override
+ public boolean canImport(TransferSupport support) {
+ if (!screen.isCartridgeChangeEnabled()) return false;
+ Transferable transf = support.getTransferable();
+ if (!ROMTransferHandlerUtil.canAccept(transf)) return false;
+ if (support.isDrop() && support.getUserDropAction() != LINK) support.setDropAction(COPY);
+ return true;
+ }
+ @Override
+ public boolean importData(TransferSupport support) {
+ if (!canImport(support)) return false;
+ Cartridge cart = ROMTransferHandlerUtil.importCartridgeData(support.getTransferable());
+ if (cart == null) return false;
+ // LINK Action means load Cartridge without auto power! :-)
+ boolean autoPower = !support.isDrop() || support.getDropAction() != LINK;
+ screen.cartridgeInsert(cart, autoPower);
+ return true;
+ }
+ private static final long serialVersionUID = 1L;
+ }
+
}
View
4 webstart/javatari-local-test.jnlp
@@ -15,9 +15,9 @@
</_security>
<resources>
<j2se version="1.6+" />
- <jar href="javatari-test.jar" main="true" />
+ <jar href="../deploy/javatari.jar" main="true" />
</resources>
<application-desc>
- <argument>-screen_cartridge_change=false</argument>
+ <_argument>-screen_cartridge_change=false</_argument>
</application-desc>
</jnlp>
Please sign in to comment.
Something went wrong with that request. Please try again.