Skip to content

Commit

Permalink
File/project templates are now adding to File/New menu immediately.
Browse files Browse the repository at this point in the history
Opening from the newly added File template menu also adds the new file
to an opened project. However, there are still some settlement needs to
do. Deleting from Settings/Templating also removes the menu items from
File/New. (reference #62)
  • Loading branch information
rat-moonshine committed Nov 24, 2017
1 parent 020e690 commit ef6e8ae
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
////////////////////////////////////////////////////////////////////////////////
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
//
// No warranty of merchantability or fitness of any kind.
// Use this software at your own risk.
//
////////////////////////////////////////////////////////////////////////////////
package actionScripts.events
{
import flash.events.Event;

public class TemplatingEvent extends Event
{
public static const ADDED_NEW_TEMPLATE:String = "ADDED_NEW_TEMPLATE";
public static const REMOVE_TEMPLATE:String = "REMOVE_TEMPLATE";

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

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

super(type, false, false);
}

public override function clone():Event
{
return new TemplatingEvent(type, isProject, label, listener);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ package actionScripts.plugin.templating
import actionScripts.events.OpenFileEvent;
import actionScripts.events.ProjectEvent;
import actionScripts.events.RenameApplicationEvent;
import actionScripts.events.TemplatingEvent;
import actionScripts.events.TreeMenuItemEvent;
import actionScripts.factory.FileLocation;
import actionScripts.plugin.IMenuPlugin;
Expand All @@ -54,6 +55,7 @@ package actionScripts.plugin.templating
import actionScripts.plugin.templating.settings.renderer.TemplateRenderer;
import actionScripts.ui.IContentWindow;
import actionScripts.ui.editor.BasicTextEditor;
import actionScripts.ui.menu.MenuPlugin;
import actionScripts.ui.menu.vo.MenuItem;
import actionScripts.ui.renderers.FTETreeItemRenderer;
import actionScripts.ui.tabview.CloseTabEvent;
Expand Down Expand Up @@ -432,6 +434,12 @@ package actionScripts.plugin.templating

// Update internal template list
readTemplates();

// send event to get the new item added immediately to File/New menu
var lbl:String = TemplatingHelper.getTemplateLabel(newTemplate);
var eventType:String = "eventNewFileFromTemplate"+lbl;
dispatcher.addEventListener(eventType, handleNewTemplateFile, false, 0, true);
dispatcher.dispatchEvent(new TemplatingEvent(TemplatingEvent.ADDED_NEW_TEMPLATE, false, lbl, eventType));
}

protected function handleProjectTemplateCreate(event:Event):void
Expand Down Expand Up @@ -464,6 +472,13 @@ package actionScripts.plugin.templating
NewTemplateRenderer(event.target).dispatchEvent(new Event('refresh'));

readTemplates();

// send event to get the new item added immediately to File/New menu
// send event to get the new item added immediately to File/New menu
var lbl:String = TemplatingHelper.getTemplateLabel(newTemplate);
var eventType:String = "eventNewProjectFromTemplate"+lbl;
dispatcher.addEventListener(eventType, handleNewProjectFile, false, 0, true);
dispatcher.dispatchEvent(new TemplatingEvent(TemplatingEvent.ADDED_NEW_TEMPLATE, true, lbl, eventType));
}

protected function handleTemplateModify(event:Event):void
Expand Down Expand Up @@ -540,6 +555,7 @@ package actionScripts.plugin.templating
var rdr:TemplateRenderer = TemplateRenderer(event.target);
var original:FileLocation = rdr.setting.originalTemplate;
var custom:FileLocation = rdr.setting.customTemplate;
var lbl:String = TemplatingHelper.getTemplateLabel(custom);

if (custom.fileBridge.exists)
{
Expand All @@ -553,11 +569,18 @@ package actionScripts.plugin.templating
isProjectOpen = true;
i.projectFolder.isRoot = true;
model.mainView.getTreeViewPanel().tree.dispatchEvent(new TreeMenuItemEvent(TreeMenuItemEvent.RIGHT_CLICK_ITEM_SELECTED, FTETreeItemRenderer.DELETE_PROJECT, i.projectFolder, false));
// remove the item from New/File menu
dispatcher.dispatchEvent(new TemplatingEvent(TemplatingEvent.REMOVE_TEMPLATE, true, lbl));
break;
}
}

if (!isProjectOpen) custom.fileBridge.deleteDirectory(true);
if (!isProjectOpen)
{
// remove the item from New/File menu
dispatcher.dispatchEvent(new TemplatingEvent(TemplatingEvent.REMOVE_TEMPLATE, true, lbl));
custom.fileBridge.deleteDirectory(true);
}
}
else
{
Expand All @@ -578,6 +601,9 @@ package actionScripts.plugin.templating
);
}
}

