Package kameleon.gui.model

Source Code of kameleon.gui.model.FileDragAndDropHandler

/*
* Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*      * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*      * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*      * The names of its contributors may not be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package kameleon.gui.model;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.net.URL;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.TransferHandler;

import kameleon.exception.KameleonException;
import kameleon.gui.exception.UnknownKeyException;
import kameleon.gui.language.SwitchLanguage;
import kameleon.gui.model.InformationMessage.State;
import kameleon.gui.util.FileConstants;
import kameleon.gui.util.LanguageConstants;

/**
* Handler for the drag and drop actions on the graphical interface.
*
* <p>When one or multiple files are dropped above the interface, the handler
* retrieves the file path of each file and passes it to the model of the
* interface.
*
* @author    Fromentin Xavier
* @version    1.0
*/
public class FileDragAndDropHandler extends TransferHandler
implements FileConstants, LanguageConstants {

  /**
   * Needed to serialize this class.
   *
   * @see    java.io.Serializable
   */
  private static final long serialVersionUID = -7847616613217340808L ;

  /**
   * Model of the graphical interface.
   */
  private Model model ;

  /**
   * Sole constructor.
   *
   * @param   model
   *       model of the graphical interface
   */
  public FileDragAndDropHandler(Model model) {
    this.model = model ;
  }// FileDragAndDropHandler(Model)

  /**
   * {@inheritDoc}
   *
   * @return  {@code true} if {@code transferFlavors} contains either
   *       {@link DataFlavor#javaFileListFlavor} or {@link DataFlavor#stringFlavor},
   *       {@code false} otherwise
   */
  @Override
  public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
    for (DataFlavor flavor : transferFlavors) {
      if (flavor.equals(DataFlavor.javaFileListFlavor)
          || flavor.equals(DataFlavor.stringFlavor)) {
        return true ;
      }// if
    }// for
    // Didn't find any that match, so:
    return false ;
  }// canImport(JComponent, DataFlavor[])

  /**
   * {@inheritDoc}
   *
   * @see javax.swing.TransferHandler#importData(javax.swing.JComponent,
   *      java.awt.datatransfer.Transferable)
   */
  @Override
  public boolean importData(JComponent comp, Transferable t) {
    for (DataFlavor flavor : t.getTransferDataFlavors()) {
      File currentFile = null ;
      try {
        if (flavor.equals(DataFlavor.javaFileListFlavor)) {
          Object data = t.getTransferData(DataFlavor.javaFileListFlavor) ;
          if (data instanceof List<?>) {
            for(File file : (List<File>) data) {
              currentFile = file ;
              this.model.addFile(file) ;
            }// for
          }// if
        } else if (flavor.equals(DataFlavor.stringFlavor)) {
          String fileOrURL = (String) t.getTransferData(flavor) ;
          URL url = new URL(fileOrURL) ;
          currentFile = new File(url.toURI()) ;
          this.model.addFile(currentFile) ;
        }// if

        return true ;
      } catch (Exception ex) {
        /*=== Possible exceptions ===
         * - IOException
         * - UnsupportedFlavorException
         * - URISyntaxException
         * - MalformedURLException
         */
        SwitchLanguage sl = SwitchLanguage.getInstance() ;
        InformationMessage imsg = null ;
        try {
          imsg = new InformationMessage(
              State.INFORMATION,
              ADD_FILE_ERROR,
              currentFile.getAbsolutePath()) ;
          this.model.addMessage(imsg) ;
          this.model.displayDebugInformation(
              new KameleonException(imsg.getMessage(), ex)) ;
        } catch (UnknownKeyException e) {
          //TODO Review
          /* This should not happen. */
        } catch (NullPointerException npe) {
          //TODO Review
          /* This should not happen ? */
        }// try
      }// try
    }// for

    return false ;
  }// importData(JComponent, Transferable)

}// class FileDragAndDropHandler
TOP

Related Classes of kameleon.gui.model.FileDragAndDropHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.