1 Star 2 Fork 1

姜焱琳 / gui-shell-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

GUI SHELL

简介

一个类似于spring-shell的框架,不同之处在于这个框架生成gui界面。

quick start

导入依赖

<!-- https://mvnrepository.com/artifact/io.gitee.qq1134380223/gui-shell-core -->
<dependency>
    <groupId>io.gitee.qq1134380223</groupId>
    <artifactId>gui-shell-core</artifactId>
    <version>1.1.1.RELEASE</version>
</dependency>

oracle jdk8 包含有javax的相关模块,而jdk11以后javafx已经不是jdk的内置模块,因此需要额外引入。

如果你使用jdk11或更高的jdk版本,那么还需要以下依赖。

<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-fxml</artifactId>
    <version>17</version>
</dependency>

编写TestMethodGroup

@GuiShellGroup("测试功能组")
public class TestMethodGroup {

    @GuiShellGroupMethod(value = "嗨", desc = "点击执行功能按钮,然后这个方法的输出将会输出到结果输出区")
    public String hi() {
        return "hi!";
    }
}

编写主方法

public class Main {
    public static void main(String[] args) {
        GuiShellApplication.runApplication("测试窗口标题", new TestMethodGroup());
    }
}

运行主方法,你会看到这个界面 img.png 展开右侧的列表中的“测试功能组”,选择“嗨”,然后点击“执行功能”按钮,这时hi()方法的的返回值就会显示在结果输出区。 img_1.png ① 来自与main方法中GuiShellApplication.runApplication的第一个参数。

TestMethodGroup@GuiShellGroup注解值,注意需要在main方法中GuiShellApplication.runApplication 的第二个参数传入TestMethodGroup实例。

hi()的注解@GuiShellGroupMethod的value值,这个值是“功能名称”。

④ 选择了“嗨”这个功能后,点击“执行功能”按钮时会执行 hi()方法,返回值会显示在结果输出区。

⑤ 功能说明,来自于hi()的注解@GuiShellGroupMethod的desc值,这个值是“功能说明”。

你可以在TestMethodGroup中添加多个注解有@GuiShellGroupMethodpublic 方法。注意,框架并不要求多个方法的@GuiShellGroupMethod的value值必须各不相同,只是如果你写相同的value值,会在使用的过程中无法区分两个value相同的方法。

GuiShellApplication.runApplication的第二个参数是变长参数,因此你也可以传入多个带有@GuiShellGroup 注解的类型的实例。同样@GuiShellGroup注解的value值并不要求必须不同,但相同的value会让使用过程中难以区分两个同名的功能组。

参数

方法可以有参数,框架会为参数生成对应的输入组件。现在修改TestMethodGrouphi()方法

import io.gitee.qq1134380223.guishellcore.annotation.Param;

@GuiShellGroup("测试功能组")
public class TestMethodGroup {

    @GuiShellGroupMethod(value = "嗨", desc = "点击执行功能按钮,然后这个函数的输出将会输出到结果输出区")
    public String hi(@Param("姓名") String name) {
        return "hi! " + name;
    }
}

重新运行程序并选择“嗨”功能,可以看到参数输入区出现一个输入姓名的输入框,输入姓名并点击执行功能。 img_2.png ① 输入框前的参数名称来自于@Param的value值

② 执行功能时,框架会将输入框中的值传入hi方法

框架能为大部分类型生成人性化的输入组件,但并非所有,框架支持的参数类型包括在以下列表中:

类型 对应的输入组件
int,Integer 整数输入框
double,Double 小数输入框
boolean,Boolean 勾选框
String 输入框
@Choices String[] 多选框
@Single @Choices String 下拉框
File 文件选择器
@Directory File 目录选择器
LocaleDate 日期选择框
上述类型的数组(注意是数组[]不是集合List) 可增加减少的上述输入框
至少有一个setter的bean类型,bean的属性字段是上述类 将上述输入框组合而成的输入框
上述类型的子类 父类对应的输入框
上述类型的数组(注意:本行没有重复) 可增加减少的上述输入框
至少有一个setter的bean类型,bean的属性字段是上述类((注意:本行没有重复)) 将上述输入框组合而成的输入框
上述类型的子类 (注意:本行没有重复) 父类对应的输入框

