Browse Source

Pull request #6077: REPORT-57806 主题获取

Merge in DESIGN/design from ~STARRYI/design:REPORT-57806 to feature/x

* commit '407df3dd184fd8625717f17a88443d5f77f844c7':
  REPORT-57806 主题获取
research/11.0
starryi 3 years ago
parent
commit
c306b900f1
  1. 153
      designer-form/src/main/java/com/fr/design/mainframe/share/ui/actions/DownloadSuitableThemeAction.java
  2. 41
      designer-form/src/main/java/com/fr/design/mainframe/share/ui/actions/Jump2DetailAction.java
  3. 163
      designer-form/src/main/java/com/fr/design/mainframe/share/ui/block/AbstractOnlineWidgetBlock.java

153
designer-form/src/main/java/com/fr/design/mainframe/share/ui/actions/DownloadSuitableThemeAction.java

@ -0,0 +1,153 @@
package com.fr.design.mainframe.share.ui.actions;
import com.fr.base.theme.FormTheme;
import com.fr.base.theme.FormThemeConfig;
import com.fr.base.theme.TemplateTheme;
import com.fr.base.theme.TemplateThemeConfig;
import com.fr.design.DesignerEnvManager;
import com.fr.design.actions.UpdateAction;
import com.fr.design.dialog.FineJOptionPane;
import com.fr.design.file.HistoryTemplateListCache;
import com.fr.design.i18n.Toolkit;
import com.fr.design.login.DesignerLoginHelper;
import com.fr.design.login.DesignerLoginSource;
import com.fr.design.mainframe.DesignerContext;
import com.fr.design.mainframe.JTemplate;
import com.fr.design.mainframe.share.util.DownloadUtils;
import com.fr.stable.StringUtils;
import com.fr.transaction.CallBackAdaptor;
import com.fr.workspace.WorkContext;
import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import java.awt.event.ActionEvent;
/**
* @author Starryi
* @version 1.0
* Created by Starryi on 2021/9/28
*/
public class DownloadSuitableThemeAction extends UpdateAction {
private final String themePath;
private boolean downloading = false;
public DownloadSuitableThemeAction(String themePath) {
this.themePath = themePath;
this.putValue(Action.SMALL_ICON, null);
this.setName(Toolkit.i18nText("Fine-Design_Share_Download_Suitable_Theme"));
}
@Override
public void actionPerformed(ActionEvent e) {
if (checkAuthority()) {
saveTheme();
}
}
private boolean checkAuthority() {
if (!WorkContext.getCurrent().isRoot()) {
FineJOptionPane.showMessageDialog(DesignerContext.getDesignerFrame(),
Toolkit.i18nText("Fine-Design_Share_Download_Suitable_Theme_No_Authority_Tip_Message"),
Toolkit.i18nText("Fine-Design_Share_Download_Suitable_Theme_No_Authority_Tip_Title"),
JOptionPane.WARNING_MESSAGE);
return false;
}
String userName = DesignerEnvManager.getEnvManager().getDesignerLoginUsername();
if (StringUtils.isEmpty(userName)) {
DesignerLoginHelper.showLoginDialog(DesignerLoginSource.NORMAL);
return false;
}
return true;
}
private void saveTheme() {
if (downloading) {
return;
}
downloading = true;
final JTemplate<?,?> template = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate();
new SwingWorker<Boolean, Void>() {
@Override
protected Boolean doInBackground() {
FormTheme theme = fetchRemoteTheme();
if (theme == null) {
return false;
}
theme = ensureThemeHasUniqueName(theme, theme.getName());
if (theme == null) {
return false;
}
String themeName = theme.getName();
saveThemeToConfig(theme, new SaveToThemeConfigCallback(template, themeName));
return true;
}
@Override
protected void done() {
downloading = false;
}
}.execute();
}
private FormTheme fetchRemoteTheme() {
return DownloadUtils.downloadThemeFile(themePath);
}
private FormTheme ensureThemeHasUniqueName(FormTheme theme, String expectedName) {
if (!FormThemeConfig.getInstance().contains(expectedName)) {
theme.setName(expectedName);
return theme;
} else {
String newName = (String) FineJOptionPane.showInputDialog(
DesignerContext.getDesignerFrame(),
Toolkit.i18nText("Fine-Design_Share_Rename_Suitable_Theme_Tip"),
Toolkit.i18nText("Fine-Design_Basic_Rename"),
FineJOptionPane.QUESTION_MESSAGE, null, null,
expectedName);
return StringUtils.isEmpty(newName) ? null : ensureThemeHasUniqueName(theme, newName);
}
}
private void saveThemeToConfig(final FormTheme theme, CallBackAdaptor callback) {
FormThemeConfig.getInstance().addTheme(theme, true, callback);
}
public static class SaveToThemeConfigCallback extends CallBackAdaptor {
private final JTemplate<?,?> template;
private final String themeName;
public SaveToThemeConfigCallback(JTemplate<?, ?> template, String themeName) {
this.template = template;
this.themeName = themeName;
}
@Override
public void afterCommit() {
super.afterCommit();
int returnVal = FineJOptionPane.showConfirmDialog(
DesignerContext.getDesignerFrame(),
Toolkit.i18nText("Fine-Design_Share_Apply_Suitable_Theme_Tip"),
Toolkit.i18nText("Fine-Design_Basic_Confirm"),
FineJOptionPane.OK_CANCEL_OPTION);
if (returnVal == JOptionPane.YES_OPTION) {
applyTheme(template, themeName);
}
}
private void applyTheme(JTemplate<?,?> template, final String name) {
TemplateThemeConfig<? extends TemplateTheme> config = template.getUsingTemplateThemeConfig();
TemplateTheme theme = config.cachedFetch(name);
template.setTemplateTheme(theme);
}
}
}

