123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.osd;
-
- import com.dmdirc.addons.ui_swing.UIUtilities;
- import com.dmdirc.interfaces.config.IdentityController;
- import com.dmdirc.util.colours.Colour;
- import com.dmdirc.ui.messages.ColourManager;
-
- import java.awt.Color;
- import java.awt.Point;
- import java.awt.Window;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.util.Timer;
- import java.util.TimerTask;
-
- import javax.swing.JDialog;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.SwingConstants;
- import javax.swing.WindowConstants;
- import javax.swing.border.LineBorder;
-
- import net.miginfocom.swing.MigLayout;
-
- /**
- * The OSD Window is an always-on-top window designed to convey information about events to the
- * user.
- */
- public class OsdWindow extends JDialog implements MouseListener, MouseMotionListener {
-
- /** A version number for this class. */
- private static final long serialVersionUID = 2;
- /** The OSD Manager that owns this window. */
- private final OsdManager osdManager;
- /** The manager to use to parse colours. */
- private final ColourManager colourManager;
- /** OSD Label. */
- private final JLabel label;
- /** OSD Panel. */
- private final JPanel panel;
- /** Starting positions of the mouse. */
- private int startX;
- private int startY;
- /** Desired position. */
- private volatile int desiredX;
- private volatile int desiredY;
- /** Is this a config instance? */
- private final boolean config;
- /** Timeout before the windows are automatically closed */
- private final Integer timeout;
-
- /**
- * Creates a new instance of OsdWindow.
- *
- * @param mainWindow The frame that parents this window.
- * @param identityController The controller to read/write settings with.
- * @param colourManager The manager to use to parse colours.
- * @param timeout Timeout period for the window. Set to -1 to use value from config
- * @param text The text to be displayed in the OSD window
- * @param config Is the window being configured (should it timeout and allow itself
- * to be moved)
- * @param x The x-axis position for the OSD Window
- * @param y The y-axis position for the OSD window
- * @param osdManager The manager that owns this OSD Window
- * @param domain This plugin's settings domain
- */
- public OsdWindow(
- final Window mainWindow,
- final IdentityController identityController,
- final OsdManager osdManager, final ColourManager colourManager,
- final int timeout, final String text, final boolean config, final int x,
- final int y, final String domain) {
- super(mainWindow, ModalityType.MODELESS);
-
- this.colourManager = colourManager;
- this.config = config;
- this.osdManager = osdManager;
-
- if (timeout < 0) {
- this.timeout = identityController.getGlobalConfiguration().getOptionInt(domain,
- "timeout", false);
- } else {
- this.timeout = timeout;
- }
-
- setFocusableWindowState(false);
- setAlwaysOnTop(true);
- setResizable(false);
- setUndecorated(true);
-
- setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
-
- desiredX = x;
- desiredY = y;
-
- setLocation(x, y);
-
- panel = new JPanel();
- panel.setBorder(new LineBorder(Color.BLACK));
- panel.setBackground(UIUtilities.convertColour(
- colourManager.getColourFromString(identityController.getGlobalConfiguration()
- .getOptionString(domain, "bgcolour"), null)));
-
- final int width = identityController.getGlobalConfiguration().getOptionInt(domain, "width");
- setContentPane(panel);
- setLayout(new MigLayout("wmin " + width + ", wmax " + width + ", ins rel, fill"));
-
- label = new JLabel(text);
- label.setForeground(UIUtilities.convertColour(
- colourManager.getColourFromString(identityController.getGlobalConfiguration()
- .getOptionString(domain, "fgcolour"), null)));
- label.setFont(label.getFont().deriveFont((float) identityController
- .getGlobalConfiguration().getOptionInt(domain, "fontSize")));
- label.setHorizontalAlignment(SwingConstants.CENTER);
- add(label, "alignx center, hmin " + label.getFont().getSize());
-
- setVisible(true);
- pack();
-
- if (config) {
- addMouseMotionListener(this);
- addMouseListener(this);
- } else {
- addMouseListener(this);
- if (this.timeout != null && this.timeout > 0) {
- new Timer("OSD Display Timer").schedule(new TimerTask() {
-
- @Override
- public void run() {
- osdManager.closeWindow(OsdWindow.this);
- }
- }, this.timeout * 1000);
- }
- }
- }
-
- @Override
- public void mouseClicked(final MouseEvent e) {
- if (!config) {
- osdManager.closeWindow(this);
- }
- }
-
- @Override
- public void mousePressed(final MouseEvent e) {
- if (config) {
- startX = e.getPoint().x;
- startY = e.getPoint().y;
- }
- }
-
- @Override
- public void mouseReleased(final MouseEvent e) {
- // Do nothing
- }
-
- @Override
- public void mouseEntered(final MouseEvent e) {
- // Do nothing
- }
-
- @Override
- public void mouseExited(final MouseEvent e) {
- // Do nothing
- }
-
- @Override
- public void mouseDragged(final MouseEvent e) {
- final Point p = e.getLocationOnScreen();
- p.translate(-1 * startX, -1 * startY);
- setLocation(p);
- }
-
- @Override
- public void mouseMoved(final MouseEvent e) {
- // Do nothing
- }
-
- /**
- * Sets the font size that this OSD uses.
- *
- * @param size The new size of the font
- */
- public void setFontSize(final int size) {
- label.setFont(label.getFont().deriveFont((float) size));
- }
-
- /**
- * Sets the background colour for this OSD.
- *
- * @param colour The background colour to use
- */
- public void setBackgroundColour(final String colour) {
- panel.setBackground(UIUtilities.convertColour(colourManager.getColourFromString(colour,
- Colour.WHITE)));
- }
-
- /**
- * Sets the foreground colour for this OSD.
- *
- * @param colour The foreground colour to use
- */
- public void setForegroundColour(final String colour) {
- label.setForeground(UIUtilities.convertColour(colourManager.getColourFromString(colour,
- Colour.WHITE)));
- }
-
- @Override
- public void setVisible(final boolean b) {
- super.setVisible(b);
-
- if (b) {
- transferFocusBackward();
- }
- }
-
- /**
- * Retrieves the desired x offset of this OSD window.
- *
- * @since 0.6.3
- * @see #setDesiredLocation(int, int)
- * @return The desired offset of this window
- */
- public int getDesiredX() {
- return desiredX;
- }
-
- /**
- * Retrieves the desired y offset of this OSD window.
- *
- * @since 0.6.3
- * @see #setDesiredLocation(int, int)
- * @return The desired offset of this window
- */
- public int getDesiredY() {
- return desiredY;
- }
-
- /**
- * Sets the desired location of this OSD window, and queues an event to move the window to the
- * desired location at some point in the future.
- * <p>
- * This method WILL NOT alter the location immediately, but will schedule an event in the AWT
- * event despatch thread which will be executed in the future.
- * <p>
- * This method will immediately update the values returned by the {@link #getDesiredX()} and
- * {@link #getDesiredY()} methods, but the {@link #getX()} and {@link #getY()} methods will
- * continue to reflect the actual location of the window.
- * <p>
- * This method is thread safe.
- *
- * @param x The desired x offset of this window
- * @param y The desired y offset of this window
- *
- * @since 0.6.3
- */
- public void setDesiredLocation(final int x, final int y) {
- this.desiredX = x;
- this.desiredY = y;
-
- UIUtilities.invokeLater(() -> setLocation(getDesiredX(), getDesiredY()));
- }
-
- @Override
- public String toString() {
- return label.getText();
- }
-
- }
|