Results 1 to 16 of 16
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,066
    Rep Power
    8

    Default new approach copying with flavors

    I have been working very hard on this stuff and I have some questions,
    though the rtf copy in both ways is working, finally?

    1) why is there a beep when starting up?
    2) why are there no line breaks (only when using the first option)?
    3) when using the first button the complete string will appear correctly on the clipboard and
    when pasting the (rtf) red color is present on my mac!
    4) why is the "isDataFlavorSupported" method never called?
    5) IMPORTANT: why I can not have a selection when using the second button
    (the text always appears completely)?

    ps the paste action can not be on the same text panel, use a second text application instead!


    Java Code:
    import java.awt.*;
    import java.awt.datatransfer.*;
    import java.awt.event.*;
    import java.io.*;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.text.rtf.RTFEditorKit;
    
    /**
     * @author willem
     */
    public final class CopyPasteTest {
        private JTextPane textpane;
        private static final DataFlavor rtfFlavor = 
                new DataFlavor("text/rtf", "Rich Text Format");
        private static final DataFlavor flavors[] = {rtfFlavor};
        private static final String RTF_STRING = "{\\rtf1\\ansi\\deff0\r\n{\\colortbl;\\"
                            + "red0\\green0\\blue0;\\red255\\green0\\blue0;}"
                            + "\r\nThis line is the default color\\line\r\n\\cf2"
                            + "\r\nThis line is red\\line\r\n\\cf1\r\n"
                            + "This line is the default color\r\n}\r\n}";
        private Clipboard cb;
        private RTFEditorKit rtfEditor;
        
    
        public CopyPasteTest(JFrame frame) {
            //create the textpane
            textpane = new JTextPane();
            rtfEditor = new RTFEditorKit();
            textpane.setEditorKit(rtfEditor);
            textpane.setText(RTF_STRING);
            
            //Create clip board object
            cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    
            JPanel ButtonPanel = new JPanel();
            JButton button = new JButton("FIRST TRY");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e){
                    emptyClipboard();
                    Object data[] = {new ByteArrayInputStream(RTF_STRING.getBytes())};
                    Transferable p = new DataTransferClass(data,flavors);
                    cb.setContents(p, null);
                }
            });
            ButtonPanel.add(button);
    
            button = new JButton("SECOND TRY");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e){
                   emptyClipboard();
                   StyledDocument styledDocument = textpane.getStyledDocument();
                   int selectionStart = textpane.getSelectionStart();
                   int selectionEnd = textpane.getSelectionEnd();
                   ByteArrayOutputStream out = new ByteArrayOutputStream();
                   try {
                        rtfEditor.write(out, styledDocument, selectionStart, selectionEnd);
                   } catch (IOException ex) {
                        Logger.getLogger(CopyPasteTest.class.getName()).log(Level.SEVERE, null, ex);
                   } catch (BadLocationException ex) {
                        Logger.getLogger(CopyPasteTest.class.getName()).log(Level.SEVERE, null, ex);
                   }
                   String rtfString = out.toString();
                   System.out.println("\nTest + " + rtfString);
                   Object data[] = {new ByteArrayInputStream(rtfString.getBytes())};
                   Transferable p = new DataTransferClass(data, flavors);
                   cb.setContents(p, null);
                }
            });
            ButtonPanel.add(button);
         
            Container contentPane = frame.getContentPane();
            contentPane.add(textpane, BorderLayout.CENTER);
            contentPane.add(ButtonPanel, BorderLayout.SOUTH);
        }
    
        public static void main(String[] args){
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Copytest");
            frame.setBounds(100, 30, 600, 400);
            new CopyPasteTest(frame);
            frame.setVisible(true);
        }
    
        static class DataTransferClass implements Transferable{
            private Object datatrans[];
            private DataFlavor flavorstrans[];
    
            DataTransferClass(Object data[], DataFlavor flavors[]) {
                datatrans = data;
                flavorstrans = flavors;
            }
    
            // the Transferable interface methods:
            public DataFlavor[] getTransferDataFlavors() {
                System.out.println("getTransferDataFlavors() " + flavorstrans);
                return flavorstrans;
            }
    
    
            public boolean isDataFlavorSupported (DataFlavor df){
                System.out.println("isDataFlavorSupported");
                if(df.getMimeType().contains("text/rtf") ||
                                        df.getMimeType().contains("text/plain")){
                    return true;
                }
                else{
                    return false;
                }
            }
    
            public Object getTransferData(DataFlavor flavor)
                                throws UnsupportedFlavorException, IOException {
                System.out.println("getTransferData " + flavor);
                    if ((flavor.getMimeType().contains("text/rtf"))) {
                        System.out.println("getTransferData data out");
                        return datatrans[0];
                    } else return null;
            }
        }
    
        public void emptyClipboard() {
            cb.setContents(new Transferable() {
                public DataFlavor[] getTransferDataFlavors() {
                    return new DataFlavor[0];
                }
    
                public boolean isDataFlavorSupported(DataFlavor flavor) {
                    return false;
                }
    
                public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
                        throw new UnsupportedFlavorException(flavor);
                }
            }, null);
    
        }
    }
    Last edited by willemjav; 03-29-2014 at 10:32 AM.

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,066
    Rep Power
    8

    Default Re: new approach copying with flavors

    Is there something wrong with: javax.swing.text.rtf.RTFEditorKit?
    https://bugs.openjdk.java.net/browse/JDK-4723383

    and interesting too:
    http://www.coderanch.com/t/493469/GU...les-Java-Swing
    Last edited by willemjav; 03-31-2014 at 09:18 AM.

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,707
    Rep Power
    5

    Default Re: new approach copying with flavors

    Not interesting to me personally, I've accepted the fact that Swing development has been shut down long ago and so am fully aware and accepting that there are remaining lingering problems in the more complicated facets of the framework, plus a growing problem domain in the fact that it is not actively kept up to date to function properly on modern OSes (such as Windows 8).

    Keep it in mind.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,066
    Rep Power
    8

    Default Re: new approach copying with flavors

    wow! So what to do?
    should I get into c++
    Last edited by willemjav; 03-31-2014 at 09:58 AM.

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,066
    Rep Power
    8

    Default Re: new approach copying with flavors

    Just starting to study JavaEE, what about that?

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,707
    Rep Power
    5

    Default Re: new approach copying with flavors

    What kind of reasoning is that? Some GUI toolkit of Java is put to sleep so I'll just go and switch to a more complicated legacy development platform?

    I would in stead research if there are perhaps more up to date ways of building GUIs using Java... Of course doing client side GUIs is already outdated in itself, everybody throws everything and the kitchen sink onto the web nowadays.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,066
    Rep Power
    8

    Default Re: new approach copying with flavors

    ok

  8. #8
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,066
    Rep Power
    8

    Default Re: new approach copying with flavors


  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,707
    Rep Power
    5

    Default Re: new approach copying with flavors

    What about it?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,066
    Rep Power
    8

    Default Re: new approach copying with flavors

    go into third party extentions and or corrections of the sun/ oracle stuff
    and using that material instead!

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,707
    Rep Power
    5

    Default Re: new approach copying with flavors

    Yes third party solutions are always an option - but they can be just as outdated as the technology itself and there is no guarantee of quality and support. Just posting a link to some cheap looking page and asking "what about this?" won't get you anywhere.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,066
    Rep Power
    8

    Default Re: new approach copying with flavors

    you just caused a paradigme shift in my thinking, thanks!

  13. #13
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,707
    Rep Power
    5

    Default Re: new approach copying with flavors

    Well... thank you for being so cool about it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,066
    Rep Power
    8

    Default Re: new approach copying with flavors

    I am, because I am a old cat.....

  15. #15
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,707
    Rep Power
    5

    Default Re: new approach copying with flavors

    Top cat!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,066
    Rep Power
    8

Similar Threads

  1. copying in flavors
    By willemjav in forum AWT / Swing
    Replies: 6
    Last Post: 03-28-2014, 07:49 AM
  2. how to approach sjcp?
    By Narasimhan in forum OCPJP
    Replies: 1
    Last Post: 10-21-2012, 08:52 AM
  3. Graphic Approach
    By kammce in forum Java Gaming
    Replies: 3
    Last Post: 10-09-2012, 02:01 PM
  4. How to approach this problem?
    By Zigster in forum New To Java
    Replies: 16
    Last Post: 04-27-2012, 05:53 PM
  5. Regex approach
    By karlito in forum Lucene
    Replies: 1
    Last Post: 11-04-2009, 05:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •