1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
52
53 }
54 public SelectPanel(Dimension dim) {
55 this();
56
57 setMaximumSize(dim);
58
59 }
60 public SelectPanel(int w, int h) {
61 this();
62
63
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 }