// remove the item from New/File menu
dispatcher.dispatchEvent(new TemplatingEvent(TemplatingEvent.REMOVE_TEMPLATE, false, lbl));
// deletes the file
custom.fileBridge.deleteFile();
}
Expand All @@ -603,6 +629,12 @@ package actionScripts.plugin.templating
if (custom.fileBridge.exists)
{
var customNewLocation:FileLocation = custom.fileBridge.parent.resolvePath(newFileName +(!custom.fileBridge.isDirectory ? ".template" : ""));
// check if no duplicate naming happens
if (customNewLocation.fileBridge.exists)
{
Alert.show(newFileName +" is already available.", "!Error");
return;
}

if (!custom.fileBridge.isDirectory)
{
Expand Down Expand Up @@ -682,6 +714,7 @@ package actionScripts.plugin.templating
openNewComponentTypeChoose(event, NewFilePopup.AS_XML);
return;
case "File":
default:
openNewComponentTypeChoose(event, NewFilePopup.AS_PLAIN_TEXT);
return;
}
Expand Down
106 changes: 71 additions & 35 deletions ide/MoonshineSharedCore/src/actionScripts/ui/menu/MenuPlugin.as
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,35 @@
////////////////////////////////////////////////////////////////////////////////
package actionScripts.ui.menu
{
import flash.display.NativeMenu;
import flash.events.Event;
import flash.utils.Dictionary;

import mx.core.FlexGlobals;
import mx.events.MenuEvent;

import actionScripts.events.GeneralEvent;
import actionScripts.events.ProjectEvent;
import actionScripts.events.ShortcutEvent;
import actionScripts.events.TemplatingEvent;
import actionScripts.factory.FileLocation;
import actionScripts.factory.NativeMenuItemLocation;
import actionScripts.locator.IDEModel;
import actionScripts.plugin.PluginBase;
import actionScripts.plugin.actionscript.as3project.vo.AS3ProjectVO;
import actionScripts.ui.menu.interfaces.ICustomMenuItem;

import flash.display.NativeMenu;
import flash.events.Event;
import flash.utils.Dictionary;

import mx.core.FlexGlobals;
import mx.events.MenuEvent;

import actionScripts.events.ShortcutEvent;
import actionScripts.factory.FileLocation;
import actionScripts.factory.NativeMenuItemLocation;
import actionScripts.locator.IDEModel;
import actionScripts.plugin.PluginBase;
import actionScripts.plugin.settings.ISettingsProvider;
import actionScripts.plugin.settings.vo.ISetting;
import actionScripts.plugin.settings.vo.MultiOptionSetting;
import actionScripts.plugin.settings.vo.NameValuePair;
import actionScripts.ui.menu.vo.CustomMenu;
import actionScripts.ui.menu.vo.CustomMenuItem;
import actionScripts.ui.menu.vo.MenuItem;
import actionScripts.utils.KeyboardShortcutManager;
import actionScripts.valueObjects.ConstantsCoreVO;
import actionScripts.valueObjects.KeyboardShortcut;
import actionScripts.valueObjects.Settings;
import actionScripts.plugin.settings.ISettingsProvider;
import actionScripts.plugin.settings.vo.ISetting;
import actionScripts.plugin.settings.vo.MultiOptionSetting;
import actionScripts.plugin.settings.vo.NameValuePair;
import actionScripts.plugin.templating.TemplatingHelper;
import actionScripts.plugin.templating.TemplatingPlugin;
import actionScripts.ui.menu.vo.CustomMenu;
import actionScripts.ui.menu.vo.CustomMenuItem;
import actionScripts.ui.menu.vo.MenuItem;
import actionScripts.utils.KeyboardShortcutManager;
import actionScripts.valueObjects.ConstantsCoreVO;
import actionScripts.valueObjects.KeyboardShortcut;
import actionScripts.valueObjects.Settings;

// This class is a singleton
public class MenuPlugin extends PluginBase implements ISettingsProvider
Expand All @@ -58,6 +60,7 @@ package actionScripts.ui.menu
public static const CHANGE_MENU_MAC_NO_MENU_STATE:String = "CHANGE_MENU_MAC_NO_MENU_STATE"; // shows absolutely no top menu
public static const CHANGE_MENU_MAC_ENABLE_STATE:String = "CHANGE_MENU_MAC_ENABLE_STATE";
public static const CHANGE_MENU_SDK_STATE:String = "CHANGE_MENU_SDK_STATE";
public static const UPDATE_NEW_MENU:String = "UPDATE_NEW_MENU:String";

private const BUILD_NATIVE_MENU:uint = 1;
private const BUILD_CUSTOM_MENU:uint = 2;
Expand All @@ -68,6 +71,7 @@ package actionScripts.ui.menu
private var eventToMenuMapping:Dictionary = new Dictionary();
private var noSDKOptionsToMenuMapping:Dictionary = new Dictionary();
private var noCodeCompletionOptionsToMenuMapping:Dictionary = new Dictionary();
private var isFileNewMenuIsEnabled:Boolean;

override public function get name():String { return "Application Menu Plugin"; }
override public function get author():String { return "Keyston Clay & Moonshine Project Team"; }
Expand Down Expand Up @@ -182,6 +186,8 @@ package actionScripts.ui.menu

dispatcher.addEventListener(ShortcutEvent.SHORTCUT_PRE_FIRED, handleShortcutPreFired);
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);

if (ConstantsCoreVO.IS_MACOS)
{
Expand All @@ -194,6 +200,7 @@ package actionScripts.ui.menu
dispatcher.addEventListener(ProjectEvent.ACTIVE_PROJECT_CHANGED, onMenusDisableStateChange);

// disable File-New menu as default
isFileNewMenuIsEnabled = false;
disableMenuOptionsForVEProject();
disableNewFileMenuOptions();
}
Expand Down Expand Up @@ -244,6 +251,7 @@ package actionScripts.ui.menu
{
var menuItem:Object = menuItems[i];
menuItem.enabled = MenuUtils.isMenuItemEnabledInVisualEditor(menuItem.label);
isFileNewMenuIsEnabled = menuItem.enabled;

if (menuItem.submenu)
{
Expand All @@ -268,15 +276,15 @@ package actionScripts.ui.menu
{
// add submenu
m.items = new Vector.<MenuItem>;
for each (file in ConstantsCoreVO.TEMPLATES_FILES)
for each (file in TemplatingPlugin.fileTemplates)
{
var fileName:String = file.fileBridge.name.substring(0,file.fileBridge.name.lastIndexOf("."));
menuitem = new MenuItem(fileName,null,fileName);
m.items.push(menuitem);
}
menuitem = new MenuItem(null);
m.items.push(menuitem);
for each (file in ConstantsCoreVO.TEMPLATES_PROJECTS)
for each (file in TemplatingPlugin.projectTemplates)
{
menuitem = new MenuItem(file.fileBridge.name,null,file.fileBridge.name);
m.items.push(menuitem);
Expand Down Expand Up @@ -369,17 +377,45 @@ package actionScripts.ui.menu
var menuBarMenu:CustomMenu = (IDEModel.getInstance().mainView.getChildAt(0) as MenuBar).menu as CustomMenu;
topNativeMenuItemsForFileNew = (menuBarMenu.items[0] as CustomMenuItem).data.items[0].data.items;
}

for (var i:int=0; i < 7; i++)
{
topNativeMenuItemsForFileNew[i].enabled = false;
}
}
else

isFileNewMenuIsEnabled = false;
for (var j:int=0; j < TemplatingPlugin.fileTemplates.length; j++)
{
topNativeMenuItemsForFileNew[j].enabled = false;
}
}

private function onNewMenuAddRequest(event:TemplatingEvent):void
{
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

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);
}
}

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;

