kerry
2 years ago
10 changed files with 598 additions and 82 deletions
@ -0,0 +1,361 @@
|
||||
package com.fr.design.file; |
||||
|
||||
import com.fr.design.constants.UIConstants; |
||||
import com.fr.design.gui.ibutton.UIButton; |
||||
import com.fr.design.gui.imenu.UIMenuItem; |
||||
import com.fr.design.gui.imenu.UIScrollPopUpMenu; |
||||
import com.fr.design.i18n.Toolkit; |
||||
import com.fr.design.mainframe.JTemplate; |
||||
import com.fr.design.utils.TemplateUtils; |
||||
import com.fr.file.FILE; |
||||
import com.fr.general.IOUtils; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.stable.collections.CollectionUtils; |
||||
import com.fr.third.javax.annotation.Nonnull; |
||||
|
||||
import javax.swing.BorderFactory; |
||||
import javax.swing.Icon; |
||||
import javax.swing.JPanel; |
||||
import javax.swing.SwingConstants; |
||||
import java.awt.BorderLayout; |
||||
import java.awt.Component; |
||||
import java.awt.Dimension; |
||||
import java.awt.event.ActionEvent; |
||||
import java.awt.event.ActionListener; |
||||
import java.awt.event.MouseAdapter; |
||||
import java.awt.event.MouseEvent; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 右侧下拉菜单的工厂类 |
||||
* @author Carlson |
||||
* @since 11.0 |
||||
* created on 2023-04-14 |
||||
**/ |
||||
public class MultiTemplateTabMenuFactory { |
||||
|
||||
private static final Icon CLOSE = IOUtils.readIcon("/com/fr/design/images/buttonicon/close_icon.png"); |
||||
private static final Icon MOUSE_OVER_CLOSE = IOUtils.readIcon("/com/fr/design/images/buttonicon/mouseoverclose icon.png"); |
||||
private static final Icon MOUSE_PRESS_CLOSE = IOUtils.readIcon("/com/fr/design/images/buttonicon/pressclose icon.png"); |
||||
|
||||
private static final int ITEM_SIZE = 25; |
||||
|
||||
private UIScrollPopUpMenu menu = null; |
||||
|
||||
private static MultiTemplateTabMenuFactory INSTANCE = new MultiTemplateTabMenuFactory(); |
||||
|
||||
private MultiTemplateTabMenuFactory() { |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 返回右侧下拉菜单的工厂类 |
||||
* @return |
||||
*/ |
||||
public static MultiTemplateTabMenuFactory getInstance() { |
||||
return INSTANCE; |
||||
} |
||||
|
||||
/** |
||||
* tab上的下拉菜单 |
||||
*/ |
||||
public UIScrollPopUpMenu createMenu() { |
||||
menu = new UIScrollPopUpMenu(); |
||||
menu.setBorder(BorderFactory.createEmptyBorder(-3, 3, 3, 0)); |
||||
|
||||
menu.add(initCloseOther()); |
||||
menu.add(createEmptyRow()); |
||||
menu.addSeparator(); |
||||
menu.add(createEmptyRow()); |
||||
menu.add(createCategory(Toolkit.i18nText("Fine-Design_Basic_Tab_Current_Category_Templates"))); |
||||
Component[] items = createCurrentCategory(); |
||||
for (Component item : items) { |
||||
menu.add(item); |
||||
} |
||||
items = createOtherCategory(); |
||||
if (items.length > 0) { |
||||
menu.addSeparator(); |
||||
menu.add(createEmptyRow()); |
||||
menu.add(createCategory(Toolkit.i18nText("Fine-Design_Basic_Tab_Other_Category_Templates"))); |
||||
for (Component item : items) { |
||||
menu.add(item); |
||||
} |
||||
} |
||||
Dimension dimension = menu.getPreferredSize(); |
||||
dimension.width += ITEM_SIZE; |
||||
menu.setPreferredSize(dimension); |
||||
return menu; |
||||
} |
||||
|
||||
/** |
||||
* 关闭其它按钮 |
||||
*/ |
||||
private UIMenuItem initCloseOther() { |
||||
UIMenuItem closeOther = new UIMenuItem(Toolkit.i18nText("Fine-Design_Basic_Tab_Close_Other_Templates_Of_Current_Category")); |
||||
closeOther.setHorizontalAlignment(SwingConstants.CENTER); |
||||
Dimension dimension = closeOther.getPreferredSize(); |
||||
dimension.height = ITEM_SIZE; |
||||
closeOther.setPreferredSize(dimension); |
||||
String currentOperator = getCurrentTabOperatorType(); |
||||
closeOther.addActionListener(new ActionListener() { |
||||
@Override |
||||
public void actionPerformed(ActionEvent e) { |
||||
|
||||
MultiTemplateTabPane.getInstance().closeAllByOperatorType(currentOperator); |
||||
} |
||||
}); |
||||
if (MultiTemplateTabPane.getInstance().getOpenedJTemplatesByOperator(currentOperator).size() <= 1) { |
||||
closeOther.setEnabled(false); |
||||
} |
||||
return closeOther; |
||||
} |
||||
|
||||
|
||||
private void closeAndFreeLock(@Nonnull JTemplate<?, ?> template) { |
||||
FILE file = template.getEditingFILE(); |
||||
// 只有是环境内的文件,才执行释放锁
|
||||
if (file != null && file.isEnvFile()) { |
||||
// release lock
|
||||
TemplateResourceManager.getResource().closeTemplate(file.getPath()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 美观用 |
||||
*/ |
||||
private JPanel createEmptyRow() { |
||||
return new JPanel() { |
||||
@Override |
||||
public Dimension getPreferredSize() { |
||||
Dimension d = super.getPreferredSize(); |
||||
d.height = 1; |
||||
return d; |
||||
} |
||||
}; |
||||
} |
||||
|
||||
/** |
||||
* 模板分类item |
||||
*/ |
||||
private UIButton createCategory(String categoryName) { |
||||
UIButton button = new UIButton(categoryName); |
||||
button.setBorderPainted(false); |
||||
button.setExtraPainted(false); |
||||
button.setPreferredSize(new Dimension(menu.getWidth(), ITEM_SIZE)); |
||||
button.setOpaque(true); |
||||
button.setBackground(UIConstants.NORMAL_BACKGROUND); |
||||
button.setHorizontalAlignment(SwingConstants.LEFT); |
||||
button.setForeground(UIConstants.FLESH_BLUE); |
||||
return button; |
||||
} |
||||
|
||||
/** |
||||
* 创建 当前分类模板 item数组 |
||||
*/ |
||||
private Component[] createCurrentCategory() { |
||||
return createListDownItem(MultiTemplateTabPane.getInstance().getOpenedJTemplatesByOperator(getCurrentTabOperatorType())); |
||||
} |
||||
|
||||
private String getCurrentTabOperatorType(){ |
||||
JTemplate jTemplate= HistoryTemplateListCache.getInstance().getCurrentEditingTemplate(); |
||||
return jTemplate.getTemplateTabOperatorType(); |
||||
} |
||||
|
||||
/** |
||||
* 创建 其它分类模板 item数组 |
||||
*/ |
||||
private Component[] createOtherCategory() { |
||||
String currentOperator = getCurrentTabOperatorType(); |
||||
List<JTemplate<?, ?>> openedTemplates = new ArrayList<>(); |
||||
Map<String, List<JTemplate<?, ?>>> map = MultiTemplateTabPane.getInstance().getOpenedJTemplatesByCategory(); |
||||
for (Map.Entry<String, List<JTemplate<?, ?>>> entry : map.entrySet()) { |
||||
if (!StringUtils.equals(currentOperator, entry.getKey())) { |
||||
openedTemplates.addAll(entry.getValue()); |
||||
} |
||||
} |
||||
return createListDownItem(openedTemplates); |
||||
} |
||||
|
||||
/** |
||||
* 根据template列表创建多个item |
||||
*/ |
||||
private Component[] createListDownItem(List<JTemplate<?, ?>> openedTemplates) { |
||||
if (!CollectionUtils.isEmpty(openedTemplates)) { |
||||
Component[] templates = new Component[openedTemplates.size()]; |
||||
for (int i = 0; i < openedTemplates.size(); i++) { |
||||
templates[i] = createListDownMenuItem(openedTemplates.get(i)); |
||||
} |
||||
return templates; |
||||
} |
||||
return new Component[0]; |
||||
} |
||||
|
||||
/** |
||||
* 根据template对象创建item |
||||
*/ |
||||
private Component createListDownMenuItem(JTemplate<?, ?> template) { |
||||
JPanel jPanel = new JPanel(); |
||||
jPanel.setPreferredSize(new Dimension(menu.getWidth(), ITEM_SIZE)); |
||||
jPanel.setLayout(new BorderLayout()); |
||||
|
||||
MenuItemButtonGroup menuItemButtonGroup = new MenuItemButtonGroup(template); |
||||
if (template == HistoryTemplateListCache.getInstance().getCurrentEditingTemplate()) { |
||||
menuItemButtonGroup.templateButton.setForeground(UIConstants.FLESH_BLUE); |
||||
} |
||||
|
||||
jPanel.add(menuItemButtonGroup.iconButton, BorderLayout.WEST); |
||||
jPanel.add(menuItemButtonGroup.templateButton, BorderLayout.CENTER); |
||||
jPanel.add(menuItemButtonGroup.closeButton, BorderLayout.EAST); |
||||
|
||||
return jPanel; |
||||
} |
||||
|
||||
/** |
||||
* menu的item由模板图标、模板名、模板关闭按钮组成 |
||||
*/ |
||||
private class MenuItemButtonGroup { |
||||
|
||||
private final UIButton iconButton; |
||||
private final UIButton templateButton; |
||||
private final UIButton closeButton; |
||||
|
||||
public MenuItemButtonGroup(JTemplate<?, ?> template) { |
||||
iconButton = createIconButton(template); |
||||
templateButton = createTemplateButton(template); |
||||
closeButton = createCloseButton(); |
||||
initListener(template); |
||||
} |
||||
|
||||
/** |
||||
* item[0] 模板图标按钮初始化 |
||||
*/ |
||||
private UIButton createIconButton(JTemplate<?, ?> template) { |
||||
UIButton button = new UIButton(template.getIcon(), template.getIcon(), template.getIcon()); |
||||
button.setPreferredSize(new Dimension(ITEM_SIZE, ITEM_SIZE)); |
||||
button.setOpaque(true); |
||||
button.setBackground(UIConstants.NORMAL_BACKGROUND); |
||||
return button; |
||||
} |
||||
|
||||
/** |
||||
* item[1] 切换模板按钮初始化 |
||||
*/ |
||||
private UIButton createTemplateButton(JTemplate<?, ?> template) { |
||||
UIButton button = new UIButton(TemplateUtils.createLockeTemplatedName(template, template.getTemplateName())); |
||||
button.setBorderPainted(false); |
||||
button.setExtraPainted(false); |
||||
button.setPreferredSize(new Dimension(menu.getWidth() - ITEM_SIZE * 2, ITEM_SIZE)); |
||||
button.setOpaque(true); |
||||
button.setBackground(UIConstants.NORMAL_BACKGROUND); |
||||
button.setHorizontalAlignment(SwingConstants.LEFT); |
||||
return button; |
||||
} |
||||
|
||||
/** |
||||
* item[2] 关闭模板图标按钮初始化 |
||||
*/ |
||||
private UIButton createCloseButton() { |
||||
UIButton button = new UIButton(CLOSE, MOUSE_OVER_CLOSE, MOUSE_PRESS_CLOSE); |
||||
button.setPreferredSize(new Dimension(ITEM_SIZE, ITEM_SIZE)); |
||||
button.setOpaque(true); |
||||
button.setBackground(UIConstants.NORMAL_BACKGROUND); |
||||
button.setVisible(false); |
||||
return button; |
||||
} |
||||
|
||||
private void initListener(JTemplate<?, ?> template) { |
||||
initIconButtonListener(); |
||||
initTemplateButtonListener(template); |
||||
initCloseButtonListener(template); |
||||
} |
||||
|
||||
/** |
||||
* item[0] 模板图标按钮鼠标事件 |
||||
*/ |
||||
private void initIconButtonListener() { |
||||
iconButton.addMouseListener(new MouseAdapter() { |
||||
@Override |
||||
public void mouseEntered(MouseEvent e) { |
||||
fireMouseEnteredEvent(); |
||||
} |
||||
|
||||
@Override |
||||
public void mouseExited(MouseEvent e) { |
||||
fireMouseExitedEvent(); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* item[1] 切换模板按钮鼠标事件 |
||||
*/ |
||||
private void initTemplateButtonListener(JTemplate<?, ?> template) { |
||||
templateButton.addMouseListener(new MouseAdapter() { |
||||
@Override |
||||
public void mouseClicked(MouseEvent e) { |
||||
menu.setVisible(false); |
||||
MultiTemplateTabPane.getInstance().switchJTemplate(template); |
||||
} |
||||
|
||||
@Override |
||||
public void mouseEntered(MouseEvent e) { |
||||
fireMouseEnteredEvent(); |
||||
} |
||||
|
||||
@Override |
||||
public void mouseExited(MouseEvent e) { |
||||
fireMouseExitedEvent(); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* item[2] 关闭模板按钮鼠标事件 |
||||
*/ |
||||
private void initCloseButtonListener(JTemplate<?, ?> template) { |
||||
closeButton.addMouseListener(new MouseAdapter() { |
||||
@Override |
||||
public void mouseClicked(MouseEvent e) { |
||||
menu.setVisible(false); |
||||
MultiTemplateTabPane.getInstance().setIsCloseCurrent(template == HistoryTemplateListCache.getInstance().getCurrentEditingTemplate()); |
||||
MultiTemplateTabPane.getInstance().closeFormat(template); |
||||
MultiTemplateTabPane.getInstance().closeSpecifiedTemplate(template); |
||||
} |
||||
|
||||
@Override |
||||
public void mouseEntered(MouseEvent e) { |
||||
fireMouseEnteredEvent(); |
||||
} |
||||
|
||||
@Override |
||||
public void mouseExited(MouseEvent e) { |
||||
fireMouseExitedEvent(); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* mouse移入item范围 |
||||
*/ |
||||
private void fireMouseEnteredEvent() { |
||||
iconButton.setBackground(UIConstants.HOVER_BLUE); |
||||
templateButton.setBackground(UIConstants.HOVER_BLUE); |
||||
closeButton.setBackground(UIConstants.HOVER_BLUE); |
||||
closeButton.setVisible(true); |
||||
} |
||||
|
||||
/** |
||||
* mouse移出item范围 |
||||
*/ |
||||
private void fireMouseExitedEvent() { |
||||
iconButton.setBackground(UIConstants.NORMAL_BACKGROUND); |
||||
templateButton.setBackground(UIConstants.NORMAL_BACKGROUND); |
||||
closeButton.setBackground(UIConstants.NORMAL_BACKGROUND); |
||||
closeButton.setVisible(false); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue