PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/TranscriptStudio.mxml

http://transcriptstudio4isha.googlecode.com/
Macromedia eXtensible Markup Language | 191 lines | 176 code | 15 blank | 0 comment | 0 complexity | e99baac297c7e824344ed0bf5521adf0 MD5 | raw file
  1<!--
  2   Transcript Studio for Isha Foundation: An XML based application that allows users to define 
  3   and store contextual metadata for contiguous sections within a text document. 
  4
  5   Copyright 2008 Mark Carter, Swami Kevala
  6
  7   This file is part of Transcript Studio for Isha Foundation.
  8
  9   Transcript Studio for Isha Foundation is free software: you can redistribute it and/or modify it 
 10   under the terms of the GNU General Public License as  published by the Free Software 
 11   Foundation, either version 3 of the License, or (at your option) any later version.
 12
 13   Transcript Studio for Isha Foundation is distributed in the hope that it will be useful, but 
 14   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 15   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 16
 17   You should have received a copy of the GNU General Public License along with 
 18   Transcript Studio for Isha Foundation. If not, see http://www.gnu.org/licenses/.
 19-->
 20
 21<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:studioNS="org.ishafoundation.archives.transcript.components.studio.*" top="20" width="1000" height="600" layout="vertical" styleName="plain" backgroundColor="0x869CA7" xmlns:local="*" creationComplete="initApp()" pageTitle="Transcript Studio">
 22	<mx:Script>
 23        <![CDATA[
 24        	import mx.managers.CursorManager;
 25        	import org.ishafoundation.archives.transcript.components.generic.DatabasePopUp;
 26        	import name.carter.mark.flex.util.remote.ClientManager;
 27        	import name.carter.mark.flex.util.Utils;
 28        	import org.ishafoundation.archives.transcript.db.DatabaseConstants;
 29        	import org.ishafoundation.archives.transcript.components.studio.text.TranscriptTextArea;
 30        	import org.ishafoundation.archives.transcript.components.studio.MainPanel;
 31        	import org.ishafoundation.archives.transcript.components.generic.LoginPopUp;
 32        	import mx.controls.Alert;
 33        	import mx.events.FlexEvent;
 34        	import mx.managers.PopUpManager;
 35        	import org.ishafoundation.archives.transcript.db.DatabaseManagerUsingEXist;
 36        	import mx.core.UIComponent;
 37        	import org.ishafoundation.archives.transcript.components.generic.LoginPopUp;
 38        	import name.carter.mark.flex.util.icon.IconUtils;
 39        	import org.ishafoundation.archives.transcript.components.studio.text.TranscriptTextArea;
 40        	import org.ishafoundation.archives.transcript.db.DatabaseManagerDemo;
 41        	import org.ishafoundation.archives.transcript.db.DatabaseManager;
 42        	
 43        	private static const ICON_NAMES_TO_PRELOAD:Array = ["chant", "default", "joke", "music", "process", "question", "quote", "story", "topic"];
 44        	
 45			private function preloadIcons(nextFunc:Function):void {
 46				trace("Preloading icons: " + ICON_NAMES_TO_PRELOAD);
 47				var count:int = 0;
 48				var completeFunc:Function = function():void {
 49					count++;
 50					if (count == ICON_NAMES_TO_PRELOAD.length) {
 51						trace("Finished preloading icons");
 52						nextFunc();
 53					}
 54				};
 55				for each (var iconName:String in ICON_NAMES_TO_PRELOAD) {
 56					var iconPath:String = Utils.getIconPath(iconName);
 57					IconUtils.preloadIcon(iconPath, completeFunc, function(msg:String):void {
 58						trace("Could not preload icon: " + msg);
 59						completeFunc();
 60					});
 61				}
 62			}
 63
 64        	private var databaseMgr:DatabaseManagerUsingEXist;
 65        	
 66			private function initApp():void {
 67				addEventListener(Event.REMOVED_FROM_STAGE, function(evt:Event):void {
 68					trace("Removed from stage");
 69				});
 70				this.explicitWidth = NaN;
 71				this.explicitHeight = NaN;
 72				var thisHandle:Application = this;
 73				trace("Using default hostname");
 74				preloadIcons(function():void {
 75					TranscriptTextArea.ICONS_ENABLED = true;
 76					testConnection(function():void {
 77		                var loginPopUp:LoginPopUp = PopUpManager.createPopUp(thisHandle, LoginPopUp, true) as LoginPopUp;
 78		                PopUpManager.centerPopUp(loginPopUp);
 79						loginPopUp.loginButton.addEventListener(MouseEvent.CLICK, loginButtonClicked);
 80						loginPopUp.passwordTextInput.addEventListener(FlexEvent.ENTER, loginButtonClicked);
 81						//status = "Logging in...";												
 82					});
 83				});
 84			}
 85			
 86			private function testConnection(successFunc:Function, databasePopUp:DatabasePopUp = null):void {
 87				var thisHandle:Application = this;
 88				CursorManager.setBusyCursor();
 89				ClientManager.testConnectionToURL(DatabaseConstants.EXIST_URL, function(response:Object):void {
 90					CursorManager.removeBusyCursor();
 91					if (databasePopUp != null) {
 92						PopUpManager.removePopUp(databasePopUp);
 93					}
 94					successFunc();
 95				}, function(evt:ErrorEvent):void {
 96					if (evt is SecurityErrorEvent && evt.text.indexOf("#2048") >= 0) {
 97						// this is an error caused by the sandbox security manager
 98						showSecurityAlert(evt.text);
 99						return;
100					}
101					CursorManager.removeBusyCursor();
102					if (databasePopUp == null) {
103						databasePopUp = PopUpManager.createPopUp(thisHandle, DatabasePopUp, true) as DatabasePopUp;
104						PopUpManager.centerPopUp(databasePopUp);
105						databasePopUp.okButton.addEventListener(MouseEvent.CLICK, function(evt:MouseEvent):void {
106							DatabaseConstants.EXIST_URL = databasePopUp.urlTextInput.text;
107							testConnection(successFunc, databasePopUp);
108						});
109					}
110					else {
111						Alert.show(DatabaseConstants.EXIST_URL, "Could not find database");						
112					}
113				});
114			}
115			
116			private function showIOAlert(text:String):void {
117				this.enabled = false;
118				Alert.show(text, "Could not find eXist DB");
119			}
120			
121			private function showSecurityAlert(text:String):void {
122				var startIndex:int = this.url.indexOf("file:///");
123				this.enabled = false; // don't allow the user to do ANYTHING?
124				var pathToAdd:String;
125				if (startIndex >= 0) {
126					pathToAdd = unescape(this.url.substring("file:///".length).replace("|", ":"));
127				}
128				else {
129					pathToAdd = null;
130				}
131				var msg:String = "";
132				if (pathToAdd != null) {
133					msg = "Try this:\n\nRight click -> Settings... -> Advanced...\n\n(...takes you to browser...)\n\n-> Global Security Settings Panel\n\nAdd the following URL to \"Always trust files in these locations\":\n\n" + pathToAdd + "\n\nFinally, restart application\n\nOriginal Exception:\n\n";
134				}
135				msg += text;
136				var al:Alert = Alert.show(msg, "Security Sandbox Violation");
137				// important to move the Alert to the top left because the settings box also comes up in the middle (and its modal!)
138				al.callLater(function():void {
139					al.x = 20;
140					al.y = 20;
141				});
142			}
143			
144			private function loginButtonClicked(event:Event):void {
145				var loginPopUp:LoginPopUp = (event.currentTarget as UIComponent).document as LoginPopUp;
146				if (!loginPopUp.loginButton.enabled) {
147					// cheeky - the user hit enter in the password text input field when the button was disabled
148					return;
149				}
150				var username:String = loginPopUp.usernameTextInput.text;
151				var password:String = loginPopUp.passwordTextInput.text;
152				databaseMgr = new DatabaseManagerUsingEXist(username, password, function():void {
153					PopUpManager.removePopUp(loginPopUp);
154					//status = "Login succeeded";
155					// make sure the data collection has been created
156					databaseMgr.query("xmldb:collection-exists($arg0)", [DatabaseConstants.DATA_COLLECTION_PATH], function(returnVal:Boolean):void {
157						if (returnVal) {
158							mainPanel.init(username, databaseMgr);
159							mainPanel.visible = true;
160						}
161						else {
162							Alert.show(DatabaseConstants.DATA_COLLECTION_PATH, "Could not find data collection");
163						}
164					},
165					function(msg:String):void {
166						Alert.show(msg);
167					});
168				}, loginFailed);
169			}
170			
171			private function loginFailed(msg:String):void {
172				//status = "Login failed";
173				var index:int = msg.lastIndexOf("URL:") + 1;
174				var url:String = index >= 0 ? msg.substr(index + 4) : msg;
175				var s:String;
176				if (msg.toLowerCase().indexOf("io error") >= 0) {
177					s = "Could not connect to server: " + url; 
178				}
179				else if (msg.toLowerCase().indexOf("version") >= 0) {
180					s = msg;
181				}
182				else {
183					s = "Incorrect username/password";
184				}
185				trace(msg);
186				Alert.show(s, "Login failed");
187			} 			
188    	]]>
189    </mx:Script>
190    <studioNS:MainPanel id="mainPanel" width="100%" height="100%" verticalGap="0"/>
191</mx:Application>