Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / gui / cad / tools / smc / SelectionCADToolContext.java @ 40557

History | View | Annotate | Download (20 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

    
25
//
26
// Vicente Caballero Navarro
27

    
28

    
29
package org.gvsig.editing.gui.cad.tools.smc;
30

    
31
import java.awt.event.InputEvent;
32

    
33
import org.gvsig.andami.PluginServices;
34
import org.gvsig.editing.gui.cad.tools.SelectionCADTool;
35

    
36

    
37
public final class SelectionCADToolContext
38
    extends statemap.FSMContext
39
{
40
//---------------------------------------------------------------
41
// Member methods.
42
//
43

    
44
    public SelectionCADToolContext(SelectionCADTool owner)
45
    {
46
        super();
47

    
48
        _owner = owner;
49
        setState(Selection.FirstPoint);
50
        Selection.FirstPoint.Entry(this);
51
    }
52

    
53
    public void addOption(String s)
54
    {
55
        _transition = "addOption";
56
        getState().addOption(this, s);
57
        _transition = "";
58
        return;
59
    }
60

    
61
    public void addPoint(double pointX, double pointY, InputEvent event)
62
    {
63
        _transition = "addPoint";
64
        getState().addPoint(this, pointX, pointY, event);
65
        _transition = "";
66
        return;
67
    }
68

    
69
    public void addValue(double d)
70
    {
71
        _transition = "addValue";
72
        getState().addValue(this, d);
73
        _transition = "";
74
        return;
75
    }
76

    
77
    public SelectionCADToolState getState()
78
        throws statemap.StateUndefinedException
79
    {
80
        if (_state == null)
81
        {
82
            throw(
83
                new statemap.StateUndefinedException());
84
        }
85

    
86
        return ((SelectionCADToolState) _state);
87
    }
88

    
89
    protected SelectionCADTool getOwner()
90
    {
91
        return (_owner);
92
    }
93

    
94
//---------------------------------------------------------------
95
// Member data.
96
//
97

    
98
    transient private SelectionCADTool _owner;
99

    
100
//---------------------------------------------------------------
101
// Inner classes.
102
//
103

    
104
    public static abstract class SelectionCADToolState
105
        extends statemap.State
106
    {
107
    //-----------------------------------------------------------
108
    // Member methods.
109
    //
110

    
111
        protected SelectionCADToolState(String name, int id)
112
        {
113
            super (name, id);
114
        }
115

    
116
        protected void Entry(SelectionCADToolContext context) {}
117
        protected void Exit(SelectionCADToolContext context) {}
118

    
119
        protected void addOption(SelectionCADToolContext context, String s)
120
        {
121
            Default(context);
122
        }
123

    
124
        protected void addPoint(SelectionCADToolContext context, double pointX, double pointY, InputEvent event)
125
        {
126
            Default(context);
127
        }
128

    
129
        protected void addValue(SelectionCADToolContext context, double d)
130
        {
131
            Default(context);
132
        }
133

    
134
        protected void Default(SelectionCADToolContext context)
135
        {
136
            throw (
137
                new statemap.TransitionUndefinedException(
138
                    "State: " +
139
                    context.getState().getName() +
140
                    ", Transition: " +
141
                    context.getTransition()));
142
        }
143

    
144
    //-----------------------------------------------------------
145
    // Member data.
146
    //
147
    }
148

    
149
    /* package */ static abstract class Selection
150
    {
151
    //-----------------------------------------------------------
152
    // Member methods.
153
    //
154

    
155
    //-----------------------------------------------------------
156
    // Member data.
157
    //
158

    
159
        //-------------------------------------------------------
160
        // Statics.
161
        //
162
        /* package */ static Selection_Default.Selection_FirstPoint FirstPoint;
163
        /* package */ static Selection_Default.Selection_SecondPoint SecondPoint;
164
        /* package */ static Selection_Default.Selection_WithSelectedFeatures WithSelectedFeatures;
165
        /* package */ static Selection_Default.Selection_WithHandlers WithHandlers;
166
        private static Selection_Default Default;
167

    
168
        static
169
        {
170
            FirstPoint = new Selection_Default.Selection_FirstPoint("Selection.FirstPoint", 0);
171
            SecondPoint = new Selection_Default.Selection_SecondPoint("Selection.SecondPoint", 1);
172
            WithSelectedFeatures = new Selection_Default.Selection_WithSelectedFeatures("Selection.WithSelectedFeatures", 2);
173
            WithHandlers = new Selection_Default.Selection_WithHandlers("Selection.WithHandlers", 3);
174
            Default = new Selection_Default("Selection.Default", -1);
175
        }
176

    
177
    }
178

    
179
    protected static class Selection_Default
180
        extends SelectionCADToolState
181
    {
182
    //-----------------------------------------------------------
183
    // Member methods.
184
    //
185

    
186
        protected Selection_Default(String name, int id)
187
        {
188
            super (name, id);
189
        }
190

    
191
        protected void addOption(SelectionCADToolContext context, String s)
192
        {
193
            SelectionCADTool ctxt = context.getOwner();
194

    
195
            if (s.equals(""))
196
            {
197
                boolean loopbackFlag =
198
                    context.getState().getName().equals(
199
                        Selection.FirstPoint.getName());
200

    
201
                if (loopbackFlag == false)
202
                {
203
                    (context.getState()).Exit(context);
204
                }
205

    
206
                context.clearState();
207
                try
208
                {
209
                    ctxt.restorePreviousTool();
210
                    ctxt.setQuestion(PluginServices.getText(this,"insert_point_selection"));
211
                    ctxt.setDescription(new String[]{"cancel"});
212
                    ctxt.end();
213
                }
214
                finally
215
                {
216
                    context.setState(Selection.FirstPoint);
217

    
218
                    if (loopbackFlag == false)
219
                    {
220
                        (context.getState()).Entry(context);
221
                    }
222

    
223
                }
224
            }
225
            else if (s.equals(PluginServices.getText(this,"cancel")))
226
            {
227
                boolean loopbackFlag =
228
                    context.getState().getName().equals(
229
                        Selection.FirstPoint.getName());
230

    
231
                if (loopbackFlag == false)
232
                {
233
                    (context.getState()).Exit(context);
234
                }
235

    
236
                context.clearState();
237
                try
238
                {
239
                    ctxt.end();
240
                }
241
                finally
242
                {
243
                    context.setState(Selection.FirstPoint);
244

    
245
                    if (loopbackFlag == false)
246
                    {
247
                        (context.getState()).Entry(context);
248
                    }
249

    
250
                }
251
            }
252
            else
253
            {
254
                boolean loopbackFlag =
255
                    context.getState().getName().equals(
256
                        Selection.FirstPoint.getName());
257

    
258
                if (loopbackFlag == false)
259
                {
260
                    (context.getState()).Exit(context);
261
                }
262

    
263
                context.clearState();
264
                try
265
                {
266
                    ctxt.throwOptionException(PluginServices.getText(this,"incorrect_option"), s);
267
                }
268
                finally
269
                {
270
                    context.setState(Selection.FirstPoint);
271

    
272
                    if (loopbackFlag == false)
273
                    {
274
                        (context.getState()).Entry(context);
275
                    }
276

    
277
                }
278
            }
279

    
280
            return;
281
        }
282

    
283
        protected void addValue(SelectionCADToolContext context, double d)
284
        {
285
            SelectionCADTool ctxt = context.getOwner();
286

    
287
            boolean loopbackFlag =
288
                context.getState().getName().equals(
289
                    Selection.FirstPoint.getName());
290

    
291
            if (loopbackFlag == false)
292
            {
293
                (context.getState()).Exit(context);
294
            }
295

    
296
            context.clearState();
297
            try
298
            {
299
                ctxt.throwValueException(PluginServices.getText(this,"incorrect_value"), d);
300
            }
301
            finally
302
            {
303
                context.setState(Selection.FirstPoint);
304

    
305
                if (loopbackFlag == false)
306
                {
307
                    (context.getState()).Entry(context);
308
                }
309

    
310
            }
311
            return;
312
        }
313

    
314
        protected void addPoint(SelectionCADToolContext context, double pointX, double pointY, InputEvent event)
315
        {
316
            SelectionCADTool ctxt = context.getOwner();
317

    
318
            boolean loopbackFlag =
319
                context.getState().getName().equals(
320
                    Selection.FirstPoint.getName());
321

    
322
            if (loopbackFlag == false)
323
            {
324
                (context.getState()).Exit(context);
325
            }
326

    
327
            context.clearState();
328
            try
329
            {
330
                ctxt.throwPointException(PluginServices.getText(this,"incorrect_point"), pointX, pointY);
331
            }
332
            finally
333
            {
334
                context.setState(Selection.FirstPoint);
335

    
336
                if (loopbackFlag == false)
337
                {
338
                    (context.getState()).Entry(context);
339
                }
340

    
341
            }
342
            return;
343
        }
344

    
345
    //-----------------------------------------------------------
346
    // Inner classse.
347
    //
348

    
349

    
350
        private static final class Selection_FirstPoint
351
            extends Selection_Default
352
        {
353
        //-------------------------------------------------------
354
        // Member methods.
355
        //
356

    
357
            private Selection_FirstPoint(String name, int id)
358
            {
359
                super (name, id);
360
            }
361

    
362
            protected void Entry(SelectionCADToolContext context)
363
            {
364
                SelectionCADTool ctxt = context.getOwner();
365

    
366
                ctxt.setQuestion(PluginServices.getText(this,"insert_point_selection"));
367
                ctxt.setDescription(new String[]{"cancel"});
368
                return;
369
            }
370

    
371
            protected void addPoint(SelectionCADToolContext context, double pointX, double pointY, InputEvent event)
372
            {
373
                SelectionCADTool ctxt = context.getOwner();
374

    
375
                if (ctxt.getType().equals(PluginServices.getText(this,"simple")) && ctxt.selectFeatures(pointX,pointY, event) && ctxt.getNextState().equals("Selection.SecondPoint"))
376
                {
377

    
378
                    (context.getState()).Exit(context);
379
                    context.clearState();
380
                    try
381
                    {
382
                        ctxt.setQuestion(PluginServices.getText(this,"insert_second_point"));
383
                        ctxt.setDescription(new String[]{"cancel"});
384
                        ctxt.addPoint(pointX, pointY, event);
385
                    }
386
                    finally
387
                    {
388
                        context.setState(Selection.SecondPoint);
389
                        (context.getState()).Entry(context);
390
                    }
391
                }
392
                else if (ctxt.getType().equals(PluginServices.getText(this,"simple")) && ctxt.getNextState().equals("Selection.WithSelectedFeatures"))
393
                {
394

    
395
                    (context.getState()).Exit(context);
396
                    context.clearState();
397
                    try
398
                    {
399
                        ctxt.setQuestion(PluginServices.getText(this,"select_handlers"));
400
                        ctxt.setDescription(new String[]{"cancel"});
401
                        ctxt.addPoint(pointX, pointY, event);
402
                        ctxt.end();
403
                    }
404
                    finally
405
                    {
406
                        context.setState(Selection.WithSelectedFeatures);
407
                        (context.getState()).Entry(context);
408
                    }
409
                }                else
410
                {
411
                    super.addPoint(context, pointX, pointY, event);
412
                }
413

    
414
                return;
415
            }
416

    
417
        //-------------------------------------------------------
418
        // Member data.
419
        //
420
        }
421

    
422
        private static final class Selection_SecondPoint
423
            extends Selection_Default
424
        {
425
        //-------------------------------------------------------
426
        // Member methods.
427
        //
428

    
429
            private Selection_SecondPoint(String name, int id)
430
            {
431
                super (name, id);
432
            }
433

    
434
            protected void addOption(SelectionCADToolContext context, String s)
435
            {
436
                SelectionCADTool ctxt = context.getOwner();
437

    
438

    
439
                (context.getState()).Exit(context);
440
                context.clearState();
441
                try
442
                {
443
                    ctxt.setQuestion(PluginServices.getText(this,"insert_point_selection"));
444
                    ctxt.setDescription(new String[]{"cancel"});
445
                    ctxt.setType(s);
446
                }
447
                finally
448
                {
449
                    context.setState(Selection.FirstPoint);
450
                    (context.getState()).Entry(context);
451
                }
452
                return;
453
            }
454

    
455
            protected void addPoint(SelectionCADToolContext context, double pointX, double pointY, InputEvent event)
456
            {
457
                SelectionCADTool ctxt = context.getOwner();
458

    
459
                if (ctxt.selectWithSecondPoint(pointX,pointY, event) > 0)
460
                {
461

    
462
                    (context.getState()).Exit(context);
463
                    context.clearState();
464
                    try
465
                    {
466
                        ctxt.setQuestion(PluginServices.getText(this,"select_handlers"));
467
                        ctxt.setDescription(new String[]{"cancel"});
468
                        ctxt.addPoint(pointX, pointY, event);
469
                        ctxt.end();
470
                    }
471
                    finally
472
                    {
473
                        context.setState(Selection.WithSelectedFeatures);
474
                        (context.getState()).Entry(context);
475
                    }
476
                }
477
                else
478
                {
479

    
480
                    (context.getState()).Exit(context);
481
                    context.clearState();
482
                    try
483
                    {
484
                        ctxt.setQuestion(PluginServices.getText(this,"insert_point_selection"));
485
                        ctxt.setDescription(new String[]{"cancel"});
486
                        ctxt.addPoint(pointX, pointY, event);
487
                    }
488
                    finally
489
                    {
490
                        context.setState(Selection.FirstPoint);
491
                        (context.getState()).Entry(context);
492
                    }
493
                }
494

    
495
                return;
496
            }
497

    
498
        //-------------------------------------------------------
499
        // Member data.
500
        //
501
        }
502

    
503
        private static final class Selection_WithSelectedFeatures
504
            extends Selection_Default
505
        {
506
        //-------------------------------------------------------
507
        // Member methods.
508
        //
509

    
510
            private Selection_WithSelectedFeatures(String name, int id)
511
            {
512
                super (name, id);
513
            }
514

    
515
            protected void addOption(SelectionCADToolContext context, String s)
516
            {
517
                SelectionCADTool ctxt = context.getOwner();
518

    
519

    
520
                (context.getState()).Exit(context);
521
                context.clearState();
522
                try
523
                {
524
                    ctxt.setQuestion(PluginServices.getText(this,"insert_point_selection"));
525
                    ctxt.setDescription(new String[]{"cancel"});
526
                    ctxt.setType(s);
527
                }
528
                finally
529
                {
530
                    context.setState(Selection.FirstPoint);
531
                    (context.getState()).Entry(context);
532
                }
533
                return;
534
            }
535

    
536
            protected void addPoint(SelectionCADToolContext context, double pointX, double pointY, InputEvent event)
537
            {
538
                SelectionCADTool ctxt = context.getOwner();
539

    
540
                if (ctxt.selectHandlers(pointX, pointY, event)>0)
541
                {
542

    
543
                    (context.getState()).Exit(context);
544
                    context.clearState();
545
                    try
546
                    {
547
                        ctxt.setQuestion(PluginServices.getText(this,"insert_destination_point"));
548
                        ctxt.setDescription(new String[]{"cancel"});
549
                        ctxt.addPoint(pointX, pointY, event);
550
                    }
551
                    finally
552
                    {
553
                        context.setState(Selection.WithHandlers);
554
                        (context.getState()).Entry(context);
555
                    }
556
                }
557
                else if (ctxt.selectFeatures(pointX,pointY, event) && ctxt.getNextState().equals("Selection.WithSelectedFeatures"))
558
                {
559
                    SelectionCADToolState endState = context.getState();
560

    
561
                    context.clearState();
562
                    try
563
                    {
564
                        ctxt.setQuestion(PluginServices.getText(this,"select_handlers"));
565
                        ctxt.setDescription(new String[]{"cancel"});
566
                        ctxt.addPoint(pointX, pointY, event);
567
                    }
568
                    finally
569
                    {
570
                        context.setState(endState);
571
                    }
572
                }
573
                else
574
                {
575

    
576
                    (context.getState()).Exit(context);
577
                    context.clearState();
578
                    try
579
                    {
580
                        ctxt.setQuestion(PluginServices.getText(this,"insert_point_selection"));
581
                        ctxt.setDescription(new String[]{"cancel"});
582
                        ctxt.addPoint(pointX, pointY, event);
583
                    }
584
                    finally
585
                    {
586
                        context.setState(Selection.FirstPoint);
587
                        (context.getState()).Entry(context);
588
                    }
589
                }
590

    
591
                return;
592
            }
593

    
594
        //-------------------------------------------------------
595
        // Member data.
596
        //
597
        }
598

    
599
        private static final class Selection_WithHandlers
600
            extends Selection_Default
601
        {
602
        //-------------------------------------------------------
603
        // Member methods.
604
        //
605

    
606
            private Selection_WithHandlers(String name, int id)
607
            {
608
                super (name, id);
609
            }
610

    
611
            protected void addPoint(SelectionCADToolContext context, double pointX, double pointY, InputEvent event)
612
            {
613
                SelectionCADTool ctxt = context.getOwner();
614

    
615

    
616
                (context.getState()).Exit(context);
617
                context.clearState();
618
                try
619
                {
620
                    ctxt.setQuestion(PluginServices.getText(this,"select_handlers"));
621
                    ctxt.setDescription(new String[]{"cancel"});
622
                    ctxt.addPoint(pointX, pointY, event);
623
                    ctxt.refresh();
624
                }
625
                finally
626
                {
627
                    context.setState(Selection.WithSelectedFeatures);
628
                    (context.getState()).Entry(context);
629
                }
630
                return;
631
            }
632

    
633
        //-------------------------------------------------------
634
        // Member data.
635
        //
636
        }
637

    
638
    //-----------------------------------------------------------
639
    // Member data.
640
    //
641
    }
642
}