@Choices String[] 多选框

修改一下功能

@GuiShellGroup("测试功能组")
public class TestMethodGroup {

    @GuiShellGroupMethod(value = "多选", desc = "点击选择选项")
    public String multiChoice(@Param("选项") @Choices({"item1", "item2", "item3"}) String[] choices) {
        return String.join(",", choices);
    }
}

效果如图: img_4.png

@Single @Choices String 单选框

修改一下功能

import io.gitee.qq1134380223.guishellcore.annotation.Choices;
import io.gitee.qq1134380223.guishellcore.annotation.Param;
import io.gitee.qq1134380223.guishellcore.annotation.Single;

@GuiShellGroup("测试功能组")
public class TestMethodGroup {

    @GuiShellGroupMethod(value = "嗨", desc = "点击执行功能按钮,然后这个函数的输出将会输出到结果输出区")
    public String hi(@Param("问候语") @Single @Choices({"hi", "hello"}) String greeting, @Param("姓名") String name) {
        return greeting + " " + name;
    }
}

效果如图: img_3.png

当@Single注解的参数类型为String时,@Single可以省略。但当其注解的类型是String[]时,则不能省略,因为省略后就不能区分是多个单选还是一个多选。 注解或String[][]或String数组的数组的数组等等时@Single同理不能省略。

File 文件输入框

@GuiShellGroup("测试功能组")
public class TestMethodGroup {

    @GuiShellGroupMethod("输入一个文件")
    public File getAFile(@Param("文件") File file) {
        return file;
    }

}

运行测试: img_7.png 点击选择文件后弹出: img_8.png 选择文件然后点击执行功能: img_9.png

@Directory File 目录输入框

import io.gitee.qq1134380223.guishellcore.annotation.Directory;

@GuiShellGroup("测试功能组")
public class TestMethodGroup {

    @GuiShellGroupMethod("输入一个目录")
    public File getAFile(@Param("目录") @Directory File file) {
        return file;
    }

}

效果同File,只是弹出的选择框时目录选择框: img_10.png

bean 组合输入框

定义一个Bean类型参数

import io.gitee.qq1134380223.guishellcore.annotation.Choices;
import io.gitee.qq1134380223.guishellcore.annotation.Param;

@Data //lombok
public class Student {
    @Param("姓名")
    String name;

    @Param("年龄")
    int year;

    @Param("性别")
    @Choices({"男", "女", "其他"})
    String gender;
}

写一个方法

import io.gitee.qq1134380223.guishellcore.annotation.Choices;
import io.gitee.qq1134380223.guishellcore.annotation.Param;
import io.gitee.qq1134380223.guishellcore.annotation.Single;

@GuiShellGroup("测试功能组")
public class TestMethodGroup {

    @GuiShellGroupMethod(value = "输入一个学生信息")
    public Student hi(@Param("学生信息") Student student) {
        return student;
    }
}

运行测试: img_5.png

数组类型参数

以学生信息为例

import io.gitee.qq1134380223.guishellcore.annotation.Choices;
import io.gitee.qq1134380223.guishellcore.annotation.Param;
import io.gitee.qq1134380223.guishellcore.annotation.Single;

@GuiShellGroup("测试功能组")
public class TestMethodGroup {

    @GuiShellGroupMethod(value = "输入一群学生信息")
    public Student[] hi(@Param("学生信息") Student[] students) {
        return students;
    }
}

运行测试: img_6.png

返回值

单击结果输出区的文字,可以进入复制模式。 img_11.png

File类型的返回值

在windows操作系统中,结果输出区会为File类型的返回值生成一个“在资源管理查看”的按钮,点击可打开文件所在目录。

img_9.png

void 类型返回值

