root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / PatternEditor.java @ 34294
History | View | Annotate | Download (7.66 KB)
1 |
package org.gvsig.app.gui.styling; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Graphics; |
5 |
import java.awt.GridLayout; |
6 |
import java.awt.Point; |
7 |
import java.awt.Rectangle; |
8 |
import java.awt.event.ActionEvent; |
9 |
import java.awt.event.ActionListener; |
10 |
import java.awt.event.MouseEvent; |
11 |
import java.awt.event.MouseListener; |
12 |
import java.awt.event.MouseMotionListener; |
13 |
import java.util.ArrayList; |
14 |
|
15 |
import javax.swing.JFrame; |
16 |
import javax.swing.JPanel; |
17 |
|
18 |
public class PatternEditor extends JPanel { |
19 |
/**
|
20 |
*
|
21 |
*/
|
22 |
private static final long serialVersionUID = 4393880700290565871L; |
23 |
private static final int NULL_STATE = 0; |
24 |
private static final int EMPTY_STATE = 1; |
25 |
private static final int FILL_STATE = 2; |
26 |
private static final int END_MARK_STATE = 3; |
27 |
private int dividers = 9; |
28 |
private int subDividersPerDivider = 5; |
29 |
private int separations = dividers * subDividersPerDivider; |
30 |
private int vGap = 5; // pixels |
31 |
private int hGap = 5; |
32 |
private float dash[]; |
33 |
private int states[] = new int[separations]; |
34 |
private MyMouseListener mouseBehavior = new MyMouseListener(); |
35 |
private Rectangle bounds; |
36 |
private int width,height,halfHeight,separation; |
37 |
private ArrayList<ActionListener> listeners=new ArrayList<ActionListener>(); |
38 |
|
39 |
public PatternEditor() {
|
40 |
super();
|
41 |
addMouseListener(mouseBehavior); |
42 |
addMouseMotionListener(mouseBehavior); |
43 |
for (int i = 0; i < states.length; i++) { |
44 |
states[i] = NULL_STATE; |
45 |
} |
46 |
states[0] = END_MARK_STATE;
|
47 |
} |
48 |
|
49 |
public float[] getDash() { |
50 |
return dash;
|
51 |
} |
52 |
|
53 |
public void setDash(float[] dash) { |
54 |
this.dash=dash;
|
55 |
obtain_states(dash); |
56 |
repaint(); |
57 |
} |
58 |
|
59 |
private void obtain_states(float[] dash) { |
60 |
int pos=0; |
61 |
if(dash == null) return; |
62 |
|
63 |
//Para el resto de casos
|
64 |
for (int i = 0; i < dash.length; i++) { |
65 |
if(i%2 == 0) { |
66 |
for (int j = 0; j < dash[i]; j++) { |
67 |
states[pos]=FILL_STATE; |
68 |
pos++; |
69 |
} |
70 |
} |
71 |
else {
|
72 |
for (int j = 0; j < dash[i]; j++) { |
73 |
states[pos]=EMPTY_STATE; |
74 |
pos++; |
75 |
} |
76 |
} |
77 |
} |
78 |
states[pos]=END_MARK_STATE; |
79 |
|
80 |
|
81 |
} |
82 |
|
83 |
private void create_Dash() { |
84 |
if (states[0] == NULL_STATE) dash=null; |
85 |
else {
|
86 |
int divisions = 1; |
87 |
boolean firstfilled = true; |
88 |
|
89 |
//To create the divisions of the dash
|
90 |
if (states[0] == EMPTY_STATE) { |
91 |
divisions ++; |
92 |
firstfilled = false;
|
93 |
} |
94 |
int k=1; |
95 |
if(states[0] != END_MARK_STATE ) { |
96 |
while (states[k] != END_MARK_STATE) {
|
97 |
if(states[k] != states[k-1])divisions++; |
98 |
k++; |
99 |
} |
100 |
} |
101 |
|
102 |
|
103 |
//To fill the dash array
|
104 |
int pos=0; |
105 |
if(divisions ==1)dash=null; |
106 |
else {
|
107 |
dash = new float[divisions]; |
108 |
if(!firstfilled) {
|
109 |
dash[0]=0; |
110 |
pos++; |
111 |
firstfilled = true;
|
112 |
} |
113 |
dash[pos]=1;
|
114 |
|
115 |
int i=1; |
116 |
while(states[i]!= END_MARK_STATE) {
|
117 |
if(states[i] == states[i-1])dash[pos]++; |
118 |
else {
|
119 |
pos++; |
120 |
dash[pos]=1;
|
121 |
} |
122 |
i++; |
123 |
} |
124 |
} |
125 |
for (int i = 0; dash!=null && i < dash.length; i++) { |
126 |
System.out.print(dash[i]+" "); |
127 |
} |
128 |
System.out.print("\n"); |
129 |
|
130 |
} |
131 |
for (int i=0;i<listeners.size();i++) { |
132 |
listeners.get(i).actionPerformed(new ActionEvent(this,ActionEvent.ACTION_FIRST,"Released")); |
133 |
} |
134 |
} |
135 |
|
136 |
public void clear_Dash() { |
137 |
dash = null;
|
138 |
for (int i = 0; i < states.length; i++) { |
139 |
states[i] = NULL_STATE; |
140 |
} |
141 |
states[0] = END_MARK_STATE;
|
142 |
repaint(); |
143 |
create_Dash(); |
144 |
|
145 |
} |
146 |
|
147 |
@Override
|
148 |
public void paintComponent(Graphics g) { |
149 |
super.paintComponent(g);
|
150 |
bounds = getBounds(); |
151 |
width = bounds.width; |
152 |
height = bounds.height-2*vGap;
|
153 |
halfHeight = height / 2;
|
154 |
separation = width / separations; |
155 |
for (int i = 0; i <= separations; i++) { |
156 |
if (i%subDividersPerDivider == 0) { |
157 |
g.drawLine(hGap+separation*i, vGap, hGap+separation*i, vGap+halfHeight-3);
|
158 |
} else {
|
159 |
g.drawLine(hGap+separation*i, vGap+(halfHeight/2), hGap+separation*i, vGap+halfHeight-3); |
160 |
} |
161 |
|
162 |
} |
163 |
|
164 |
for(int i = 0; i < states.length ; i++) { |
165 |
switch (states[i]) {
|
166 |
case END_MARK_STATE:
|
167 |
g.setColor(Color.GRAY);
|
168 |
break;
|
169 |
case NULL_STATE:
|
170 |
continue;
|
171 |
case FILL_STATE:
|
172 |
g.setColor(Color.BLACK);
|
173 |
break;
|
174 |
case EMPTY_STATE:
|
175 |
g.setColor(Color.WHITE);
|
176 |
break;
|
177 |
} |
178 |
bounds.setLocation(0, 0); |
179 |
g.fillRect(hGap+separation*i, (vGap)+height/2, separation, halfHeight);
|
180 |
|
181 |
} |
182 |
bounds.setLocation(0, 0); |
183 |
g.drawRect(hGap,(vGap)+height/2 ,separation*separations,halfHeight);
|
184 |
|
185 |
} |
186 |
public static void main(String[] args) { |
187 |
JFrame f = new JFrame(); |
188 |
final PatternEditor pe1 = new PatternEditor(); |
189 |
final PatternEditor pe2 = new PatternEditor(); |
190 |
JPanel content = new JPanel(new GridLayout(2, 1)); |
191 |
pe1.addActionListener(new ActionListener() { |
192 |
public void actionPerformed(ActionEvent e) { |
193 |
pe2.setDash(pe1.getDash()); |
194 |
} |
195 |
}); |
196 |
|
197 |
content.add(pe1); |
198 |
content.add(pe2); |
199 |
f.setContentPane(content); |
200 |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
201 |
f.pack(); |
202 |
f.setVisible(true);
|
203 |
|
204 |
} |
205 |
|
206 |
private class MyMouseListener implements MouseListener, MouseMotionListener { |
207 |
private Point initialPoint = null; |
208 |
private Point endPoint = null; |
209 |
|
210 |
|
211 |
public void mouseEntered(MouseEvent e) { /* nothing */ } |
212 |
public void mouseExited(MouseEvent e){ /* nothing */ } |
213 |
public void mouseReleased(MouseEvent e) { /* nothing */ } |
214 |
public void mouseMoved(MouseEvent e) { /* nothing */ } |
215 |
|
216 |
public void mouseClicked(MouseEvent e){ |
217 |
initialPoint = getPoint(e); |
218 |
} |
219 |
|
220 |
public void mousePressed(MouseEvent e) { |
221 |
initialPoint = getPoint(e); |
222 |
if(isValidPoint(initialPoint)) {
|
223 |
if (states[getRect(initialPoint)]== EMPTY_STATE)
|
224 |
states[getRect(initialPoint)] = FILL_STATE; |
225 |
else if (states[getRect(initialPoint)]== FILL_STATE) |
226 |
states[getRect(initialPoint)] = EMPTY_STATE; |
227 |
|
228 |
repaint(); |
229 |
create_Dash(); |
230 |
} |
231 |
} |
232 |
private Point getPoint(MouseEvent e) { |
233 |
Point p = new Point();//getLocation(); |
234 |
Point p2 = e.getPoint();
|
235 |
return new Point(p2.x - p.x, p2.y - p.y); |
236 |
} |
237 |
|
238 |
private synchronized void doIt() { |
239 |
if(!isValidPoint(endPoint) || !isValidPoint(initialPoint))return; |
240 |
|
241 |
if(isValidPoint(endPoint) && isValidPoint(initialPoint)) {
|
242 |
int stateIndex = getRect(initialPoint);
|
243 |
if (stateIndex>=separations) return; |
244 |
switch (states[stateIndex]) {
|
245 |
case END_MARK_STATE:
|
246 |
case NULL_STATE:
|
247 |
enlarge(); |
248 |
break;
|
249 |
case FILL_STATE:
|
250 |
swap(); |
251 |
break;
|
252 |
} |
253 |
repaint(); |
254 |
create_Dash(); |
255 |
} |
256 |
} |
257 |
|
258 |
private void swap() { |
259 |
if(!isValidPoint(endPoint) || !isValidPoint(initialPoint))return; |
260 |
|
261 |
if(states[getRect(endPoint)] == EMPTY_STATE)
|
262 |
states[getRect(endPoint)]= FILL_STATE; |
263 |
|
264 |
else return; |
265 |
} |
266 |
|
267 |
private synchronized void enlarge() { |
268 |
|
269 |
if(!isValidPoint(endPoint) || !isValidPoint(initialPoint))
|
270 |
return;
|
271 |
int index = getRect(endPoint);
|
272 |
if (index >= separations)
|
273 |
return;
|
274 |
if(isValidPoint(endPoint) && isValidPoint(initialPoint)) {
|
275 |
for (int i = index-1; i >= 0; i--) { |
276 |
if (states[i] != FILL_STATE)
|
277 |
states[i] = EMPTY_STATE; |
278 |
} |
279 |
for (int i = index+1; i < states.length; i++) { |
280 |
states[i] = NULL_STATE; |
281 |
} |
282 |
|
283 |
|
284 |
states[index] = END_MARK_STATE; |
285 |
} |
286 |
} |
287 |
|
288 |
private boolean isValidPoint(Point p) { |
289 |
if (getRect(p)!= -1) |
290 |
return true; |
291 |
return false; |
292 |
} |
293 |
|
294 |
private int getRect(Point p) { |
295 |
if (( vGap+halfHeight <= p.y ) && ( p.y <= vGap+2*halfHeight )) |
296 |
if((hGap <= p.x) && (p.x <= hGap+(separation*separations)))
|
297 |
return (p.x - hGap) / separation;
|
298 |
return -1; |
299 |
} |
300 |
|
301 |
public void mouseDragged(MouseEvent e) { |
302 |
endPoint = getPoint(e); |
303 |
if(isValidPoint(endPoint)) {
|
304 |
doIt(); |
305 |
} |
306 |
} |
307 |
|
308 |
} |
309 |
|
310 |
public void addActionListener(ActionListener patternChange) { |
311 |
listeners.add(patternChange); |
312 |
|
313 |
} |
314 |
|
315 |
} |