Skip to content

Commit

Permalink
- Add/remove item to File/New (immediate) fixed for Windows
Browse files Browse the repository at this point in the history
- Renaming a template from Settings/Templating now renames the item in
File/New dialog (tested in Windows)

Trouble:
- Firing the template creation event after item renamed inside File/New

Known issue:
- Renaming label do not renaming item in right-click-project-File/New

(reference #62)
  • Loading branch information
rat-moonshine committed Nov 27, 2017
1 parent 2ff135b commit ea798dc
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,26 @@ package actionScripts.events
{
public static const ADDED_NEW_TEMPLATE:String = "ADDED_NEW_TEMPLATE";
public static const REMOVE_TEMPLATE:String = "REMOVE_TEMPLATE";
public static const RENAME_TEMPLATE:String = "RENAME_TEMPLATE";

public var label:String;
public var newLabel:String;
public var listener:String;
public var isProject:Boolean;

public function TemplatingEvent(type:String, isProject:Boolean, label:String, listener:String=null)
public function TemplatingEvent(type:String, isProject:Boolean, label:String, listener:String=null, newLabel:String=null)
{
this.isProject = isProject;
this.label = label;
this.newLabel = newLabel;
this.listener = listener;

super(type, false, false);
}

public override function clone():Event
{
return new TemplatingEvent(type, isProject, label, listener);
return new TemplatingEvent(type, isProject, label, listener, newLabel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -624,15 +624,17 @@ package actionScripts.plugin.templating
// Resetting a template just removes it from app-storage
var rdr:TemplateRenderer = TemplateRenderer(event.target);
var custom:FileLocation = rdr.setting.customTemplate;
var newFileName:String = event.value as String;
var oldFileName:String = TemplatingHelper.getTemplateLabel(custom);
var newFileNameWithExtension:String = event.value as String;
var newFileName:String = newFileNameWithExtension.split(".")[0];

if (custom.fileBridge.exists)
{
var customNewLocation:FileLocation = custom.fileBridge.parent.resolvePath(newFileName +(!custom.fileBridge.isDirectory ? ".template" : ""));
var customNewLocation:FileLocation = custom.fileBridge.parent.resolvePath(newFileNameWithExtension +(!custom.fileBridge.isDirectory ? ".template" : ""));
// check if no duplicate naming happens
if (customNewLocation.fileBridge.exists)
{
Alert.show(newFileName +" is already available.", "!Error");
Alert.show(newFileNameWithExtension +" is already available.", "!Error");
return;
}

Expand All @@ -648,7 +650,7 @@ package actionScripts.plugin.templating
&& ed.currentFile.fileBridge.nativePath == custom.fileBridge.nativePath)
{
ed.currentFile = customNewLocation;
ed.label = newFileName;
ed.label = newFileNameWithExtension;
}
}
custom.fileBridge.moveTo(customNewLocation, true);
Expand All @@ -658,6 +660,21 @@ package actionScripts.plugin.templating
rdr.setting.customTemplate = customNewLocation;
rdr.setting.label = rdr.setting.customTemplate.fileBridge.name;
rdr.dispatchEvent(new Event('refresh'));

// remove the existing File/New listener
if (!custom.fileBridge.isDirectory)
{
dispatcher.removeEventListener("eventNewFileFromTemplate"+ oldFileName, handleNewTemplateFile);
dispatcher.addEventListener("eventNewFileFromTemplate"+ newFileName, handleNewTemplateFile);
}
else
{
dispatcher.removeEventListener("eventNewFileFromTemplate"+ oldFileName, handleNewProjectFile);
dispatcher.addEventListener("eventNewFileFromTemplate"+ newFileName, handleNewProjectFile);
}

// updating file/new menu
dispatcher.dispatchEvent(new TemplatingEvent(TemplatingEvent.RENAME_TEMPLATE, true, oldFileName, null, newFileName));
}
}

Expand Down Expand Up @@ -714,25 +731,22 @@ package actionScripts.plugin.templating
openNewComponentTypeChoose(event, NewFilePopup.AS_XML);
return;
case "File":
default:
openNewComponentTypeChoose(event, NewFilePopup.AS_PLAIN_TEXT);
return;
}

