apparently people want to be able to save more than once. tsk
[potlatch2.git] / net / systemeD / potlatch2 / Toolbox.as
1 package net.systemeD.potlatch2 {
2
3         import flash.events.Event;
4         import flash.events.MouseEvent;
5         import mx.containers.Panel;
6         import net.systemeD.halcyon.connection.*;
7     import net.systemeD.potlatch2.controller.*;
8     import net.systemeD.potlatch2.tools.*;
9
10         /*
11                 Floating toolbox palette
12         */
13
14         public class Toolbox extends Panel {
15                 
16                 private var entity:Entity;
17                 private var controller:EditController;
18
19                 public function Toolbox(){
20                         super();
21                 }
22                 
23                 public function init(controller:EditController):void {
24                         this.controller=controller;
25                 }
26
27                 override protected function createChildren():void {
28                         super.createChildren();
29                         super.titleBar.addEventListener(MouseEvent.MOUSE_DOWN,handleDown);
30                         super.titleBar.addEventListener(MouseEvent.MOUSE_UP,handleUp);
31                 }
32
33                 public function setEntity(entity:Entity):void {
34                         this.entity=entity;
35                         dispatchEvent(new Event("updateSkin"));
36                         dispatchEvent(new Event("updateAlpha"));
37                 }
38
39                 private function handleDown(e:Event):void {
40                         this.startDrag();
41                 }
42
43                 private function handleUp(e:Event):void {
44                         this.stopDrag();
45                 }
46                 
47                 public function toggle():void {
48                         this.visible=!this.visible;
49                 }
50                 
51                 // --------------------------------------------------------------------------------
52                 // Enable/disable toolbox buttons
53                 // (ideally we'd use CSS to set alpha in disabled state, but Flex's CSS
54                 //  capabilities aren't up to it)
55                 
56         [Bindable(event="updateSkin")]
57                 public function canDo(op:String):Boolean {
58                         switch (op) {
59                                 case 'delete':                          return (entity is Way || entity is Node);
60                                 case 'reverseDirection':        return (entity is Way);
61                                 case 'quadrilateralise':        return (entity is Way && Way(entity).isArea());
62                                 case 'straighten':                      return (entity is Way && !Way(entity).isArea());
63                                 case 'circularise':                     return (entity is Way && Way(entity).isArea());
64                                 case 'split':                           return (entity is Node && controller.state is SelectedWayNode);
65                                 case 'parallelise':                     return (entity is Way);
66                         }
67                         return false;
68                 }
69
70         [Bindable(event="updateAlpha")]
71                 public function getAlpha(op:String):Number {
72                         if (canDo(op)) { return 1; }
73                         return 0.5;
74                 }
75         
76
77                 // --------------------------------------------------------------------------------
78                 // Individual toolbox actions
79
80                 public function doDelete():void {
81                         if (entity is Node) { controller.connection.unregisterPOI(Node(entity)); }
82                         entity.remove(MainUndoStack.getGlobalStack().addAction);
83
84                         if (controller.state is SelectedWayNode) {
85                                 controller.setState(new SelectedWay(SelectedWayNode(controller.state).selectedWay));
86                         } else {
87                                 controller.setState(new NoSelection());
88                         }
89                 }
90
91         public function doReverseDirection():void {
92             if (entity is Way) { 
93                 Way(entity).reverseNodes(MainUndoStack.getGlobalStack().addAction);
94             }
95         }
96
97                 public function doQuadrilateralise():void {
98                         if (entity is Way) {
99                                 Quadrilateralise.quadrilateralise(Way(entity));
100                         }
101                 }
102
103                 public function doStraighten():void {
104                         if (entity is Way) {
105                                 Straighten.straighten(Way(entity),controller.map);
106                         }
107                 }
108
109                 public function doCircularise():void {
110                         if (entity is Way) {
111                                 Circularise.circularise(Way(entity),controller.map);
112                         }
113                 }
114
115                 public function doSplit():void {
116                         if (entity is Node && controller.state is SelectedWayNode) {
117                                 controller.setState(SelectedWayNode(controller.state).splitWay());
118                         }
119                 }
120                 
121                 public function doParallelise():void {
122                         if (entity is Way) {
123                                 controller.setState(new SelectedParallelWay(Way(entity)));
124                         }
125                 }
126
127         }
128 }