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

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

    
36

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

    
44
    public BreakCADToolContext(BreakCADTool owner)
45
    {
46
        super();
47

    
48
        _owner = owner;
49
        setState(Break.FirstPoint);
50
        Break.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 BreakCADToolState getState()
78
        throws statemap.StateUndefinedException
79
    {
80
        if (_state == null)
81
        {
82
            throw(
83
                new statemap.StateUndefinedException());
84
        }
85

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

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

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

    
98
    transient private BreakCADTool _owner;
99

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

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

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

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

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

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

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

    
134
        protected void Default(BreakCADToolContext 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 Break
150
    {
151
    //-----------------------------------------------------------
152
    // Member methods.
153
    //
154

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

    
159
        //-------------------------------------------------------
160
        // Statics.
161
        //
162
        /* package */ static Break_Default.Break_FirstPoint FirstPoint;
163
        /* package */ static Break_Default.Break_SecondPoint SecondPoint;
164
        private static Break_Default Default;
165

    
166
        static
167
        {
168
            FirstPoint = new Break_Default.Break_FirstPoint("Break.FirstPoint", 0);
169
            SecondPoint = new Break_Default.Break_SecondPoint("Break.SecondPoint", 1);
170
            Default = new Break_Default("Break.Default", -1);
171
        }
172

    
173
    }
174

    
175
    protected static class Break_Default
176
        extends BreakCADToolState
177
    {
178
    //-----------------------------------------------------------
179
    // Member methods.
180
    //
181

    
182
        protected Break_Default(String name, int id)
183
        {
184
            super (name, id);
185
        }
186

    
187
        protected void addOption(BreakCADToolContext context, String s)
188
        {
189
            BreakCADTool ctxt = context.getOwner();
190

    
191
            if (s.equals(PluginServices.getText(this,"cancel")))
192
            {
193
                boolean loopbackFlag =
194
                    context.getState().getName().equals(
195
                        Break.FirstPoint.getName());
196

    
197
                if (loopbackFlag == false)
198
                {
199
                    (context.getState()).Exit(context);
200
                }
201

    
202
                context.clearState();
203
                try
204
                {
205
                    ctxt.end();
206
                }
207
                finally
208
                {
209
                    context.setState(Break.FirstPoint);
210

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

    
216
                }
217
            }
218
            else
219
            {
220
                boolean loopbackFlag =
221
                    context.getState().getName().equals(
222
                        Break.FirstPoint.getName());
223

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

    
229
                context.clearState();
230
                try
231
                {
232
                    ctxt.throwOptionException(PluginServices.getText(this,"incorrect_option"), s);
233
                }
234
                finally
235
                {
236
                    context.setState(Break.FirstPoint);
237

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

    
243
                }
244
            }
245

    
246
            return;
247
        }
248

    
249
        protected void addValue(BreakCADToolContext context, double d)
250
        {
251
            BreakCADTool ctxt = context.getOwner();
252

    
253
            boolean loopbackFlag =
254
                context.getState().getName().equals(
255
                    Break.FirstPoint.getName());
256

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

    
262
            context.clearState();
263
            try
264
            {
265
                ctxt.throwValueException(PluginServices.getText(this,"incorrect_value"), d);
266
            }
267
            finally
268
            {
269
                context.setState(Break.FirstPoint);
270

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

    
276
            }
277
            return;
278
        }
279

    
280
        protected void addPoint(BreakCADToolContext context, double pointX, double pointY, InputEvent event)
281
        {
282
            BreakCADTool ctxt = context.getOwner();
283

    
284
            boolean loopbackFlag =
285
                context.getState().getName().equals(
286
                    Break.FirstPoint.getName());
287

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

    
293
            context.clearState();
294
            try
295
            {
296
                ctxt.throwPointException(PluginServices.getText(this,"incorrect_point"), pointX, pointY);
297
            }
298
            finally
299
            {
300
                context.setState(Break.FirstPoint);
301

    
302
                if (loopbackFlag == false)
303
                {
304
                    (context.getState()).Entry(context);
305
                }
306

    
307
            }
308
            return;
309
        }
310

    
311
    //-----------------------------------------------------------
312
    // Inner classse.
313
    //
314

    
315

    
316
        private static final class Break_FirstPoint
317
            extends Break_Default
318
        {
319
        //-------------------------------------------------------
320
        // Member methods.
321
        //
322

    
323
            private Break_FirstPoint(String name, int id)
324
            {
325
                super (name, id);
326
            }
327

    
328
            protected void Entry(BreakCADToolContext context)
329
            {
330
                BreakCADTool ctxt = context.getOwner();
331

    
332
                ctxt.selection();
333
                ctxt.setQuestion(PluginServices.getText(this,"insert_first_point"));
334
                ctxt.setDescription(new String[]{"cancel"});
335
                return;
336
            }
337

    
338
            protected void addOption(BreakCADToolContext context, String s)
339
            {
340
                BreakCADTool ctxt = context.getOwner();
341

    
342
                BreakCADToolState endState = context.getState();
343

    
344
                context.clearState();
345
                try
346
                {
347
                    ctxt.setQuestion(PluginServices.getText(this,"insert_first_point"));
348
                    ctxt.setDescription(new String[]{"cancel"});
349
                    ctxt.addOption(s);
350
                }
351
                finally
352
                {
353
                    context.setState(endState);
354
                }
355
                return;
356
            }
357

    
358
            protected void addPoint(BreakCADToolContext context, double pointX, double pointY, InputEvent event)
359
            {
360
                BreakCADTool ctxt = context.getOwner();
361

    
362
                if (ctxt.intersects(pointX,pointY))
363
                {
364

    
365
                    (context.getState()).Exit(context);
366
                    context.clearState();
367
                    try
368
                    {
369
                        ctxt.setQuestion(PluginServices.getText(this,"insert_second_point"));
370
                        ctxt.setDescription(new String[]{"cancel"});
371
                        ctxt.addPoint(pointX, pointY, event);
372
                    }
373
                    finally
374
                    {
375
                        context.setState(Break.SecondPoint);
376
                        (context.getState()).Entry(context);
377
                    }
378
                }
379
                else if (!ctxt.intersects(pointX,pointY))
380
                {
381
                    BreakCADToolState endState = context.getState();
382

    
383
                    context.clearState();
384
                    try
385
                    {
386
                        ctxt.setQuestion(PluginServices.getText(this,"insert_first_point"));
387
                        ctxt.setDescription(new String[]{"cancel"});
388
                    }
389
                    finally
390
                    {
391
                        context.setState(endState);
392
                    }
393
                }                else
394
                {
395
                    super.addPoint(context, pointX, pointY, event);
396
                }
397

    
398
                return;
399
            }
400

    
401
        //-------------------------------------------------------
402
        // Member data.
403
        //
404
        }
405

    
406
        private static final class Break_SecondPoint
407
            extends Break_Default
408
        {
409
        //-------------------------------------------------------
410
        // Member methods.
411
        //
412

    
413
            private Break_SecondPoint(String name, int id)
414
            {
415
                super (name, id);
416
            }
417

    
418
            protected void addOption(BreakCADToolContext context, String s)
419
            {
420
                BreakCADTool ctxt = context.getOwner();
421

    
422

    
423
                (context.getState()).Exit(context);
424
                context.clearState();
425
                try
426
                {
427
                    ctxt.setQuestion(PluginServices.getText(this,"insert_first_point"));
428
                    ctxt.setDescription(new String[]{"cancel"});
429
                    ctxt.addOption(s);
430
                }
431
                finally
432
                {
433
                    context.setState(Break.FirstPoint);
434
                    (context.getState()).Entry(context);
435
                }
436
                return;
437
            }
438

    
439
            protected void addPoint(BreakCADToolContext context, double pointX, double pointY, InputEvent event)
440
            {
441
                BreakCADTool ctxt = context.getOwner();
442

    
443
                if (ctxt.intersects(pointX,pointY))
444
                {
445

    
446
                    (context.getState()).Exit(context);
447
                    context.clearState();
448
                    try
449
                    {
450
                        ctxt.setQuestion(PluginServices.getText(this,"insert_first_point"));
451
                        ctxt.setDescription(new String[]{"cancel"});
452
                        ctxt.addPoint(pointX, pointY, event);
453
                        ctxt.end();
454
                    }
455
                    finally
456
                    {
457
                        context.setState(Break.FirstPoint);
458
                        (context.getState()).Entry(context);
459
                    }
460
                }
461
                else if (!ctxt.intersects(pointX,pointY))
462
                {
463
                    BreakCADToolState endState = context.getState();
464

    
465
                    context.clearState();
466
                    try
467
                    {
468
                        ctxt.setQuestion(PluginServices.getText(this,"insert_second_point"));
469
                        ctxt.setDescription(new String[]{"cancel"});
470
                    }
471
                    finally
472
                    {
473
                        context.setState(endState);
474
                    }
475
                }                else
476
                {
477
                    super.addPoint(context, pointX, pointY, event);
478
                }
479

    
480
                return;
481
            }
482

    
483
        //-------------------------------------------------------
484
        // Member data.
485
        //
486
        }
487

    
488
    //-----------------------------------------------------------
489
    // Member data.
490
    //
491
    }
492
}