00001 package edu.stanford.hci.r3.tools.design.acrobat;
00002
00003 import java.awt.BorderLayout;
00004 import java.awt.Color;
00005 import java.awt.Component;
00006 import java.awt.Container;
00007 import java.awt.Font;
00008 import java.awt.Graphics;
00009 import java.awt.Graphics2D;
00010 import java.awt.dnd.DnDConstants;
00011 import java.awt.dnd.DropTarget;
00012 import java.awt.dnd.DropTargetAdapter;
00013 import java.awt.dnd.DropTargetDragEvent;
00014 import java.awt.dnd.DropTargetDropEvent;
00015 import java.awt.dnd.DropTargetEvent;
00016 import java.awt.image.BufferedImage;
00017 import java.io.File;
00018 import java.util.List;
00019 import java.util.TooManyListenersException;
00020
00021 import javax.swing.BorderFactory;
00022 import javax.swing.JFrame;
00023 import javax.swing.JLabel;
00024 import javax.swing.JPanel;
00025 import javax.swing.WindowConstants;
00026
00027 import edu.stanford.hci.r3.PaperToolkit;
00028 import edu.stanford.hci.r3.util.WindowUtils;
00029 import edu.stanford.hci.r3.util.components.BufferedImagePanel;
00030 import edu.stanford.hci.r3.util.graphics.GraphicsUtils;
00031 import edu.stanford.hci.r3.util.graphics.ImageCache;
00032
00045 public class AcrobatDesignerLauncher {
00046
00047 private static BufferedImagePanel activeArea;
00048
00049 private static DropTarget dropTarget;
00050
00051 private static DropTargetAdapter dropTargetAdapter;
00052
00053 private static FileTransferHandler fileTransferHandler;
00054
00055 private static final Font FONT = new Font("Trebuchet MS", Font.BOLD, 28);
00056
00057 private static JFrame frame;
00058
00059 private static JPanel mainPanel;
00060
00064 private static List<File> onlyPDFs;
00065
00069 private static BufferedImage pdfLogo;
00070
00074 private static final Color TRANSLUCENT_GRAY = new Color(50, 50, 50, 88);
00075
00079 private static Component getActiveArea() {
00080 if (activeArea == null) {
00081 activeArea = new BufferedImagePanel() {
00082 public void paintComponent(Graphics g) {
00083 super.paintComponent(g);
00084 if (onlyPDFs == null) {
00085 return;
00086 }
00087
00088
00089 final Graphics2D g2d = (Graphics2D) g;
00090 g2d.setRenderingHints(GraphicsUtils.getBestRenderingHints());
00091 g2d.setColor(TRANSLUCENT_GRAY);
00092 g2d.setFont(FONT);
00093 int stringY = (int) ((activeArea.getHeight() - onlyPDFs.size() * 40) / 2.0);
00094 for (File f : onlyPDFs) {
00095 g2d.drawString(f.getName(), 50, stringY);
00096 stringY += 50;
00097 }
00098 }
00099 };
00100 activeArea.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.LIGHT_GRAY));
00101 activeArea.setBackground(Color.DARK_GRAY);
00102 fileTransferHandler = new FileTransferHandler();
00103 activeArea.setTransferHandler(fileTransferHandler);
00104
00105 try {
00106 dropTarget = new DropTarget();
00107 dropTarget.setComponent(activeArea);
00108 dropTarget.addDropTargetListener(getDropTargetAdapter());
00109 } catch (TooManyListenersException e) {
00110 e.printStackTrace();
00111 }
00112
00113 pdfLogo = ImageCache.loadBufferedImage(AcrobatDesignerLauncher.class
00114 .getResource("/icons/pdfIcon.png"));
00115 }
00116 return activeArea;
00117 }
00118
00122 private static DropTargetAdapter getDropTargetAdapter() {
00123 if (dropTargetAdapter == null) {
00124 dropTargetAdapter = new DropTargetAdapter() {
00125 public void dragEnter(DropTargetDragEvent dtde) {
00126 frame.setAlwaysOnTop(true);
00127 frame.setAlwaysOnTop(false);
00128
00129
00130
00131
00132 onlyPDFs = fileTransferHandler.getOnlyPDFs(activeArea, dtde.getTransferable());
00133
00134 if (onlyPDFs.size() > 0) {
00135 activeArea.setBackground(Color.WHITE);
00136 activeArea.setImageCentered(pdfLogo);
00137 }
00138 }
00139
00143 public void dragExit(DropTargetEvent dte) {
00144 hideBackground();
00145 }
00146
00150 public void drop(DropTargetDropEvent dtde) {
00151 dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
00152 fileTransferHandler.importData(activeArea, dtde.getTransferable());
00153
00154 try {
00155 Thread.sleep(500);
00156 } catch (InterruptedException e) {
00157 e.printStackTrace();
00158 }
00159
00160 hideBackground();
00161 }
00162
00163 private void hideBackground() {
00164 activeArea.setBackground(Color.DARK_GRAY);
00165 activeArea.setImage(null);
00166 onlyPDFs = null;
00167 }
00168 };
00169 }
00170 return dropTargetAdapter;
00171 }
00172
00176 private static Component getLabel() {
00177 JLabel label = new JLabel("<html>Drag a PDF on to the active area below to start the "
00178 + "R3 Acrobat Designer. You must have Acrobat Pro <br/>on your system, "
00179 + "with the R3 plugin installed.</html>");
00180 label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
00181 return label;
00182 }
00183
00187 private static Container getMainPanel() {
00188 if (mainPanel == null) {
00189 mainPanel = new JPanel();
00190 mainPanel.setLayout(new BorderLayout());
00191 mainPanel.add(getLabel(), BorderLayout.NORTH);
00192 mainPanel.add(getActiveArea(), BorderLayout.CENTER);
00193 }
00194 return mainPanel;
00195 }
00196
00200 public static void main(String[] args) {
00201 start();
00202 }
00203
00207 public static JFrame start() {
00208 PaperToolkit.initializeLookAndFeel();
00209 if (frame == null) {
00210 frame = new JFrame("Acrobat Designer Launcher");
00211 frame.setContentPane(getMainPanel());
00212 frame.setSize(640, 480);
00213 frame.setLocation(WindowUtils.getWindowOrigin(frame, WindowUtils.DESKTOP_CENTER));
00214 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
00215 }
00216 frame.setVisible(true);
00217 return frame;
00218 }
00219 }