View Javadoc

1   /*
2    * associated an image with many clickable zones. Each zone is associated to a JPAnel 
3    * witch appears when a zone (Shape) is mouseclicked
4    */
5   package org.lsst.ccs.utilities.zonesui;
6   
7   import org.lsst.ccs.utilities.tracers.Tracer;
8   
9   import javax.swing.ImageIcon;
10  import javax.swing.JOptionPane;
11  import javax.swing.JPanel;
12  import javax.swing.SwingUtilities;
13  import java.awt.BasicStroke;
14  import java.awt.Color;
15  import java.awt.Dimension;
16  import java.awt.Graphics;
17  import java.awt.Graphics2D;
18  import java.awt.Image;
19  import java.awt.Point;
20  import java.awt.RenderingHints;
21  import java.awt.Stroke;
22  import java.awt.event.MouseEvent;
23  import java.awt.event.MouseListener;
24  import java.awt.event.MouseMotionListener;
25  import java.util.List;
26  
27  /**
28   *
29   * @author Pascal Durieu
30   */
31  public class ImageZones extends JPanel implements MouseListener, MouseMotionListener {
32  
33      //Map whith the Shapes define the clickables zones (key)
34      private List<Zone> tableShape;
35      
36      //description of the image & clickable zones
37      private ImageDescription description;
38      private ImageIcon icon;
39      private Image img;
40      private Dimension dim;
41      private Stroke stroke = new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 5.0f, new float[] {5.0f}, 0.0f);
42      private Color color = Color.BLACK ;
43  
44      //associating the key (clickable zones) with a JPanel
45      private InteractionProducer interaction;
46      private PositionListener posListener ;
47  
48      //display mousecoords
49      private String message = "";
50      int messageX = 0, messageY = 0; 
51         
52      public ImageZones(ImageDescription id, InteractionProducer ip, PositionListener posListener) {
53          this.description = id;
54          this.interaction = ip;
55          this.posListener = posListener ;
56          
57          icon = id.getIcon();
58          tableShape = id.getShapeDescription();
59          img = icon.getImage();
60          dim = new Dimension(icon.getIconWidth(),icon.getIconHeight());        
61  
62          addMouseListener(this);
63          addMouseMotionListener(this);
64      }
65      @Override
66      public Dimension getMinimumSize() {
67          Dimension dim2 = new Dimension(icon.getIconWidth()/2,icon.getIconHeight()/2);
68          return dim2;
69      }
70      @Override
71      public Dimension getMaximumSize() {return dim;}
72      @Override
73      public Dimension getPreferredSize() {return dim;}
74  
75      public Stroke getStroke() {
76          return stroke;
77      }
78  
79      public void setStroke(Stroke stroke) {
80          this.stroke = stroke;
81      }
82  
83      public void setDashedLineWidth(int points){
84          float width = (float) points ;
85          this.setStroke( new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 5.0f, new float[] {5.0f}, 0.0f));
86      }
87  
88      public Color getDashedLineColor() {
89          return color;
90      }
91  
92      public void setDashedLineColor(Color color) {
93          this.color = color;
94      }
95  
96      //drawing the image and clickable zones
97      @Override
98      public void paintComponent(Graphics g) {
99          Graphics2D g2 = (Graphics2D)g;
100         super.paintComponent(g2);
101         g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
102         
103         //drawn image
104         g2.drawImage(img, 0, 0, img.getWidth(this), img.getHeight(this), this);
105         
106         //drawn the clickable zones
107         g2.setColor(getDashedLineColor());
108         g2.setStroke(getStroke());
109         for(Zone zone: tableShape) {
110             g2.draw(zone.getShape());
111         }
112 
113         //TODO: get this out and put it in a different JLAbel
114         //simple mouse coords
115         g2.setColor(Color.BLACK);
116         messageY = g2.getFont().getSize();
117         g2.drawString(message, messageX, messageY); 
118     }
119 
120     //when clicked on a shape identified as a clickable zone,
121     //a JDialog is loading, displayed a JPanel
122     @Override
123     public void mouseClicked(MouseEvent e) {
124         Point pt = e.getPoint();
125         for(Zone zone : tableShape) {
126             if (zone.getShape().contains(pt)) {
127                 String key = zone.getKey();
128                 JPanel jpan = interaction.getInteraction(key);
129                 if (jpan != null) {
130                     ClickDialog cd = new ClickDialog(SwingUtilities.windowForComponent(this),key, false, jpan);   
131                     //System.out.println(interaction.getInteraction(key).toString()+" [key = "+key+"]");
132                     assert Tracer.trace(interaction.getInteraction(key).toString() + " [key = " + key + "]");
133                     cd.setLocation(e.getXOnScreen(), e.getYOnScreen());  
134                 }
135                 else {
136                     JOptionPane.showMessageDialog(SwingUtilities.windowForComponent(this),
137                         "JPanel not found for the key \""+key+"\"",
138                          "JPanel error",
139                          JOptionPane.ERROR_MESSAGE);
140                 }
141                 break;
142             }
143         }
144     }
145     
146     @Override
147     public void mouseMoved(MouseEvent me) {
148         Point pt = me.getPoint();
149         String tooltipText="" ;
150         for(Zone zone : tableShape) {
151             if (zone.getShape().contains(pt)) {
152                 tooltipText = zone.getTipText();
153                 break ;
154             }
155         }
156         this.setToolTipText(tooltipText);
157         //: put message in a different JLabel : do not call repaint!
158         if(posListener != null) {
159             posListener.currentCoords(me.getX(), me.getY());
160         }
161         //message= "("+me.getX()+", "+me.getY()+")";
162         // repaint();
163     }
164    
165     @Override
166     public void mousePressed(MouseEvent e) {}
167     @Override
168     public void mouseReleased(MouseEvent e) {}
169     @Override
170     public void mouseEntered(MouseEvent me) {}
171     @Override
172     public void mouseExited(MouseEvent me) {}
173     @Override
174     public void mouseDragged(MouseEvent me) {}
175 
176 }
177