srss.core.TweetDialog.java Source code

Java tutorial

Introduction

Here is the source code for srss.core.TweetDialog.java

Source

package srss.core;

import java.awt.Component;
import java.awt.Container;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;

import twitter4j.StatusUpdate;
import twitter4j.TwitterException;

/**[Module TweetDialog.java]
 Copyright(c) 2015 mrtska.starring
 This software is released under the MIT License.
 http://opensource.org/licenses/mit-license.php
 Created on: 2015/03/23
*/
public class TweetDialog extends JDialog implements KeyListener, MouseListener, MouseMotionListener {

    private JButton button;
    private JLabel pathl;

    private final Point pp = new Point();

    public TweetDialog(final File screenshot) throws IOException {

        super(SRSS.getFrame(), "");

        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setSize(600, 400);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setLayout(null);

        ImageIcon image = new ImageIcon(ImageIO.read(screenshot));

        this.pathl = new JLabel(image);
        JScrollPane imagePane = new JScrollPane(pathl);
        this.pathl.addKeyListener(this);
        this.pathl.addMouseListener(this);
        this.pathl.addMouseMotionListener(this);

        imagePane.setBounds(10, 10, 580, 300);
        this.getContentPane().add(imagePane);

        final JTextArea tweet = new JTextArea(1, 40);
        tweet.addKeyListener(this);

        JScrollPane scroll = new JScrollPane(tweet);

        scroll.setBounds(10, 320, 480, 40);
        this.getContentPane().add(scroll);

        //
        this.button = new JButton("");
        this.button.setBounds(500, 320, 80, 40);
        this.button.addKeyListener(this);
        this.button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                StatusUpdate status = new StatusUpdate(tweet.getText());
                status.setMedia(screenshot);
                try {
                    SRSS.getTwitterInstance().updateStatus(status);
                    TweetDialog.this.dispose();
                } catch (TwitterException e1) {
                    e1.printStackTrace();
                }

            }
        });
        this.getContentPane().add(button);
        //------
        this.setVisible(true);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {

        if (e.isControlDown()) {

            if (e.getKeyCode() == KeyEvent.VK_ENTER) {

                this.button.doClick();
            }
        }

        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {

            this.dispose();
            SRSS.getFrame().setVisible(true);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {

        pp.setLocation(e.getPoint());
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        Component c = e.getComponent();
        Container p = SwingUtilities.getUnwrappedParent(c);
        if (p instanceof JViewport) {
            JViewport vport = (JViewport) p;
            Point cp = SwingUtilities.convertPoint(c, e.getPoint(), vport);
            Point vp = vport.getViewPosition();
            vp.translate(pp.x - cp.x, pp.y - cp.y);
            ((JComponent) c).scrollRectToVisible(new Rectangle(vp, vport.getSize()));
            pp.setLocation(cp);
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

}