Ignore:
Timestamp:
05/10/18 16:04:51 (6 years ago)
Author:
davidb
Message:

Reworking of the code-base to separate logic from graphics. This version of Expeditee now supports a JFX graphics as an alternative to SWING

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/importer/FrameDNDTransferHandler.java

    r919 r1102  
    1919package org.expeditee.importer;
    2020
    21 import java.awt.Point;
    2221import java.awt.datatransfer.DataFlavor;
    2322import java.awt.datatransfer.UnsupportedFlavorException;
     
    3231import javax.swing.TransferHandler;
    3332
     33import org.expeditee.core.Point;
     34import org.expeditee.gio.swing.SwingConversions;
    3435import org.expeditee.gui.DisplayIO;
    3536import org.expeditee.gui.FrameGraphics;
     
    6566        }
    6667
    67         private FrameDNDTransferHandler() {
    68 
     68        private FrameDNDTransferHandler()
     69        {
    6970                // Add standard file importers - order from most ideal to last resort
    7071                // (if competing)
     
    7475                _standardFileImporters.add(new pdfImporter());
    7576                _standardFileImporters.add(new TextImporter());
    76                 _standardFileImporters.add(new FilePathImporter()); // Filepath importer
    77                 // as last resort
     77                _standardFileImporters.add(new FilePathImporter()); // Filepath importer as last resort
    7878
    7979                try {
    80                         _URIListDataflavorString = new DataFlavor(
    81                                         "text/uri-list;class=java.lang.String");
    82                         _URIListDataflavorCharArray = new DataFlavor(
    83                                         "text/uri-list;class=\"[C\"");
    84                 } catch (ClassNotFoundException e) { // This would never happen,
    85                         // java.lang.String is always
    86                         // present
     80                        _URIListDataflavorString = new DataFlavor("text/uri-list;class=java.lang.String");
     81                        _URIListDataflavorCharArray = new DataFlavor("text/uri-list;class=\"[C\"");
     82                       
     83                // This would never happen, java.lang.String is always present
     84                } catch (ClassNotFoundException e) {
    8785                        e.printStackTrace();
    8886                        _URIListDataflavorString = null;
     
    104102         *             if importer is null.
    105103         */
    106         public void addCustomFileImporter(FileImporter importer) {
    107                 if (importer == null)
    108                         throw new NullPointerException("importer");
    109                 if (!_customFileImporters.contains(importer))
     104        public void addCustomFileImporter(FileImporter importer)
     105        {
     106                if (importer == null) throw new NullPointerException("importer");
     107               
     108                if (!_customFileImporters.contains(importer)) {
    110109                        _customFileImporters.add(importer);
     110                }
    111111
    112112        }
     
    121121         *             if importer is null.
    122122         */
    123         public void removeCustomFileImporter(FileImporter importer) {
    124                 if (importer == null)
    125                         throw new NullPointerException("importer");
     123        public void removeCustomFileImporter(FileImporter importer)
     124        {
     125                if (importer == null) throw new NullPointerException("importer");
     126               
    126127                _customFileImporters.remove(importer);
    127128        }
     
    135136
    136137                // we only import Strings
    137                 if (support.isDataFlavorSupported(DataFlavor.stringFlavor)
    138                                 || support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
    139                                 || support.isDataFlavorSupported(_URIListDataflavorString)
    140                                 || support.isDataFlavorSupported(_URIListDataflavorCharArray)) {
    141 
     138                if (    support.isDataFlavorSupported(DataFlavor.stringFlavor) ||
     139                                support.isDataFlavorSupported(DataFlavor.javaFileListFlavor) ||
     140                                support.isDataFlavorSupported(_URIListDataflavorString) ||
     141                                support.isDataFlavorSupported(_URIListDataflavorCharArray))
     142                {
    142143                        // check if the source actions (a bitwise-OR of supported actions)
    143144                        // contains the COPY action
     
    154155
    155156        @Override
    156         public boolean importData(TransferSupport support) {
    157 
    158                 if (!canImport(support) || DisplayIO.getCurrentFrame() == null)
    159                         return false;
    160 
    161                 // Get the drop location of where to lot the import
     157        public boolean importData(TransferSupport support)
     158        {
     159                if (!canImport(support) || DisplayIO.getCurrentFrame() == null) return false;
     160
     161                // Get the location of where to drop the import
    162162                DropLocation location = support.getDropLocation();
    163163
    164                 // Covert it into expeditee space
    165                 Point expediteeDropPoint = location.getDropPoint();
     164                // Convert it into expeditee space
     165                Point expediteeDropPoint = SwingConversions.fromSwingPoint(location.getDropPoint());
    166166
    167167                try {
     
    170170                        // keep trying until first
    171171                        // data flavor recognized.
    172                         for (DataFlavor df : support.getTransferable()
    173                                         .getTransferDataFlavors()) {
     172                        for (DataFlavor df : support.getTransferable().getTransferDataFlavors()) {
    174173
    175174                                System.out.println(df);
     
    177176                                if (df == DataFlavor.stringFlavor) { // import as text item
    178177
    179                                         String str = (String) support.getTransferable()
    180                                                         .getTransferData(DataFlavor.stringFlavor);
     178                                        String str = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
    181179
    182180                                        if (str != null && str.length() > 0) {
     
    185183                                        }
    186184
    187                                         // Usually Windows and MAC enviroments
    188                                 } else if (df == DataFlavor.javaFileListFlavor
    189                                                 || df.getSubType().equals("x-java-file-list")) { // Windows
    190                                         // has
    191                                         // other
    192                                         // random
    193                                         // types...
    194 
    195                                         List<? extends File> files = (List<? extends File>) support
    196                                                         .getTransferable().getTransferData(
    197                                                                         DataFlavor.javaFileListFlavor);
     185                                // Usually Windows and MAC enviroments
     186                                // Windows has other random types...
     187                                } else if (df == DataFlavor.javaFileListFlavor || df.getSubType().equals("x-java-file-list")) {
     188
     189                                        List<? extends File> files = (List<? extends File>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
    198190
    199191                                        importFileList(files, expediteeDropPoint);
     
    201193                                        return true;
    202194
    203                                         // Usually GNOME and KDE enviroments
     195                                // Usually GNOME and KDE enviroments
    204196                                } else if (df.equals(_URIListDataflavorString)) {
    205197
    206                                         String data = (String) support.getTransferable()
    207                                                         .getTransferData(_URIListDataflavorString);
     198                                        String data = (String) support.getTransferable().getTransferData(_URIListDataflavorString);
    208199
    209200                                        List<File> files = textURIListToFileList(data);
     
    215206                                } else if (df.equals(_URIListDataflavorCharArray)) {
    216207
    217                                         char[] data = (char[]) support.getTransferable()
    218                                                         .getTransferData(_URIListDataflavorCharArray);
     208                                        char[] data = (char[]) support.getTransferable().getTransferData(_URIListDataflavorCharArray);
    219209
    220210                                        String uriString = new String(data);
     
    229219
    230220                } catch (UnsupportedFlavorException e) {
    231                         MessageBay
    232                                         .displayMessage("Drag and drop for that type of data is not supported");
     221                        MessageBay.displayMessage("Drag and drop for that type of data is not supported");
    233222                } catch (IOException e) {
    234223                        e.printStackTrace();
     
    246235         *
    247236         * @param expediteeDropPoint
    248          *            The location in the current ecpeditee frame of where to drop
     237         *            The location in the current expeditee frame of where to drop
    249238         *            the text item.
    250239         */
    251         public static Text importString(String text, Point expediteeDropPoint) {
    252 
     240        public static Text importString(String text, Point expediteeDropPoint)
     241        {
    253242                assert (DisplayIO.getCurrentFrame() != null);
    254243                assert (text != null && text.length() > 0);
    255244
    256                 Text importedTextItem = new Text(DisplayIO.getCurrentFrame()
    257                                 .getNextItemID(), text);
     245                Text importedTextItem = new Text(DisplayIO.getCurrentFrame().getNextItemID(), text);
    258246                importedTextItem.setPosition(expediteeDropPoint);
    259247
     
    264252        }
    265253
    266         public void importFileList(List<? extends File> files,
    267                         Point expediteeDropPoint) throws IOException {
    268 
    269                 Point currentPoint = expediteeDropPoint.getLocation();
    270 
    271                 for (File fileToImport : files) { // import files one by one
     254        public void importFileList(List<? extends File> files, Point expediteeDropPoint) throws IOException
     255        {
     256                Point currentPoint = new Point(expediteeDropPoint);
     257
     258                 // import files one by one
     259                for (File fileToImport : files) {
    272260
    273261                        Item lastItem = importFile(fileToImport, currentPoint);
     
    296284         * @throws IOException
    297285         */
    298         public Item importFile(File f, Point expediteeDropPoint) throws IOException {
     286        public Item importFile(File f, Point expediteeDropPoint) throws IOException
     287        {
    299288                assert (f != null);
    300289
     
    302291                // importing routines...
    303292                Item lastCreatedItem;
    304                 if (null == (lastCreatedItem = performFileImport(_customFileImporters,
    305                                 f, expediteeDropPoint))) {
    306 
     293                if (null == (lastCreatedItem = performFileImport(_customFileImporters, f, expediteeDropPoint))) {
    307294                        // Standard file importing
    308                         lastCreatedItem = performFileImport(_standardFileImporters, f,
    309                                         expediteeDropPoint);
    310 
    311                 }
     295                        lastCreatedItem = performFileImport(_standardFileImporters, f, expediteeDropPoint);
     296                }
     297               
    312298                return lastCreatedItem;
    313299        }
    314300
    315         private Item performFileImport(List<FileImporter> importers, File f,
    316                         Point expediteeDropPoint) throws IOException {
    317 
     301        private Item performFileImport(List<FileImporter> importers, File f, Point expediteeDropPoint) throws IOException
     302        {
    318303                for (FileImporter fi : importers) {
    319304                        Item lastCreated = fi.importFile(f, expediteeDropPoint);
    320                         if (lastCreated != null)
    321                                 return lastCreated;
     305                       
     306                        if (lastCreated != null) return lastCreated;
    322307                }
    323308
     
    326311
    327312        /**
     313         * Converts a string formatted as a list of URIs into a list of Files.
     314         *
    328315         * Code adopted from SUN - java BUG ID 4899516 workaround for KDE/GNOME
    329316         * Desktops
     
    334321         * @return The list of FILES in the uriListString. Never null.
    335322         */
    336         private List<File> textURIListToFileList(String uriListString) {
    337 
     323        private List<File> textURIListToFileList(String uriListString)
     324        {
    338325                List<File> fileList = new LinkedList<File>();
    339326
    340                 for (StringTokenizer st = new StringTokenizer(uriListString, "\r\n"); st
    341                                 .hasMoreTokens();) {
     327                for (StringTokenizer st = new StringTokenizer(uriListString, "\r\n"); st.hasMoreTokens();) {
    342328
    343329                        String s = st.nextToken();
Note: See TracChangeset for help on using the changeset viewer.