Java by API/java.awt.datatransfer/DataFlavor

Материал из Java эксперт
Перейти к: навигация, поиск

DataFlavor: getHumanPresentableName()

   <source lang="java">

/* Mime type: text/plain; class=java.io.InputStream; charset=ASCII Primary type: text Subtype: plain Parameter: ASCII Name: Plain ASCII text Representation class: class java.io.InputStream

  • /

import java.awt.datatransfer.DataFlavor; public class MainClass {

 public static void main(String[] args) {
   DataFlavor df = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text");
   System.out.println("Mime type: " + df.getMimeType());
   System.out.println("Primary type: " + df.getPrimaryType());
   System.out.println("Subtype: " + df.getSubType());
   System.out.println("Parameter: " + df.getParameter("charset"));
   System.out.println("Name: " + df.getHumanPresentableName());
   String s = df.getRepresentationClass().toString();
   System.out.println("Representation class: " + s + "\n");
 }

}


      </source>
   
  
 
  



DataFlavor: getMimeType()

   <source lang="java">

/* Mime type: text/plain; class=java.io.InputStream; charset=ASCII Primary type: text Subtype: plain Parameter: ASCII Name: Plain ASCII text Representation class: class java.io.InputStream

  • /

import java.awt.datatransfer.DataFlavor; public class MainClass {

 public static void main(String[] args) {
   DataFlavor df = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text");
   System.out.println("Mime type: " + df.getMimeType());
   System.out.println("Primary type: " + df.getPrimaryType());
   System.out.println("Subtype: " + df.getSubType());
   System.out.println("Parameter: " + df.getParameter("charset"));
   System.out.println("Name: " + df.getHumanPresentableName());
   String s = df.getRepresentationClass().toString();
   System.out.println("Representation class: " + s + "\n");
 }

}


      </source>
   
  
 
  



DataFlavor: getParameter(String paramName)

   <source lang="java">

/* Mime type: text/plain; class=java.io.InputStream; charset=ASCII Primary type: text Subtype: plain Parameter: ASCII Name: Plain ASCII text Representation class: class java.io.InputStream

  • /

import java.awt.datatransfer.DataFlavor; public class MainClass {

 public static void main(String[] args) {
   DataFlavor df = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text");
   System.out.println("Mime type: " + df.getMimeType());
   System.out.println("Primary type: " + df.getPrimaryType());
   System.out.println("Subtype: " + df.getSubType());
   System.out.println("Parameter: " + df.getParameter("charset"));
   System.out.println("Name: " + df.getHumanPresentableName());
   String s = df.getRepresentationClass().toString();
   System.out.println("Representation class: " + s + "\n");
 }

}


      </source>
   
  
 
  



DataFlavor: getPrimaryType()

   <source lang="java">

/* Mime type: text/plain; class=java.io.InputStream; charset=ASCII Primary type: text Subtype: plain Parameter: ASCII Name: Plain ASCII text Representation class: class java.io.InputStream

  • /

import java.awt.datatransfer.DataFlavor; public class MainClass {

 public static void main(String[] args) {
   DataFlavor df = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text");
   System.out.println("Mime type: " + df.getMimeType());
   System.out.println("Primary type: " + df.getPrimaryType());
   System.out.println("Subtype: " + df.getSubType());
   System.out.println("Parameter: " + df.getParameter("charset"));
   System.out.println("Name: " + df.getHumanPresentableName());
   String s = df.getRepresentationClass().toString();
   System.out.println("Representation class: " + s + "\n");
 }

}


      </source>
   
  
 
  



DataFlavor: getRepresentationClass()

   <source lang="java">

/* Mime type: text/plain; class=java.io.InputStream; charset=ASCII Primary type: text Subtype: plain Parameter: ASCII Name: Plain ASCII text Representation class: class java.io.InputStream

  • /

import java.awt.datatransfer.DataFlavor; public class MainClass {

 public static void main(String[] args) {
   DataFlavor df = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text");
   System.out.println("Mime type: " + df.getMimeType());
   System.out.println("Primary type: " + df.getPrimaryType());
   System.out.println("Subtype: " + df.getSubType());
   System.out.println("Parameter: " + df.getParameter("charset"));
   System.out.println("Name: " + df.getHumanPresentableName());
   String s = df.getRepresentationClass().toString();
   System.out.println("Representation class: " + s + "\n");
 }

}


      </source>
   
  
 
  



DataFlavor: getSubType()

   <source lang="java">

/* Mime type: text/plain; class=java.io.InputStream; charset=ASCII Primary type: text Subtype: plain Parameter: ASCII Name: Plain ASCII text Representation class: class java.io.InputStream

  • /

import java.awt.datatransfer.DataFlavor; public class MainClass {

 public static void main(String[] args) {
   DataFlavor df = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text");
   System.out.println("Mime type: " + df.getMimeType());
   System.out.println("Primary type: " + df.getPrimaryType());
   System.out.println("Subtype: " + df.getSubType());
   System.out.println("Parameter: " + df.getParameter("charset"));
   System.out.println("Name: " + df.getHumanPresentableName());
   String s = df.getRepresentationClass().toString();
   System.out.println("Representation class: " + s + "\n");
 }

}


      </source>
   
  
 
  



DataFlavor: isMimeTypeEqual(DataFlavor dataFlavor)

   <source lang="java">

/* Mime type: application/x-java-serialized-object; class=java.awt.Button Primary type: application Subtype: x-java-serialized-object Name: AWT Button Representation class: class java.awt.Button df1 equals df2: false

  • /

import java.awt.datatransfer.DataFlavor; public class MainClass {

 public static void main(String[] args) {
   DataFlavor df1 = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text");
   DataFlavor df2 = new DataFlavor(java.awt.Button.class, "AWT Button");
   System.out.println("Mime type: " + df2.getMimeType());
   System.out.println("Primary type: " + df2.getPrimaryType());
   System.out.println("Subtype: " + df2.getSubType());
   System.out.println("Name: " + df2.getHumanPresentableName());
   String s = df2.getRepresentationClass().toString();
   System.out.println("Representation class: " + s + "\n");
   System.out.println("df1 equals df2: " + df1.isMimeTypeEqual(df2));
 }

}

      </source>
   
  
 
  



DataFlavor.javaFileListFlavor

   <source lang="java">

/* Mime type: application/x-java-serialized-object; class=java.lang.String Primary type: application Subtype: x-java-serialized-object Name: Unicode String Representation class: class java.lang.String Mime type: application/x-java-file-list; class=java.util.List Primary type: application Subtype: x-java-file-list Name: application/x-java-file-list Representation class: interface java.util.List

  • /

import java.awt.datatransfer.DataFlavor; public class MainClass {

 public static void main(String[] args) {
   DataFlavor df = DataFlavor.stringFlavor;
   System.out.println ("Mime type: " + df.getMimeType ());
   System.out.println ("Primary type: " + df.getPrimaryType ());
   System.out.println ("Subtype: " + df.getSubType ());
   System.out.println ("Name: " + df.getHumanPresentableName ());
   String s = df.getRepresentationClass ().toString ();
   System.out.println ("Representation class: " + s + "\n");
   df = DataFlavor.javaFileListFlavor;
   System.out.println ("Mime type: " + df.getMimeType ());
   System.out.println ("Primary type: " + df.getPrimaryType ());
   System.out.println ("Subtype: " + df.getSubType ());
   System.out.println ("Name: " + df.getHumanPresentableName ());
   s = df.getRepresentationClass ().toString ();
   System.out.println ("Representation class: " + s);
 }

}

      </source>
   
  
 
  



DataFlavor.stringFlavor

   <source lang="java">

import java.awt.BorderLayout; import java.awt.Color; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.dnd.DnDConstants; import java.awt.dnd.DragGestureEvent; import java.awt.dnd.DragGestureListener; import java.awt.dnd.DragSource; import java.awt.dnd.DragSourceContext; import java.awt.dnd.DragSourceDragEvent; import java.awt.dnd.DragSourceDropEvent; import java.awt.dnd.DragSourceEvent; import java.awt.dnd.DragSourceListener; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.dnd.InvalidDnDOperationException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class MainClass extends JFrame implements ActionListener, DropTargetListener {

 DragLabel source = new DragLabel("Drag and drop me to the following JButton", JLabel.CENTER);
 JButton target = new JButton();
 MainClass(String title) {
   super(title);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   source.setForeground(Color.red);
   getContentPane().add(source, BorderLayout.NORTH);
   target.addActionListener(this);
   getContentPane().add(target, BorderLayout.SOUTH);
   new DropTarget(target, DnDConstants.ACTION_COPY_OR_MOVE, this);
   setSize(205, 100);
   setVisible(true);
 }
 public void actionPerformed(ActionEvent e) {
   JButton b = (JButton) e.getSource();
   b.setText("");
   source.setText("Drag and drop me to the following JButton");
 }
 public void dragEnter(DropTargetDragEvent e) {
   System.out.println("Entering drop target #1");
 }
 public void dragExit(DropTargetEvent e) {
   System.out.println("Exiting drop target #1");
 }
 public void dragOver(DropTargetDragEvent e) {
   System.out.println("Dragging over drop target #1");
 }
 public void drop(DropTargetDropEvent e) {
   System.out.println("Dropping");
   try {
     Transferable t = e.getTransferable();
     if (e.isDataFlavorSupported(DataFlavor.stringFlavor)) {
       e.acceptDrop(e.getDropAction());
       String s;
       s = (String) t.getTransferData(DataFlavor.stringFlavor);
       target.setText(s);
       e.dropComplete(true);
     } else
       e.rejectDrop();
   } catch (java.io.IOException e2) {
   } catch (UnsupportedFlavorException e2) {
   }
 }
 public void dropActionChanged(DropTargetDragEvent e) {
   System.out.println("Drop action changed #1");
 }
 public static void main(String[] args) {
   new MainClass("Drag and Drop Demo");
 }

} class DragLabel extends JLabel implements DragGestureListener, DragSourceListener {

 private DragSource ds = DragSource.getDefaultDragSource();
 public DragLabel(String s, int alignment) {
   super(s, alignment);
   int action = DnDConstants.ACTION_COPY_OR_MOVE;
   ds.createDefaultDragGestureRecognizer(this, action, this);
 }
 public void dragGestureRecognized(DragGestureEvent e) {
   try {
     Transferable t = new StringSelection(getText());
     e.startDrag(DragSource.DefaultCopyNoDrop, t, this);
   } catch (InvalidDnDOperationException e2) {
     System.out.println(e2);
   }
 }
 public void dragDropEnd(DragSourceDropEvent e) {
   System.out.println("Drag and drop end");
   if (e.getDropSuccess() == false) {
     System.out.println("unsuccessful");
     return;
   }
   int action = e.getDropAction();
   if ((action & DnDConstants.ACTION_MOVE) != 0)
     setText("");
 }
 public void dragEnter(DragSourceDragEvent e) {
   System.out.println("Entering drop target #2");
   DragSourceContext ctx = e.getDragSourceContext();
   int action = e.getDropAction();
   if ((action & DnDConstants.ACTION_COPY) != 0)
     ctx.setCursor(DragSource.DefaultCopyDrop);
   else
     ctx.setCursor(DragSource.DefaultCopyNoDrop);
 }
 public void dragExit(DragSourceEvent e) {
   System.out.println("Exiting drop target #2");
 }
 public void dragOver(DragSourceDragEvent e) {
   System.out.println("Dragging over drop target #2");
 }
 public void dropActionChanged(DragSourceDragEvent e) {
   System.out.println("Drop action changed #2");
 }

}

      </source>
   
  
 
  



new DataFlavor(Class representationClass, String humanPresentableName)

   <source lang="java">

/* Mime type: application/x-java-serialized-object; class=java.awt.Button Primary type: application Subtype: x-java-serialized-object Name: AWT Button Representation class: class java.awt.Button df1 equals df2: false

  • /

import java.awt.datatransfer.DataFlavor; public class MainClass {

 public static void main(String[] args) {
   DataFlavor df1 = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text");
   DataFlavor df2 = new DataFlavor(java.awt.Button.class, "AWT Button");
   System.out.println("Mime type: " + df2.getMimeType());
   System.out.println("Primary type: " + df2.getPrimaryType());
   System.out.println("Subtype: " + df2.getSubType());
   System.out.println("Name: " + df2.getHumanPresentableName());
   String s = df2.getRepresentationClass().toString();
   System.out.println("Representation class: " + s + "\n");
   System.out.println("df1 equals df2: " + df1.isMimeTypeEqual(df2));
 }

}

      </source>
   
  
 
  



new DataFlavor(String mimeType, String humanPresentableName)

   <source lang="java">

/* Mime type: application/x-java-serialized-object; class=java.awt.Button Primary type: application Subtype: x-java-serialized-object Name: AWT Button Representation class: class java.awt.Button df1 equals df2: false

  • /

import java.awt.datatransfer.DataFlavor; public class MainClass {

 public static void main(String[] args) {
   DataFlavor df1 = new DataFlavor("text/plain; charset=ASCII", "Plain ASCII text");
   DataFlavor df2 = new DataFlavor(java.awt.Button.class, "AWT Button");
   System.out.println("Mime type: " + df2.getMimeType());
   System.out.println("Primary type: " + df2.getPrimaryType());
   System.out.println("Subtype: " + df2.getSubType());
   System.out.println("Name: " + df2.getHumanPresentableName());
   String s = df2.getRepresentationClass().toString();
   System.out.println("Representation class: " + s + "\n");
   System.out.println("df1 equals df2: " + df1.isMimeTypeEqual(df2));
 }

}

      </source>