Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / smc / SplineCADToolContext.java @ 29616

History | View | Annotate | Download (14.3 KB)

1

    
2
//
3
// Vicente Caballero Navarro
4

    
5

    
6
package org.gvsig.editing.gui.cad.tools.smc;
7

    
8
import java.awt.event.InputEvent;
9

    
10
import org.gvsig.andami.PluginServices;
11
import org.gvsig.editing.gui.cad.tools.SplineCADTool;
12

    
13

    
14
public final class SplineCADToolContext
15
    extends statemap.FSMContext
16
{
17
//---------------------------------------------------------------
18
// Member methods.
19
//
20

    
21
    public SplineCADToolContext(SplineCADTool owner)
22
    {
23
        super();
24

    
25
        _owner = owner;
26
        setState(Spline.FirstPoint);
27
        Spline.FirstPoint.Entry(this);
28
    }
29

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

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

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

    
54
    public void endPoint(double pointX, double pointY, InputEvent event)
55
    {
56
        _transition = "endPoint";
57
        getState().endPoint(this, pointX, pointY, event);
58
        _transition = "";
59
        return;
60
    }
61

    
62
    public SplineCADToolState getState()
63
        throws statemap.StateUndefinedException
64
    {
65
        if (_state == null)
66
        {
67
            throw(
68
                new statemap.StateUndefinedException());
69
        }
70

    
71
        return ((SplineCADToolState) _state);
72
    }
73

    
74
    protected SplineCADTool getOwner()
75
    {
76
        return (_owner);
77
    }
78

    
79
//---------------------------------------------------------------
80
// Member data.
81
//
82

    
83
    transient private SplineCADTool _owner;
84

    
85
//---------------------------------------------------------------
86
// Inner classes.
87
//
88

    
89
    public static abstract class SplineCADToolState
90
        extends statemap.State
91
    {
92
    //-----------------------------------------------------------
93
    // Member methods.
94
    //
95

    
96
        protected SplineCADToolState(String name, int id)
97
        {
98
            super (name, id);
99
        }
100

    
101
        protected void Entry(SplineCADToolContext context) {}
102
        protected void Exit(SplineCADToolContext context) {}
103

    
104
        protected void addOption(SplineCADToolContext context, String s)
105
        {
106
            Default(context);
107
        }
108

    
109
        protected void addPoint(SplineCADToolContext context, double pointX, double pointY, InputEvent event)
110
        {
111
            Default(context);
112
        }
113

    
114
        protected void addValue(SplineCADToolContext context, double d)
115
        {
116
            Default(context);
117
        }
118

    
119
        protected void endPoint(SplineCADToolContext context, double pointX, double pointY, InputEvent event)
120
        {
121
            Default(context);
122
        }
123

    
124
        protected void Default(SplineCADToolContext context)
125
        {
126
            throw (
127
                new statemap.TransitionUndefinedException(
128
                    "State: " +
129
                    context.getState().getName() +
130
                    ", Transition: " +
131
                    context.getTransition()));
132
        }
133

    
134
    //-----------------------------------------------------------
135
    // Member data.
136
    //
137
    }
138

    
139
    /* package */ static abstract class Spline
140
    {
141
    //-----------------------------------------------------------
142
    // Member methods.
143
    //
144

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

    
149
        //-------------------------------------------------------
150
        // Statics.
151
        //
152
        /* package */ static Spline_Default.Spline_FirstPoint FirstPoint;
153
        /* package */ static Spline_Default.Spline_NextPoint NextPoint;
154
        private static Spline_Default Default;
155

    
156
        static
157
        {
158
            FirstPoint = new Spline_Default.Spline_FirstPoint("Spline.FirstPoint", 0);
159
            NextPoint = new Spline_Default.Spline_NextPoint("Spline.NextPoint", 1);
160
            Default = new Spline_Default("Spline.Default", -1);
161
        }
162

    
163
    }
164

    
165
    protected static class Spline_Default
166
        extends SplineCADToolState
167
    {
168
    //-----------------------------------------------------------
169
    // Member methods.
170
    //
171

    
172
        protected Spline_Default(String name, int id)
173
        {
174
            super (name, id);
175
        }
176

    
177
        protected void addOption(SplineCADToolContext context, String s)
178
        {
179
            SplineCADTool ctxt = context.getOwner();
180

    
181
            if (s.equals(PluginServices.getText(this,"cancel")))
182
            {
183
                boolean loopbackFlag =
184
                    context.getState().getName().equals(
185
                        Spline.FirstPoint.getName());
186

    
187
                if (loopbackFlag == false)
188
                {
189
                    (context.getState()).Exit(context);
190
                }
191

    
192
                context.clearState();
193
                try
194
                {
195
                    ctxt.cancel();
196
                }
197
                finally
198
                {
199
                    context.setState(Spline.FirstPoint);
200

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

    
206
                }
207
            }
208
            else if (s.equals(""))
209
            {
210
                boolean loopbackFlag =
211
                    context.getState().getName().equals(
212
                        Spline.FirstPoint.getName());
213

    
214
                if (loopbackFlag == false)
215
                {
216
                    (context.getState()).Exit(context);
217
                }
218

    
219
                context.clearState();
220
                try
221
                {
222
                    ctxt.endGeometry();
223
                }
224
                finally
225
                {
226
                    context.setState(Spline.FirstPoint);
227

    
228
                    if (loopbackFlag == false)
229
                    {
230
                        (context.getState()).Entry(context);
231
                    }
232

    
233
                }
234
            }
235
            else
236
            {
237
                boolean loopbackFlag =
238
                    context.getState().getName().equals(
239
                        Spline.FirstPoint.getName());
240

    
241
                if (loopbackFlag == false)
242
                {
243
                    (context.getState()).Exit(context);
244
                }
245

    
246
                context.clearState();
247
                try
248
                {
249
                    ctxt.throwOptionException(PluginServices.getText(this,"incorrect_option"), s);
250
                }
251
                finally
252
                {
253
                    context.setState(Spline.FirstPoint);
254

    
255
                    if (loopbackFlag == false)
256
                    {
257
                        (context.getState()).Entry(context);
258
                    }
259

    
260
                }
261
            }
262

    
263
            return;
264
        }
265

    
266
        protected void addValue(SplineCADToolContext context, double d)
267
        {
268
            SplineCADTool ctxt = context.getOwner();
269

    
270
            boolean loopbackFlag =
271
                context.getState().getName().equals(
272
                    Spline.FirstPoint.getName());
273

    
274
            if (loopbackFlag == false)
275
            {
276
                (context.getState()).Exit(context);
277
            }
278

    
279
            context.clearState();
280
            try
281
            {
282
                ctxt.throwValueException(PluginServices.getText(this,"incorrect_value"), d);
283
            }
284
            finally
285
            {
286
                context.setState(Spline.FirstPoint);
287

    
288
                if (loopbackFlag == false)
289
                {
290
                    (context.getState()).Entry(context);
291
                }
292

    
293
            }
294
            return;
295
        }
296

    
297
        protected void addPoint(SplineCADToolContext context, double pointX, double pointY, InputEvent event)
298
        {
299
            SplineCADTool ctxt = context.getOwner();
300

    
301
            boolean loopbackFlag =
302
                context.getState().getName().equals(
303
                    Spline.FirstPoint.getName());
304

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

    
310
            context.clearState();
311
            try
312
            {
313
                ctxt.throwPointException(PluginServices.getText(this,"incorrect_point"), pointX, pointY);
314
            }
315
            finally
316
            {
317
                context.setState(Spline.FirstPoint);
318

    
319
                if (loopbackFlag == false)
320
                {
321
                    (context.getState()).Entry(context);
322
                }
323

    
324
            }
325
            return;
326
        }
327

    
328
        protected void endPoint(SplineCADToolContext context, double pointX, double pointY, InputEvent event)
329
        {
330
            SplineCADTool ctxt = context.getOwner();
331

    
332
            boolean loopbackFlag =
333
                context.getState().getName().equals(
334
                    Spline.FirstPoint.getName());
335

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

    
341
            context.clearState();
342
            try
343
            {
344
                ctxt.addPoint(pointX, pointY, event);
345
                ctxt.endGeometry();
346
            }
347
            finally
348
            {
349
                context.setState(Spline.FirstPoint);
350

    
351
                if (loopbackFlag == false)
352
                {
353
                    (context.getState()).Entry(context);
354
                }
355

    
356
            }
357
            return;
358
        }
359

    
360
    //-----------------------------------------------------------
361
    // Inner classse.
362
    //
363

    
364

    
365
        private static final class Spline_FirstPoint
366
            extends Spline_Default
367
        {
368
        //-------------------------------------------------------
369
        // Member methods.
370
        //
371

    
372
            private Spline_FirstPoint(String name, int id)
373
            {
374
                super (name, id);
375
            }
376

    
377
            protected void Entry(SplineCADToolContext context)
378
            {
379
                SplineCADTool ctxt = context.getOwner();
380

    
381
                ctxt.setQuestion(PluginServices.getText(this,"insert_first_point"));
382
                ctxt.setDescription(new String[]{"cancel"});
383
                return;
384
            }
385

    
386
            protected void addPoint(SplineCADToolContext context, double pointX, double pointY, InputEvent event)
387
            {
388
                SplineCADTool ctxt = context.getOwner();
389

    
390

    
391
                (context.getState()).Exit(context);
392
                context.clearState();
393
                try
394
                {
395
                    ctxt.setQuestion(PluginServices.getText(this,"insert_next_point")+", "+
396
                                        PluginServices.getText(this,"close")+" "+
397
                                        "["+PluginServices.getText(this,"SplineCADTool.close")+"] "+
398
                                        PluginServices.getText(this,"cad.or")+" "+
399
                                        PluginServices.getText(this,"end")+" "+
400
                                        "["+PluginServices.getText(this,"SplineCADTool.end")+"]");
401
                    ctxt.setDescription(new String[]{"close", "terminate", "cancel"});
402
                    ctxt.addPoint(pointX, pointY, event);
403
                }
404
                finally
405
                {
406
                    context.setState(Spline.NextPoint);
407
                    (context.getState()).Entry(context);
408
                }
409
                return;
410
            }
411

    
412
        //-------------------------------------------------------
413
        // Member data.
414
        //
415
        }
416

    
417
        private static final class Spline_NextPoint
418
            extends Spline_Default
419
        {
420
        //-------------------------------------------------------
421
        // Member methods.
422
        //
423

    
424
            private Spline_NextPoint(String name, int id)
425
            {
426
                super (name, id);
427
            }
428

    
429
            protected void addOption(SplineCADToolContext context, String s)
430
            {
431
                SplineCADTool ctxt = context.getOwner();
432

    
433
                if (s.equalsIgnoreCase(PluginServices.getText(this,"SplineCADTool.close")) || s.equals(PluginServices.getText(this,"close")))
434
                {
435

    
436
                    (context.getState()).Exit(context);
437
                    context.clearState();
438
                    try
439
                    {
440
                        ctxt.addOption(s);
441
                        ctxt.closeGeometry();
442
                        ctxt.endGeometry();
443
                        ctxt.end();
444
                    }
445
                    finally
446
                    {
447
                        context.setState(Spline.FirstPoint);
448
                        (context.getState()).Entry(context);
449
                    }
450
                }
451
                else if (s.equalsIgnoreCase(PluginServices.getText(this,"SplineCADTool.end")) || s.equals(PluginServices.getText(this,"terminate")))
452
                {
453

    
454
                    (context.getState()).Exit(context);
455
                    context.clearState();
456
                    try
457
                    {
458
                        ctxt.addOption(s);
459
                        ctxt.endGeometry();
460
                        ctxt.end();
461
                    }
462
                    finally
463
                    {
464
                        context.setState(Spline.FirstPoint);
465
                        (context.getState()).Entry(context);
466
                    }
467
                }                else
468
                {
469
                    super.addOption(context, s);
470
                }
471

    
472
                return;
473
            }
474

    
475
            protected void addPoint(SplineCADToolContext context, double pointX, double pointY, InputEvent event)
476
            {
477
                SplineCADTool ctxt = context.getOwner();
478

    
479
                SplineCADToolState endState = context.getState();
480

    
481
                context.clearState();
482
                try
483
                {
484
                    ctxt.setQuestion(PluginServices.getText(this,"insert_next_point")+", "+
485
                                PluginServices.getText(this,"close")+" "+
486
                                "["+PluginServices.getText(this,"SplineCADTool.close")+"] "+
487
                                PluginServices.getText(this,"cad.or")+" "+
488
                                PluginServices.getText(this,"end")+" "+
489
                                "["+PluginServices.getText(this,"SplineCADTool.end")+"]");
490
                    ctxt.setDescription(new String[]{"close", "terminate", "cancel"});
491
                    ctxt.addPoint(pointX, pointY, event);
492
                }
493
                finally
494
                {
495
                    context.setState(endState);
496
                }
497
                return;
498
            }
499

    
500
        //-------------------------------------------------------
501
        // Member data.
502
        //
503
        }
504

    
505
    //-----------------------------------------------------------
506
    // Member data.
507
    //
508
    }
509
}