コード例 #1
0
 // Remove the old text if the action is a MOVE.
 // However, we do not allow dropping on top of the selected text, so in that case do nothing.
 protected void exportDone(JComponent c, Transferable data, int action) {
   if (c != textComponent) { // ###
     System.out.println("*** exportDone(): c=" + c);
   }
   System.out.println(
       ">>> exportDone(): action="
           + action
           + ", MOVE="
           + MOVE
           + ", shouldRemove="
           + shouldRemove);
   if (shouldRemove && (action == MOVE)) {
     if ((p0 != null) && (p1 != null) && (p0.getOffset() != p1.getOffset())) {
       try {
         textComponent.getDocument().remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
       } catch (BadLocationException e) {
         System.out.println("*** exportDone(): Can't remove text from source.");
       }
     }
   }
   source = null;
 }
コード例 #2
0
 public boolean importData(JComponent c, Transferable t) {
   if (c != textComponent) { // ### never happens, we dont share transfer handlers
     System.out.println("*** importData(): c=" + c);
   }
   try {
     DataFlavor[] flavors = t.getTransferDataFlavors();
     boolean hasStringFlavor = t.isDataFlavorSupported(DataFlavor.stringFlavor);
     boolean hasImageFlavor = t.isDataFlavorSupported(DataFlavor.imageFlavor);
     boolean hasFilelistFlavor = t.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
     //
     System.out.println(">>> import data to text panel (" + flavors.length + " flavors)");
     // ### for (int i = 0; i < flavors.length; i++) {
     // ###	System.out.println(flavors[i]);
     // ###}
     System.out.println("  >   string flavor supported: " + hasStringFlavor);
     System.out.println("  >    image flavor supported: " + hasImageFlavor);
     System.out.println("  > filelist flavor supported: " + hasFilelistFlavor);
     //
     // We do not allow dropping on top of the selected text
     if ((source == textComponent)
         && (textComponent.getCaretPosition() >= p0.getOffset())
         && (textComponent.getCaretPosition() <= p1.getOffset())) {
       shouldRemove = false;
       System.out.println(
           ">>> dropping on top of the selected text is not allowed -- import canceled");
       return true;
     }
     //
     if (hasStringFlavor) {
       String data = (String) t.getTransferData(DataFlavor.stringFlavor);
       int pos = textComponent.getCaretPosition();
       if (DeepaMehtaUtils.isImage(data)) {
         HTMLEditorKit kit = (HTMLEditorKit) ((JEditorPane) textComponent).getEditorKit();
         HTMLDocument doc = (HTMLDocument) textComponent.getDocument();
         String html = "<img src=\"" + data + "\"></img>";
         kit.insertHTML(doc, pos, html, 0, 0, HTML.Tag.IMG); // ### <img> not XML conform
         // ### doc.insertBeforeStart(doc.getParagraphElement(pos), html);
         System.out.println(">>> IMG tag inserted: \"" + html + "\"");
       } else {
         textComponent.getDocument().insertString(pos, data, null);
         System.out.println(">>> regular text inserted: \"" + data + "\"");
       }
     } else if (hasFilelistFlavor) {
       java.util.List files = (java.util.List) t.getTransferData(DataFlavor.javaFileListFlavor);
       System.out.println("    " + files.size() + " files:");
       for (int i = 0; i < files.size(); i++) {
         File file = (File) files.get(i);
         String filename = file.getName();
         System.out.println("    " + file);
         if (DeepaMehtaUtils.isHTML(filename)) {
           String html = DeepaMehtaUtils.readFile(file);
           textComponent.setText(html); // ### replace instead insert
           textComponent.setCaretPosition(0);
           // ### ((JEditorPane) textComponent).setPage("file://" + file);	// ### replace instead
           // insert
           // ### setDirty("dropping HTML file");
           System.out.println(">>> HTML inserted (read from file)");
           break; // ### max one file is inserted
         } else if (DeepaMehtaUtils.isImage(filename)) {
           HTMLEditorKit kit = (HTMLEditorKit) ((JEditorPane) textComponent).getEditorKit();
           HTMLDocument doc = (HTMLDocument) textComponent.getDocument();
           int pos = textComponent.getCaretPosition();
           String imagefile = file.getPath().replace('\\', '/'); // ###
           String html = "<img src=\"" + imagefile + "\"></img>";
           kit.insertHTML(doc, pos, html, 0, 0, HTML.Tag.IMG); // ### <img> not XML conform
           // ### doc.insertBeforeStart(doc.getParagraphElement(pos), html);
           System.out.println(">>> IMG tag inserted: \"" + html + "\"");
         } else {
           System.out.println(
               "### importData(): only implemented for HTML files -- import canceled");
         }
       }
     } else {
       System.out.println("*** importData(): no supported flavor " + c);
     }
     return true;
   } catch (UnsupportedFlavorException ufe) {
     System.out.println("*** while dropping to text panel: " + ufe);
   } catch (BadLocationException ble) {
     System.out.println("*** while dropping to text panel: " + ble);
   } catch (IOException ioe) {
     System.out.println("*** while dropping to text panel: " + ioe);
   }
   //
   return super.importData(c, t);
 }