for (i = 0; i < fileTemplates.length; i++)
{
fileTemplate = fileTemplates[i];
if ( TemplatingHelper.getTemplateLabel(fileTemplate) == eventName )
{
var customTemplate:FileLocation = getCustomFileFor(fileTemplate);
if (customTemplate.fileBridge.exists)
default:
for (i = 0; i < fileTemplates.length; i++)
{
fileTemplate = customTemplate;
fileTemplate = fileTemplates[i];
if ( TemplatingHelper.getTemplateLabel(fileTemplate) == eventName )
{
if (fileTemplate.fileBridge.exists)
{
openNewComponentTypeChoose(event, NewFilePopup.AS_CUSTOM, fileTemplate);
break;
}
}
}

createFile(fileTemplate);
return;
}
}
}
else
Expand Down Expand Up @@ -856,14 +870,15 @@ package actionScripts.plugin.templating
}
}

protected function openNewComponentTypeChoose(event:Event, openType:String):void
protected function openNewComponentTypeChoose(event:Event, openType:String, fileTemplate:FileLocation=null):void
{
if (!newFilePopup)
{
newFilePopup = PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, NewFilePopup, true) as NewFilePopup;
newFilePopup.addEventListener(CloseEvent.CLOSE, handleFilePopupClose);
newFilePopup.addEventListener(NewFileEvent.EVENT_NEW_FILE, onFileCreateRequest);
newFilePopup.openType = openType;
newFilePopup.fileTemplate = fileTemplate;

// newFileEvent sends by TreeView when right-clicked
// context menu
Expand Down
69 changes: 57 additions & 12 deletions ide/MoonshineSharedCore/src/actionScripts/ui/menu/MenuPlugin.as
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ package actionScripts.ui.menu
dispatcher.addEventListener(CHANGE_MENU_SDK_STATE, onSDKStateChange);
dispatcher.addEventListener(TemplatingEvent.ADDED_NEW_TEMPLATE, onNewMenuAddRequest, false, 0, true);
dispatcher.addEventListener(TemplatingEvent.REMOVE_TEMPLATE, onNewMenuRemoveRequest, false, 0, true);
dispatcher.addEventListener(TemplatingEvent.RENAME_TEMPLATE, onNewMenuRenameRequest, false, 0, true);

if (ConstantsCoreVO.IS_MACOS)
{
Expand Down Expand Up @@ -390,31 +391,75 @@ package actionScripts.ui.menu
{
var tmpMI:MenuItem = new MenuItem(event.label, null, event.listener);
var menuItem:* = createNewMenuItem(tmpMI);

var tmpTopMenu:Object = FlexGlobals.topLevelApplication.nativeApplication.menu;
var itemsInTopMenu:Array = tmpTopMenu.items; // top-level menus, i.e. Moonshine, File etc.
var subItemsInItemOfTopMenu:Array = itemsInTopMenu[1].submenu.items; // i.e. File
var itemToAddAt:int = event.isProject ? TemplatingPlugin.projectTemplates.length + TemplatingPlugin.fileTemplates.length : TemplatingPlugin.fileTemplates.length - 1;
var menuObject:Object = (menuItem is NativeMenuItemLocation) ? NativeMenuItemLocation(menuItem).item.getNativeMenuItem : menuItem;
if (!isFileNewMenuIsEnabled) menuObject.enabled = false;

if (menuItem)
{
var itemToAddAt:int = event.isProject ? TemplatingPlugin.projectTemplates.length + TemplatingPlugin.fileTemplates.length : TemplatingPlugin.fileTemplates.length - 1;
var menuObject:Object = (menuItem is NativeMenuItemLocation) ? NativeMenuItemLocation(menuItem).item.getNativeMenuItem : menuItem;
if (!isFileNewMenuIsEnabled) menuObject.enabled = false;
subItemsInItemOfTopMenu[0].submenu.items[0].menu.addItemAt(menuObject, itemToAddAt);
if (buildingNativeMenu)
{
var tmpTopMenu:Object = FlexGlobals.topLevelApplication.nativeApplication.menu;
var itemsInTopMenu:Array = tmpTopMenu.items; // top-level menus, i.e. Moonshine, File etc.
var subItemsInItemOfTopMenu:Array = itemsInTopMenu[1].submenu.items; // i.e. File
subItemsInItemOfTopMenu[0].submenu.items[0].menu.addItemAt(menuObject, itemToAddAt);
}
else
{
var menuBarMenu:CustomMenu = (IDEModel.getInstance().mainView.getChildAt(0) as MenuBar).menu as CustomMenu;
CustomMenuItem(menuBarMenu.items[0].submenu.items[0]).data.items.insertAt(itemToAddAt, menuObject);
}
}
}

private function onNewMenuRemoveRequest(event:TemplatingEvent):void
{
var tmpTopMenu:Object = FlexGlobals.topLevelApplication.nativeApplication.menu;
var itemsInTopMenu:Array = tmpTopMenu.items; // top-level menus, i.e. Moonshine, File etc.
var subItemsInItemOfTopMenu:Array = itemsInTopMenu[1].submenu.items[0].submenu.items;
var subItemsInItemOfTopMenu:Object;
if (buildingNativeMenu)
{
var tmpTopMenu:Object = FlexGlobals.topLevelApplication.nativeApplication.menu;
var itemsInTopMenu:Array = tmpTopMenu.items; // top-level menus, i.e. Moonshine, File etc.
subItemsInItemOfTopMenu = itemsInTopMenu[1].submenu.items[0].submenu.items;
}
else
{
var menuBarMenu:CustomMenu = (IDEModel.getInstance().mainView.getChildAt(0) as MenuBar).menu as CustomMenu;
subItemsInItemOfTopMenu = CustomMenuItem(menuBarMenu.items[0].submenu.items[0]).data.items;
}

for (var i:int=0; i < subItemsInItemOfTopMenu.length; i++)
{
if (subItemsInItemOfTopMenu[i].label == event.label)
{
if (buildingNativeMenu) itemsInTopMenu[1].submenu.items[0].submenu.items[0].menu.removeItemAt(i);
else CustomMenuItem(menuBarMenu.items[0].submenu.items[0]).data.items.removeAt(i);
return;
}
}
}

private function onNewMenuRenameRequest(event:TemplatingEvent):void
{
var menuObject:Object;
var subItemsInItemOfTopMenu:Object;
if (buildingNativeMenu)
{
var tmpTopMenu:Object = FlexGlobals.topLevelApplication.nativeApplication.menu;
subItemsInItemOfTopMenu = tmpTopMenu.items[1].submenu.items[0].submenu.items;
}
else
{
var menuBarMenu:CustomMenu = (IDEModel.getInstance().mainView.getChildAt(0) as MenuBar).menu as CustomMenu;
subItemsInItemOfTopMenu = CustomMenuItem(menuBarMenu.items[0].submenu.items[0]).data.items;
}

for (var i:int=0; i < subItemsInItemOfTopMenu.length; i++)
{
if (subItemsInItemOfTopMenu[i].label == event.label)
{
itemsInTopMenu[1].submenu.items[0].submenu.items[0].menu.removeItemAt(i);
/*if (buildingNativeMenu) itemsInTopMenu[1].submenu.items[0].submenu.items[0].menu.removeItemAt(i);
else*/ subItemsInItemOfTopMenu[i].label = event.newLabel;
subItemsInItemOfTopMenu[i].data.event = "eventNewFileFromTemplate"+ event.newLabel;
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@
public static var AS_PLAIN_TEXT:String = "AS_PLAIN_TEXT";
public static var AS_XML:String = "AS_XML";
public static var AS_CUSTOM:String = "AS_CUSTOM";
[Bindable] public var wrapperBelongToProject:ProjectVO;
public var wrapperOfFolderLocation:FileWrapper;
public var folderLocation:FileLocation;
public var openType:String;
public var fileTemplate:FileLocation;
[Bindable] private var modifiedName:String = "";
[Bindable] private var projectFolders:ArrayCollection;
Expand Down Expand Up @@ -137,7 +139,10 @@
var validateArr: Array = new Array(new ValidatorType(sValidator, txtDestination, "Source Package"));
if (SimpleValidator.validate(validateArr))
{
var tmpEvent:NewFileEvent = new NewFileEvent(NewFileEvent.EVENT_NEW_FILE, null, (openType == AS_XML) ? ConstantsCoreVO.TEMPLATE_XML : ConstantsCoreVO.TEMPLATE_TEXT, wrapperOfFolderLocation);
if (openType == AS_XML) fileTemplate = ConstantsCoreVO.TEMPLATE_XML;
else if (openType == AS_PLAIN_TEXT) fileTemplate = ConstantsCoreVO.TEMPLATE_TEXT;
var tmpEvent:NewFileEvent = new NewFileEvent(NewFileEvent.EVENT_NEW_FILE, null, fileTemplate, wrapperOfFolderLocation);
tmpEvent.fileName = txtFileName.text;
dispatchEvent(tmpEvent);
Expand All @@ -161,7 +166,7 @@
{
minHeight = height;
title = (openType == AS_XML) ? "New XML File" : "New File";
if (openType == AS_PLAIN_TEXT) txtFileName.restrict = "0-9A-Za-z_.";
txtFileName.restrict = "0-9A-Za-z_.";
if (wrapperBelongToProject)
{
Expand Down

0 comments on commit ea798dc

Please sign in to comment.