From 9a2383421b5008019db063aeaf63dd4af81daec8 Mon Sep 17 00:00:00 2001 From: richie Date: Fri, 30 Aug 2019 20:44:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E5=A4=9A=E7=9A=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fanruan/api/conf/ConfigurationKit.java | 41 +++++++++++++++++++ .../java/com/fanruan/api/conf/Runner.java | 11 +++++ .../fanruan/api/conf/xml/XmlHolderKit.java | 26 ++++++++++++ .../com/fanruan/api/data/ConnectionKit.java | 11 +++++ .../connection/NameDatabaseConnection.java | 4 -- .../ui/component/UIPlaceholderTextField.java | 4 ++ .../api/design/ui/component/UIToolbar.java | 6 +-- .../api/design/ui/toolbar/ToolBarDef.java | 36 ++++++++++++++++ .../java/com/fanruan/api/net/CloudKit.java | 4 +- src/main/java/com/fanruan/api/util/IOKit.java | 16 ++++++++ .../com/fanruan/api/net/http/HttpKitTest.java | 3 ++ 11 files changed, 152 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/fanruan/api/conf/ConfigurationKit.java create mode 100644 src/main/java/com/fanruan/api/conf/Runner.java create mode 100644 src/main/java/com/fanruan/api/conf/xml/XmlHolderKit.java delete mode 100644 src/main/java/com/fanruan/api/data/connection/NameDatabaseConnection.java create mode 100644 src/main/java/com/fanruan/api/design/ui/toolbar/ToolBarDef.java diff --git a/src/main/java/com/fanruan/api/conf/ConfigurationKit.java b/src/main/java/com/fanruan/api/conf/ConfigurationKit.java new file mode 100644 index 0000000..b433465 --- /dev/null +++ b/src/main/java/com/fanruan/api/conf/ConfigurationKit.java @@ -0,0 +1,41 @@ +package com.fanruan.api.conf; + +import com.fr.config.Configuration; +import com.fr.config.holder.ConfigChangeListener; +import com.fr.transaction.Configurations; +import com.fr.transaction.ValidateProxy; +import com.fr.transaction.WorkerFacade; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-30 + * 配置监听管理器 + */ +public class ConfigurationKit { + + /** + * 注册监听配置变化的监听器 + * + * @param listener 监听器 + */ + public static void registerListener(ConfigChangeListener listener) { + ValidateProxy.getInstance().getValidateManager().registerListener(listener); + } + + /** + * 保存配置 + * + * @param type 配置的类型 + * @param action 保存动作 + */ + public static void modify(Class type, Runner action) { + Configurations.modify(new WorkerFacade(type) { + @Override + public void run() { + action.run(); + } + }); + } + +} diff --git a/src/main/java/com/fanruan/api/conf/Runner.java b/src/main/java/com/fanruan/api/conf/Runner.java new file mode 100644 index 0000000..b80c76e --- /dev/null +++ b/src/main/java/com/fanruan/api/conf/Runner.java @@ -0,0 +1,11 @@ +package com.fanruan.api.conf; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-30 + */ +public interface Runner { + + void run(); +} diff --git a/src/main/java/com/fanruan/api/conf/xml/XmlHolderKit.java b/src/main/java/com/fanruan/api/conf/xml/XmlHolderKit.java new file mode 100644 index 0000000..85c156d --- /dev/null +++ b/src/main/java/com/fanruan/api/conf/xml/XmlHolderKit.java @@ -0,0 +1,26 @@ +package com.fanruan.api.conf.xml; + +import com.fr.config.holder.Conf; +import com.fr.config.holder.impl.xml.XmlConf; +import com.fr.stable.xml.XMLable; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-30 + * 将xml文件写入fine_conf_entity的帮助类,不建议使用 + */ +public class XmlHolderKit { + + /** + * 创建一个要写入配置的xml多项 + * @param t 配置对象 + * @param clazz 类型 + * @param xmlTag xml标签 + * @param 类型 + * @return 配置对象 + */ + public static Conf obj(T t, Class clazz, String xmlTag) { + return new XmlConf(t, clazz, xmlTag); + } +} diff --git a/src/main/java/com/fanruan/api/data/ConnectionKit.java b/src/main/java/com/fanruan/api/data/ConnectionKit.java index e22f3e9..3931a38 100644 --- a/src/main/java/com/fanruan/api/data/ConnectionKit.java +++ b/src/main/java/com/fanruan/api/data/ConnectionKit.java @@ -2,6 +2,7 @@ package com.fanruan.api.data; import com.fanruan.api.util.TypeKit; import com.fr.data.impl.Connection; +import com.fr.data.impl.NameDatabaseConnection; import com.fr.file.ConnectionConfig; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -35,4 +36,14 @@ public class ConnectionKit { } return null; } + + /** + * 生成一个使用名字来引用的数据连接对象 + * + * @param name 数据连接的名字 + * @return 数据连接 + */ + public static Connection createNameConnection(String name) { + return new NameDatabaseConnection(name); + } } diff --git a/src/main/java/com/fanruan/api/data/connection/NameDatabaseConnection.java b/src/main/java/com/fanruan/api/data/connection/NameDatabaseConnection.java deleted file mode 100644 index 408e9e8..0000000 --- a/src/main/java/com/fanruan/api/data/connection/NameDatabaseConnection.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.fanruan.api.data.connection; - -public class NameDatabaseConnection extends com.fr.data.impl.NameDatabaseConnection{ -} diff --git a/src/main/java/com/fanruan/api/design/ui/component/UIPlaceholderTextField.java b/src/main/java/com/fanruan/api/design/ui/component/UIPlaceholderTextField.java index f8147ac..158c391 100644 --- a/src/main/java/com/fanruan/api/design/ui/component/UIPlaceholderTextField.java +++ b/src/main/java/com/fanruan/api/design/ui/component/UIPlaceholderTextField.java @@ -14,6 +14,10 @@ public class UIPlaceholderTextField extends PlaceholderTextField { super(); } + public UIPlaceholderTextField(int columns) { + super(columns); + } + public UIPlaceholderTextField(String placeHolder) { super(); setPlaceholder(placeHolder); diff --git a/src/main/java/com/fanruan/api/design/ui/component/UIToolbar.java b/src/main/java/com/fanruan/api/design/ui/component/UIToolbar.java index ddd8efb..d097378 100644 --- a/src/main/java/com/fanruan/api/design/ui/component/UIToolbar.java +++ b/src/main/java/com/fanruan/api/design/ui/component/UIToolbar.java @@ -1,9 +1,7 @@ package com.fanruan.api.design.ui.component; -import com.fr.design.gui.itoolbar.UIToolBarUI; - -public class UIToolbar extends com.fr.design.gui.itoolbar.UIToolbar{ - public UIToolbar(){ +public class UIToolbar extends com.fr.design.gui.itoolbar.UIToolbar { + public UIToolbar() { } diff --git a/src/main/java/com/fanruan/api/design/ui/toolbar/ToolBarDef.java b/src/main/java/com/fanruan/api/design/ui/toolbar/ToolBarDef.java new file mode 100644 index 0000000..8de1198 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/toolbar/ToolBarDef.java @@ -0,0 +1,36 @@ +package com.fanruan.api.design.ui.toolbar; + +import com.fanruan.api.design.ui.component.UIToolbar; +import com.fr.design.gui.itoolbar.UIToolBarUI; + +import javax.swing.*; +import java.awt.*; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-30 + */ +public class ToolBarDef extends com.fr.design.menu.ToolBarDef { + + public static UIToolbar createJToolBar(final Color background) { + UIToolbar toolbar = new UIToolbar(FlowLayout.LEFT); + toolbar.setUI(new UIToolBarUI() { + @Override + public void paint(Graphics g, JComponent c) { + Graphics2D g2 = (Graphics2D) g; + g2.setColor(background); + g2.fillRect(0, 0, c.getWidth(), c.getHeight()); + } + }); + toolbar.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0)); + return toolbar; + } + + public static UIToolbar createJToolBar() { + UIToolbar toolbar = new UIToolbar(FlowLayout.LEFT); + toolbar.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0)); + return toolbar; + } + +} diff --git a/src/main/java/com/fanruan/api/net/CloudKit.java b/src/main/java/com/fanruan/api/net/CloudKit.java index 775ea6e..27eae54 100644 --- a/src/main/java/com/fanruan/api/net/CloudKit.java +++ b/src/main/java/com/fanruan/api/net/CloudKit.java @@ -20,7 +20,7 @@ public class CloudKit { * @param defaultSiteUrl 默认值 * @return 键对应的值,如果没有值则返回默认值 */ - public @Nullable String acquireUrlByKind(@NotNull String kind, @NotNull String defaultSiteUrl) { + public static @Nullable String acquireUrlByKind(@NotNull String kind, @NotNull String defaultSiteUrl) { return CloudCenter.getInstance().acquireUrlByKind(kind, defaultSiteUrl); } @@ -30,7 +30,7 @@ public class CloudKit { * @param kind 键 * @return 键对应的值,如果没有值则返回空 */ - public @Nullable String acquireUrlByKind(@NotNull String kind) { + public static @Nullable String acquireUrlByKind(@NotNull String kind) { return CloudCenter.getInstance().acquireUrlByKind(kind); } } diff --git a/src/main/java/com/fanruan/api/util/IOKit.java b/src/main/java/com/fanruan/api/util/IOKit.java index 5b223e7..d7b560d 100644 --- a/src/main/java/com/fanruan/api/util/IOKit.java +++ b/src/main/java/com/fanruan/api/util/IOKit.java @@ -125,6 +125,19 @@ public class IOKit { return CommonIOUtils.inputStream2String(is, charset.toString()); } + /** + * 从输入流中按指定的编码读取字符串 + * + * @param is 输入流 + * @param charset 读取字符串的编码方式 + * @return 读取出来的字符串 + * @throws UnsupportedEncodingException 编码类型不支持则抛出此异常 + */ + public static String inputStream2String(InputStream is, String charset) + throws UnsupportedEncodingException { + return CommonIOUtils.inputStream2String(is, charset); + } + /** * 将输入流转为byte数组 *

@@ -214,6 +227,7 @@ public class IOKit { /** * 关闭通道 + * * @param device 通道 */ public static void close(Channel device) { @@ -222,6 +236,7 @@ public class IOKit { /** * 关闭输出流 + * * @param stream 输出流 */ public static void close(OutputStream stream) { @@ -230,6 +245,7 @@ public class IOKit { /** * 关闭输入流 + * * @param stream 输入流 */ public static void close(InputStream stream) { diff --git a/src/test/java/com/fanruan/api/net/http/HttpKitTest.java b/src/test/java/com/fanruan/api/net/http/HttpKitTest.java index a034a4a..b038634 100644 --- a/src/test/java/com/fanruan/api/net/http/HttpKitTest.java +++ b/src/test/java/com/fanruan/api/net/http/HttpKitTest.java @@ -1,6 +1,7 @@ package com.fanruan.api.net.http; import com.fanruan.api.Prepare; +import com.fanruan.api.macro.EncodeConstants; import com.fanruan.api.net.http.rs.HttpRequest; import com.fanruan.api.net.http.rs.HttpResponseType; import com.fanruan.api.net.http.rs.StreamResponseHandle; @@ -72,6 +73,8 @@ public class HttpKitTest extends Prepare { InputStream in = HttpKit.post("https://cloud.fanruan.com/site", map, HttpResponseType.STREAM); String text = IOKit.inputStream2String(in, StandardCharsets.UTF_8); assertEquals("{\"value\":\"http://bbs.fanruan.com/\"}", text); + String text2 = IOKit.inputStream2String(in, EncodeConstants.ENCODING_UTF_8); + assertEquals("{\"value\":\"http://bbs.fanruan.com/\"}", text2); } catch (SocketTimeoutException ignore) { } catch (Exception e) { e.printStackTrace();