View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.gui.graph;
25  
26  import java.awt.*;
27  import java.awt.event.*;
28  import javax.swing.*;
29  
30  import java.util.*;
31  
32  public class SelectPanel extends JPanel
33                           implements MouseListener, MouseMotionListener {
34  
35      // protected Dimension psize = new Dimension(400,400);
36  
37      protected Point anchor    = new Point(0,0);
38      protected Point stretched = new Point(0,0);
39      protected Point last      = new Point(0,0);
40      protected Point end       = new Point(0,0);
41  
42      protected boolean   firstStretch = true;
43      protected boolean   regionSelected = false;
44      protected Rectangle region = new Rectangle(0,0,0,0);
45      protected Vector    items = new Vector();
46  
47      public SelectPanel() {
48         super(true);
49         this.addMouseListener(this);
50         this.addMouseMotionListener(this);
51         // setSize(psize);
52         // validate();
53      }
54      public SelectPanel(Dimension dim) {
55         this();
56         // psize = new Dimension(dim); setSize(dim);
57         setMaximumSize(dim);
58         // validate();
59      }
60      public SelectPanel(int w, int h) {
61         this();
62         //psize = new Dimension(w,h); setSize(w,h);
63         //validate();
64      }
65  
66      public void mouseClicked(MouseEvent evt) {}
67      public void mouseEntered(MouseEvent evt) {}
68      public void mouseExited(MouseEvent evt) {}
69      public void mouseMoved(MouseEvent evt) {}
70  
71      boolean source_ok = false;
72      public void mousePressed(MouseEvent evt) {
73         source_ok = (evt.getSource() == this);
74  
75         if ( !source_ok ) return;
76         if ( SwingUtilities.isLeftMouseButton(evt) )
77            this.anchor(evt.getPoint());
78      }
79      public void mouseDragged(MouseEvent evt) {
80         if ( !source_ok ) return;
81         if ( SwingUtilities.isLeftMouseButton(evt) )
82            this.stretch(evt.getPoint());
83      }
84      public void mouseReleased(MouseEvent evt) {
85         if ( !source_ok ) return;
86         if ( SwingUtilities.isLeftMouseButton(evt) )
87            this.end(evt.getPoint());
88         source_ok = false;
89      }
90  
91      public void drawRegion(Graphics graphics) {
92         graphics.drawRect(region.x, region.y, region.width, region.height);
93      }
94  
95      public void clearRegion(Graphics graphics) {
96         Color c = graphics.getColor();
97         graphics.setColor(this.getBackground());
98         graphics.drawRect(region.x, region.y, region.width, region.height);
99         graphics.setColor(c);
100     }
101     public void drawLast(Graphics graphics) {
102        Rectangle rect = lastBounds();
103        graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
104     }
105     public void drawNext(Graphics graphics) {
106        Rectangle rect = currentBounds();
107        graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
108     }
109 
110     public Point getAnchor   () { return anchor;    }
111     public Point getStretched() { return stretched; }
112     public Point getLast     () { return last;      }
113     public Point getEnd      () { return end;       }
114 
115     public void anchor(Point p) {
116        firstStretch = true;
117        stretched.x = last.x = anchor.x = p.x;
118        stretched.y = last.y = anchor.y = p.y;
119 
120        if ( regionSelected && !region.contains(anchor.x,anchor.y) ) {
121           Graphics g = this.getGraphics();
122           if (g != null) clearRegion(g);
123           regionSelected = false;
124        }
125     }
126     public void stretch(Point p) {
127        last.x      = stretched.x;
128        last.y      = stretched.y;
129        stretched.x = p.x;
130        stretched.y = p.y;
131 
132        Graphics g = this.getGraphics();
133        if ( g != null && !regionSelected ) {
134           g.setXORMode(this.getBackground());
135 
136           if ( firstStretch ) firstStretch = false;
137           else                drawLast(g);
138           drawNext(g);
139        }
140     }
141     public void end(Point p) {
142        last.x = end.x = p.x;
143        last.y = end.y = p.y;
144 
145        Graphics g = this.getGraphics();
146        if ( g != null && !regionSelected ) {
147           g.setXORMode(this.getBackground());
148           drawLast(g);
149           region.setBounds( stretched.x < anchor.x ? stretched.x : anchor.x,
150                             stretched.y < anchor.y ? stretched.y : anchor.y,
151                             Math.abs(stretched.x - anchor.x),
152                             Math.abs(stretched.y - anchor.y));
153           if ( findItems() ) {
154              regionSelected = true;
155              drawRegion(g);
156           }
157        }
158     }
159     public Rectangle currentBounds() {
160        return new Rectangle(stretched.x < anchor.x ? stretched.x : anchor.x,
161                             stretched.y < anchor.y ? stretched.y : anchor.y,
162                             Math.abs(stretched.x - anchor.x),
163                             Math.abs(stretched.y - anchor.y));
164     }
165 
166     public Rectangle lastBounds() {
167        return new Rectangle(last.x < anchor.x ? last.x : anchor.x,
168                             last.y < anchor.y ? last.y : anchor.y,
169                             Math.abs(last.x - anchor.x),
170                             Math.abs(last.y - anchor.y));
171     }
172 
173     public Vector boundedItems() {
174        if ( regionSelected ) return items;
175        return new Vector();
176     }
177 
178     protected boolean findItems() {
179        Component[] all_items = getComponents();
180        items.removeAllElements();
181 
182        for( int i = 0; i < all_items.length; i++ ) {
183           Rectangle r = all_items[i].getBounds();
184           if ( region.contains(r.x,r.y) &&
185                region.contains(r.x+r.width,r.y+r.height) )
186              items.addElement(all_items[i]);
187        }
188        return !items.isEmpty();
189     }
190 }