fly.li
3 years ago
26 changed files with 590 additions and 427 deletions
@ -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); |
||||
} |
||||
} |
||||
} |
@ -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(); |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue