package org.chartsy.main.managers;
import com.harrison.lee.twitpic4j.TwitPic;
import com.harrison.lee.twitpic4j.TwitPicResponse;
import com.harrison.lee.twitpic4j.exception.TwitPicException;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.io.IOException;
import java.util.prefs.Preferences;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;
import org.chartsy.chatsy.chat.util.log.NotifyUtil;
import org.chartsy.main.ChartFrame;
import org.chartsy.main.utils.ImageExporter;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.util.NbPreferences;
import org.openide.windows.WindowManager;
/**
*
* @author Viorel
*/
public final class TwitterManager
{
private static TwitterManager instance;
private static String API_KEY = "bee72a07687b449aedf8c9f9d93d17fb";
private String user = "";
private String pass = "";
private TwitPic twitPic;
private PhotoCaptionPanel captionPanel;
public static TwitterManager getDefault()
{
if (instance == null) {
instance = new TwitterManager();
}
return instance;
}
private TwitterManager()
{
Preferences pref = NbPreferences.forModule(FacebookManager.class);
user = pref.get("twitter_user", "");
pass = pref.get("twitter_pass", "");
}
public void publishChart(final ChartFrame chartFrame, final String caption)
{
if (user.isEmpty() || pass.isEmpty()) {
NotifyUtil.warn("Acount Data", "You need to specify and account username and password.", false);
return;
}
final ProgressHandle handle = ProgressHandleFactory.createHandle("Uploading chart image to wall ...");
final Runnable task = new Runnable()
{
@Override
public void run()
{
handle.start();
handle.switchToIndeterminate();
if (twitPic == null) {
twitPic = new TwitPic(user, pass);
}
TwitPicResponse response = null;
try {
File file = ImageExporter.getDefault().getExportedFile(chartFrame, getClass());
if (caption != null && !caption.isEmpty()) {
String text = caption;
text += "\n\n";
text += "Image generated by @chartsyorg. http://www.chartsy.org.";
response = twitPic.uploadAndPost(file, text);
} else {
String text = "Image generated by @chartsyorg. http://www.chartsy.org.";
response = twitPic.uploadAndPost(file, text);
}
} catch (TwitPicException tpex) {
NotifyUtil.error("Upload Error", tpex.getMessage(), tpex, false);
} catch (IOException ioex) {
NotifyUtil.error("Upload Error", ioex.getMessage(), ioex, false);
}
if (response != null) {
NotifyUtil.info("Upload Done", "File uploaded successfully.", false);
}
handle.finish();
}
};
WindowManager.getDefault().invokeWhenUIReady(task);
}
public PhotoCaptionPanel getPhotoCaptionPanel()
{
if (captionPanel == null)
{
captionPanel = new PhotoCaptionPanel();
}
captionPanel.reset();
return captionPanel;
}
public String getPhotoCaption()
{
if (captionPanel == null)
{
captionPanel = new PhotoCaptionPanel();
return "";
}
return captionPanel.getCaption();
}
public class PhotoCaptionPanel extends JPanel
{
private JLabel captionLbl;
private JTextPane captionTxt;
private JScrollPane captionSp;
public PhotoCaptionPanel()
{
super(new GridBagLayout());
setBorder(new TitledBorder("Photo Caption:"));
GridBagConstraints lastConstraints = new GridBagConstraints();
lastConstraints.fill = GridBagConstraints.HORIZONTAL;
lastConstraints.anchor = GridBagConstraints.NORTHWEST;
lastConstraints.weightx = 1.0;
lastConstraints.gridwidth = GridBagConstraints.REMAINDER;
lastConstraints.insets = new Insets(5, 5, 5, 5);
GridBagConstraints labelConstraints = (GridBagConstraints) lastConstraints.clone();
labelConstraints.weightx = 0.0;
labelConstraints.gridwidth = 1;
captionLbl = new JLabel("Caption:");
add(captionLbl, labelConstraints);
captionTxt = new JTextPane();
captionTxt.setPreferredSize(new Dimension(300, 200));
captionSp = new JScrollPane(captionTxt, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(captionSp, lastConstraints);
}
public void reset()
{
captionTxt.setText("");
}
public String getCaption()
{
return captionTxt.getText();
}
}
}