Example usage for java.awt.dnd DropTarget DropTarget

List of usage examples for java.awt.dnd DropTarget DropTarget

Introduction

In this page you can find the example usage for java.awt.dnd DropTarget DropTarget.

Prototype

public DropTarget(Component c, DropTargetListener dtl) throws HeadlessException 

Source Link

Document

Creates a DropTarget given the Component to associate itself with, and the DropTargetListener to handle event processing.

Usage

From source file:xtrememp.PlaylistManager.java

private void initComponents() {
    JToolBar toolBar = new JToolBar();
    toolBar.setFloatable(false);//w ww .  jav a  2 s . c  o m
    openPlaylistButton = new JButton(Utilities.DOCUMENT_OPEN_ICON);
    openPlaylistButton.setToolTipText(tr("MainFrame.PlaylistManager.OpenPlaylist"));
    openPlaylistButton.addActionListener(this);
    toolBar.add(openPlaylistButton);
    savePlaylistButton = new JButton(Utilities.DOCUMENT_SAVE_ICON);
    savePlaylistButton.setToolTipText(tr("MainFrame.PlaylistManager.SavePlaylist"));
    savePlaylistButton.addActionListener(this);
    toolBar.add(savePlaylistButton);
    toolBar.addSeparator();
    addToPlaylistButton = new JButton(Utilities.LIST_ADD_ICON);
    addToPlaylistButton.setToolTipText(tr("MainFrame.PlaylistManager.AddToPlaylist"));
    addToPlaylistButton.addActionListener(this);
    toolBar.add(addToPlaylistButton);
    remFromPlaylistButton = new JButton(Utilities.LIST_REMOVE_ICON);
    remFromPlaylistButton.setToolTipText(tr("MainFrame.PlaylistManager.RemoveFromPlaylist"));
    remFromPlaylistButton.addActionListener(this);
    remFromPlaylistButton.setEnabled(false);
    toolBar.add(remFromPlaylistButton);
    clearPlaylistButton = new JButton(Utilities.EDIT_CLEAR_ICON);
    clearPlaylistButton.setToolTipText(tr("MainFrame.PlaylistManager.ClearPlaylist"));
    clearPlaylistButton.addActionListener(this);
    clearPlaylistButton.setEnabled(false);
    toolBar.add(clearPlaylistButton);
    toolBar.addSeparator();
    moveUpButton = new JButton(Utilities.GO_UP_ICON);
    moveUpButton.setToolTipText(tr("MainFrame.PlaylistManager.MoveUp"));
    moveUpButton.addActionListener(this);
    moveUpButton.setEnabled(false);
    toolBar.add(moveUpButton);
    moveDownButton = new JButton(Utilities.GO_DOWN_ICON);
    moveDownButton.setToolTipText(tr("MainFrame.PlaylistManager.MoveDown"));
    moveDownButton.addActionListener(this);
    moveDownButton.setEnabled(false);
    toolBar.add(moveDownButton);
    toolBar.addSeparator();
    mediaInfoButton = new JButton(Utilities.MEDIA_INFO_ICON);
    mediaInfoButton.setToolTipText(tr("MainFrame.PlaylistManager.MediaInfo"));
    mediaInfoButton.addActionListener(this);
    mediaInfoButton.setEnabled(false);
    toolBar.add(mediaInfoButton);
    toolBar.add(Box.createHorizontalGlue());
    searchTextField = new SearchTextField(15);
    searchTextField.setMaximumSize(new Dimension(120, searchTextField.getPreferredSize().height));
    searchTextField.getTextField().getDocument().addDocumentListener(new SearchFilterListener());
    toolBar.add(searchTextField);
    toolBar.add(Box.createHorizontalStrut(6));
    this.add(toolBar, BorderLayout.NORTH);

    playlistTable = new JTable(playlistTableModel, playlistTableColumnModel);
    playlistTable.setDefaultRenderer(String.class, new PlaylistCellRenderer());
    playlistTable.setActionMap(null);

    playlistTable.getTableHeader().addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent ev) {
            if (SwingUtilities.isRightMouseButton(ev)
                    || (MouseInfo.getNumberOfButtons() == 1 && ev.isControlDown())) {
                playlistTableColumnModel.getPopupMenu().show(playlistTable.getTableHeader(), ev.getX(),
                        ev.getY());
                return;
            }

            int clickedColumn = playlistTableColumnModel.getColumnIndexAtX(ev.getX());
            PlaylistTableColumn playlistColumn = playlistTableColumnModel.getColumn(clickedColumn);
            playlistTableColumnModel.resetAll(playlistColumn.getModelIndex());
            playlistColumn.setSortOrderUp(!playlistColumn.isSortOrderUp());
            playlistTableModel.sort(playlistColumn.getComparator());

            colorizeRow();
        }
    });
    playlistTable.setFillsViewportHeight(true);
    playlistTable.setShowGrid(false);
    playlistTable.setRowSelectionAllowed(true);
    playlistTable.setColumnSelectionAllowed(false);
    playlistTable.setDragEnabled(false);
    playlistTable.setFont(playlistTable.getFont().deriveFont(Font.BOLD));
    playlistTable.setIntercellSpacing(new Dimension(0, 0));
    playlistTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    playlistTable.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent ev) {
            int selectedRow = playlistTable.rowAtPoint(ev.getPoint());
            if (SwingUtilities.isLeftMouseButton(ev) && ev.getClickCount() == 2) {
                if (selectedRow != -1) {
                    playlist.setCursorPosition(selectedRow);
                    controlListener.acOpenAndPlay();
                }
            }
        }
    });
    playlistTable.getSelectionModel().addListSelectionListener(this);
    playlistTable.getColumnModel().getSelectionModel().addListSelectionListener(this);
    playlistTable.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            // View Media Info
            if (e.getKeyCode() == KeyEvent.VK_I && e.getModifiers() == KeyEvent.CTRL_MASK) {
                viewMediaInfo();
            } // Select all
            else if (e.getKeyCode() == KeyEvent.VK_A && e.getModifiers() == KeyEvent.CTRL_MASK) {
                playlistTable.selectAll();
            } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                // Move selected track(s) up
                if (e.getModifiers() == KeyEvent.ALT_MASK) {
                    moveUp();
                } // Select previous track
                else {
                    if (playlistTable.getSelectedRow() > 0) {
                        int previousRowIndex = playlistTable.getSelectedRow() - 1;
                        playlistTable.clearSelection();
                        playlistTable.addRowSelectionInterval(previousRowIndex, previousRowIndex);
                        makeRowVisible(previousRowIndex);
                    }
                }
            } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                // Move selected track(s) down
                if (e.getModifiers() == KeyEvent.ALT_MASK) {
                    moveDown();
                } // Select next track
                else {
                    if (playlistTable.getSelectedRow() < playlistTable.getRowCount() - 1) {
                        int nextRowIndex = playlistTable.getSelectedRow() + 1;
                        playlistTable.clearSelection();
                        playlistTable.addRowSelectionInterval(nextRowIndex, nextRowIndex);
                        makeRowVisible(nextRowIndex);
                    }
                }
            } // Play selected track
            else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                int selectedRow = playlistTable.getSelectedRow();
                if (selectedRow != -1) {
                    playlist.setCursorPosition(selectedRow);
                    controlListener.acOpenAndPlay();
                }
            } // Add new tracks
            else if (e.getKeyCode() == KeyEvent.VK_INSERT) {
                addFilesDialog(false);
            } // Delete selected tracks
            else if (e.getKeyCode() == KeyEvent.VK_DELETE) {
                remove();
            }
        }
    });
    XtremeMP.getInstance().getMainFrame().setDropTarget(new DropTarget(playlistTable, this));
    JScrollPane ptScrollPane = new JScrollPane(playlistTable);
    ptScrollPane.setActionMap(null);
    this.add(ptScrollPane, BorderLayout.CENTER);
}

From source file:zsk.JFCMainClient.java

/**
 * @param pane// ww w .  ja va 2 s .  c o m
 * @param downloadDir 
 */
public void addComponentsToPane(final Container pane, String downloadDir) {
    this.panel = new JPanel();

    this.panel.setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.anchor = GridBagConstraints.WEST;

    JFCMainClient.dlm = new DefaultListModel<String>();
    this.urllist = new JList<String>(JFCMainClient.dlm);
    // TODO maybe we add a button to remove added URLs from list?
    //      this.userlist.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
    this.urllist.setFocusable(false);
    this.textarea = new JTextArea(2, 2);
    this.textarea.setEditable(true);
    this.textarea.setFocusable(false);

    JScrollPane leftscrollpane = new JScrollPane(this.urllist);
    JScrollPane rightscrollpane = new JScrollPane(this.textarea);
    this.middlepane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftscrollpane, rightscrollpane);
    this.middlepane.setOneTouchExpandable(true);
    this.middlepane.setDividerLocation(150);

    Dimension minimumSize = new Dimension(25, 25);
    leftscrollpane.setMinimumSize(minimumSize);
    rightscrollpane.setMinimumSize(minimumSize);

    this.directorybutton = new JButton("", createImageIcon("images/open.png", ""));
    gbc.gridx = 0;
    gbc.gridy = 0;
    this.directorybutton.addActionListener(this);
    this.panel.add(this.directorybutton, gbc);

    this.saveconfigcheckbox = new JCheckBox(isgerman() ? "Konfig. speichern" : "Save config");
    this.saveconfigcheckbox.setSelected(false);

    this.saveconfigcheckbox.addItemListener(this);
    this.panel.add(this.saveconfigcheckbox);

    this.saveconfigcheckbox.setEnabled(false);

    // TODO check if initial download directory exists
    // assume that at least the users homedir exists
    //if (System.getProperty("user.home").equals("/home/knoedel")) shomedir = "/home/knoedel/YouTube Downloads/";
    debugoutput("user.home: ".concat(System.getProperty("user.home")).concat("  shomedir: ".concat(shomedir)));
    debugoutput("os.name: ".concat(System.getProperty("os.name")));
    debugoutput("os.arch: ".concat(System.getProperty("os.arch")));
    debugoutput("os.version: ".concat(System.getProperty("os.version")));
    debugoutput("Locale.getDefault: ".concat(Locale.getDefault().toString()));

    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.directorytextfield = new JTextField(downloadDir, 20 + (JFCMainClient.getbDEBUG() ? 48 : 0));
    //this.directorytextfield = new JTextField( shomedir, 20+(JFCMainClient.getbDEBUG()?48:0) );
    this.directorytextfield.setEnabled(false);
    this.directorytextfield.setFocusable(true);
    this.directorytextfield.addActionListener(this);
    this.panel.add(this.directorytextfield, gbc);

    JLabel dirhint = new JLabel(isgerman() ? "Speichern im Ordner:" : "Download to folder:");

    gbc.gridx = 0;
    gbc.gridy = 1;
    this.panel.add(dirhint, gbc);

    debugoutput(String.format("heigth x width: %d x %d", Toolkit.getDefaultToolkit().getScreenSize().width,
            Toolkit.getDefaultToolkit().getScreenSize().height));

    this.middlepane.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize().width / 3,
            Toolkit.getDefaultToolkit().getScreenSize().height / 4 + (JFCMainClient.getbDEBUG() ? 200 : 0)));

    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weighty = 2;
    gbc.weightx = 2;
    gbc.gridwidth = 2;
    this.panel.add(this.middlepane, gbc);

    // radio buttons for resolution to download
    JFCMainClient.frame.hdbutton = new JRadioButton("HD");
    JFCMainClient.frame.hdbutton.setActionCommand("hd");
    JFCMainClient.frame.hdbutton.addActionListener(this);
    JFCMainClient.frame.hdbutton.setToolTipText("1080p/720p");
    JFCMainClient.frame.stdbutton = new JRadioButton("Std");
    JFCMainClient.frame.stdbutton.setActionCommand("std");
    JFCMainClient.frame.stdbutton.addActionListener(this);
    JFCMainClient.frame.stdbutton.setToolTipText("480p/360p");
    JFCMainClient.frame.ldbutton = new JRadioButton("LD");
    JFCMainClient.frame.ldbutton.setActionCommand("ld");
    JFCMainClient.frame.ldbutton.addActionListener(this);
    JFCMainClient.frame.ldbutton.setToolTipText("< 360p");

    JFCMainClient.frame.stdbutton.setSelected(true);
    JFCMainClient.frame.hdbutton.setEnabled(true);
    JFCMainClient.frame.ldbutton.setEnabled(true);

    ButtonGroup bgroup = new ButtonGroup();
    bgroup.add(JFCMainClient.frame.hdbutton);
    bgroup.add(JFCMainClient.frame.stdbutton);
    bgroup.add(JFCMainClient.frame.ldbutton);

    JPanel radiopanel = new JPanel(new GridLayout(1, 0));
    radiopanel.add(JFCMainClient.frame.hdbutton);
    radiopanel.add(JFCMainClient.frame.stdbutton);
    radiopanel.add(JFCMainClient.frame.ldbutton);

    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.gridheight = 0;
    gbc.gridwidth = 0;
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.NORTHEAST;
    this.panel.add(radiopanel, gbc);

    // radio buttons for video format to download
    JFCMainClient.frame.mpgbutton = new JRadioButton("MPEG");
    JFCMainClient.frame.mpgbutton.setActionCommand("mpg");
    JFCMainClient.frame.mpgbutton.addActionListener(this);
    JFCMainClient.frame.mpgbutton.setToolTipText("Codec: H.264 MPEG-4");
    JFCMainClient.frame.webmbutton = new JRadioButton("WEBM");
    JFCMainClient.frame.webmbutton.setActionCommand("webm");
    JFCMainClient.frame.webmbutton.addActionListener(this);
    JFCMainClient.frame.webmbutton.setToolTipText("Codec: Google/On2's VP8 or Googles WebM");
    JFCMainClient.frame.flvbutton = new JRadioButton("FLV");
    JFCMainClient.frame.flvbutton.setActionCommand("flv");
    JFCMainClient.frame.flvbutton.addActionListener(this);
    JFCMainClient.frame.flvbutton.setToolTipText("Codec: Flash Video (FLV1)");

    bgroup = new ButtonGroup();
    bgroup.add(JFCMainClient.frame.mpgbutton);
    bgroup.add(JFCMainClient.frame.webmbutton);
    bgroup.add(JFCMainClient.frame.flvbutton);

    JFCMainClient.frame.mpgbutton.setSelected(true);
    JFCMainClient.frame.mpgbutton.setEnabled(true);
    JFCMainClient.frame.webmbutton.setEnabled(true);
    JFCMainClient.frame.flvbutton.setEnabled(true);

    JFCMainClient.frame.save3dcheckbox = new JCheckBox("3D");
    JFCMainClient.frame.save3dcheckbox.setToolTipText("stereoscopic video");
    JFCMainClient.frame.save3dcheckbox.setSelected(false);
    JFCMainClient.frame.save3dcheckbox.setEnabled(true);
    JFCMainClient.frame.save3dcheckbox.addItemListener(this);

    radiopanel = new JPanel(new GridLayout(1, 0));
    radiopanel.add(JFCMainClient.frame.save3dcheckbox);
    radiopanel.add(JFCMainClient.frame.mpgbutton);
    radiopanel.add(JFCMainClient.frame.webmbutton);
    radiopanel.add(JFCMainClient.frame.flvbutton);

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridheight = 0;
    gbc.gridwidth = 0;
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.NORTHEAST;
    this.panel.add(radiopanel, gbc);

    JLabel hint = new JLabel(
            isgerman() ? "eingeben, reinkopieren, reinziehen von YT-Webadressen oder YT-Videobilder:"
                    : "Type, paste or drag'n drop a YouTube video address:");

    gbc.fill = 0;
    gbc.gridwidth = 0;
    gbc.gridheight = 1;
    gbc.weightx = 0;
    gbc.weighty = 0;
    gbc.gridx = 0;
    gbc.gridy = 4;
    gbc.anchor = GridBagConstraints.WEST;
    this.panel.add(hint, gbc);

    this.textinputfield = new JTextField(20);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 5;
    gbc.gridwidth = 2;
    this.textinputfield.setEnabled(true);
    this.textinputfield.setFocusable(true);
    this.textinputfield.addActionListener(this);
    this.textinputfield.getDocument().addDocumentListener(this);
    this.panel.add(this.textinputfield, gbc);

    this.quitbutton = new JButton("", createImageIcon("images/exit.png", ""));
    gbc.gridx = 2;
    gbc.gridy = 5;
    gbc.gridwidth = 0;
    this.quitbutton.addActionListener(this);
    this.quitbutton.setActionCommand("quit");
    this.quitbutton.setToolTipText("Exit.");

    this.panel.add(this.quitbutton, gbc);

    pane.add(this.panel);
    addWindowListener(this);

    JFCMainClient.frame.setDropTarget(new DropTarget(this, this));
    JFCMainClient.frame.textarea.setTransferHandler(null); // otherwise the dropped text would be inserted

}