Example usage for javax.swing.event CaretListener CaretListener

List of usage examples for javax.swing.event CaretListener CaretListener

Introduction

In this page you can find the example usage for javax.swing.event CaretListener CaretListener.

Prototype

CaretListener

Source Link

Usage

From source file:Proiect.uploadFTP.java

public void actionFTP() {
    adressf.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent e) {
            InetAddress thisIp;//w  ww .  j  a  v  a 2  s  . c  o m
            try {
                thisIp = InetAddress.getLocalHost();
                titleFTP.setText("Connection: " + thisIp.getHostAddress() + " -> " + adressf.getText());
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            }
        }
    });

    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            saveState();
            uploadFTP.dispose();
            tree.dispose();
        }
    });

    connect.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            FTPClient client = new FTPClient();
            FileInputStream fis = null;
            String pass = String.valueOf(passf.getPassword());
            try {
                if (filename == null) {
                    status.setText("File does not exist!");
                } else {
                    // Server address
                    client.connect(adressf.getText());
                    // Login credentials
                    client.login(userf.getText(), pass);
                    if (client.isConnected()) {
                        status.setText("Succesfull transfer!");
                        // File type
                        client.setFileType(FTP.BINARY_FILE_TYPE);
                        // File location
                        File file = new File(filepath);
                        fis = new FileInputStream(file);
                        // Change the folder on the server
                        client.changeWorkingDirectory(folderf.getText());
                        // Save the file on the server
                        client.storeFile(filename, fis);
                    } else {
                        status.setText("Transfer failed!");
                    }
                }
                client.logout();
            } catch (IOException e1) {
                Encrypter.printException(e1);
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                    client.disconnect();
                } catch (IOException e1) {
                    Encrypter.printException(e1);
                }
            }
        }
    });

    browsef.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            int retval = chooserf.showOpenDialog(chooserf);
            if (retval == JFileChooser.APPROVE_OPTION) {
                status.setText("");
                filename = chooserf.getSelectedFile().getName().toString();
                filepath = chooserf.getSelectedFile().getPath();
                filenf.setText(chooserf.getSelectedFile().getName().toString());
            }
        }
    });

    adv.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            tree.setSize(220, uploadFTP.getHeight());
            tree.setLocation(uploadFTP.getX() + 405, uploadFTP.getY());
            tree.setResizable(false);
            tree.setIconImage(Toolkit.getDefaultToolkit()
                    .getImage(getClass().getClassLoader().getResource("assets/ico.png")));
            tree.setUndecorated(true);
            tree.getRootPane().setBorder(BorderFactory.createLineBorder(Encrypter.color_black, 2));
            tree.setVisible(true);
            tree.setLayout(new BorderLayout());

            JLabel labeltree = new JLabel("Server documents");
            labeltree.setOpaque(true);
            labeltree.setBackground(Encrypter.color_light);
            labeltree.setBorder(BorderFactory.createMatteBorder(8, 10, 10, 0, Encrypter.color_light));
            labeltree.setForeground(Encrypter.color_blue);
            labeltree.setFont(Encrypter.font16);

            JButton refresh = new JButton("");
            ImageIcon refresh_icon = getImageIcon("assets/icons/refresh.png");
            refresh.setIcon(refresh_icon);
            refresh.setBackground(Encrypter.color_light);
            refresh.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
            refresh.setForeground(Encrypter.color_black);
            refresh.setFont(Encrypter.font16);
            refresh.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

            final FTPClient client = new FTPClient();
            DefaultMutableTreeNode top = new DefaultMutableTreeNode(adressf.getText());
            DefaultMutableTreeNode files = null;
            DefaultMutableTreeNode leaf = null;

            final JTree tree_view = new JTree(top);
            tree_view.setForeground(Encrypter.color_black);
            tree_view.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
            tree_view.putClientProperty("JTree.lineStyle", "None");
            tree_view.setBackground(Encrypter.color_light);
            JScrollPane scrolltree = new JScrollPane(tree_view);
            scrolltree.setBackground(Encrypter.color_light);
            scrolltree.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));

            UIManager.put("Tree.textBackground", Encrypter.color_light);
            UIManager.put("Tree.selectionBackground", Encrypter.color_blue);
            UIManager.put("Tree.selectionBorderColor", Encrypter.color_blue);

            tree_view.updateUI();

            final String pass = String.valueOf(passf.getPassword());
            try {
                client.connect(adressf.getText());
                client.login(userf.getText(), pass);
                client.enterLocalPassiveMode();
                if (client.isConnected()) {
                    try {
                        FTPFile[] ftpFiles = client.listFiles();
                        for (FTPFile ftpFile : ftpFiles) {
                            files = new DefaultMutableTreeNode(ftpFile.getName());
                            top.add(files);
                            if (ftpFile.getType() == FTPFile.DIRECTORY_TYPE) {
                                FTPFile[] ftpFiles1 = client.listFiles(ftpFile.getName());
                                for (FTPFile ftpFile1 : ftpFiles1) {
                                    leaf = new DefaultMutableTreeNode(ftpFile1.getName());
                                    files.add(leaf);
                                }
                            }
                        }
                    } catch (IOException e1) {
                        Encrypter.printException(e1);
                    }
                    client.disconnect();
                } else {
                    status.setText("Failed connection!");
                }
            } catch (IOException e1) {
                Encrypter.printException(e1);
            } finally {
                try {
                    client.disconnect();
                } catch (IOException e1) {
                    Encrypter.printException(e1);
                }
            }

            tree.add(labeltree, BorderLayout.NORTH);
            tree.add(scrolltree, BorderLayout.CENTER);
            tree.add(refresh, BorderLayout.SOUTH);

            uploadFTP.addComponentListener(new ComponentListener() {

                public void componentMoved(ComponentEvent e) {
                    tree.setLocation(uploadFTP.getX() + 405, uploadFTP.getY());
                }

                public void componentShown(ComponentEvent e) {
                }

                public void componentResized(ComponentEvent e) {
                }

                public void componentHidden(ComponentEvent e) {
                }
            });

            uploadFTP.addWindowListener(new WindowListener() {
                public void windowActivated(WindowEvent e) {
                    tree.toFront();
                }

                public void windowOpened(WindowEvent e) {
                }

                public void windowIconified(WindowEvent e) {
                }

                public void windowDeiconified(WindowEvent e) {
                }

                public void windowDeactivated(WindowEvent e) {
                }

                public void windowClosing(WindowEvent e) {
                }

                public void windowClosed(WindowEvent e) {
                }
            });

            refresh.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    tree.dispose();
                    tree.setVisible(true);
                }
            });
        }
    });

}

