package com.mime.qweibo.examples;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InvalidPropertiesFormatException;
import java.util.List;
import java.util.Properties;
import javax.swing.DropMode;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.table.JTableHeader;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import com.mime.qweibo.OauthKey;
import com.mime.qweibo.QAsyncHandler;
import com.mime.qweibo.QParameter;
import com.mime.qweibo.QWeiboRequest;
public class QWeiboUIMain {
private static JFrame mFrame;
private static Container mContainer;
private static String mAppKey;
private static String mAppSecret;
private static String mTokenKey;
private static String mTokenSecret;
private static String mVerify;
private static String mError;
/** Initial page. Input your App Key and App Secret*/
private static final int OPTION_INIT = 1;
/** GUI to input your verifier (from browser) */
private static final int OPTION_VERIFY = 2;
private static final int OPTION_MAIN = 3;
private static final int OPTION_ERROR = 4;
private static String APP_FILE = "qweibo_config.properties";
private static final String KEY_APP_KEY = "app_key";
private static final String KEY_APP_SECRET = "app_secret";
private static final String KEY_TOKEN_KEY = "token_key";
private static final String KEY_TOKEN_SECRET = "token_secret";
private static Properties mConfig = new Properties();
/**
* @param args
*/
public static void main(String[] args) {
mFrame = new JFrame();
mFrame.setTitle("QWeibo SDK Samples");
mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mContainer = mFrame.getContentPane();
mContainer.setLayout(null);
initKeys();
setUpUI(OPTION_INIT);
}
private static void initKeys() {
File propFile = new File(APP_FILE);
if (propFile.exists()) {
InputStream is = null;
try {
is = new FileInputStream(propFile);
mConfig.loadFromXML(is);
mAppKey = mConfig.getProperty(KEY_APP_KEY);
mAppSecret = mConfig.getProperty(KEY_APP_SECRET);
mTokenKey = mConfig.getProperty(KEY_TOKEN_KEY);
mTokenSecret = mConfig.getProperty(KEY_TOKEN_SECRET);
} catch (InvalidPropertiesFormatException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
mAppKey = "";
mAppSecret = "";
mTokenKey = "";
mTokenSecret = "";
}
mVerify = "";
}
private static void setUpUI(int option) {
switch (option) {
case OPTION_INIT:
mFrame.setResizable(false);
mFrame.setBounds(200, 100, 480, 240);
JLabel labelAppKey = new JLabel("AppKey:");
labelAppKey.setBounds(10, 10, 80, 30);
mContainer.add(labelAppKey);
final JTextField txtAppKey = new JTextField();
txtAppKey.setBounds(90, 10, 320, 30);
txtAppKey.setText(mAppKey);
mContainer.add(txtAppKey);
JLabel labelAppSecret = new JLabel("AppSecret:");
labelAppSecret.setBounds(10, 50, 80, 30);
mContainer.add(labelAppSecret);
final JPasswordField txtAppSecret = new JPasswordField();
txtAppSecret.setBounds(90, 50, 320, 30);
txtAppSecret.setText(mAppSecret);
txtAppSecret.setEchoChar('*');
mContainer.add(txtAppSecret);
final JButton okBtn = new JButton("Continue");
okBtn.setBounds(100, 100, 300, 40);
okBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String appKey = txtAppKey.getText();
boolean changed = false;
if (!appKey.equals(mAppKey)) {
mAppKey = appKey;
changed = true;
}
String appSecret = new String(txtAppSecret.getPassword());
if (!appSecret.equals(mAppSecret)) {
mAppSecret = appSecret;
changed = true;
}
if (isNullOrEmpty(mAppKey) || isNullOrEmpty(mAppSecret)) {
return;
}
if (changed) {
mTokenKey = "";
mTokenSecret = "";
}
if (isNullOrEmpty(mTokenKey) || isNullOrEmpty(mTokenSecret)) {
String resStr = null;
QWeiboSyncApi api = new QWeiboSyncApi();
resStr = api.getRequestToken(mAppKey, mAppSecret);
if (parseToken(resStr)) {
setUpUI(OPTION_VERIFY);
// request verify code
requestVerify();
} else {
mError = "Can't parse request token string!~\nTry again later!";
setUpUI(OPTION_ERROR);
}
} else {
setUpUI(OPTION_MAIN);
}
}
});
mContainer.add(okBtn);
break;
case OPTION_VERIFY:
mFrame.setVisible(false);
mContainer.removeAll();
JLabel labelVerify = new JLabel(
"Input the oauth_verifier obtained from your broswer's address bar:");
labelVerify.setBounds(10, 20, 400, 30);
mContainer.add(labelVerify);
final JTextField txtVerify = new JTextField();
txtVerify.setBounds(10, 50, 430, 30);
txtVerify.setText(mVerify);
mContainer.add(txtVerify);
final JButton okVerifyBtn = new JButton("Continue");
okVerifyBtn.setBounds(100, 100, 300, 40);
okVerifyBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mVerify = txtVerify.getText();
String resStr = null;
QWeiboSyncApi api = new QWeiboSyncApi();
resStr = api.getAccessToken(mAppKey, mAppSecret, mTokenKey,
mTokenSecret, mVerify);
if (parseToken(resStr)) {
saveProperties();
setUpUI(OPTION_MAIN);
} else {
mError = "Can't parse access token string!~\nTry again later!";
setUpUI(OPTION_ERROR);
}
}
});
mContainer.add(okVerifyBtn);
break;
case OPTION_ERROR:
mFrame.setVisible(false);
mContainer.removeAll();
final JTextPane errorPane = new JTextPane();
errorPane.setBounds(20, 10, 400, 180);
errorPane.setText(mError);
mContainer.add(errorPane);
break;
case OPTION_MAIN:
mFrame.setVisible(false);
mContainer.removeAll();
mFrame.setSize(820, 620);
// response panel
final JTextPane response = new JTextPane();
JScrollPane jsp = new JScrollPane(response);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel panelResopnse = new JPanel();
panelResopnse.setLayout(new BorderLayout());
panelResopnse.setBorder(new javax.swing.border.TitledBorder(null,
"Response",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION,
new java.awt.Font("Tahoma", 1, 16), new java.awt.Color(0,
102, 102)));
panelResopnse.add(jsp);
panelResopnse.setBounds(400, 10, 400, 580);