diff --git a/README.md b/README.md index fad6a66..a186647 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -StarUML 2 Extension for Generating Getters and Setters -====================================================== +StarUML Extension for Generating Getters and Setters +==================================================== This extension allows to generate getters and setters for selected attributes or selected classes. @@ -11,7 +11,7 @@ Open Extension Manager *(Tools > Extension Manager)* and select *Registry* Tab a ## Usage -To generate getters and setters, select attributes or classes first and then select **Tools > Generate Getters and Setters (Alt+Ctrl+G)**. +To generate getters and setters, select attributes or classes first and then select **Tools > Generate Getters and Setters (Ctrl+Alt+G)**. --- diff --git a/keymaps/keymap.json b/keymaps/keymap.json new file mode 100644 index 0000000..3c09c2d --- /dev/null +++ b/keymaps/keymap.json @@ -0,0 +1,3 @@ +{ + "cmdctrl-alt-g": "gettersetter:generate" +} diff --git a/main.js b/main.js index 50f41f5..536a421 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 MKLab. All rights reserved. + * Copyright (c) 2014-2018 Minkyu Lee. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -21,107 +21,82 @@ * */ -/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */ -/*global define, $, _, window, app, type, appshell, document */ +/** + * Change first character to upper case + */ +function firstUpperCase (name) { + if (name.length > 0) { + return name[0].toUpperCase() + name.substr(1, name.length - 1) + } + return '' +} -define(function (require, exports, module) { - "use strict"; +/** + * Generate a getter and a setter for an attribute + * + * @param {UMLAttribute} attr + */ +function generateGetterSetter (attr) { + var _class = attr._parent - var AppInit = app.getModule("utils/AppInit"), - Commands = app.getModule("command/Commands"), - CommandManager = app.getModule("command/CommandManager"), - MenuManager = app.getModule("menu/MenuManager"), - Repository = app.getModule("core/Repository"), - OperationBuilder = app.getModule("core/OperationBuilder"), - SelectionManager = app.getModule("engine/SelectionManager"), - UML = app.getModule("uml/UML"); + var builder = app.repository.getOperationBuilder() + builder.begin('generate getter & setter') - /** - * Commands IDs - */ - var CMD_GENERATE_GETTERSETTER = 'tools.generate-gettersetter'; + // Getter + var _getter = new type.UMLOperation() + _getter.name = 'get' + firstUpperCase(attr.name) + _getter.visibility = type.UMLModelElement.VK_PUBLIC + _getter._parent = _class + var _param1 = new type.UMLParameter() + _param1.direction = type.UMLParameter.DK_RETURN + _param1.type = attr.type + _param1._parent = _getter + _getter.parameters.push(_param1) + builder.insert(_getter) + builder.fieldInsert(_class, 'operations', _getter) - /** - * Change first character to upper case - */ - function firstUpperCase(name) { - if (name.length > 0) { - return name[0].toUpperCase() + name.substr(1, name.length - 1); - } - return ""; - } - - /** - * Generate a getter and a setter for an attribute - * - * @param {UMLAttribute} attr - */ - function generateGetterSetter(attr) { - var _class = attr._parent; - - OperationBuilder.begin("generate getter & setter"); + // Setter + var _setter = new type.UMLOperation() + _setter.name = 'set' + firstUpperCase(attr.name) + _setter.visibility = type.UMLModelElement.VK_PUBLIC + _setter._parent = _class + var _param2 = new type.UMLParameter() + _param2.direction = type.UMLParameter.DK_IN + _param2.name = 'value' + _param2.type = attr.type + _param2._parent = _setter + _setter.parameters.push(_param2) + builder.insert(_setter) + builder.fieldInsert(_class, 'operations', _setter) - // Getter - var _getter = new type.UMLOperation(); - _getter.name = "get" + firstUpperCase(attr.name); - _getter.visibility = UML.VK_PUBLIC; - _getter._parent = _class; - var _param1 = new type.UMLParameter(); - _param1.direction = UML.DK_RETURN; - _param1.type = attr.type; - _param1._parent = _getter; - _getter.parameters.add(_param1); - OperationBuilder.insert(_getter); - OperationBuilder.fieldInsert(_class, "operations", _getter); + builder.end() + var cmd = builder.getOperation() + app.repository.doOperation(cmd) +} - // Setter - var _setter = new type.UMLOperation(); - _setter.name = "set" + firstUpperCase(attr.name); - _setter.visibility = UML.VK_PUBLIC; - _setter._parent = _class; - var _param2 = new type.UMLParameter(); - _param2.direction = UML.DK_IN; - _param2.name = "value"; - _param2.type = attr.type; - _param2._parent = _setter; - _setter.parameters.add(_param2); - OperationBuilder.insert(_setter); - OperationBuilder.fieldInsert(_class, "operations", _setter); - - OperationBuilder.end(); - var cmd = OperationBuilder.getOperation(); - Repository.doOperation(cmd); - } - - /** - * Command Handler for Generating Getters and Setters - * - * @param {Element} base - * @param {string} path - * @param {Object} options - * @return {$.Promise} - */ - function _handleGenerate(base, path, options) { - var result = new $.Deferred(); - - var selected = SelectionManager.getSelectedModels(); - selected.forEach(function (e) { - if (e instanceof type.UMLAttribute) { - generateGetterSetter(e); - } else if (e instanceof type.UMLClassifier) { - e.attributes.forEach(function (attr) { - generateGetterSetter(attr); - }); - } - }); - return result.promise(); +/** + * Command Handler for Generating Getters and Setters + * + * @param {Element} base + * @param {string} path + * @param {Object} options + * @return {$.Promise} + */ +function _handleGenerate (base, path, options) { + var selected = app.selections.getSelectedModels() + selected.forEach(function (e) { + if (e instanceof type.UMLAttribute) { + generateGetterSetter(e) + } else if (e instanceof type.UMLClassifier) { + e.attributes.forEach(function (attr) { + generateGetterSetter(attr) + }) } + }) +} - // Register Commands - CommandManager.register("Generate Getters & Setters", CMD_GENERATE_GETTERSETTER, _handleGenerate); - - var menu, menuItem; - menu = MenuManager.getMenu(Commands.TOOLS); - menuItem = menu.addMenuItem(CMD_GENERATE_GETTERSETTER, ["Ctrl-Alt-G"]); +function init () { + app.commands.register('gettersetter:generate', _handleGenerate) +} -}); \ No newline at end of file +exports.init = init diff --git a/menus/menu.json b/menus/menu.json new file mode 100644 index 0000000..a0fcacc --- /dev/null +++ b/menus/menu.json @@ -0,0 +1,10 @@ +{ + "menu": [ + { + "id": "tools", + "submenu": [ + { "label": "Generate Getters & Setters", "id": "tools.gettersetter", "command": "gettersetter:generate" } + ] + } + ] +} diff --git a/package.json b/package.json index ccec037..205ae9d 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,18 @@ { - "name": "niklauslee.staruml-gettersetter", - "title": "Generate Getters and Setters", - "description": "Generate getters and setters for selected attributes or selected classes", - "homepage": "https://github.com/niklauslee/staruml-gettersetter", - "issues": "https://github.com/niklauslee/staruml-gettersetter/issues", - "keywords": ["getter", "setter"], - "version": "0.9.0", - "author": { - "name": "Minkyu Lee", - "email": "niklaus.lee@gmail.com", - "url": "https://github.com/niklauslee" - }, - "license": "MIT", - "engines": { - "staruml": ">=2.0.0" - } + "name": "niklauslee.staruml-gettersetter", + "title": "Generate Getters and Setters", + "description": "Generate getters and setters for selected attributes or selected classes", + "homepage": "https://github.com/niklauslee/staruml-gettersetter", + "issues": "https://github.com/niklauslee/staruml-gettersetter/issues", + "keywords": ["getter", "setter"], + "version": "0.9.1", + "author": { + "name": "Minkyu Lee", + "email": "niklaus.lee@gmail.com", + "url": "https://github.com/niklauslee" + }, + "license": "MIT", + "engines": { + "staruml": "^3.0.0" + } }