All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.alantea.swing.graph.GraphMarker Maven / Gradle / Ivy

package net.alantea.swing.graph;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;

import net.alantea.horizon.message.Messenger;

@SuppressWarnings("serial")
public class GraphMarker extends GraphElement
{
   public static final String ID_MARKERSELECTED = "MarkerSelected";
   public static final String ID_MARKERDESELECTED = "MarkerDeselected";
   
   private Color normalBackground;
   private Color selectedBackground;
   private Color normalForeground;
   private Color enteredForeground;
   
   private boolean entered;
   private boolean selected;
   private boolean selectable = true;
   
   private String type = "";

   public void paintBackground(Graphics g)
   {
      fillRectangle(g, getBackground(), 0.0, 0.0, 1.0, 1.0);
   }
   
   public void paintBorder(Graphics g)
   {
      drawRectangle(g,getForeground(), 0.0, 0.0, 1.0, 1.0, 3);
   }
   
   public boolean isSelected()
   {
      return selected;
   }
   
   public void setSelected(boolean value)
   {
      if (value == selected) {
         return;
      }
      else if (value)
      {
         super.setBackground(selectedBackground);
         Messenger.sendMessage(this, ID_MARKERSELECTED, this);
      }
      else
      {
         super.setBackground(normalBackground);
         Messenger.sendMessage(this, ID_MARKERDESELECTED, this);
      }
      selected = value;
      repaint();
   }
   
   public boolean isSelectable()
   {
      return selectable;
   }
   
   public void setSelectable(boolean flag)
   {
      if (selectable)
      {
         selectable = flag;
         if (selected && !flag)
         {
            super.setBackground(normalBackground);
            Messenger.sendMessage(this, ID_MARKERDESELECTED, this);
            selected = false;
            repaint();
         }
      }
   }
   
   public String getType()
   {
      return type;
   }

   public void setType(String type)
   {
      this.type = type;
   }

   @Override
   protected boolean onMouseEntered(MouseEvent event)
   {
      super.onMouseEntered(event);
      super.setForeground(enteredForeground);
      entered = true;
      repaint();
      return false;
   }
   
   @Override
   protected boolean onMouseExited(MouseEvent event)
   {
      super.onMouseExited(event);
      super.setForeground(normalForeground);
      entered = false;
      repaint();
      return false;
   }
   
   @Override
   protected boolean onMouseClick(MouseEvent event)
   {
      if (selectable)
      {
         setSelected(! selected);
      }
      return true;
   }

   @Override
   public void setForeground(Color color)
   {
      normalForeground = color;
      if (!entered)
      {
         super.setForeground(normalForeground);
         repaint();
      }
      if (enteredForeground == null)
      {
         enteredForeground = color;
      }
   }
   
   public void setEnteredForeground(Color color)
   {
      enteredForeground = color;
      if (entered)
      {
         super.setForeground(enteredForeground);
         repaint();
      }
   }

   @Override
   public void setBackground(Color color)
   {
      normalBackground = color;
      if (!entered)
      {
         super.setBackground(normalBackground);
         repaint();
      }
      if (selectedBackground == null)
      {
         selectedBackground = color;
      }
   }
   
   public void setSelectedBackground(Color color)
   {
      selectedBackground = color;
      if (selected)
      {
         super.setBackground(selectedBackground);
         repaint();
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy