more work on roar plugin

git-svn-id: http://svn.igniterealtime.org/svn/repos/spark/trunk@12364 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Wolf Posdorfer
2011-05-11 12:43:02 +00:00
committed by wolf.posdorfer
parent 2451200c34
commit a1cfce2fd9
6 changed files with 226 additions and 137 deletions

View File

@ -22,6 +22,11 @@ package org.jivesoftware.spark.roar;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.plugin.Plugin;
/**
* The Main Class of the Roar Plugin
* @author wolf.posdorfer
*
*/
public class Roar implements Plugin {
private RoarMessageListener _listener;
@ -29,7 +34,6 @@ public class Roar implements Plugin {
@Override
public void initialize() {
RoarPreference pref = new RoarPreference();
SparkManager.getPreferenceManager().addPreference(pref);

View File

@ -19,7 +19,6 @@
*/
package org.jivesoftware.spark.roar;
import java.awt.Dimension;
import java.awt.Toolkit;
@ -33,16 +32,18 @@ import org.jivesoftware.spark.ui.GlobalMessageListener;
/**
* Message Listener
*
* @author wolf.posdorfer
*
*
*/
public class RoarMessageListener implements GlobalMessageListener{
public class RoarMessageListener implements GlobalMessageListener {
private int _lastusedXpos;
private int _lastusedYpos;
private Dimension _screensize;
private int _amount;
private final int WIDTH = 300;
private final int HEIGHT = 80;
@ -51,42 +52,46 @@ public class RoarMessageListener implements GlobalMessageListener{
_lastusedXpos = _screensize.width - 5;
_lastusedYpos = 5;
_amount = 0;
}
@Override
public void messageReceived(ChatRoom room, Message message) {
RoarProperties props = new RoarProperties();
if (props.getShowingPopups()
&& (_amount < props.getMaximumPopups() || props.getMaximumPopups() == 0)) {
ImageIcon icon = SparkRes.getImageIcon(SparkRes.SPARK_IMAGE_32x32);
ImageIcon icon = SparkRes.getImageIcon(SparkRes.SPARK_IMAGE_32x32);
String nickname = StringUtils.parseName(message.getFrom());
RoarPanel.popupWindow(this, icon, nickname, message.getBody(), _lastusedXpos, _lastusedYpos);
_lastusedYpos += HEIGHT+5;
String nickname = StringUtils.parseName(message.getFrom());
if(_lastusedYpos>= _screensize.height-90)
{
_lastusedXpos -= WIDTH+5;
_lastusedYpos = 5;
RoarPanel.popupWindow(this, icon, nickname, message.getBody(),
_lastusedXpos, _lastusedYpos, props.getBackgroundColor(),
props.getDuration());
++_amount;
_lastusedYpos += HEIGHT + 5;
if (_lastusedYpos >= _screensize.height - 90) {
_lastusedXpos -= WIDTH + 5;
_lastusedYpos = 5;
}
}
}
public void closingRoarPanel(int x, int y)
{
if(_lastusedYpos>(y-5))
{
_lastusedYpos=y-5;
public void closingRoarPanel(int x, int y) {
if (_lastusedYpos > (y - 5)) {
_lastusedYpos = y - 5;
}
if(_lastusedXpos<(x+5))
{
_lastusedXpos=x+WIDTH+5;
if (_lastusedXpos < (x + 5)) {
_lastusedXpos = x + WIDTH + 5;
}
--_amount;
}
@Override

View File

@ -24,15 +24,11 @@ import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
@ -40,66 +36,27 @@ import javax.swing.JWindow;
import com.sun.awt.AWTUtilities;
public class RoarPanel {
private static ImageIcon xicon = new ImageIcon(
"C:\\Dokumente und Einstellungen\\wolf.posdorfer\\Desktop\\eclipse\\spark\\src\\resources\\images\\close_dark.png");
private static ImageIcon icon = new ImageIcon(
"C:\\Dokumente und Einstellungen\\wolf.posdorfer\\Desktop\\eclipse\\spark\\src\\resources\\images\\spark-32x32.png");
private static String _text = "laaaaaaaaaaaaaaaaaaaaaaaaang laaaaaaaaaaaaaaaang\nlaaaaaaaaaaaaaaaang\nlaaaaaaaaaaaaaaaang";
private static int _width = 300;
private static int _height = 80;
public static void main(String[] args) throws InterruptedException {
int x = Toolkit.getDefaultToolkit().getScreenSize().width;
RoarMessageListener rml = new RoarMessageListener();
popupWindow(rml, icon, "head1", _text, x, 0);
Thread.sleep(500);
popupWindow(rml, icon, "head2", _text, x, 5 + 80);
Thread.sleep(500);
popupWindow(rml, icon, "head3", _text, x, 5 + 80 + 5 + 80);
Thread.sleep(500);
popupWindow(rml, icon, "head4", _text, x, 5 + 80 + 5 + 80 + 5 + 80);
Thread.sleep(500);
popupWindow(rml, icon, "head5", _text, x, 5 + 80 + 5 + 80 + 5 + 80 + 5
+ 80);
}
private static JWindow createWindow(Icon icon, String head, String body,
int posx, int posy) {
int posx, int posy, Color backgroundcolor, Color headerColor, Color messageColor) {
final JWindow window = new JWindow();
JPanel windowpanel = new JPanel(new GridBagLayout());
windowpanel.setBackground(Color.BLACK);
windowpanel.setBackground(backgroundcolor);
AWTUtilities.setWindowShape(window, new RoundRectangle2D.Float(0, 0,
_width, _height, 20, 20));
AWTUtilities.setWindowOpaque(window, true);
JLabel close = new JLabel(xicon);
close.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
window.dispose();
}
});
JLabel text = new JLabel("<HTML>" + body + "</HTML>");
text.setForeground(Color.WHITE);
text.setForeground(messageColor);
JLabel header = new JLabel(head);
header.setForeground(Color.RED);
header.setForeground(headerColor);
header.setFont(new Font(header.getFont().getName(), Font.BOLD, header
.getFont().getSize() + 2));
@ -120,21 +77,10 @@ public class RoarPanel {
{
AWTUtilities.setWindowOpacity(window, 0.9f);
window.setVisible(true);
//
// for(float i=3; i<10;i++)
// {
// AWTUtilities.setWindowOpacity(window, i/10.f);
// try {
// Thread.sleep(70);
// } catch (InterruptedException e) {
// }
// }
}
public static void popupWindow(final RoarMessageListener owner, Icon icon, String head, String text, int x, int y) {
final JWindow window = createWindow(icon, head, text, x, y);
public static void popupWindow(final RoarMessageListener owner, Icon icon, String head, String text, int x, int y, Color backgroundcolor, int duration) {
final JWindow window = createWindow(icon, head, text, x, y,backgroundcolor, Color.RED, Color.WHITE);
fadein(window);
@ -149,7 +95,7 @@ public class RoarPanel {
};
Timer t = new Timer();
t.schedule(closeTimer, 3000);
t.schedule(closeTimer, duration);
}
}

View File

@ -37,9 +37,12 @@ public class RoarPreference implements Preference {
private RoarPreferencePanel _prefPanel;
private RoarProperties _props;
public RoarPreference() {
_props = new RoarProperties();
try {
if (EventQueue.isDispatchThread()) {
_prefPanel = new RoarPreferencePanel();
@ -90,15 +93,19 @@ public class RoarPreference implements Preference {
@Override
public void load() {
_prefPanel.setBackgroundColor(_props.getBackgroundColor());
_prefPanel.setShowingPopups(_props.getShowingPopups());
_prefPanel.setDuration(_props.getDuration());
_prefPanel.setAmount(_props.getMaximumPopups());
}
@Override
public void commit() {
RoarProperties props = new RoarProperties();
props.setBackgroundColor(_prefPanel.getBackgroundColor());
props.save();
_props.setDuration(_prefPanel.getDuration());
_props.setShowingPopups(_prefPanel.getShowingPopups());
_props.setBackgroundColor(_prefPanel.getBackgroundColor());
_props.setMaximumPopups(_prefPanel.getAmount());
_props.save();
}
@ -114,7 +121,7 @@ public class RoarPreference implements Preference {
@Override
public Object getData() {
return null;
return _props;
}
@Override

View File

@ -21,10 +21,15 @@ package org.jivesoftware.spark.roar;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
@ -43,38 +48,106 @@ public class RoarPreferencePanel extends JPanel {
private Image _backgroundimage;
private JTextField _color;
private JTextField _duration;
private JTextField _amount;
private JCheckBox _checkbox;
public RoarPreferencePanel() {
JPanel contents = new JPanel();
contents.setLayout(new GridBagLayout());
contents.setBackground(new Color(0,0,0,0));
this.setLayout(new VerticalFlowLayout());
contents.setBorder(BorderFactory.createTitledBorder("Roar Settings"));
add(contents);
ClassLoader cl = getClass().getClassLoader();
_backgroundimage = new ImageIcon(cl.getResource("background.png"))
.getImage();
_color = new JTextField();
_duration = new JTextField();
_amount = new JTextField();
_checkbox = new JCheckBox("Popups enabled");
_checkbox.setBackground(new Color(0,0,0,0));
RoarProperties props = new RoarProperties();
Insets in = new Insets(5,5,5,5);
String farbe = props.getProperty(RoarProperties.BACKGROUNDCOLOR);
contents.add(new JLabel("Background color:"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, in, 0, 0));
contents.add(_color, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, in, 0, 0));
contents.add(new JLabel("Duration in ms:"), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, in, 0, 0));
contents.add(_duration, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, in, 0, 0));
contents.add(new JLabel("Maximum Popups on Screen (0=infinity):"), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, in, 0, 0));
contents.add(_amount, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, in, 0, 0));
contents.add(_checkbox, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, in, 0, 0));
_color = new JTextField(farbe);
_color.setSize(200, 20);
JButton button = new JButton("activate");
this.add(button);
this.add(_color);
}
public void paintComponent(Graphics g) {
g.drawImage(_backgroundimage, 0, 0, this.getWidth(), this.getHeight(),
this);
int imgwi = _backgroundimage.getWidth(null);
int imghe = _backgroundimage.getHeight(null);
int x = this.getSize().width;
x = (x/2)-(imgwi/2) < 0 ? 0 : (x/2)-(imgwi/2) ;
int y = this.getSize().height;
y = (y/2) -(imghe/2)< 0 ? 0 : y/2-(imghe/2) ;
g.drawImage(_backgroundimage, x, y, this);
}
public Color getBackgroundColor() {
try {
String[] arr = _color.getText().split(",");
return RoarProperties.convertString(_color.getText());
}
public void setBackgroundColor(Color c)
{
_color.setText(RoarProperties.convertColor(c));
}
public boolean getShowingPopups() {
return _checkbox.isSelected();
}
return new Color(Integer.parseInt(arr[0]),
Integer.parseInt(arr[1]), Integer.parseInt(arr[2]));
public void setShowingPopups(boolean pop) {
_checkbox.setSelected(pop);
}
public int getDuration() {
try {
return Integer.parseInt(_duration.getText());
} catch (Exception e) {
return Color.BLACK;
return 3000;
}
}
public void setDuration(int duration)
{
_duration.setText(""+duration);
}
public void setAmount(int am)
{
_amount.setText(""+am);
}
public int getAmount()
{
return Integer.parseInt(_amount.getText());
}
}

View File

@ -27,12 +27,19 @@ import java.io.IOException;
import java.util.Properties;
import org.jivesoftware.Spark;
/**
* RoarProperties file stuff
* @author wolf.posdorfer
*
*/
public class RoarProperties {
private Properties props;
private File configFile;
public static final String BACKGROUNDCOLOR = "backgroundcolor";
public static final String DURATION = "duration";
public static final String ACTIVE = "active";
public static final String AMOUNT = "amount";
public RoarProperties() {
this.props = new Properties();
@ -48,24 +55,25 @@ public class RoarProperties {
private File getConfigFile() {
if (configFile == null)
configFile = new File(Spark.getSparkUserHome(), "roar.properties");
return configFile;
}
public void save() {
try {
props.store(new FileOutputStream(getConfigFile()), "");
props.store(new FileOutputStream(getConfigFile()),
"Storing ROAR properties");
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean showingPopups() {
return getBoolean("popups", true);
public boolean getShowingPopups() {
return getBoolean(ACTIVE, true);
}
public void setShowingPopups(boolean popups) {
setBoolean("popups", popups);
setBoolean(ACTIVE, popups);
}
public Color getBackgroundColor() {
@ -76,6 +84,28 @@ public class RoarProperties {
setColor(BACKGROUNDCOLOR, c);
}
public int getDuration() {
return getInt(DURATION) == 0 ? 3000 : getInt(DURATION);
}
public void setDuration(int dur) {
setInt(DURATION, dur);
}
public int getMaximumPopups() {
return getInt(AMOUNT);
}
public void setMaximumPopups(int amount) {
setInt(AMOUNT, amount);
}
// ===============================================================================
// ===============================================================================
// ===============================================================================
private boolean getBoolean(String property, boolean defaultValue) {
return Boolean.parseBoolean(props.getProperty(property,
Boolean.toString(defaultValue)));
@ -85,29 +115,53 @@ public class RoarProperties {
props.setProperty(property, Boolean.toString(value));
}
private int getInt(String property) {
return Integer.parseInt(props.getProperty(property, "0"));
}
private void setInt(String property, int integer) {
props.setProperty(property, "" + integer);
}
private void setColor(String property, Color color) {
String c = color.getRed() + "," + color.getGreen() + ","
+ color.getBlue();
props.setProperty(property, c);
props.setProperty(property, convertColor(color));
}
private Color getColor(String property) {
try {
String c = props.getProperty(property);
String[] arr = c.split(",");
return new Color(Integer.parseInt(arr[0]),
Integer.parseInt(arr[1]), Integer.parseInt(arr[2]));
} catch (Exception e) {
setColor(property,Color.BLACK);
save();
return Color.BLACK;
}
return convertString(props.getProperty(property));
}
public String getProperty(String property)
{
public String getProperty(String property) {
return props.getProperty(property);
}
/**
* Converts a {@link String} matching xxx,xxx,xxx to a {@link Color}<br>
* where xxx is a number from 0 to 255
*
* @param s
* @return
*/
public static Color convertString(String s) {
try {
String[] arr = s.split(",");
return new Color(Integer.parseInt(arr[0]),
Integer.parseInt(arr[1]), Integer.parseInt(arr[2]));
} catch (Exception e) {
return Color.black;
}
}
/**
* Converts a {@link Color} to a {@link String} in this format:<br>
* <b>xxx,xxx,xxx</b> <br>
* where xxx is a number from 0 to 255
*
* @param color
* @return
*/
public static String convertColor(Color color) {
return color.getRed() + "," + color.getGreen() + "," + color.getBlue();
}
}