source: ComponentRegistry/trunk/ComponentBrowserGui/src/main/flex/clarin/cmdi/componentregistry/editor/EditorAS.as @ 1733

Last change on this file since 1733 was 1733, checked in by twagoo, 12 years ago

Editor: Vertical divider to show both component editor and component selection overview simultaneously. Refs #161

File size: 6.0 KB
Line 
1// ActionScript file
2import clarin.cmdi.componentregistry.browser.BrowserColumns;
3import clarin.cmdi.componentregistry.common.ItemDescription;
4import clarin.cmdi.componentregistry.editor.model.CMDModelFactory;
5import clarin.cmdi.componentregistry.editor.model.CMDSpec;
6import clarin.cmdi.componentregistry.importer.UploadCompleteEvent;
7import clarin.cmdi.componentregistry.services.ComponentInfoService;
8import clarin.cmdi.componentregistry.services.ComponentListService;
9import clarin.cmdi.componentregistry.services.ComponentUsageCheckEvent;
10import clarin.cmdi.componentregistry.services.ComponentUsageService;
11import clarin.cmdi.componentregistry.services.Config;
12import clarin.cmdi.componentregistry.services.ProfileInfoService;
13import clarin.cmdi.componentregistry.services.UploadService;
14
15import flash.events.Event;
16
17import mx.controls.Alert;
18import mx.events.CloseEvent;
19import mx.managers.CursorManager;
20
21
22private var profileSrv:ProfileInfoService = new ProfileInfoService();
23private var componentSrv:ComponentInfoService = new ComponentInfoService();
24private var itemDescription:ItemDescription;
25
26[Bindable]
27private var componentsSrv:ComponentListService = ComponentListService.getInstance(Config.instance.userSpace);
28
29[Bindable]
30public var cmdComponent:XML;
31
32[Bindable]
33private var cmdSpec:CMDSpec;
34
35[Bindable]
36private var browserColumns:BrowserColumns = new BrowserColumns();
37
38[Bindable]
39private var uploadService:UploadService = new UploadService();
40
41
42public function init():void {
43        cmdSpec  = CMDSpec.createEmptyProfile();
44        profileSrv.addEventListener(ProfileInfoService.PROFILE_LOADED, profileLoaded);
45        componentSrv.addEventListener(ComponentInfoService.COMPONENT_LOADED, componentLoaded);
46        uploadService.addEventListener(UploadCompleteEvent.UPLOAD_COMPLETE, handleSaveComplete);
47        uploadService.init(uploadProgress);
48        Config.instance.addEventListener(Config.USER_SPACE_TOGGLE_EVENT, toggleUserSpace);
49}
50
51private function toggleUserSpace(event:Event):void {
52        componentsSrv = ComponentListService.getInstance(Config.instance.userSpace);
53}
54
55private function profileLoaded(event:Event):void {
56        var cmdComponent:XML = profileSrv.profile.profileSource;
57        this.cmdSpec = CMDModelFactory.createModel(cmdComponent, profileSrv.profile.description);
58        CursorManager.removeBusyCursor();
59}
60
61private function componentLoaded(event:Event):void {
62        var cmdComponent:XML = componentSrv.component.componentMD.xml;
63        this.cmdSpec = CMDModelFactory.createModel(cmdComponent, componentSrv.component.description);
64        CursorManager.removeBusyCursor();
65}
66
67
68public function setDescription(itemDescription:ItemDescription):void {
69        if (itemDescription) {
70                this.itemDescription = itemDescription;
71                CursorManager.setBusyCursor();
72                if (itemDescription.isProfile) {
73                        profileSrv.load(itemDescription);
74                } else {
75                        componentSrv.load(itemDescription);
76                }
77        }
78}
79
80private function publishSpec():void {
81        Alert.show("If your profile/component is ready to be used by other people press ok, otherwise press cancel and save it in your workspace or continue editing.", "Publish", Alert.OK | Alert.CANCEL, null, handlePublishAlert);
82}
83
84private function handlePublishAlert(event:CloseEvent):void {
85        if (event.detail == Alert.OK) {
86                saveSpec(false, UploadService.PUBLISH);
87        }
88}
89
90private function saveSpec(inUserSpace:Boolean, uploadAction:int):void {
91        //      Alert.show(xmlEditor.cmdSpec.toXml());
92        if (xmlEditor.validate()) {
93                var item:ItemDescription = new ItemDescription();
94                item.description = xmlEditor.cmdSpec.headerDescription;
95                item.name = xmlEditor.cmdSpec.headerName;
96                item.isProfile = xmlEditor.cmdSpec.isProfile;
97                item.groupName = xmlEditor.cmdSpec.groupName;
98                item.domainName = xmlEditor.cmdSpec.domainName;
99                item.isInUserSpace = inUserSpace;
100                if (itemDescription && itemDescription.isInUserSpace) {
101                        item.id = xmlEditor.cmdSpec.headerId;
102                }
103               
104                // Private components that are in updated require usage check call. If in use, the user can choose whether or not to save the changes .
105                if(inUserSpace && uploadAction == UploadService.UPDATE && !item.isProfile){
106                        checkUsage(item, inUserSpace);
107                }else{
108                        doUpload(uploadAction,item);
109                }
110        } else {
111                errorMessageField.text = "Validation errors: red colored fields are invalid.";
112        }
113}
114
115/**
116 * Calls usage check for the specified component. If in use, asks user whether to proceed; if positive, initiates update.
117 */
118private function checkUsage(item:ItemDescription, inUserSpace:Boolean = true, uploadAction:int = UploadService.UPDATE):void{
119        var componentUsageService:ComponentUsageService = new ComponentUsageService(item,inUserSpace);
120        componentUsageService.addEventListener(ComponentUsageCheckEvent.COMPONENT_IN_USE, 
121                function (event:ComponentUsageCheckEvent):void{
122                        if(event.isComponentInUse){
123                                var messageBody:String = "The component you are about to save is used by the following component(s) and/or profile(s):\n\n";
124                                for each(var name:String in event.itemUsingComponent){
125                                        messageBody += " - " + name + "\n";
126                                }
127                                messageBody += "\nChanges in this component will affect the above. Do you want to proceed?";
128                                Alert.show(messageBody,"Component is used", Alert.YES|Alert.NO,null,
129                                        function (eventObj:CloseEvent):void{
130                                                if(eventObj.detail == Alert.YES){
131                                                        doUpload(uploadAction, item);
132                                                }
133                                        });
134                        } else {
135                                doUpload(uploadAction,item);
136                        }
137                });
138        componentUsageService.checkUsage();
139}
140
141private function doUpload(uploadAction:int, item:ItemDescription):void{
142        uploadService.upload(uploadAction, item, xmlEditor.cmdSpec.toXml());
143}
144
145private function handleSaveComplete(event:UploadCompleteEvent):void {
146        setDescription(event.itemDescription);
147        parentApplication.viewStack.switchToBrowse(event.itemDescription);
148}
149
150
151private function handleEditorChange(event:Event):void {
152        errorMessageField.text = "";
153        uploadProgress.visible = false;
154        uploadProgress.includeInLayout = false;
155}
156
157private function initPaletteOverview():void {
158        componentsPaletteOverview.dataGrid.dragEnabled = true;
159        componentsPaletteOverview.dataGrid.allowMultipleSelection = true;
160        componentsPaletteOverview.dataGrid.resizableColumns = true;
161}
162
163public function getType():String {
164        return Config.VIEW_EDIT;
165}
Note: See TracBrowser for help on using the repository browser.