for (var i:int=0; i < subItemsInItemOfTopMenu.length; i++)
{
for (var j:int=0; j < 7; j++)
if (subItemsInItemOfTopMenu[i].label == event.label)
{
topNativeMenuItemsForFileNew[j].enabled = false;
itemsInTopMenu[1].submenu.items[0].submenu.items[0].menu.removeItemAt(i);
return;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,18 +536,18 @@ package actionScripts.ui.renderers

private function contextMenuSelectHandler(event:ContextMenuEvent):void
{
disableMenuItems(contextMenu.items);
disableMenuItems(contextMenu["items"]);
}

private function disableMenuItems(items:Array):void
{
var currentProject:ProjectVO = UtilsCore.getProjectFromProjectFolder(data as FileWrapper);
for each (var item:NativeMenuItem in items)
{
item.enabled = MenuUtils.isMenuItemEnabledInVisualEditor(item.label, currentProject);
if (item.submenu)
item.enabled = MenuUtils.isMenuItemEnabledInVisualEditor(item["label"], currentProject);
if (item["submenu"])
{
disableMenuItems(item.submenu.items);
disableMenuItems(item["submenu"].items);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ package actionScripts.ui.tabview
}
else
{
othersButton.visible = false;
othersButton.visible = false;
}

// Each item draws vertical separators on both sides, overlap by 1 px to not have duplicate lines.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ package actionScripts.valueObjects
import actionScripts.factory.FileLocation;
import actionScripts.plugin.core.sourcecontrol.ISourceControlProvider;

import flash.filesystem.File;

[Bindable] dynamic public class FileWrapper
{
public var projectReference: ProjectReferenceVO;
Expand Down Expand Up @@ -74,7 +72,7 @@ package actionScripts.valueObjects

for (var i:int = 0; i < directoryListingCount; i++)
{
var currentDirectory:File = directoryListing[i];
var currentDirectory:Object = directoryListing[i];
var hasHiddenPath:Boolean = projectReference.hiddenPaths.some(function(item:FileLocation, index:int, arr:Vector.<FileLocation>):Boolean
{
return currentDirectory.nativePath == item.fileBridge.nativePath;
Expand Down

0 comments on commit ef6e8ae

Please sign in to comment.