会输出“功能已执行完毕,没有任何返回值”

其他类型的返回值

这个框架会尽量的使用json格式数据表达返回的对象,如果返回的对象无法用json表达则会显示它toString()后的值。

属性

import io.gitee.qq1134380223.guishellcore.annotation.Choices;
import io.gitee.qq1134380223.guishellcore.annotation.GuiShellGroupProperty;
import io.gitee.qq1134380223.guishellcore.annotation.Param;
import io.gitee.qq1134380223.guishellcore.annotation.Single;

@GuiShellGroup("测试功能组")
public class TestMethodGroup {
    @GuiShellGroupProperty("字段属性")
    int i = 0;

    public int getI() {//字段属性必须有getter
        return i;
    }

    @GuiShellGroupProperty("方法属性")
    public int getIp1() {//方法属性必须public且无参
        return i + 1;
    }

    @GuiShellGroupMethod(value = "改变属性")
    public void hi() {
        i++;
    }
}

每次点击“执行功能”属性区都会刷新并更新属性的值。 img_12.png

点击属性的字符,可以进入复制模式。 img_13.png

File类型的属性

例子:

import io.gitee.qq1134380223.guishellcore.annotation.Directory;
import io.gitee.qq1134380223.guishellcore.annotation.GuiShellGroupProperty;

@GuiShellGroup("测试功能组")
public class TestMethodGroup {

    @GuiShellGroupProperty("目录")
    File dir;

    public File getDir() {
        return dir;
    }

    @GuiShellGroupMethod("输入一个目录")
    public void getAFile(@Param("目录") @Directory File file) {
        dir = file;
    }

}

对于File类型的属性,在windows操作系统下,会为其生成一个“在资源管理器中查看”的按钮。 img_14.png

其他类型的属性

对于其他类型的属性,框架会尽量以json的格式展示其内容,如果不能则以其toString的值展示。

以Student类为例子:

import io.gitee.qq1134380223.guishellcore.annotation.Choices;
import io.gitee.qq1134380223.guishellcore.annotation.GuiShellGroupProperty;
import io.gitee.qq1134380223.guishellcore.annotation.Param;
import io.gitee.qq1134380223.guishellcore.annotation.Single;

@GuiShellGroup("测试功能组")
public class TestMethodGroup {
    @GuiShellGroupProperty("学生信息")
    Student student;

    public Student getStudent() {
        return student;
    }

    @GuiShellGroupMethod(value = "输入一个学生信息")
    public void hi(@Param("学生信息") Student student) {
        this.student = student;
    }
}

效果如图: img_15.png

耗时任务

如果一个@GuiShellGroupMethod注解的函数是耗时任务,可以为其加上@Async注解,这样在执行耗时任务的时候就会输出“功能正在执行,请稍后”。

示例:

@GuiShellGroup("耗时任务")
public class Test {
    @GuiShellGroupMethod("耗时任务")
    @Async
    public void test() {
        //耗时任务的代码
    }
}

整合Spring Boot

整合springboot的步骤如下

  1. 创建springboot 项目
  2. 引入gui-shell-core依赖
  3. 编写功能组类,并加上@Component注解

注意gui-shell-core依赖中包含springboot的启动配置,无需手动调用GuiShellApplication.runApplication

使用spring boot 的ioc功能,你能向功能组中注入你需要的bean,比如dao,service等

配置窗口标题

在application.properties中配置

guishell.title=你的标题

kotlin

使用kotlin可以简化开发,至少不用写那么多public、getter和setter。使用spring boot initializer可以很方便的创建基于kotlin的springboot项目,这样在项目中就可以很方便的使用kotlin开发功能。

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] 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.

简介

写一些普通的java 函数,再给他们加上一些注解,这个框架会为带有注解的函数创建一个可视化的调用界面。 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/qq1134380223/gui-shell-core.git
git@gitee.com:qq1134380223/gui-shell-core.git
qq1134380223
gui-shell-core
gui-shell-core
master

搜索帮助