From source file:tk.tomby.tedit.core.Buffer.java

/**
 * Creates a new Buffer object./*w ww  .  j av a2  s. c  om*/
 */
public Buffer() {
    super();

    setLayout(new BorderLayout());

    JPanel internalPanel = new JPanel();
    internalPanel.setLayout(new BorderLayout());

    editor = new ColourTextArea();

    int red = PreferenceManager.getInt("general.editor.background.red", 0);
    int green = PreferenceManager.getInt("general.editor.background.green", 0);
    int blue = PreferenceManager.getInt("general.editor.background.blue", 0);
    editor.setBackground(new Color(red, green, blue));

    red = PreferenceManager.getInt("general.editor.foreground.red", 0);
    green = PreferenceManager.getInt("general.editor.foreground.green", 0);
    blue = PreferenceManager.getInt("general.editor.foreground.blue", 0);
    editor.setForeground(new Color(red, green, blue));

    red = PreferenceManager.getInt("general.editor.selection.red", 0);
    green = PreferenceManager.getInt("general.editor.selection.green", 0);
    blue = PreferenceManager.getInt("general.editor.selection.blue", 0);
    editor.setSelectionColor(new Color(red, green, blue));

    String font = PreferenceManager.getString("general.editor.font", "Monospaced");
    int size = PreferenceManager.getInt("general.editor.fontSize", 12);
    editor.setFont(new Font(font, Font.PLAIN, size));

    editor.setEditable(true);
    editor.setDragEnabled(true);
    editor.setEditorKit(EditorKitManager.createEditorKit(getExtension(DEFAULT_FILE_NAME)));

    InputMap map = editor.getInputMap(JComponent.WHEN_FOCUSED);

    for (InputMap imap = map; imap != null; imap = imap.getParent()) {
        imap.remove(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK, false));
        imap.remove(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK, false));
        imap.remove(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK, false));
        imap.remove(KeyStroke.getKeyStroke('A', InputEvent.CTRL_MASK, false));
    }

    editor.setInputMap(JComponent.WHEN_FOCUSED, map);

    editor.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent evt) {
            MessageManager.sendMessage(new BufferMessage(evt.getSource(), BufferMessage.CARET_EVENT));
        }
    });

    internalPanel.add(BorderLayout.CENTER, editor);

    if (PreferenceManager.getBoolean("general.editor.lineNumbers", false)) {
        lines = new LineNumbering();
        lines.setPreferredSize(new Dimension(50, 0));
        lines.setFont(new Font(font, Font.PLAIN, size));
        lines.setFocusable(false);
        lines.setDocument(editor.getDocument());

        internalPanel.add(BorderLayout.WEST, lines);
    }

    JScrollPane scroll = new JScrollPane(internalPanel);
    scroll.getVerticalScrollBar().setUnitIncrement(10);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.getViewport().setScrollMode(JViewport.BLIT_SCROLL_MODE);

    add(BorderLayout.CENTER, scroll);

    editor.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                WorkspaceManager.getPopupMenu().show(evt.getComponent(), evt.getX(), evt.getY());
            }
        }

        public void mouseReleased(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                WorkspaceManager.getPopupMenu().show(evt.getComponent(), evt.getX(), evt.getY());
            }
        }
    });

    undo = new UndoManager();

    undoableListener = new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());

            MessageManager.sendMessage(new BufferMessage(evt.getSource(), BufferMessage.UNDOABLE_EDIT_EVENT));

            if (!modifiedState) {
                setModifiedState(true);
            }
        }
    };

    editor.getDocument().addUndoableEditListener(undoableListener);

    MessageManager.addMessageListener(MessageManager.PREFERENCE_GROUP_NAME, this);

    fileName = DEFAULT_FILE_NAME;
}