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.
390 lines
15 KiB
390 lines
15 KiB
package com.fr.design.mainframe.theme; |
|
|
|
import com.fr.base.theme.TemplateTheme; |
|
import com.fr.base.theme.TemplateThemeConfig; |
|
import com.fr.base.theme.settings.ThemeThumbnail; |
|
import com.fr.design.dialog.BasicPane; |
|
import com.fr.design.dialog.FineJOptionPane; |
|
import com.fr.design.gui.frpane.AttributeChangeListener; |
|
import com.fr.design.gui.ibutton.UIButton; |
|
import com.fr.design.gui.ilable.UILabel; |
|
import com.fr.design.i18n.Toolkit; |
|
import com.fr.design.layout.FRGUIPaneFactory; |
|
import com.fr.design.mainframe.theme.dialog.TemplateThemeProfileDialog; |
|
import com.fr.design.mainframe.theme.edit.ui.LabelUtils; |
|
import com.fr.design.mainframe.theme.ui.AutoCheckTextField; |
|
import com.fr.design.mainframe.theme.ui.AutoCheckThemeNameTextField; |
|
import com.fr.design.mainframe.toast.DesignerToastMsgUtil; |
|
import com.fr.design.utils.gui.GUICoreUtils; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.transaction.CallBackAdaptor; |
|
import com.fr.widgettheme.ThemePreviewTerminal; |
|
import com.fr.widgettheme.theme.widget.theme.WidgetThemeDisplayConstants; |
|
|
|
import javax.swing.BorderFactory; |
|
import javax.swing.JDialog; |
|
import javax.swing.JOptionPane; |
|
import javax.swing.JPanel; |
|
import javax.swing.SwingUtilities; |
|
import java.awt.BorderLayout; |
|
import java.awt.Color; |
|
import java.awt.Dimension; |
|
import java.awt.Image; |
|
import java.awt.event.ActionEvent; |
|
import java.awt.event.ActionListener; |
|
import java.awt.event.WindowAdapter; |
|
import java.awt.event.WindowEvent; |
|
|
|
import static com.fr.design.i18n.Toolkit.i18nText; |
|
|
|
/** |
|
* @author Starryi |
|
* @version 1.0 |
|
* Created by Starryi on 2021/8/13 |
|
*/ |
|
public abstract class TemplateThemeProfilePane<T extends TemplateTheme> extends BasicPane { |
|
public static final int TITLE_BORDER_FONT = 12; |
|
public static final int LEFT_TITLE_PANE_WIDTH = 627; |
|
public static final int LEFT_TITLE_PANE_HEIGHT = 539; |
|
public static final int PREVIEW_PANE_WIDTH = LEFT_TITLE_PANE_WIDTH - 10; |
|
public static final int PREVIEW_PANE_HEIGHT = LEFT_TITLE_PANE_HEIGHT - TITLE_BORDER_FONT - 16; |
|
|
|
protected TemplateThemePreviewPane<T> themePreviewPane; |
|
protected TemplateThemeEditorPane<T> themeEditorPane; |
|
protected UILabel leftTitleLabel; |
|
protected UILabel leftTitlePromptLabel; |
|
protected boolean isPopulating = false; |
|
protected boolean isMutable = false; |
|
|
|
protected final TemplateThemeConfig<T> config; |
|
|
|
private UIButton saveButton = new UIButton(); |
|
private UIButton saveAsButton = new UIButton(); |
|
|
|
private TemplateThemeProfileActionListener actionListener = new TemplateThemeProfileActionAdapter(); |
|
|
|
public TemplateThemeProfilePane(TemplateThemeConfig<T> config) { |
|
super(); |
|
this.config = config; |
|
initializePane(); |
|
} |
|
|
|
private void initializePane() { |
|
setLayout(new BorderLayout(5, 0)); |
|
setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 6)); |
|
add(createLeftPane(), BorderLayout.CENTER); |
|
add(createRightPane(), BorderLayout.EAST); |
|
} |
|
|
|
@Override |
|
public String title4PopupWindow() { |
|
return Toolkit.i18nText("Fine-Design_Basic_Template_Theme_Profile_Dialog_Title"); |
|
} |
|
|
|
private JPanel createLeftPane() { |
|
JPanel titleContainer = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
|
titleContainer.setPreferredSize(new Dimension(LEFT_TITLE_PANE_WIDTH, LEFT_TITLE_PANE_HEIGHT)); |
|
JPanel previewContainer = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
|
previewContainer.setBorder(BorderFactory.createEmptyBorder(5, 4, 10, 4)); |
|
titleContainer.add(createTitlePane(), BorderLayout.NORTH); |
|
titleContainer.add(previewContainer, BorderLayout.CENTER); |
|
|
|
themePreviewPane = createThemePreviewPane(); |
|
themePreviewPane.setPreferredSize(new Dimension(PREVIEW_PANE_WIDTH, PREVIEW_PANE_HEIGHT)); |
|
previewContainer.add(themePreviewPane, BorderLayout.CENTER); |
|
|
|
return titleContainer; |
|
} |
|
private JPanel createRightPane() { |
|
themeEditorPane = createThemeEditorPane(); |
|
themeEditorPane.addAttributeChangeListener(new AttributeChangeListener() { |
|
@Override |
|
public void attributeChange() { |
|
if (isPopulating) { |
|
return; |
|
} |
|
ThemePreviewTerminal terminal = themeEditorPane.getWidgetStyleEditorTerminal(); |
|
themePreviewPane.refresh(updateBean(), terminal); |
|
String prompt = terminal == ThemePreviewTerminal.PC ? Toolkit.i18nText("Fine-Design_Theme_Control_PC_Prompt") : Toolkit.i18nText("Fine-Design_Theme_Control_Mobile_Prompt"); |
|
leftTitlePromptLabel.setText(prompt); |
|
saveButton.setEnabled(themeEditorPane.checkNameValid() && isMutable); |
|
} |
|
}); |
|
themeEditorPane.addThemeNameCheckListener(new AutoCheckTextField.CheckListener() { |
|
@Override |
|
public void onChecked(String error, boolean valid) { |
|
if (isPopulating) { |
|
return; |
|
} |
|
saveButton.setEnabled(valid && isMutable); |
|
actionListener.onThemeNameChecked(themeEditorPane.getThemeName(), valid); |
|
} |
|
}); |
|
|
|
return themeEditorPane; |
|
} |
|
|
|
public abstract TemplateThemePreviewPane<T> createThemePreviewPane(); |
|
public abstract TemplateThemeEditorPane<T> createThemeEditorPane(); |
|
|
|
public void populateBean(T theme) { |
|
isPopulating = true; |
|
|
|
isMutable = theme.isMutable(); |
|
|
|
themeEditorPane.populateBean(theme); |
|
|
|
themePreviewPane.refresh(theme); |
|
|
|
String name = theme.getName(); |
|
|
|
if (saveButton != null) { |
|
saveButton.setEnabled(isMutable); |
|
} |
|
if (saveAsButton != null) { |
|
saveAsButton.setEnabled(StringUtils.isNotEmpty(name)); |
|
} |
|
actionListener.onInitialize(theme); |
|
|
|
isPopulating = false; |
|
} |
|
|
|
public T updateBean() { |
|
T theme = themeEditorPane.updateBean(); |
|
|
|
Image thumbnailImage = themePreviewPane.createThumbnailImage(); |
|
if (thumbnailImage != null) { |
|
ThemeThumbnail thumbnail = new ThemeThumbnail(); |
|
thumbnail.setImage(thumbnailImage); |
|
theme.setThumbnail(thumbnail); |
|
} |
|
|
|
return theme; |
|
} |
|
|
|
public TemplateThemeEditorPane<T> getThemeEditorPane() { |
|
return themeEditorPane; |
|
} |
|
|
|
public void save(CallBackAdaptor callBack) { |
|
T theme; |
|
try { |
|
theme = (T) updateBean().clone(); |
|
} catch (CloneNotSupportedException ex) { |
|
FineLoggerFactory.getLogger().error(ex.getMessage(), ex); |
|
return; |
|
} |
|
save(theme, callBack); |
|
} |
|
public void save(String name, CallBackAdaptor callBack) { |
|
T theme = null; |
|
try { |
|
theme = (T) updateBean().clone(); |
|
} catch (CloneNotSupportedException ex) { |
|
FineLoggerFactory.getLogger().error(ex.getMessage(), ex); |
|
return; |
|
} |
|
theme.setName(name); |
|
save(theme, callBack); |
|
} |
|
private void save(T theme, CallBackAdaptor callBack) { |
|
theme.setRemovable(true); |
|
theme.setMutable(true); |
|
config.addTheme(theme, true, new CallBackAdaptor() { |
|
@Override |
|
public void afterCommit() { |
|
super.afterCommit(); |
|
themeEditorPane.setThemeNameEditable(false); |
|
callBack.afterCommit(); |
|
} |
|
|
|
@Override |
|
public void afterRollback() { |
|
super.afterRollback(); |
|
FineJOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(TemplateThemeProfilePane.this), |
|
i18nText("Fine-Design_Basic_Template_Theme_Operation_Failed_Tip"), |
|
i18nText("Fine-Design_Basic_Alert"), |
|
JOptionPane.WARNING_MESSAGE); |
|
} |
|
}); |
|
} |
|
|
|
public UIButton createSaveButton(final TemplateThemeProfileDialog<T> profileDialog) { |
|
saveButton = new UIButton(); |
|
saveButton.setText(Toolkit.i18nText("Fine-Design_Basic_Template_Theme_Profile_Pane_Save")); |
|
saveButton.setEnabled(false); |
|
saveButton.addActionListener(new ActionListener() { |
|
@Override |
|
public void actionPerformed(ActionEvent e) { |
|
save(new CallBackAdaptor() { |
|
@Override |
|
public void afterCommit() { |
|
super.afterCommit(); |
|
saveButton.setEnabled(false); |
|
saveAsButton.setEnabled(true); |
|
actionListener.onSaved(config.cachedFetch(getName())); |
|
SwingUtilities.invokeLater(new Runnable() { |
|
@Override |
|
public void run() { |
|
DesignerToastMsgUtil.toastPrompt(Toolkit.i18nText("Fine-Design_Basic_Template_Theme_Profile_Pane_Save_Successfully")); |
|
profileDialog.exit(); |
|
} |
|
}); |
|
} |
|
}); |
|
} |
|
}); |
|
return saveButton; |
|
} |
|
public UIButton createSaveAsButton(final TemplateThemeProfileDialog<T> profileDialog) { |
|
saveAsButton = new UIButton(); |
|
saveAsButton.removeAll(); |
|
saveAsButton.setText(Toolkit.i18nText("Fine-Design_Basic_Template_Theme_Profile_Pane_Save_As")); |
|
saveAsButton.setEnabled(false); |
|
saveAsButton.addActionListener(new ActionListener() { |
|
@Override |
|
public void actionPerformed(ActionEvent e) { |
|
new SaveAsDialog(profileDialog).setVisible(true); |
|
} |
|
}); |
|
return saveAsButton; |
|
} |
|
private class SaveAsDialog extends JDialog { |
|
private final TemplateThemeProfileDialog<T> parent; |
|
|
|
private AutoCheckThemeNameTextField<T> nameTextField; |
|
private UILabel nameErrorLabel; |
|
private UIButton confirmButton; |
|
|
|
public SaveAsDialog(TemplateThemeProfileDialog<T> dialog) { |
|
super(dialog, ModalityType.APPLICATION_MODAL); |
|
|
|
setTitle(Toolkit.i18nText("Fine-Design_Basic_Template_Theme_Save_As_Dialog_Title")); |
|
setResizable(false); |
|
setSize(new Dimension(300, 140)); |
|
GUICoreUtils.centerWindow(this); |
|
|
|
parent = dialog; |
|
|
|
initializeComponents(); |
|
add(createContentPane(), BorderLayout.CENTER); |
|
add(createActionsContainer(), BorderLayout.SOUTH); |
|
|
|
addWindowListener(new WindowAdapter() { |
|
public void windowClosing(WindowEvent e) { |
|
exit(); |
|
} |
|
}); |
|
} |
|
|
|
private void initializeComponents() { |
|
nameTextField = new AutoCheckThemeNameTextField<>(); |
|
nameTextField.setThemeConfig(config); |
|
nameTextField.setPreferredSize(new Dimension(180, 20)); |
|
nameTextField.setNameCheckListener(new AutoCheckTextField.CheckListener() { |
|
@Override |
|
public void onChecked(String error, boolean valid) { |
|
nameErrorLabel.setVisible(StringUtils.isNotEmpty(error)); |
|
nameErrorLabel.setText(error); |
|
confirmButton.setEnabled(valid); |
|
} |
|
}); |
|
|
|
nameErrorLabel = LabelUtils.createLabel(StringUtils.EMPTY, Color.RED); |
|
nameErrorLabel.setBorder(BorderFactory.createEmptyBorder(10, 20, 0, 0)); |
|
|
|
confirmButton = new UIButton(Toolkit.i18nText("Fine-Design_Basic_Confirm")); |
|
confirmButton.setEnabled(false); |
|
confirmButton.addActionListener(new ActionListener() { |
|
@Override |
|
public void actionPerformed(ActionEvent e) { |
|
save(nameTextField.getText(), new CallBackAdaptor() { |
|
@Override |
|
public void afterCommit() { |
|
super.afterCommit(); |
|
exit(); |
|
parent.exit(); |
|
} |
|
}); |
|
} |
|
}); |
|
} |
|
|
|
private JPanel createActionsContainer() { |
|
|
|
UIButton cancelButton = new UIButton(Toolkit.i18nText("Fine-Design_Basic_Cancel")); |
|
cancelButton.addActionListener(new ActionListener() { |
|
@Override |
|
public void actionPerformed(ActionEvent e) { |
|
exit(); |
|
} |
|
}); |
|
|
|
JPanel container = FRGUIPaneFactory.createRightFlowInnerContainer_S_Pane(); |
|
container.add(confirmButton); |
|
container.add(cancelButton); |
|
|
|
return container; |
|
} |
|
|
|
private JPanel createContentPane() { |
|
JPanel container = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
|
|
|
JPanel nameTextPane = FRGUIPaneFactory.createBoxFlowInnerContainer_S_Pane(20, 5); |
|
nameTextPane.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0)); |
|
nameTextPane.add(new UILabel(Toolkit.i18nText("Fine-Design_Basic_Template_Theme_Save_As_Pane_Name_Label"))); |
|
nameTextPane.add(nameTextField); |
|
|
|
container.add(nameTextPane, BorderLayout.CENTER); |
|
container.add(nameErrorLabel, BorderLayout.SOUTH); |
|
|
|
return container; |
|
} |
|
|
|
public void exit() { |
|
this.dispose(); |
|
} |
|
} |
|
|
|
public void addProfileActionListener(TemplateThemeProfileActionListener actionListener) { |
|
if (actionListener != null) { |
|
this.actionListener = actionListener; |
|
} |
|
} |
|
public interface TemplateThemeProfileActionListener { |
|
|
|
void onInitialize(TemplateTheme theme); |
|
|
|
void onThemeNameChecked(String name, boolean valid); |
|
|
|
void onSaved(TemplateTheme theme); |
|
} |
|
public static class TemplateThemeProfileActionAdapter implements TemplateThemeProfileActionListener { |
|
|
|
@Override |
|
public void onInitialize(TemplateTheme theme) {} |
|
|
|
@Override |
|
public void onThemeNameChecked(String name, boolean valid) { } |
|
|
|
@Override |
|
public void onSaved(TemplateTheme theme) {} |
|
} |
|
|
|
/** |
|
* 创建预览界面标题面板 |
|
*/ |
|
protected JPanel createTitlePane() { |
|
JPanel titlePane = new JPanel(); |
|
titlePane.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); |
|
titlePane.setLayout(new BorderLayout()); |
|
leftTitleLabel = new UILabel(Toolkit.i18nText("Fine-Design_Basic_Template_Theme_Preview_Pane_Title")); |
|
leftTitlePromptLabel = new UILabel(Toolkit.i18nText("Fine-Design_Theme_Control_PC_Prompt")); |
|
leftTitleLabel.setForeground(WidgetThemeDisplayConstants.THEME_PREVIEW_TITLE_COLOR); |
|
leftTitlePromptLabel.setForeground(WidgetThemeDisplayConstants.COMPATIBLE_STYLE_FONT_COLOR); |
|
titlePane.add(leftTitleLabel, BorderLayout.WEST); |
|
titlePane.add(leftTitlePromptLabel, BorderLayout.EAST); |
|
return titlePane; |
|
} |
|
} |