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 / SplineCADToolContext.java @ 40557

History | View | Annotate | Download (15.2 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.SplineCADTool;
35

    
36

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

    
44
    public SplineCADToolContext(SplineCADTool owner)
45
    {
46
        super();
47

    
48
        _owner = owner;
49
        setState(Spline.FirstPoint);
50
        Spline.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 void endPoint(double pointX, double pointY, InputEvent event)
78
    {
79
        _transition = "endPoint";
80
        getState().endPoint(this, pointX, pointY, event);
81
        _transition = "";
82
        return;
83
    }
84

    
85
    public SplineCADToolState getState()
86
        throws statemap.StateUndefinedException
87
    {
88
        if (_state == null)
89
        {
90
            throw(
91
                new statemap.StateUndefinedException());
92
        }
93

    
94
        return ((SplineCADToolState) _state);
95
    }
96

    
97
    protected SplineCADTool getOwner()
98
    {
99
        return (_owner);
100
    }
101

    
102
//---------------------------------------------------------------
103
// Member data.
104
//
105

    
106
    transient private SplineCADTool _owner;
107

    
108
//---------------------------------------------------------------
109
// Inner classes.
110
//
111

    
112
    public static abstract class SplineCADToolState
113
        extends statemap.State
114
    {
115
    //-----------------------------------------------------------
116
    // Member methods.
117
    //
118

    
119
        protected SplineCADToolState(String name, int id)
120
        {
121
            super (name, id);
122
        }
123

    
124
        protected void Entry(SplineCADToolContext context) {}
125
        protected void Exit(SplineCADToolContext context) {}
126

    
127
        protected void addOption(SplineCADToolContext context, String s)
128
        {
129
            Default(context);
130
        }
131

    
132
        protected void addPoint(SplineCADToolContext context, double pointX, double pointY, InputEvent event)
133
        {
134
            Default(context);
135
        }
136

    
137
        protected void addValue(SplineCADToolContext context, double d)
138
        {
139
            Default(context);
140
        }
141

    
142
        protected void endPoint(SplineCADToolContext context, double pointX, double pointY, InputEvent event)
143
        {
144
            Default(context);
145
        }
146

    
147
        protected void Default(SplineCADToolContext context)
148
        {
149
            throw (
150
                new statemap.TransitionUndefinedException(
151
                    "State: " +
152
                    context.getState().getName() +
153
                    ", Transition: " +
154
                    context.getTransition()));
155
        }
156

    
157
    //-----------------------------------------------------------
158
    // Member data.
159
    //
160
    }
161

    
162
    /* package */ static abstract class Spline
163
    {
164
    //-----------------------------------------------------------
165
    // Member methods.
166
    //
167

    
168
    //-----------------------------------------------------------
169
    // Member data.
170
    //
171

    
172
        //-------------------------------------------------------
173
        // Statics.
174
        //
175
        /* package */ static Spline_Default.Spline_FirstPoint FirstPoint;
176
        /* package */ static Spline_Default.Spline_NextPoint NextPoint;
177
        private static Spline_Default Default;
178

    
179
        static
180
        {
181
            FirstPoint = new Spline_Default.Spline_FirstPoint("Spline.FirstPoint", 0);
182
            NextPoint = new Spline_Default.Spline_NextPoint("Spline.NextPoint", 1);
183
            Default = new Spline_Default("Spline.Default", -1);
184
        }
185

    
186
    }
187

    
188
    protected static class Spline_Default
189
        extends SplineCADToolState
190
    {
191
    //-----------------------------------------------------------
192
    // Member methods.
193
    //
194

    
195
        protected Spline_Default(String name, int id)
196
        {
197
            super (name, id);
198
        }
199

    
200
        protected void addOption(SplineCADToolContext context, String s)
201
        {
202
            SplineCADTool ctxt = context.getOwner();
203

    
204
            if (s.equals(PluginServices.getText(this,"cancel")))
205
            {
206
                boolean loopbackFlag =
207
                    context.getState().getName().equals(
208
                        Spline.FirstPoint.getName());
209

    
210
                if (loopbackFlag == false)
211
                {
212
                    (context.getState()).Exit(context);
213
                }
214

    
215
                context.clearState();
216
                try
217
                {
218
                    ctxt.cancel();
219
                }
220
                finally
221
                {
222
                    context.setState(Spline.FirstPoint);
223

    
224
                    if (loopbackFlag == false)
225
                    {
226
                        (context.getState()).Entry(context);
227
                    }
228

    
229
                }
230
            }
231
            else if (s.equals(""))
232
            {
233
                boolean loopbackFlag =
234
                    context.getState().getName().equals(
235
                        Spline.FirstPoint.getName());
236

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

    
242
                context.clearState();
243
                try
244
                {
245
                    ctxt.endGeometry();
246
                }
247
                finally
248
                {
249
                    context.setState(Spline.FirstPoint);
250

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

    
256
                }
257
            }
258
            else
259
            {
260
                boolean loopbackFlag =
261
                    context.getState().getName().equals(
262
                        Spline.FirstPoint.getName());
263

    
264
                if (loopbackFlag == false)
265
                {
266
                    (context.getState()).Exit(context);
267
                }
268

    
269
                context.clearState();
270
                try
271
                {
272
                    ctxt.throwOptionException(PluginServices.getText(this,"incorrect_option"), s);
273
                }
274
                finally
275
                {
276
                    context.setState(Spline.FirstPoint);
277

    
278
                    if (loopbackFlag == false)
279
                    {
280
                        (context.getState()).Entry(context);
281
                    }
282

    
283
                }
284
            }
285

    
286
            return;
287
        }
288

    
289
        protected void addValue(SplineCADToolContext context, double d)
290
        {
291
            SplineCADTool ctxt = context.getOwner();
292

    
293
            boolean loopbackFlag =
294
                context.getState().getName().equals(
295
                    Spline.FirstPoint.getName());
296

    
297
            if (loopbackFlag == false)
298
            {
299
                (context.getState()).Exit(context);
300
            }
301

    
302
            context.clearState();
303
            try
304
            {
305
                ctxt.throwValueException(PluginServices.getText(this,"incorrect_value"), d);
306
            }
307
            finally
308
            {
309
                context.setState(Spline.FirstPoint);
310

    
311
                if (loopbackFlag == false)
312
                {
313
                    (context.getState()).Entry(context);
314
                }
315

    
316
            }
317
            return;
318
        }
319

    
320
        protected void addPoint(SplineCADToolContext context, double pointX, double pointY, InputEvent event)
321
        {
322
            SplineCADTool ctxt = context.getOwner();
323

    
324
            boolean loopbackFlag =
325
                context.getState().getName().equals(
326
                    Spline.FirstPoint.getName());
327

    
328
            if (loopbackFlag == false)
329
            {
330
                (context.getState()).Exit(context);
331
            }
332

    
333
            context.clearState();
334
            try
335
            {
336
                ctxt.throwPointException(PluginServices.getText(this,"incorrect_point"), pointX, pointY);
337
            }
338
            finally
339
            {
340
                context.setState(Spline.FirstPoint);
341

    
342
                if (loopbackFlag == false)
343
                {
344
                    (context.getState()).Entry(context);
345
                }
346

    
347
            }
348
            return;
349
        }
350

    
351
        protected void endPoint(SplineCADToolContext context, double pointX, double pointY, InputEvent event)
352
        {
353
            SplineCADTool ctxt = context.getOwner();
354

    
355
            boolean loopbackFlag =
356
                context.getState().getName().equals(
357
                    Spline.FirstPoint.getName());
358

    
359
            if (loopbackFlag == false)
360
            {
361
                (context.getState()).Exit(context);
362
            }
363

    
364
            context.clearState();
365
            try
366
            {
367
                ctxt.addPoint(pointX, pointY, event);
368
                ctxt.endGeometry();
369
            }
370
            finally
371
            {
372
                context.setState(Spline.FirstPoint);
373

    
374
                if (loopbackFlag == false)
375
                {
376
                    (context.getState()).Entry(context);
377
                }
378

    
379
            }
380
            return;
381
        }
382

    
383
    //-----------------------------------------------------------
384
    // Inner classse.
385
    //
386

    
387

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

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

    
400
            protected void Entry(SplineCADToolContext context)
401
            {
402
                SplineCADTool ctxt = context.getOwner();
403

    
404
                ctxt.setQuestion(PluginServices.getText(this,"insert_first_point"));
405
                ctxt.setDescription(new String[]{"cancel"});
406
                return;
407
            }
408

    
409
            protected void addPoint(SplineCADToolContext context, double pointX, double pointY, InputEvent event)
410
            {
411
                SplineCADTool ctxt = context.getOwner();
412

    
413

    
414
                (context.getState()).Exit(context);
415
                context.clearState();
416
                try
417
                {
418
                    ctxt.setQuestion(PluginServices.getText(this,"insert_next_point")+", "+
419
                                        PluginServices.getText(this,"close")+" "+
420
                                        "["+PluginServices.getText(this,"SplineCADTool.close")+"] "+
421
                                        PluginServices.getText(this,"cad.or")+" "+
422
                                        PluginServices.getText(this,"end")+" "+
423
                                        "["+PluginServices.getText(this,"SplineCADTool.end")+"]");
424
                    ctxt.setDescription(new String[]{"close", "terminate", "cancel"});
425
                    ctxt.addPoint(pointX, pointY, event);
426
                }
427
                finally
428
                {
429
                    context.setState(Spline.NextPoint);
430
                    (context.getState()).Entry(context);
431
                }
432
                return;
433
            }
434

    
435
        //-------------------------------------------------------
436
        // Member data.
437
        //
438
        }
439

    
440
        private static final class Spline_NextPoint
441
            extends Spline_Default
442
        {
443
        //-------------------------------------------------------
444
        // Member methods.
445
        //
446

    
447
            private Spline_NextPoint(String name, int id)
448
            {
449
                super (name, id);
450
            }
451

    
452
            protected void addOption(SplineCADToolContext context, String s)
453
            {
454
                SplineCADTool ctxt = context.getOwner();
455

    
456
                if (s.equalsIgnoreCase(PluginServices.getText(this,"SplineCADTool.close")) || s.equals(PluginServices.getText(this,"close")))
457
                {
458

    
459
                    (context.getState()).Exit(context);
460
                    context.clearState();
461
                    try
462
                    {
463
                        ctxt.addOption(s);
464
                        ctxt.closeGeometry();
465
                        ctxt.endGeometry();
466
                        ctxt.end();
467
                    }
468
                    finally
469
                    {
470
                        context.setState(Spline.FirstPoint);
471
                        (context.getState()).Entry(context);
472
                    }
473
                }
474
                else if (s.equalsIgnoreCase(PluginServices.getText(this,"SplineCADTool.end")) || s.equals(PluginServices.getText(this,"terminate")))
475
                {
476

    
477
                    (context.getState()).Exit(context);
478
                    context.clearState();
479
                    try
480
                    {
481
                        ctxt.addOption(s);
482
                        ctxt.endGeometry();
483
                        ctxt.end();
484
                    }
485
                    finally
486
                    {
487
                        context.setState(Spline.FirstPoint);
488
                        (context.getState()).Entry(context);
489
                    }
490
                }                else
491
                {
492
                    super.addOption(context, s);
493
                }
494

    
495
                return;
496
            }
497

    
498
            protected void addPoint(SplineCADToolContext context, double pointX, double pointY, InputEvent event)
499
            {
500
                SplineCADTool ctxt = context.getOwner();
501

    
502
                SplineCADToolState endState = context.getState();
503

    
504
                context.clearState();
505
                try
506
                {
507
                    ctxt.setQuestion(PluginServices.getText(this,"insert_next_point")+", "+
508
                                PluginServices.getText(this,"close")+" "+
509
                                "["+PluginServices.getText(this,"SplineCADTool.close")+"] "+
510
                                PluginServices.getText(this,"cad.or")+" "+
511
                                PluginServices.getText(this,"end")+" "+
512
                                "["+PluginServices.getText(this,"SplineCADTool.end")+"]");
513
                    ctxt.setDescription(new String[]{"close", "terminate", "cancel"});
514
                    ctxt.addPoint(pointX, pointY, event);
515
                }
516
                finally
517
                {
518
                    context.setState(endState);
519
                }
520
                return;
521
            }
522

    
523
        //-------------------------------------------------------
524
        // Member data.
525
        //
526
        }
527

    
528
    //-----------------------------------------------------------
529
    // Member data.
530
    //
531
    }
532
}