Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / smc / PolygonCADToolContext.java @ 9121

History | View | Annotate | Download (16.7 KB)

1

    
2
//
3
// Vicente Caballero Navarro
4

    
5

    
6
package com.iver.cit.gvsig.gui.cad.tools.smc;
7

    
8
import com.iver.cit.gvsig.gui.cad.tools.PolygonCADTool;
9
import java.awt.event.InputEvent;
10
import com.iver.andami.PluginServices;
11

    
12
public final class PolygonCADToolContext
13
    extends statemap.FSMContext
14
{
15
//---------------------------------------------------------------
16
// Member methods.
17
//
18

    
19
    public PolygonCADToolContext(PolygonCADTool owner)
20
    {
21
        super();
22

    
23
        _owner = owner;
24
        setState(Polygon.NumberOrCenterPoint);
25
        Polygon.NumberOrCenterPoint.Entry(this);
26
    }
27

    
28
    public void addOption(String s)
29
    {
30
        _transition = "addOption";
31
        getState().addOption(this, s);
32
        _transition = "";
33
        return;
34
    }
35

    
36
    public void addPoint(double pointX, double pointY, InputEvent event)
37
    {
38
        _transition = "addPoint";
39
        getState().addPoint(this, pointX, pointY, event);
40
        _transition = "";
41
        return;
42
    }
43

    
44
    public void addValue(double d)
45
    {
46
        _transition = "addValue";
47
        getState().addValue(this, d);
48
        _transition = "";
49
        return;
50
    }
51

    
52
    public PolygonCADToolState getState()
53
        throws statemap.StateUndefinedException
54
    {
55
        if (_state == null)
56
        {
57
            throw(
58
                new statemap.StateUndefinedException());
59
        }
60

    
61
        return ((PolygonCADToolState) _state);
62
    }
63

    
64
    protected PolygonCADTool getOwner()
65
    {
66
        return (_owner);
67
    }
68

    
69
//---------------------------------------------------------------
70
// Member data.
71
//
72

    
73
    transient private PolygonCADTool _owner;
74

    
75
//---------------------------------------------------------------
76
// Inner classes.
77
//
78

    
79
    public static abstract class PolygonCADToolState
80
        extends statemap.State
81
    {
82
    //-----------------------------------------------------------
83
    // Member methods.
84
    //
85

    
86
        protected PolygonCADToolState(String name, int id)
87
        {
88
            super (name, id);
89
        }
90

    
91
        protected void Entry(PolygonCADToolContext context) {}
92
        protected void Exit(PolygonCADToolContext context) {}
93

    
94
        protected void addOption(PolygonCADToolContext context, String s)
95
        {
96
            Default(context);
97
        }
98

    
99
        protected void addPoint(PolygonCADToolContext context, double pointX, double pointY, InputEvent event)
100
        {
101
            Default(context);
102
        }
103

    
104
        protected void addValue(PolygonCADToolContext context, double d)
105
        {
106
            Default(context);
107
        }
108

    
109
        protected void Default(PolygonCADToolContext context)
110
        {
111
            throw (
112
                new statemap.TransitionUndefinedException(
113
                    "State: " +
114
                    context.getState().getName() +
115
                    ", Transition: " +
116
                    context.getTransition()));
117
        }
118

    
119
    //-----------------------------------------------------------
120
    // Member data.
121
    //
122
    }
123

    
124
    /* package */ static abstract class Polygon
125
    {
126
    //-----------------------------------------------------------
127
    // Member methods.
128
    //
129

    
130
    //-----------------------------------------------------------
131
    // Member data.
132
    //
133

    
134
        //-------------------------------------------------------
135
        // Statics.
136
        //
137
        /* package */ static Polygon_Default.Polygon_NumberOrCenterPoint NumberOrCenterPoint;
138
        /* package */ static Polygon_Default.Polygon_CenterPoint CenterPoint;
139
        /* package */ static Polygon_Default.Polygon_OptionOrRadiusOrPoint OptionOrRadiusOrPoint;
140
        /* package */ static Polygon_Default.Polygon_RadiusOrPoint RadiusOrPoint;
141
        private static Polygon_Default Default;
142

    
143
        static
144
        {
145
            NumberOrCenterPoint = new Polygon_Default.Polygon_NumberOrCenterPoint("Polygon.NumberOrCenterPoint", 0);
146
            CenterPoint = new Polygon_Default.Polygon_CenterPoint("Polygon.CenterPoint", 1);
147
            OptionOrRadiusOrPoint = new Polygon_Default.Polygon_OptionOrRadiusOrPoint("Polygon.OptionOrRadiusOrPoint", 2);
148
            RadiusOrPoint = new Polygon_Default.Polygon_RadiusOrPoint("Polygon.RadiusOrPoint", 3);
149
            Default = new Polygon_Default("Polygon.Default", -1);
150
        }
151

    
152
    }
153

    
154
    protected static class Polygon_Default
155
        extends PolygonCADToolState
156
    {
157
    //-----------------------------------------------------------
158
    // Member methods.
159
    //
160

    
161
        protected Polygon_Default(String name, int id)
162
        {
163
            super (name, id);
164
        }
165

    
166
        protected void addOption(PolygonCADToolContext context, String s)
167
        {
168
            PolygonCADTool ctxt = context.getOwner();
169

    
170
            if (s.equals(PluginServices.getText(this,"cancel")))
171
            {
172
                boolean loopbackFlag =
173
                    context.getState().getName().equals(
174
                        Polygon.NumberOrCenterPoint.getName());
175

    
176
                if (loopbackFlag == false)
177
                {
178
                    (context.getState()).Exit(context);
179
                }
180

    
181
                context.clearState();
182
                try
183
                {
184
                    ctxt.end();
185
                }
186
                finally
187
                {
188
                    context.setState(Polygon.NumberOrCenterPoint);
189

    
190
                    if (loopbackFlag == false)
191
                    {
192
                        (context.getState()).Entry(context);
193
                    }
194

    
195
                }
196
            }
197
            else
198
            {
199
                boolean loopbackFlag =
200
                    context.getState().getName().equals(
201
                        Polygon.NumberOrCenterPoint.getName());
202

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

    
208
                context.clearState();
209
                try
210
                {
211
                    ctxt.throwOptionException(PluginServices.getText(this,"incorrect_option"), s);
212
                }
213
                finally
214
                {
215
                    context.setState(Polygon.NumberOrCenterPoint);
216

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

    
222
                }
223
            }
224

    
225
            return;
226
        }
227

    
228
        protected void addValue(PolygonCADToolContext context, double d)
229
        {
230
            PolygonCADTool ctxt = context.getOwner();
231

    
232
            boolean loopbackFlag =
233
                context.getState().getName().equals(
234
                    Polygon.NumberOrCenterPoint.getName());
235

    
236
            if (loopbackFlag == false)
237
            {
238
                (context.getState()).Exit(context);
239
            }
240

    
241
            context.clearState();
242
            try
243
            {
244
                ctxt.throwValueException(PluginServices.getText(this,"incorrect_value"), d);
245
            }
246
            finally
247
            {
248
                context.setState(Polygon.NumberOrCenterPoint);
249

    
250
                if (loopbackFlag == false)
251
                {
252
                    (context.getState()).Entry(context);
253
                }
254

    
255
            }
256
            return;
257
        }
258

    
259
        protected void addPoint(PolygonCADToolContext context, double pointX, double pointY, InputEvent event)
260
        {
261
            PolygonCADTool ctxt = context.getOwner();
262

    
263
            boolean loopbackFlag =
264
                context.getState().getName().equals(
265
                    Polygon.NumberOrCenterPoint.getName());
266

    
267
            if (loopbackFlag == false)
268
            {
269
                (context.getState()).Exit(context);
270
            }
271

    
272
            context.clearState();
273
            try
274
            {
275
                ctxt.throwPointException(PluginServices.getText(this,"incorrect_point"), pointX, pointY);
276
            }
277
            finally
278
            {
279
                context.setState(Polygon.NumberOrCenterPoint);
280

    
281
                if (loopbackFlag == false)
282
                {
283
                    (context.getState()).Entry(context);
284
                }
285

    
286
            }
287
            return;
288
        }
289

    
290
    //-----------------------------------------------------------
291
    // Inner classse.
292
    //
293

    
294

    
295
        private static final class Polygon_NumberOrCenterPoint
296
            extends Polygon_Default
297
        {
298
        //-------------------------------------------------------
299
        // Member methods.
300
        //
301

    
302
            private Polygon_NumberOrCenterPoint(String name, int id)
303
            {
304
                super (name, id);
305
            }
306

    
307
            protected void Entry(PolygonCADToolContext context)
308
            {
309
                PolygonCADTool ctxt = context.getOwner();
310

    
311
                ctxt.setQuestion(PluginServices.getText(this,"insert_number_sides"));
312
                ctxt.setDescription(new String[]{"cancel"});
313
                return;
314
            }
315

    
316
            protected void addPoint(PolygonCADToolContext context, double pointX, double pointY, InputEvent event)
317
            {
318
                PolygonCADTool ctxt = context.getOwner();
319

    
320

    
321
                (context.getState()).Exit(context);
322
                context.clearState();
323
                try
324
                {
325
                    ctxt.setQuestion(PluginServices.getText(this,"into_circle")+" "+
326
                                        "["+PluginServices.getText(this,"PolygonCADTool.into_circle")+"]"+
327
                                    PluginServices.getText(this,"cad.or")+" "+
328
                                    PluginServices.getText(this,"circumscribed")+
329
                                           "["+PluginServices.getText(this,"PolygonCADTool.circumscribed")+"]");
330
                    ctxt.setDescription(new String[]{"into_circle", "circumscribed", "cancel"});
331
                    ctxt.addPoint(pointX, pointY, event);
332
                }
333
                finally
334
                {
335
                    context.setState(Polygon.OptionOrRadiusOrPoint);
336
                    (context.getState()).Entry(context);
337
                }
338
                return;
339
            }
340

    
341
            protected void addValue(PolygonCADToolContext context, double d)
342
            {
343
                PolygonCADTool ctxt = context.getOwner();
344

    
345
                if (d<3)
346
                {
347
                    PolygonCADToolState endState = context.getState();
348

    
349
                    context.clearState();
350
                    try
351
                    {
352
                        ctxt.throwValueException(PluginServices.getText(this,"num_lados_insuficiente"), d);
353
                    }
354
                    finally
355
                    {
356
                        context.setState(endState);
357
                    }
358
                }
359
                else if (d>2)
360
                {
361

    
362
                    (context.getState()).Exit(context);
363
                    context.clearState();
364
                    try
365
                    {
366
                        ctxt.setQuestion(PluginServices.getText(this,"insert_central_point_polygon"));
367
                        ctxt.setDescription(new String[]{"cancel"});
368
                        ctxt.addValue(d);
369
                    }
370
                    finally
371
                    {
372
                        context.setState(Polygon.CenterPoint);
373
                        (context.getState()).Entry(context);
374
                    }
375
                }                else
376
                {
377
                    super.addValue(context, d);
378
                }
379

    
380
                return;
381
            }
382

    
383
        //-------------------------------------------------------
384
        // Member data.
385
        //
386
        }
387

    
388
        private static final class Polygon_CenterPoint
389
            extends Polygon_Default
390
        {
391
        //-------------------------------------------------------
392
        // Member methods.
393
        //
394

    
395
            private Polygon_CenterPoint(String name, int id)
396
            {
397
                super (name, id);
398
            }
399

    
400
            protected void addPoint(PolygonCADToolContext context, double pointX, double pointY, InputEvent event)
401
            {
402
                PolygonCADTool ctxt = context.getOwner();
403

    
404

    
405
                (context.getState()).Exit(context);
406
                context.clearState();
407
                try
408
                {
409
                    ctxt.setQuestion(PluginServices.getText(this,"into_circle")+" "+
410
                                "["+PluginServices.getText(this,"PolygonCADTool.into_circle")+"]"+
411
                            PluginServices.getText(this,"cad.or")+" "+
412
                            PluginServices.getText(this,"circumscribed")+
413
                                   "["+PluginServices.getText(this,"PolygonCADTool.circumscribed")+"]");
414
                    ctxt.setDescription(new String[]{"into_circle", "circumscribed", "cancel"});
415
                    ctxt.addPoint(pointX, pointY, event);
416
                }
417
                finally
418
                {
419
                    context.setState(Polygon.OptionOrRadiusOrPoint);
420
                    (context.getState()).Entry(context);
421
                }
422
                return;
423
            }
424

    
425
        //-------------------------------------------------------
426
        // Member data.
427
        //
428
        }
429

    
430
        private static final class Polygon_OptionOrRadiusOrPoint
431
            extends Polygon_Default
432
        {
433
        //-------------------------------------------------------
434
        // Member methods.
435
        //
436

    
437
            private Polygon_OptionOrRadiusOrPoint(String name, int id)
438
            {
439
                super (name, id);
440
            }
441

    
442
            protected void addOption(PolygonCADToolContext context, String s)
443
            {
444
                PolygonCADTool ctxt = context.getOwner();
445

    
446

    
447
                (context.getState()).Exit(context);
448
                context.clearState();
449
                try
450
                {
451
                    ctxt.setQuestion(PluginServices.getText(this,"insert_radius"));
452
                    ctxt.setDescription(new String[]{"cancel"});
453
                    ctxt.addOption(s);
454
                }
455
                finally
456
                {
457
                    context.setState(Polygon.RadiusOrPoint);
458
                    (context.getState()).Entry(context);
459
                }
460
                return;
461
            }
462

    
463
            protected void addPoint(PolygonCADToolContext context, double pointX, double pointY, InputEvent event)
464
            {
465
                PolygonCADTool ctxt = context.getOwner();
466

    
467

    
468
                (context.getState()).Exit(context);
469
                context.clearState();
470
                try
471
                {
472
                    ctxt.addPoint(pointX, pointY, event);
473
                    ctxt.end();
474
                }
475
                finally
476
                {
477
                    context.setState(Polygon.NumberOrCenterPoint);
478
                    (context.getState()).Entry(context);
479
                }
480
                return;
481
            }
482

    
483
            protected void addValue(PolygonCADToolContext context, double d)
484
            {
485
                PolygonCADTool ctxt = context.getOwner();
486

    
487

    
488
                (context.getState()).Exit(context);
489
                context.clearState();
490
                try
491
                {
492
                    ctxt.addValue(d);
493
                    ctxt.end();
494
                }
495
                finally
496
                {
497
                    context.setState(Polygon.NumberOrCenterPoint);
498
                    (context.getState()).Entry(context);
499
                }
500
                return;
501
            }
502

    
503
        //-------------------------------------------------------
504
        // Member data.
505
        //
506
        }
507

    
508
        private static final class Polygon_RadiusOrPoint
509
            extends Polygon_Default
510
        {
511
        //-------------------------------------------------------
512
        // Member methods.
513
        //
514

    
515
            private Polygon_RadiusOrPoint(String name, int id)
516
            {
517
                super (name, id);
518
            }
519

    
520
            protected void addPoint(PolygonCADToolContext context, double pointX, double pointY, InputEvent event)
521
            {
522
                PolygonCADTool ctxt = context.getOwner();
523

    
524

    
525
                (context.getState()).Exit(context);
526
                context.clearState();
527
                try
528
                {
529
                    ctxt.addPoint(pointX, pointY, event);
530
                    ctxt.end();
531
                }
532
                finally
533
                {
534
                    context.setState(Polygon.NumberOrCenterPoint);
535
                    (context.getState()).Entry(context);
536
                }
537
                return;
538
            }
539

    
540
            protected void addValue(PolygonCADToolContext context, double d)
541
            {
542
                PolygonCADTool ctxt = context.getOwner();
543

    
544

    
545
                (context.getState()).Exit(context);
546
                context.clearState();
547
                try
548
                {
549
                    ctxt.addValue(d);
550
                    ctxt.end();
551
                }
552
                finally
553
                {
554
                    context.setState(Polygon.NumberOrCenterPoint);
555
                    (context.getState()).Entry(context);
556
                }
557
                return;
558
            }
559

    
560
        //-------------------------------------------------------
561
        // Member data.
562
        //
563
        }
564

    
565
    //-----------------------------------------------------------
566
    // Member data.
567
    //
568
    }
569
}