Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / styling / PatternEditor.java @ 40560

History | View | Annotate | Download (8.62 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.gui.styling;
25

    
26
import java.awt.Color;
27
import java.awt.Graphics;
28
import java.awt.GridLayout;
29
import java.awt.Point;
30
import java.awt.Rectangle;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
import java.awt.event.MouseEvent;
34
import java.awt.event.MouseListener;
35
import java.awt.event.MouseMotionListener;
36
import java.util.ArrayList;
37

    
38
import javax.swing.JFrame;
39
import javax.swing.JPanel;
40

    
41
public class PatternEditor extends JPanel {
42
        /**
43
         * 
44
         */
45
        private static final long serialVersionUID = 4393880700290565871L;
46
        private static final int NULL_STATE = 0;
47
        private static final int EMPTY_STATE = 1;
48
        private static final int FILL_STATE = 2;
49
        private static final int END_MARK_STATE = 3;
50
        private int dividers = 9;
51
        private int subDividersPerDivider = 5;
52
        private int separations = dividers * subDividersPerDivider;
53
        private int vGap = 5; // pixels
54
        private int hGap = 5;
55
        private float dash[];
56
        private int states[] = new int[separations];
57
        private MyMouseListener mouseBehavior = new MyMouseListener();
58
        private Rectangle bounds;
59
        private int width,height,halfHeight,separation;
60
        private ArrayList<ActionListener> listeners=new ArrayList<ActionListener>();
61

    
62
        public PatternEditor() {
63
                super();
64
                addMouseListener(mouseBehavior);
65
                addMouseMotionListener(mouseBehavior);
66
                for (int i = 0; i < states.length; i++) {
67
                        states[i] = NULL_STATE;
68
                }
69
                states[0] = END_MARK_STATE;
70
        }
71

    
72
        public float[] getDash() {
73
                return dash;
74
        }
75

    
76
        public void setDash(float[] dash) {
77
                this.dash=dash;
78
                obtain_states(dash);
79
                repaint();
80
        }
81

    
82
        private void obtain_states(float[] dash) {
83
                int pos=0;
84
                if(dash == null) return;
85

    
86
                //Para el resto de casos
87
                for (int i = 0; i < dash.length; i++) {
88
                        if(i%2 == 0) {
89
                                for (int j = 0; j < dash[i]; j++) {
90
                                        states[pos]=FILL_STATE;
91
                                        pos++;
92
                                }
93
                        }
94
                        else {
95
                                for (int j = 0; j < dash[i]; j++) {
96
                                        states[pos]=EMPTY_STATE;
97
                                        pos++;
98
                                }
99
                        }
100
                }
101
                states[pos]=END_MARK_STATE;
102

    
103

    
104
        }
105

    
106
        private void create_Dash() {
107
                if (states[0] == NULL_STATE) dash=null;
108
                else {
109
                        int divisions = 1;
110
                        boolean firstfilled = true;
111
                        
112
                        //To create the divisions of the dash
113
                        if (states[0] == EMPTY_STATE) {
114
                                divisions ++;
115
                                firstfilled = false;
116
                        }
117
                        int k=1;
118
                        if(states[0] != END_MARK_STATE ) {
119
                                while (states[k] != END_MARK_STATE) {
120
                                        if(states[k] != states[k-1])divisions++;
121
                                        k++;
122
                                }
123
                        }
124
                        
125
                        
126
                        //To fill the dash array
127
                        int pos=0;
128
                        if(divisions ==1)dash=null;
129
                        else {
130
                                dash = new float[divisions];
131
                                if(!firstfilled) {
132
                                        dash[0]=0;
133
                                        pos++;
134
                                        firstfilled = true;
135
                                }
136
                                dash[pos]=1;
137

    
138
                                int i=1;
139
                                while(states[i]!= END_MARK_STATE) {
140
                                        if(states[i] == states[i-1])dash[pos]++;
141
                                        else {
142
                                                pos++;
143
                                                dash[pos]=1;
144
                                        }
145
                                        i++;
146
                                }
147
                        }
148
                        for (int i = 0; dash!=null &&  i < dash.length; i++) {
149
                                System.out.print(dash[i]+" ");
150
                        }
151
                        System.out.print("\n");
152
                        
153
                }        
154
                for (int i=0;i<listeners.size();i++) {
155
                        listeners.get(i).actionPerformed(new ActionEvent(this,ActionEvent.ACTION_FIRST,"Released"));        
156
                }
157
        }
158

    
159
        public void clear_Dash() {
160
                dash = null;
161
                for (int i = 0; i < states.length; i++) {
162
                        states[i] = NULL_STATE;
163
                }
164
                states[0] = END_MARK_STATE;
165
                repaint();
166
                create_Dash();
167

    
168
        }
169

    
170
        @Override
171
        public void paintComponent(Graphics g) {
172
                super.paintComponent(g);
173
                bounds = getBounds();
174
                width = bounds.width;        
175
                height = bounds.height-2*vGap;
176
                halfHeight = height / 2;
177
                separation = width / separations;
178
                for (int i = 0; i <= separations; i++) {
179
                        if (i%subDividersPerDivider == 0) {
180
                                g.drawLine(hGap+separation*i, vGap, hGap+separation*i, vGap+halfHeight-3);
181
                        } else {
182
                                g.drawLine(hGap+separation*i, vGap+(halfHeight/2), hGap+separation*i, vGap+halfHeight-3);
183
                        }
184

    
185
                }
186

    
187
                for(int i = 0; i < states.length ; i++) {
188
                        switch (states[i]) {
189
                        case END_MARK_STATE:
190
                                g.setColor(Color.GRAY);
191
                                break;
192
                        case NULL_STATE:
193
                                continue;
194
                        case FILL_STATE:
195
                                g.setColor(Color.BLACK);
196
                                break;
197
                        case EMPTY_STATE:
198
                                g.setColor(Color.WHITE);
199
                                break;
200
                        }
201
                        bounds.setLocation(0, 0);
202
                        g.fillRect(hGap+separation*i, (vGap)+height/2, separation, halfHeight);
203
                        
204
                }
205
                bounds.setLocation(0, 0);
206
                g.drawRect(hGap,(vGap)+height/2 ,separation*separations,halfHeight);
207

    
208
        }
209
        public static void main(String[] args) {
210
                JFrame f = new JFrame();
211
                final PatternEditor pe1 = new PatternEditor(); 
212
                final PatternEditor pe2 = new PatternEditor();
213
                JPanel content = new JPanel(new GridLayout(2, 1));
214
                pe1.addActionListener(new ActionListener() {
215
                        public void actionPerformed(ActionEvent e) {
216
                                pe2.setDash(pe1.getDash());
217
                        }
218
                });
219
                
220
                content.add(pe1);
221
                content.add(pe2);
222
                f.setContentPane(content);
223
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
224
                f.pack();
225
                f.setVisible(true);
226

    
227
        }
228

    
229
        private class MyMouseListener implements MouseListener, MouseMotionListener {
230
                private Point initialPoint = null;
231
                private Point endPoint = null;
232
                
233
        
234
                public void mouseEntered(MouseEvent e) { /* nothing */ }
235
                public void mouseExited(MouseEvent e){ /* nothing */ }
236
                public void mouseReleased(MouseEvent e) { /* nothing */ }
237
                public void mouseMoved(MouseEvent e) { /* nothing */ }
238

    
239
                public void mouseClicked(MouseEvent e){ 
240
                        initialPoint = getPoint(e);
241
                }
242
                
243
                public void mousePressed(MouseEvent e) {
244
                        initialPoint = getPoint(e);
245
                        if(isValidPoint(initialPoint)) {
246
                                if (states[getRect(initialPoint)]== EMPTY_STATE)
247
                                        states[getRect(initialPoint)] = FILL_STATE;
248
                                else if (states[getRect(initialPoint)]== FILL_STATE)
249
                                        states[getRect(initialPoint)] = EMPTY_STATE;
250
                                
251
                                repaint();
252
                                create_Dash();
253
                        }
254
                }
255
                private Point getPoint(MouseEvent e) {
256
                        Point p = new Point();//getLocation();
257
                        Point p2 = e.getPoint();
258
                        return new Point(p2.x - p.x, p2.y - p.y);
259
                }
260

    
261
                private synchronized void doIt() {
262
                        if(!isValidPoint(endPoint) || !isValidPoint(initialPoint))return;
263
                        
264
                        if(isValidPoint(endPoint) && isValidPoint(initialPoint)) {
265
                                int stateIndex = getRect(initialPoint);
266
                                if (stateIndex>=separations) return;
267
                                switch (states[stateIndex]) {
268
                                case END_MARK_STATE:
269
                                case NULL_STATE:
270
                                        enlarge();
271
                                        break;
272
                                case FILL_STATE:
273
                                        swap();
274
                                        break;
275
                                }
276
                                repaint();
277
                                create_Dash();
278
                        }
279
                }
280
                
281
                private void swap() {
282
                        if(!isValidPoint(endPoint) || !isValidPoint(initialPoint))return;
283
                        
284
                        if(states[getRect(endPoint)] == EMPTY_STATE)
285
                                states[getRect(endPoint)]= FILL_STATE;
286

    
287
                        else return;
288
                }
289
                
290
                private synchronized void enlarge() {
291
                        
292
                        if(!isValidPoint(endPoint) || !isValidPoint(initialPoint))
293
                                return;
294
                        int index = getRect(endPoint);
295
                        if (index >= separations)
296
                                return;
297
                        if(isValidPoint(endPoint) && isValidPoint(initialPoint)) {
298
                                for (int i = index-1; i >= 0; i--) {
299
                                        if (states[i] != FILL_STATE)
300
                                                states[i] = EMPTY_STATE;
301
                                }
302
                                for (int i = index+1; i < states.length; i++) {
303
                                        states[i] = NULL_STATE;
304
                                }
305
                                
306
                                
307
                                states[index] = END_MARK_STATE;
308
                        }
309
                }
310

    
311
                private boolean isValidPoint(Point p) {
312
                        if (getRect(p)!= -1)
313
                                return true;
314
                        return false;
315
                }
316

    
317
                private int getRect(Point p) {
318
                        if (( vGap+halfHeight <= p.y ) && ( p.y <= vGap+2*halfHeight ))
319
                                if((hGap <= p.x) && (p.x <= hGap+(separation*separations)))
320
                                        return (p.x - hGap) / separation;
321
                        return -1;
322
                }
323

    
324
                public void mouseDragged(MouseEvent e) {
325
                        endPoint = getPoint(e);
326
                        if(isValidPoint(endPoint)) {
327
                                doIt();
328
                        }
329
                }
330

    
331
        }
332

    
333
        public void addActionListener(ActionListener patternChange) {
334
                listeners.add(patternChange);
335
                
336
        }
337

    
338
}