41
designer-form/src/main/java/com/fr/design/mainframe/share/ui/actions/Jump2DetailAction.java

@ -0,0 +1,41 @@
package com.fr.design.mainframe.share.ui.actions;
import com.fr.design.actions.UpdateAction;
import com.fr.design.i18n.Toolkit;
import com.fr.form.share.bean.OnlineShareWidget;
import com.fr.stable.StringUtils;
import javax.swing.Action;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* @author Starryi
* @version 1.0
* Created by Starryi on 2021/9/28
*/
public class Jump2DetailAction extends UpdateAction {
private static final String ONLINE_WIDGET_DETAIL_FORMATTED_URL = "https://market.fanruan.com/reuse/%s";
private final String id;
public Jump2DetailAction(String id) {
this.id = id;
this.putValue(Action.SMALL_ICON, null);
this.setName(Toolkit.i18nText("Fine-Design_Share_Jump_To_Detail"));
}
@Override
public void actionPerformed(ActionEvent e) {
if (StringUtils.isNotEmpty(id)) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(String.format(ONLINE_WIDGET_DETAIL_FORMATTED_URL, id)));
} catch (IOException | URISyntaxException ioException) {
ioException.printStackTrace();
}
}
}
}

163
designer-form/src/main/java/com/fr/design/mainframe/share/ui/block/AbstractOnlineWidgetBlock.java

