Skip to content

Commit

Permalink
Update for v3
Browse files Browse the repository at this point in the history
  • Loading branch information
niklauslee committed May 16, 2018
1 parent 2492479 commit 7e48b32
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 115 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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)**.

---

Expand Down
3 changes: 3 additions & 0 deletions keymaps/keymap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cmdctrl-alt-g": "gettersetter:generate"
}
167 changes: 71 additions & 96 deletions main.js
Original file line number Diff line number Diff line change
@@ -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"),
Expand All @@ -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)
}

});
exports.init = init
10 changes: 10 additions & 0 deletions menus/menu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"menu": [
{
"id": "tools",
"submenu": [
{ "label": "Generate Getters & Setters", "id": "tools.gettersetter", "command": "gettersetter:generate" }
]
}
]
}
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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": "[email protected]",
"url": "https://github.com/niklauslee"
},
"license": "MIT",
"engines": {
"staruml": "^3.0.0"
}
}

0 comments on commit 7e48b32

Please sign in to comment.