From 6df075061c641e5a8418b45083ba8a8576ef0a7b Mon Sep 17 00:00:00 2001 From: richie Date: Thu, 18 Mar 2021 16:53:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E9=9B=86lo?= =?UTF-8?q?ck=E7=89=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/design/ui/component/UILockButton.java | 55 ++++++++ .../design/work/ConnectionComboBoxPanel.java | 65 ++++------ .../work/compat/ConnectionLockHelper.java | 50 +++++++ .../component/ItemEditableComboBoxPanel.java | 122 ++++++++++++++++++ 4 files changed, 255 insertions(+), 37 deletions(-) create mode 100644 src/main/java/com/fanruan/api/design/ui/component/UILockButton.java create mode 100644 src/main/java/com/fanruan/api/design/work/compat/ConnectionLockHelper.java create mode 100644 src/main/java/com/fanruan/api/design/work/component/ItemEditableComboBoxPanel.java diff --git a/src/main/java/com/fanruan/api/design/ui/component/UILockButton.java b/src/main/java/com/fanruan/api/design/ui/component/UILockButton.java new file mode 100644 index 0000000..6fabf6e --- /dev/null +++ b/src/main/java/com/fanruan/api/design/ui/component/UILockButton.java @@ -0,0 +1,55 @@ +package com.fanruan.api.design.ui.component; + +import com.fr.design.editlock.EditLockChangeEvent; +import com.fr.design.editlock.EditLockChangeListener; +import com.fr.design.editlock.EditLockUtils; +import com.fr.report.LockItem; + +import javax.swing.*; + +/** + * @author Yvan + * @version 10.0 + * Created by Yvan on 2021/1/20 + */ +public class UILockButton extends UIButton implements EditLockChangeListener { + + /** + * 锁定状态图标 + */ + private final Icon lockedIcon; + /** + * 正常状态图标 + */ + private final Icon normalIcon; + /** + * 锁定状态的提示信息 + */ + private final String lockedTooltips; + /** + * 正常状态的提示信息 + */ + private final String normalTooltips; + + public UILockButton(Icon lockedIcon, Icon normalIcon, String lockedTooltips, String normalTooltips) { + super(); + this.lockedIcon = lockedIcon; + this.normalIcon = normalIcon; + this.lockedTooltips = lockedTooltips; + this.normalTooltips = normalTooltips; + init(); + } + + private void init() { + boolean locked = EditLockUtils.isLocked(LockItem.CONNECTION); + this.setIcon(locked ? lockedIcon : normalIcon); + this.setToolTipText(locked ? lockedTooltips : normalTooltips); + } + + @Override + public void updateLockedState(EditLockChangeEvent event) { + this.setIcon(event.isLocked() ? lockedIcon : normalIcon); + this.setToolTipText(event.isLocked() ? lockedTooltips : normalTooltips); + this.repaint(); + } +} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java b/src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java index 2992ff5..4846c13 100644 --- a/src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java +++ b/src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java @@ -5,34 +5,28 @@ import com.fanruan.api.data.ConnectionKit; import com.fanruan.api.data.open.BaseConnection; import com.fanruan.api.design.DesignKit; import com.fanruan.api.design.macro.DataSourceConstants; +import com.fanruan.api.design.ui.component.UIButton; +import com.fanruan.api.design.work.compat.ConnectionLockHelper; +import com.fanruan.api.design.work.component.ItemEditableComboBoxPanel; import com.fanruan.api.generic.Runner; import com.fanruan.api.util.AssistKit; import com.fanruan.api.util.StringKit; -import com.fr.base.svg.IconUtils; import com.fr.data.impl.Connection; import com.fr.design.DesignerEnvManager; import com.fr.design.data.datapane.connect.ConnectionListPane; import com.fr.design.data.datapane.connect.ConnectionShowPane; -import com.fr.design.data.datapane.connect.ItemEditableComboBoxPanel; import com.fr.design.dialog.BasicDialog; import com.fr.design.dialog.DialogActionAdapter; -import com.fr.design.editlock.ConnectionLockChangeChecker; -import com.fr.design.editlock.EditLockUtils; -import com.fr.design.gui.ibutton.UIButton; -import com.fr.design.gui.ibutton.UILockButton; import com.fr.file.ConnectionConfig; -import com.fr.report.LockItem; +import com.fr.general.GeneralUtils; import com.fr.stable.NameReference; -import com.fr.stable.StringUtils; import com.fr.transaction.WorkerCallBack; import com.fr.workspace.WorkContext; import com.fr.workspace.server.connection.DBConnectAuth; import org.jetbrains.annotations.Nullable; -import javax.swing.SwingUtilities; -import java.awt.Dimension; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +import javax.swing.*; +import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.ArrayList; @@ -66,20 +60,15 @@ public class ConnectionComboBoxPanel extends ItemEditableComboBoxPanel { @Override protected UIButton initEditButton(UIButton editButton, Dimension buttonSize) { - editButton = new UILockButton( - EditLockUtils.CONNECTION_LOCKED_ICON, - IconUtils.readIcon("/com/fr/design/images/m_web/connection"), - EditLockUtils.CONNECTION_LOCKED_TOOLTIPS, - null - ); - editButton.setPreferredSize(buttonSize); - editButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent evt) { - editItems(); - } - }); - ConnectionLockChangeChecker.getInstance().addEditLockChangeListener((UILockButton) editButton); - return editButton; + if (isChangedVersion()) { + return ConnectionLockHelper.generateLockButton(buttonSize, e -> editItems()); + } else { + return super.initEditButton(editButton, buttonSize); + } + } + + private boolean isChangedVersion() { + return GeneralUtils.readBuildNO().compareTo("2021.03.15") > 0; } protected Iterator items() { @@ -120,14 +109,12 @@ public class ConnectionComboBoxPanel extends ItemEditableComboBoxPanel { protected void editItems() { - // 尝试为数据连接加锁 - boolean actionLock = EditLockUtils.lock(LockItem.CONNECTION); - if (!actionLock) { - // 锁定失败,代表已经被其他用户锁定,跳出弹窗提示 - EditLockUtils.showLockMessage(this); - return; + if (isChangedVersion()) { + boolean lock = ConnectionLockHelper.doLock(this); + if (lock) { + return; + } } - // 锁定成功,执行后续操作 final ConnectionListPane connectionListPane = new ConnectionListPane(); final ConnectionConfig connectionConfig = ConnectionConfig.getInstance(); ConnectionConfig cloned = connectionConfig.mirror(); @@ -155,8 +142,7 @@ public class ConnectionComboBoxPanel extends ItemEditableComboBoxPanel { @Override public void afterCommit() { DesignKit.getDesignerBean("databasename").refreshBeanElement(); - // 关闭定义数据连接页面,为其解锁 - EditLockUtils.unlock(LockItem.CONNECTION); + unlock(); } @Override @@ -169,14 +155,19 @@ public class ConnectionComboBoxPanel extends ItemEditableComboBoxPanel { @Override public void doCancel() { super.doCancel(); - // 关闭定义数据连接页面,为其解锁 - EditLockUtils.unlock(LockItem.CONNECTION); + unlock(); } }); connectionListDialog.setVisible(true); refreshItems(); } + private void unlock() { + if (isChangedVersion()) { + ConnectionLockHelper.unlock(); + } + } + /** * @param connection 数据库链接 */ diff --git a/src/main/java/com/fanruan/api/design/work/compat/ConnectionLockHelper.java b/src/main/java/com/fanruan/api/design/work/compat/ConnectionLockHelper.java new file mode 100644 index 0000000..19e1c17 --- /dev/null +++ b/src/main/java/com/fanruan/api/design/work/compat/ConnectionLockHelper.java @@ -0,0 +1,50 @@ +package com.fanruan.api.design.work.compat; + +import com.fanruan.api.design.ui.component.UIButton; +import com.fanruan.api.design.ui.component.UILockButton; +import com.fr.base.svg.IconUtils; +import com.fr.design.editlock.ConnectionLockChangeChecker; +import com.fr.design.editlock.EditLockUtils; +import com.fr.report.LockItem; + +import java.awt.*; +import java.awt.event.ActionListener; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2021/3/18 + * 用于兼容没有数据连接lock的版本 + */ +public class ConnectionLockHelper { + + public static UIButton generateLockButton(Dimension buttonSize, ActionListener listener) { + UILockButton editButton = new UILockButton( + EditLockUtils.CONNECTION_LOCKED_ICON, + IconUtils.readIcon("/com/fr/design/images/m_web/connection"), + EditLockUtils.CONNECTION_LOCKED_TOOLTIPS, + null + ); + editButton.setPreferredSize(buttonSize); + editButton.addActionListener(listener); + ConnectionLockChangeChecker.getInstance().addEditLockChangeListener(editButton); + return editButton; + } + + public static boolean doLock(Component component) { + // 尝试为数据连接加锁 + boolean actionLock = EditLockUtils.lock(LockItem.CONNECTION); + if (!actionLock) { + // 锁定失败,代表已经被其他用户锁定,跳出弹窗提示 + EditLockUtils.showLockMessage(component); + return true; + } + return false; + } + + public static void unlock() { + // 关闭定义数据连接页面,为其解锁 + EditLockUtils.unlock(LockItem.CONNECTION); + } + +} diff --git a/src/main/java/com/fanruan/api/design/work/component/ItemEditableComboBoxPanel.java b/src/main/java/com/fanruan/api/design/work/component/ItemEditableComboBoxPanel.java new file mode 100644 index 0000000..ec356ac --- /dev/null +++ b/src/main/java/com/fanruan/api/design/work/component/ItemEditableComboBoxPanel.java @@ -0,0 +1,122 @@ +package com.fanruan.api.design.work.component; + +import com.fanruan.api.design.ui.component.UIButton; +import com.fanruan.api.design.ui.component.UIComboBox; +import com.fanruan.api.util.IOKit; +import com.fanruan.api.util.StringKit; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2021/3/18 + */ +public abstract class ItemEditableComboBoxPanel extends JPanel { + + private static final long serialVersionUID = 1L; + + protected static final Object EMPTY = new Object() { + public String toString() { + return StringKit.EMPTY; + } + }; + + protected UIComboBox itemComboBox; + protected UIButton editButton; + protected UIButton refreshButton; + + public ItemEditableComboBoxPanel() { + super(); + + initComponents(); + } + + protected void initComponents() { + this.setLayout(new BorderLayout(4, 4)); + Dimension buttonSize = new Dimension(26, 20); + itemComboBox = new UIComboBox<>(); + itemComboBox.setEnabled(true); + this.add(itemComboBox, BorderLayout.CENTER); + refreshButton = new UIButton(IOKit.readIcon("/com/fr/design/images/control/refresh.png")); + JPanel jPanel = new JPanel(new GridLayout(0, 2, 4, 4)); + editButton = initEditButton(editButton, buttonSize); + jPanel.add(editButton); + jPanel.add(refreshButton); + this.add(jPanel, BorderLayout.EAST); + refreshButton.setPreferredSize(buttonSize); + refreshButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + refreshItems(); + } + }); + } + + protected UIButton initEditButton(UIButton editButton, Dimension buttonSize) { + editButton = new UIButton(IOKit.readIcon("/com/fr/design/images/control/control-center2.png")); + editButton.setPreferredSize(buttonSize); + editButton.addActionListener(evt -> editItems()); + return editButton; + } + + + /** + * 给itemComboBox添加ActionListener + */ + public void addComboBoxActionListener(ActionListener l) { + itemComboBox.addActionListener(l); + } + + protected void refreshItems() { + // 记录原来选中的Item,重新加载后需要再次选中 + Object lastSelectedItem = itemComboBox.getSelectedItem(); + + DefaultComboBoxModel model = ((DefaultComboBoxModel) itemComboBox.getModel()); + model.removeAllElements(); + + // 先加EMPTY,再加items + model.addElement(EMPTY); + + java.util.Iterator itemIt = items(); + while (itemIt.hasNext()) { + model.addElement(itemIt.next()); + } + + // 再次选中之前选中的Item + int idx = model.getIndexOf(lastSelectedItem); + if (idx < 0) { + idx = 0; + } + itemComboBox.setSelectedIndex(idx); + } + + /* + * 得到其中的itemComboBox所选中的Item + */ + public String getSelectedItem() { + Object selected = itemComboBox.getSelectedItem(); + + return selected instanceof String ? (String) selected : null; + } + + /* + * 选中name项 + */ + public void setSelectedItem(String name) { + DefaultComboBoxModel model = ((DefaultComboBoxModel) itemComboBox.getModel()); + model.setSelectedItem(name); + } + + /* + * 刷新ComboBox.items + */ + protected abstract java.util.Iterator items(); + + /* + * 弹出对话框编辑Items + */ + protected abstract void editItems(); +}