You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
303 lines
8.3 KiB
303 lines
8.3 KiB
package com.fine.component.popup; |
|
|
|
import javax.swing.JComponent; |
|
import javax.swing.JLayeredPane; |
|
import javax.swing.JRootPane; |
|
import javax.swing.SwingUtilities; |
|
import java.awt.CardLayout; |
|
import java.awt.Component; |
|
import java.awt.Container; |
|
import java.awt.Window; |
|
import java.awt.event.MouseAdapter; |
|
import java.awt.event.WindowAdapter; |
|
import java.awt.event.WindowEvent; |
|
|
|
/** |
|
* 遮罩功能: 遮罩面板管理器 |
|
* |
|
* @author Levy.Xie |
|
* @since 12.0 |
|
* Created on 2024/12/12 |
|
*/ |
|
public class GlassPopupManager { |
|
|
|
private static GlassPopupManager instance; |
|
protected WindowSnapshots windowSnapshots; |
|
protected JLayeredPane layerPane; |
|
protected Container contentPane; |
|
|
|
private GlassPopupManager() { |
|
init(); |
|
} |
|
|
|
private void init() { |
|
layerPane = new JLayeredPane(); |
|
layerPane.setLayout(new CardLayout()); |
|
} |
|
|
|
/** |
|
* 添加并显示弹窗组件 |
|
* |
|
* @param component 组件 |
|
* @param name 组件名 |
|
*/ |
|
protected void addAndShowPopup(GlassPaneChild component, String name) { |
|
GlassPopup popup = new GlassPopup(this, component); |
|
instance.layerPane.applyComponentOrientation(instance.contentPane.getComponentOrientation()); |
|
popup.applyComponentOrientation(instance.contentPane.getComponentOrientation()); |
|
if (name != null) { |
|
popup.setName(name); |
|
} |
|
layerPane.setLayer(popup, JLayeredPane.DRAG_LAYER); |
|
layerPane.add(popup, 0); |
|
popup.setVisible(true); |
|
popup.setShowPopup(true); |
|
if (!layerPane.isVisible()) { |
|
layerPane.setVisible(true); |
|
} |
|
layerPane.grabFocus(); |
|
} |
|
|
|
private void updateLayout() { |
|
for (Component com : layerPane.getComponents()) { |
|
com.revalidate(); |
|
} |
|
} |
|
|
|
/** |
|
* 设置遮罩父窗口 |
|
* |
|
* @param container 遮罩父容器 |
|
*/ |
|
public static void install(Container container) { |
|
instance = new GlassPopupManager(); |
|
Window window; |
|
if (container instanceof Window) { |
|
window = (Window) container; |
|
} else { |
|
window = SwingUtilities.getWindowAncestor(container); |
|
} |
|
instance.windowSnapshots = new WindowSnapshots(window); |
|
|
|
window.addWindowStateListener(new WindowAdapter() { |
|
@Override |
|
public void windowStateChanged(WindowEvent e) { |
|
SwingUtilities.invokeLater(() -> { |
|
instance.updateLayout(); |
|
}); |
|
} |
|
}); |
|
|
|
JRootPane rootPane = SwingUtilities.getRootPane(container); |
|
instance.contentPane = rootPane.getContentPane(); |
|
rootPane.setGlassPane(instance.layerPane); |
|
|
|
interceptMouseEvents(instance.layerPane); |
|
} |
|
|
|
private static void interceptMouseEvents(JComponent layerPane) { |
|
// 拦截鼠标事件 |
|
layerPane.addMouseListener(new MouseAdapter() { |
|
}); |
|
layerPane.addMouseMotionListener(new MouseAdapter() { |
|
}); |
|
} |
|
|
|
/** |
|
* 显示组件 |
|
* |
|
* @param component 组件 |
|
* @param name 组件名 |
|
*/ |
|
public static void showPopup(GlassPaneChild component, String name) { |
|
if (component.getMouseListeners().length == 0) { |
|
component.addMouseListener(new MouseAdapter() { |
|
}); |
|
} |
|
instance.addAndShowPopup(component, name); |
|
} |
|
|
|
/** |
|
* 显示组件 |
|
* |
|
* @param component 组件 |
|
*/ |
|
public static void showPopup(GlassPaneChild component) { |
|
showPopup(component, null); |
|
} |
|
|
|
/** |
|
* 压栈,放入弹窗组件 |
|
* |
|
* @param component 组件 |
|
* @param name 组件名 |
|
*/ |
|
public static void push(GlassPaneChild component, String name) { |
|
for (Component com : instance.layerPane.getComponents()) { |
|
if (com.getName() != null && com.getName().equals(name)) { |
|
if (com instanceof GlassPopup) { |
|
GlassPopup popup = (GlassPopup) com; |
|
popup.getComponentLayer().pushComponent(component); |
|
break; |
|
} |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* 出栈,弹出弹窗组件 |
|
* |
|
* @param name 组件名 |
|
*/ |
|
public static void pop(String name) { |
|
for (Component com : instance.layerPane.getComponents()) { |
|
if (com.getName() != null && com.getName().equals(name)) { |
|
if (com instanceof GlassPopup) { |
|
GlassPopup popup = (GlassPopup) com; |
|
popup.getComponentLayer().popComponent(); |
|
break; |
|
} |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* 出栈,弹出弹窗组件 |
|
* |
|
* @param component 组件 |
|
*/ |
|
public static void pop(Component component) { |
|
for (Component com : instance.layerPane.getComponents()) { |
|
if (com instanceof GlassPopup) { |
|
GlassPopup popup = (GlassPopup) com; |
|
if (popup.getComponent() == component) { |
|
popup.getComponentLayer().popComponent(); |
|
} |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* 基于序号关闭弹窗组件 |
|
* |
|
* @param index 序号 |
|
*/ |
|
public static void closePopup(int index) { |
|
index = instance.layerPane.getComponentCount() - 1 - index; |
|
if (index >= 0 && index < instance.layerPane.getComponentCount()) { |
|
if (instance.layerPane.getComponent(index) instanceof GlassPopup) { |
|
GlassPopup popup = (GlassPopup) instance.layerPane.getComponent(index); |
|
popup.setShowPopup(false); |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* 基于名称关闭组件 |
|
* |
|
* @param name 组件名称 |
|
*/ |
|
public static void closePopup(String name) { |
|
for (Component com : instance.layerPane.getComponents()) { |
|
if (com.getName() != null && com.getName().equals(name)) { |
|
if (com instanceof GlassPopup) { |
|
GlassPopup popup = (GlassPopup) com; |
|
popup.setShowPopup(false); |
|
} |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* 关闭组件 |
|
* |
|
* @param component 组件 |
|
*/ |
|
public static void closePopup(Component component) { |
|
for (Component com : instance.layerPane.getComponents()) { |
|
if (com instanceof GlassPopup) { |
|
GlassPopup popup = (GlassPopup) com; |
|
if (popup.getComponent() == component) { |
|
popup.setShowPopup(false); |
|
} |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* 关闭最后一个组件 |
|
*/ |
|
public static void closePopupLast() { |
|
closePopup(getPopupCount() - 1); |
|
} |
|
|
|
/** |
|
* 关闭全部组件 |
|
*/ |
|
public static void closePopupAll() { |
|
for (Component com : instance.layerPane.getComponents()) { |
|
if (com instanceof GlassPopup) { |
|
GlassPopup popup = (GlassPopup) com; |
|
popup.setShowPopup(false); |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* 基于组件名,判断组件是否显示 |
|
* |
|
* @param name 组件名 |
|
* @return 是否显示 |
|
*/ |
|
public static boolean isShowing(String name) { |
|
boolean act = false; |
|
for (Component com : instance.layerPane.getComponents()) { |
|
if (com.getName() != null && com.getName().equals(name)) { |
|
act = true; |
|
break; |
|
} |
|
} |
|
return act; |
|
} |
|
|
|
/** |
|
* 判断组件是否显示 |
|
* |
|
* @param component 组件 |
|
* @return 是否显示 |
|
*/ |
|
public static boolean isShowing(Component component) { |
|
boolean act = false; |
|
for (Component com : instance.layerPane.getComponents()) { |
|
if (com instanceof GlassPopup) { |
|
GlassPopup popup = (GlassPopup) com; |
|
if (popup.getComponent() == component) { |
|
act = true; |
|
break; |
|
} |
|
} |
|
} |
|
return act; |
|
} |
|
|
|
/** |
|
* 获取弹窗组件数量 |
|
* |
|
* @return 弹窗数量 |
|
*/ |
|
public static int getPopupCount() { |
|
return instance.layerPane.getComponentCount(); |
|
} |
|
|
|
/** |
|
* 移除组件 |
|
* |
|
* @param popup 弹窗组件 |
|
*/ |
|
protected synchronized void removePopup(Component popup) { |
|
layerPane.remove(popup); |
|
if (layerPane.getComponentCount() == 0) { |
|
layerPane.setVisible(false); |
|
} |
|
} |
|
}
|
|
|