automatically select comment TextField on clicking 'save'
[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);" />
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:DataGridColumn editable="true" dataField="v" headerText="Value"/>
41             </mx:columns>
42           </mx:DataGrid>        
43                   <mx:HBox horizontalAlign="right" width="100%">
44                     <mx:LinkButton label="Delete" click="removeTag()" enabled="{advancedTagGrid.selectedItem != null? true : false}"/>
45                     <mx:LinkButton label="Add" click="addNewTag()"/>
46                   </mx:HBox>
47                   <mx:HRule width="100%" />
48         </mx:VBox>
49       </mx:ViewStack>
50       <mx:LinkBar dataProvider="{tagStack}"/>
51     </mx:VBox>
52     
53     <mx:VBox width="100%" height="100%" id="createChangesetTab">
54       <mx:VBox width="100%" height="100%" id="infoBox"/>
55       <mx:Spacer height="100%"/>
56       <mx:ProgressBar label="Creating changeset" labelPlacement="bottom" width="100%"
57           indeterminate="true" id="saveProgress"/>
58     </mx:VBox>
59     
60     <mx:VBox width="100%" height="100%" id="failureTab">
61       <mx:Text width="100%" styleName="failText" text="{failureText}"/>
62     </mx:VBox>
63
64     <mx:VBox width="100%" height="100%" id="successTab">
65       <mx:Text width="100%">
66         <mx:htmlText><![CDATA[<b>All data uploaded!</b>]]></mx:htmlText>
67       </mx:Text>
68     </mx:VBox>
69   </mx:ViewStack>
70
71   <mx:ControlBar>
72     <mx:Spacer width="100%"/>
73     <mx:Button id="cancelButton" label="Cancel" click="close();"/>
74     <mx:Button id="saveButton" label="Save >" click="startSave();"/>
75   </mx:ControlBar>
76   
77   <mx:Script><![CDATA[
78   
79     import mx.controls.*;
80     import mx.managers.PopUpManager;
81         import mx.core.Application;
82     import mx.events.DataGridEvent;
83     import mx.events.DataGridEventReason;
84     
85     import net.systemeD.halcyon.connection.*;
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 addNewTag():void {
111                 changesetTags.addItem( { k:'(new key)', v:'(new value)' } );
112                 advancedTagGrid.editedItemPosition = { rowIndex: changesetTags.length-1, columnIndex: 0 };
113         }
114
115         private function removeTag():void {
116                 changesetTags.removeItemAt(advancedTagGrid.selectedIndex);
117         }
118     
119     private function startSave():void {
120     
121         // move to next sequence
122         processSequence.selectedChild = createChangesetTab;
123         saveButton.enabled = false;
124         
125         var tags:Object = new Object();
126         for each (var tag:Object in changesetTags) {
127            tags[tag['k']] = tag['v'];
128         }
129         
130         // add the listeners
131         conn.addEventListener(Connection.NEW_CHANGESET, changesetCreated);
132         conn.addEventListener(Connection.NEW_CHANGESET_ERROR, changesetError);
133         conn.createChangeset(tags);
134     }
135
136     private function allowForEdit(event:DataGridEvent):void {
137         /* check before editing the tag grid that it's neither created_by nor version tags */
138         var item:Object = ((event.currentTarget as DataGrid).dataProvider as ArrayCollection)[event.rowIndex];
139         if(item.k == 'created_by' || item.k == 'version' || item.k == 'build') {
140           event.preventDefault();
141         }
142     }
143
144     private function verifyInput(event:DataGridEvent):void {
145         /* check before saving any change that the new key isn't created_by nor version */
146         // it is OK if the user cancels the edit
147         if( event.reason == DataGridEventReason.CANCELLED ) return;
148
149         var editor:TextInput = (event.currentTarget as DataGrid).itemEditorInstance as TextInput;
150
151         if( event.dataField == "k" ) {
152           if( editor.text == 'created_by' || editor.text == 'version' || editor.text == 'build') {
153             event.preventDefault();
154           }
155         }
156     }
157
158     private function changesetCreated(event:EntityEvent=null):void {
159         var changeset:Changeset = conn.getActiveChangeset();
160         addStatus("Saving to changeset "+changeset.id);
161         
162         saveProgress.label = "Uploading changes";
163         conn.addEventListener(Connection.SAVE_COMPLETED, saveCompleted);
164         conn.uploadChanges();
165     }
166     
167     private function changesetError(event:Event):void {
168         fail("Error creating changeset");
169     }
170     
171     private function saveCompleted(event:SaveCompleteEvent):void {
172         if ( event.saveOK )
173             succeed("All Data Saved!");
174         else
175             fail("Failure when uploading data");
176
177         saveButton.parent.removeChild(saveButton);
178         cancelButton.label = "Close";
179     }
180     
181     private function addStatus(text:String):void {
182         var label:Text = new Text();
183         label.text = text;
184         
185         infoBox.addChild(label);
186     }
187     
188     private function succeed(text:String):void {
189         processSequence.selectedChild = successTab;
190     }
191     
192     private function fail(text:String):void {
193         processSequence.selectedChild = failureTab;
194         failureText = text;
195     }
196     
197     private function close():void {
198         conn.removeEventListener(Connection.NEW_CHANGESET, changesetCreated);
199         conn.removeEventListener(Connection.NEW_CHANGESET_ERROR, changesetError);
200         conn.removeEventListener(Connection.SAVE_COMPLETED, saveCompleted);
201         PopUpManager.removePopUp(this);
202     }
203   ]]></mx:Script>
204 </mx:TitleWindow>
205