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