Home arrow static arrow Java Programming [Archive] - How can I use Drap and Drop in Linux system?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - How can I use Drap and Drop in Linux system?
This topic has 5 replies on 1 page.

Posts:4
Registered: 6/16/04
How can I use Drap and Drop in Linux system?  
Jul 30, 2004 2:03 AM



 
I try to use DnD in a item from "explorer" in Linux into my application, but it does atually not work. The same version is work well on Windows. Below is code (3 separated files):

/* * FileAndTextTransferHandler.java is used by the 1.4 * DragFileDemo.java example. */ import java.io.*;import java.util.*;import java.awt.*;import java.awt.datatransfer.*;import java.awt.event.*;import javax.swing.*;import javax.swing.text.*; class FileAndTextTransferHandler extends TransferHandler {    private DataFlavor fileFlavor, stringFlavor;    private TabbedPaneController tpc;    private JTextArea source;    private boolean shouldRemove;    protected String newline = "\n"//Start and end position in the source text.    //We need this information when performing a MOVE    //in order to remove the dragged text from the source.    Position p0 = null, p1 = null;     FileAndTextTransferHandler(TabbedPaneController t) {       tpc = t;       fileFlavor = DataFlavor.javaFileListFlavor;       stringFlavor = DataFlavor.stringFlavor;    }     public boolean importData(JComponent c, Transferable t) {        JTextArea tc;         if (!canImport(c, t.getTransferDataFlavors())) {            return false;        }        //A real application would load the file in another        //thread in order to not block the UI.  This step        //was omitted here to simplify the code.        try {            if (hasFileFlavor(t.getTransferDataFlavors())) {                String str = null;                java.util.List files =                     (java.util.List)t.getTransferData(fileFlavor);                for (int i = 0; i < files.size(); i++) {                    File file = (File)files.get(i);                    //Tell the tabbedpane controller to add                    //a new tab with the name of this file                    //on the tab.  The text area that will                    //display the contents of the file is returned.                    tc = tpc.addTab(file.toString());                     BufferedReader in = nulltry {                        in = new BufferedReader(new FileReader(file));                         while ((str = in.readLine()) != null) {                            tc.append(str + newline);                        }                    } catch (IOException ioe) {                        System.out.println(                          "importData: Unable to read from file " +                           file.toString());                    } finally {                        if (in != null) {                            try {                                in.close();                            } catch (IOException ioe) {                                 System.out.println(                                  "importData: Unable to close file " +                                   file.toString());                            }                        }                    }                }                return true;            } else if (hasStringFlavor(t.getTransferDataFlavors())) {                tc = (JTextArea)c;                if (tc.equals(source) && (tc.getCaretPosition() >= p0.getOffset()) &&                                         (tc.getCaretPosition() <= p1.getOffset())) {                    shouldRemove = false;                    return true;                }                String str = (String)t.getTransferData(stringFlavor);                tc.replaceSelection(str);                return true;            }        } catch (UnsupportedFlavorException ufe) {            System.out.println("importData: unsupported data flavor");        } catch (IOException ieo) {            System.out.println("importData: I/O exception");        }        return false;    }     protected Transferable createTransferable(JComponent c) {        source = (JTextArea)c;        int start = source.getSelectionStart();        int end = source.getSelectionEnd();        Document doc = source.getDocument();        if (start == end) {            return null;        }        try {            p0 = doc.createPosition(start);            p1 = doc.createPosition(end);        } catch (BadLocationException e) {            System.out.println(              "Can't create position - unable to remove text from source.");        }        shouldRemove = true;        String data = source.getSelectedText();        return new StringSelection(data);    }     public int getSourceActions(JComponent c) {        return COPY_OR_MOVE;    }     //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 (shouldRemove && (action == MOVE)) {            if ((p0 != null) && (p1 != null) &&                (p0.getOffset() != p1.getOffset())) {                try {                    JTextComponent tc = (JTextComponent)c;                    tc.getDocument().remove(                       p0.getOffset(), p1.getOffset() - p0.getOffset());                } catch (BadLocationException e) {                    System.out.println("Can't remove text from source.");                }            }        }        source = null;    }     public boolean canImport(JComponent c, DataFlavor[] flavors) {        if (hasFileFlavor(flavors))   { return true; }        if (hasStringFlavor(flavors)) { return true; }        return false;    }     private boolean hasFileFlavor(DataFlavor[] flavors) {        for (int i = 0; i < flavors.length; i++) {            if (fileFlavor.equals(flavors[i])) {                return true;            }        }        return false;    }     private boolean hasStringFlavor(DataFlavor[] flavors) {        for (int i = 0; i < flavors.length; i++) {            if (stringFlavor.equals(flavors[i])) {                return true;            }        }        return false;    }} 


/* * TabbedPaneController.java is used by the 1.4  * DragFileDemo.java example. */import java.awt.event.*;import java.awt.*;import javax.swing.*; /* * Class that manages area where the contents of * files are displayed.  When no files are present, * there is a simple JTextArea instructing users * to drop a file.  As soon as a file is dropped, * a JTabbedPane is placed into the window and * each file is displayed under its own tab. * When all the files are removed, the JTabbedPane * is removed from the window and the simple * JTextArea is again displayed. */public class TabbedPaneController {    JPanel tabbedPanel = null;    JTabbedPane tabbedPane;    JPanel emptyFilePanel = null;    JTextArea emptyFileArea = null;    FileAndTextTransferHandler transferHandler;    boolean noFiles = true;    String fileSeparator;     public TabbedPaneController(JTabbedPane tb, JPanel tp) {        tabbedPane = tb;        tabbedPanel = tp;        transferHandler = new FileAndTextTransferHandler(this);        fileSeparator = System.getProperty("file.separator");        //The split method in the String class uses        //regular expressions to define the text used for        //the split.  The forward slash "\" is a special        //character and must be escaped.  Some look and feels,        //such as Microsoft Windows, use the forward slash to        //delimit the path.        if ("\\".equals(fileSeparator)) {            fileSeparator = "\\\\";        }        init();    }     public JTextArea addTab(String filename) {        if (noFiles) {            tabbedPanel.remove(emptyFilePanel);            tabbedPanel.add(tabbedPane, BorderLayout.CENTER);            noFiles = false;        }        String[] str = filename.split(fileSeparator);        return makeTextPanel(str[str.length-1], filename);    }     //Remove all tabs and their components, then put the default    //file area back.    public void clearAll() {        if (noFiles == false) {            tabbedPane.removeAll();            tabbedPanel.remove(tabbedPane);        }        init();    }     private void init() {        String defaultText =             "Select one or more files from the file chooser and drop here...";        noFiles = true;        if (emptyFilePanel == null) {            emptyFileArea = new JTextArea(20,15);            emptyFileArea.setEditable(false);            emptyFileArea.setDragEnabled(true);            emptyFileArea.setTransferHandler(transferHandler);            emptyFileArea.setMargin(new Insets(5,5,5,5));            JScrollPane fileScrollPane = new JScrollPane(emptyFileArea);            emptyFilePanel = new JPanel(new BorderLayout(), false);            emptyFilePanel.add(fileScrollPane, BorderLayout.CENTER);        }        tabbedPanel.add(emptyFilePanel, BorderLayout.CENTER);        tabbedPanel.repaint();        emptyFileArea.setText(defaultText);    }     protected JTextArea makeTextPanel(String name, String toolTip) {        JTextArea fileArea = new JTextArea(20,15);        fileArea.setDragEnabled(true);        fileArea.setTransferHandler(transferHandler);        fileArea.setMargin(new Insets(5,5,5,5));        JScrollPane fileScrollPane = new JScrollPane(fileArea);        tabbedPane.addTab(name, null, (Component)fileScrollPane, toolTip);        tabbedPane.setSelectedComponent((Component)fileScrollPane);        return fileArea;    }} 


/* * DragFileDemo.java is a 1.4 example that * requires the following file: *    FileAndTextTransferHandler.java *    TabbedPaneController.java */import java.awt.event.*;import java.awt.*;import javax.swing.*; public class DragFileDemo extends JPanel                          implements ActionListener {    JTextArea fileArea;    JFileChooser fc;    JButton clear;    TabbedPaneController tpc;     public DragFileDemo() {        super(new BorderLayout());         fc = new JFileChooser();;        fc.setMultiSelectionEnabled(true);        fc.setDragEnabled(true);        fc.setControlButtonsAreShown(false);        JPanel fcPanel = new JPanel(new BorderLayout());        fcPanel.add(fc, BorderLayout.CENTER);         clear = new JButton("Clear All");        clear.addActionListener(this);        JPanel buttonPanel = new JPanel(new BorderLayout());        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));        buttonPanel.add(clear, BorderLayout.LINE_END);         JPanel upperPanel = new JPanel(new BorderLayout());        upperPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));        upperPanel.add(fcPanel, BorderLayout.CENTER);        upperPanel.add(buttonPanel, BorderLayout.PAGE_END);         //The TabbedPaneController manages the panel that        //contains the tabbed pane.  When there are no files        //the panel contains a plain text area.  Then, as        //files are dropped onto the area, the tabbed panel         //replaces the file area.        JTabbedPane tabbedPane = new JTabbedPane();        JPanel tabPanel = new JPanel(new BorderLayout());        tabPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));        tpc = new TabbedPaneController(tabbedPane, tabPanel);         JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,            upperPanel, tabPanel);        splitPane.setDividerLocation(400);        splitPane.setPreferredSize(new Dimension(530, 650));        add(splitPane, BorderLayout.CENTER);    }     public void setDefaultButton() {        getRootPane().setDefaultButton(clear);    }     public void actionPerformed(ActionEvent e) {        if (e.getSource() == clear) {            tpc.clearAll();        }    }     /**     * Create the GUI and show it.  For thread safety,     * this method should be invoked from the     * event-dispatching thread.     */    private static void createAndShowGUI() {        //Make sure we have nice window decorations.        JFrame.setDefaultLookAndFeelDecorated(true);         //Create and set up the window.        JFrame frame = new JFrame("DragFileDemo");        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         //Create and set up the menu bar and content pane.        DragFileDemo demo = new DragFileDemo();        demo.setOpaque(true); //content panes must be opaque        frame.setContentPane(demo);         //Display the window.        frame.pack();        frame.setVisible(true);        demo.setDefaultButton();    }     public static void main(String[] args) {        //Schedule a job for the event-dispatching thread:        //creating and showing this application's GUI.        javax.swing.SwingUtilities.invokeLater(new Runnable() {            public void run() {                createAndShowGUI();            }        });    }}
 

Posts:2,909
Registered: 13.8.2003
Re: How can I use Drap and Drop in Linux system?  
Jul 30, 2004 2:10 AM (reply 1 of 5)



 
Things with linux are a bit different. If I'm not horribly mistaken, the DnD working would depend entirely on your explorer. With windows it's easy to create a universal dragon droppings system, but with the diversity of linux desktop environments and other things I assume it's a bit more difficult.

What desktop environment are you using and which explorer?
 

Posts:4
Registered: 6/16/04
Re: How can I use Drap and Drop in Linux system?  
Jul 30, 2004 2:40 AM (reply 2 of 5)



 
I'm currently using Linux Fedora system.

Thank you.
 

Posts:4
Registered: 6/16/04
Re: How can I use Drap and Drop in Linux system?  
Jul 30, 2004 2:40 AM (reply 3 of 5)



 
I'm currently using Linux Fedora system.

Thank you.
 

Posts:4,500
Registered: 17.04.98
Re: How can I use Drap and Drop in Linux system?  
Jul 30, 2004 2:49 AM (reply 4 of 5)



 
I'm currently using Linux Fedora system.
Doesn't matter. There's no standard way for D&D on Linux, no single API.
Every application has its own mechanism which may or may not be compatible with any other.
Gnome and KDE groups are doing some work to provide a common standard but as you know the Linux zealots are completely opposed to anyone telling them what to do (such as creating standards) and won't in general follow them.
 

Posts:2,909
Registered: 13.8.2003
Re: How can I use Drap and Drop in Linux system?  
Jul 30, 2004 3:07 AM (reply 5 of 5)



 
Yes, it's a good thing that windows adheres to standards, such as the life critical Drag And Drop.
 
This topic has 5 replies on 1 page.