4d87dd1395bc58177bbe817267d8d2f25f8031ca
[potlatch2.git] / net / systemeD / potlatch2 / save / SaveDialog.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:TitleWindow
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4         layout="vertical"
5         horizontalAlign="center" title="Save Changes"
6         width="350" height="330" verticalGap="0">
7
8   <mx:ArrayCollection id="changesetTags">
9     <mx:Object k="created_by" v="Potlatch 2"/>
10     <mx:Object k="version" v="{Application.application.version}"/>
11     <mx:Object k="build" v="{Application.application.build_number}"/>
12   </mx:ArrayCollection>
13   
14   <mx:ViewStack id="processSequence" width="100%" height="100%" 
15       creationPolicy="all" creationComplete="if (doSkip) skipInput() else comment.setFocus()">
16   
17     <!-- section for entering tags -->
18     <mx:VBox width="100%" height="100%" verticalGap="0">
19       <mx:ViewStack id="tagStack" width="100%" height="100%">
20         <mx:VBox width="100%" height="100%" label="Simple">
21           <mx:Text width="100%">
22             <mx:text>
23                Please enter a description of your edits. This will be used to give other
24                mappers an idea of what changes you are making.
25             </mx:text>
26           </mx:Text>
27           <mx:Label text="Comment:"/>
28           <mx:TextArea id="comment" width="100%" height="100%" change="commentChanged(event);" textInput="monitorEnter(event);" />
29         </mx:VBox>
30         
31         <mx:VBox width="100%" height="100%" label="Advanced">
32           <mx:Label text="Changeset tags:"/>
33           <mx:DataGrid editable="true" width="100%" id="advancedTagGrid"
34               dataProvider="{changesetTags}"
35               itemEditBeginning="allowForEdit(event)"
36               itemEditEnd="verifyInput(event)">
37             <mx:columns>
38                 <mx:DataGridColumn editable="true" dataField="k" headerText="Key">
39                     <mx:itemEditor><mx:Component><mx:TextInput restrict="&#x0020;-&#x10FFFF;" /></mx:Component></mx:itemEditor>
40                 </mx:DataGridColumn>
41                 <mx:DataGridColumn editable="true" dataField="v" headerText="Value">
42                     <mx:itemEditor><mx:Component><mx:TextInput restrict="&#x0020;-&#x10FFFF;" /></mx:Component></mx:itemEditor>
43                 </mx:DataGridColumn>
44             </mx:columns>
45           </mx:DataGrid>        
46                   <mx:HBox horizontalAlign="right" width="100%">
47                     <mx:LinkButton label="Delete" click="removeTag()" enabled="{advancedTagGrid.selectedItem != null? true : false}"/>
48                     <mx:LinkButton label="Add" click="addNewTag()"/>
49                   </mx:HBox>
50                   <mx:HRule width="100%" />
51         </mx:VBox>
52       </mx:ViewStack>
53       <mx:LinkBar dataProvider="{tagStack}"/>
54     </mx:VBox>
55     
56     <mx:VBox width="100%" height="100%" id="createChangesetTab">
57       <mx:VBox width="100%" height="100%" id="infoBox"/>
58       <mx:Spacer height="100%"/>
59       <mx:ProgressBar label="Creating changeset" labelPlacement="bottom" width="100%"
60           indeterminate="true" id="saveProgress"/>
61     </mx:VBox>
62     
63     <mx:VBox width="100%" height="100%" id="failureTab">
64       <mx:Text width="100%" styleName="failText" text="{failureText}"/>
65     </mx:VBox>
66
67   </mx:ViewStack>
68
69   <mx:ControlBar>
70     <mx:Spacer width="100%"/>
71     <mx:Button id="cancelButton" label="Cancel" click="close();" styleName="titleWindowButton" />
72     <mx:Button id="saveButton" label="Save >" click="startSave();" styleName="titleWindowButton" />
73   </mx:ControlBar>
74   
75   <mx:Script><![CDATA[
76   
77     import mx.controls.*;
78     import mx.managers.PopUpManager;
79         import mx.core.Application;
80     import mx.events.DataGridEvent;
81     import mx.events.DataGridEventReason;
82     
83     import net.systemeD.halcyon.connection.*;
84     import net.systemeD.halcyon.AttentionEvent;
85     
86     private var _connection:Connection;
87         private var doSkip:Boolean = false;
88     
89     [Bindable]
90     private var failureText:String = "";
91
92         public function setConnection(connection:Connection):void {
93                 _connection=connection;
94         }
95
96         public function dontPrompt():void {
97                 if (processSequence.initialized) { skipInput(); } else { doSkip=true; }
98         }
99
100         private function skipInput():void {
101         processSequence.selectedChild = createChangesetTab;
102                 saveButton.enabled = false;
103                 changesetCreated();
104         }
105
106         private function commentChanged(event:Event):void {
107                 for (var i:int=changesetTags.length-1; i>0; i--) {
108                         if (changesetTags[i]['k']=='comment') { changesetTags.removeItemAt(i); }
109                 }
110                 if (event.target.text!='') changesetTags.addItem( { k:'comment', v: event.target.text } );
111     }
112
113         private function monitorEnter(event:TextEvent):void {
114                 if (event.text=="\n") { event.preventDefault(); startSave(); }
115         }
116
117         private function addNewTag():void {
118                 changesetTags.addItem( { k:'(new key)', v:'(new value)' } );
119                 advancedTagGrid.editedItemPosition = { rowIndex: changesetTags.length-1, columnIndex: 0 };
120         }
121
122         private function removeTag():void {
123                 changesetTags.removeItemAt(advancedTagGrid.selectedIndex);
124         }
125     
126     private function startSave():void {
127     
128         // move to next sequence
129         processSequence.selectedChild = createChangesetTab;
130         saveButton.enabled = false;
131         
132         var tags:Object = new Object();
133         for each (var tag:Object in changesetTags) {
134            tags[tag['k']] = tag['v'];
135         }
136         
137         // add the listeners
138         _connection.addEventListener(Connection.NEW_CHANGESET, changesetCreated);
139         _connection.addEventListener(Connection.NEW_CHANGESET_ERROR, changesetError);
140         _connection.createChangeset(tags);
141     }
142
143     private function allowForEdit(event:DataGridEvent):void {
144         /* check before editing the tag grid that it's neither created_by nor version tags */
145         var item:Object = ((event.currentTarget as DataGrid).dataProvider as ArrayCollection)[event.rowIndex];
146         if(item.k == 'created_by' || item.k == 'version' || item.k == 'build') {
147           event.preventDefault();
148         }
149     }
150
151     private function verifyInput(event:DataGridEvent):void {
152         /* check before saving any change that the new key isn't created_by nor version */
153         // it is OK if the user cancels the edit
154         if( event.reason == DataGridEventReason.CANCELLED ) return;
155
156         var editor:TextInput = (event.currentTarget as DataGrid).itemEditorInstance as TextInput;
157
158         if( event.dataField == "k" ) {
159           if( editor.text == 'created_by' || editor.text == 'version' || editor.text == 'build') {
160             event.preventDefault();
161           }
162         }
163     }
164
165     private function changesetCreated(event:EntityEvent=null):void {
166         var changeset:Changeset = _connection.getActiveChangeset();
167         addStatus("Saving to changeset "+changeset.id);
168         
169         saveProgress.label = "Uploading changes";
170         _connection.addEventListener(Connection.SAVE_COMPLETED, saveCompleted);
171         _connection.uploadChanges();
172     }
173     
174     private function changesetError(event:Event):void {
175         fail("Error creating changeset");
176     }
177     
178     private function saveCompleted(event:SaveCompleteEvent):void {
179         if ( event.saveOK ) {
180                         _connection.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Changes successfully saved"));
181             close();
182         } else {
183             fail("Failure when uploading data");
184                 }
185         if (saveButton && saveButton.parent) saveButton.parent.removeChild(saveButton);
186         cancelButton.label = "Close";
187     }
188     
189     private function addStatus(text:String):void {
190         var label:Text = new Text();
191         label.text = text;
192         
193         infoBox.addChild(label);
194     }
195     
196     private function fail(text:String):void {
197         processSequence.selectedChild = failureTab;
198         failureText = text;
199     }
200     
201     private function close():void {
202         _connection.removeEventListener(Connection.NEW_CHANGESET, changesetCreated);
203         _connection.removeEventListener(Connection.NEW_CHANGESET_ERROR, changesetError);
204         _connection.removeEventListener(Connection.SAVE_COMPLETED, saveCompleted);
205         PopUpManager.removePopUp(this);
206     }
207   ]]></mx:Script>
208 </mx:TitleWindow>
209