@ -1,25 +1,13 @@
package com.fr.design.mainframe.share.ui.block;
import com.fr.base.theme.FormTheme;
import com.fr.base.theme.FormThemeConfig;
import com.fr.base.theme.TemplateTheme;
import com.fr.base.theme.TemplateThemeConfig;
import com.fr.design.DesignerEnvManager;
import com.fr.design.actions.UpdateAction;
import com.fr.design.constants.UIConstants;
import com.fr.design.dialog.FineJOptionPane;
import com.fr.design.file.HistoryTemplateListCache;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.gui.imenu.UIPopupMenu;
import com.fr.design.i18n.Toolkit;
import com.fr.design.login.DesignerLoginHelper;
import com.fr.design.login.DesignerLoginSource;
import com.fr.design.mainframe.DesignerContext;
import com.fr.design.mainframe.JTemplate;
import com.fr.design.mainframe.share.ui.actions.DownloadSuitableThemeAction;
import com.fr.design.mainframe.share.ui.actions.Jump2DetailAction;
import com.fr.design.mainframe.share.ui.online.OnlineResourceManager;
import com.fr.design.mainframe.share.ui.online.OnlineWidgetSelectPane;
import com.fr.design.mainframe.share.ui.online.ResourceLoader;
import com.fr.design.mainframe.share.util.DownloadUtils;
import com.fr.design.utils.gui.GUICoreUtils;
import com.fr.form.share.bean.OnlineShareWidget;
import com.fr.form.share.constants.ShareComponentConstants;
@ -27,31 +15,21 @@ import com.fr.log.FineLoggerFactory;
import com.fr.stable.EncodeConstants;
import com.fr.stable.StringUtils;
import com.fr.third.springframework.web.util.UriUtils;
import com.fr.transaction.CallBackAdaptor;
import com.fr.workspace.WorkContext;
import org.jetbrains.annotations.NotNull;
import javax.imageio.ImageIO;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.SwingWorker;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
/**
* Created by kerry on 2020-11-22
*/
public abstract class AbstractOnlineWidgetBlock extends PreviewWidgetBlock<OnlineShareWidget> implements ResourceLoader {
private static final String ONLINE_WIDGET_DETAIL_FORMATTED_URL = "https://market.fanruan.com/reuse/%s";
private final OnlineWidgetSelectPane parentPane;
private UILabel coverLabel;
@ -145,144 +123,9 @@ public abstract class AbstractOnlineWidgetBlock extends PreviewWidgetBlock<Onlin
popupMenu.add(new DownloadSuitableThemeAction(suitableThemeFile).createMenuItem());
}
popupMenu.add(new Jump2DetailAction().createMenuItem());
popupMenu.add(new Jump2DetailAction(getWidget().getId()).createMenuItem());
return popupMenu;
}
private static class DownloadSuitableThemeAction extends UpdateAction {
private final String themePath;
private boolean downloading = false;
public DownloadSuitableThemeAction(String themePath) {
this.themePath = themePath;
this.putValue(Action.SMALL_ICON, null);
this.setName(Toolkit.i18nText("Fine-Design_Share_Download_Suitable_Theme"));
}
@Override
public void actionPerformed(ActionEvent e) {
if (checkAuthority()) {
saveTheme();
}
}
private boolean checkAuthority() {
if (!WorkContext.getCurrent().isRoot()) {
FineJOptionPane.showMessageDialog(DesignerContext.getDesignerFrame(),
Toolkit.i18nText("Fine-Design_Share_Download_Suitable_Theme_No_Authority_Tip_Message"),
Toolkit.i18nText("Fine-Design_Share_Download_Suitable_Theme_No_Authority_Tip_Title"),
JOptionPane.WARNING_MESSAGE);
return false;
}
String userName = DesignerEnvManager.getEnvManager().getDesignerLoginUsername();
if (StringUtils.isEmpty(userName)) {
DesignerLoginHelper.showLoginDialog(DesignerLoginSource.NORMAL);
return false;
}
return true;
}
private void saveTheme() {
if (downloading) {
return;
}
downloading = true;
final JTemplate<?,?> template = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate();
new SwingWorker<Boolean, Void>() {
@Override
protected Boolean doInBackground() {
FormTheme theme = fetchRemoteTheme();
if (theme == null) {
return false;
}
theme = ensureThemeHasUniqueName(theme, theme.getName());
if (theme == null) {
return false;
}
String themeName = theme.getName();
saveThemeToConfig(theme, new CallBackAdaptor() {
@Override
public void afterCommit() {
super.afterCommit();
int returnVal = FineJOptionPane.showConfirmDialog(
DesignerContext.getDesignerFrame(),
Toolkit.i18nText("Fine-Design_Share_Apply_Suitable_Theme_Tip"),
Toolkit.i18nText("Fine-Design_Basic_Confirm"),
FineJOptionPane.OK_CANCEL_OPTION);
if (returnVal == JOptionPane.YES_OPTION) {
applyTheme(template, themeName);
}
}
});
return true;
}
@Override
protected void done() {
downloading = false;
}
}.execute();
}
private FormTheme fetchRemoteTheme() {
return DownloadUtils.downloadThemeFile(themePath);
}
private FormTheme ensureThemeHasUniqueName(FormTheme theme, String expectedName) {
if (!FormThemeConfig.getInstance().contains(expectedName)) {
theme.setName(expectedName);
return theme;
} else {
String newName = (String) FineJOptionPane.showInputDialog(
DesignerContext.getDesignerFrame(),
Toolkit.i18nText("Fine-Design_Share_Rename_Suitable_Theme_Tip"),
Toolkit.i18nText("Fine-Design_Basic_Rename"),
FineJOptionPane.QUESTION_MESSAGE, null, null,
expectedName);
return StringUtils.isEmpty(newName) ? null : ensureThemeHasUniqueName(theme, newName);
}
}
private void saveThemeToConfig(final FormTheme theme, CallBackAdaptor callback) {
FormThemeConfig.getInstance().addTheme(theme, true, callback);
}
private void applyTheme(JTemplate<?,?> template, final String name) {
TemplateThemeConfig<? extends TemplateTheme> config = template.getUsingTemplateThemeConfig();
TemplateTheme theme = config.cachedFetch(name);
template.setTemplateTheme(theme);
}
}
private class Jump2DetailAction extends UpdateAction {
public Jump2DetailAction() {
this.putValue(Action.SMALL_ICON, null);
this.setName(Toolkit.i18nText("Fine-Design_Share_Jump_To_Detail"));
}
@Override
public void actionPerformed(ActionEvent e) {
OnlineShareWidget widget = getWidget();
String id = widget.getId();
if (StringUtils.isNotEmpty(id)) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(String.format(ONLINE_WIDGET_DETAIL_FORMATTED_URL, id)));
} catch (IOException | URISyntaxException ioException) {
ioException.printStackTrace();
}
}
}
}
}

Loading…
Cancel
Save