8 changed files with 571 additions and 27 deletions
@ -0,0 +1,181 @@
|
||||
package com.fr.design.mainframe.toast; |
||||
|
||||
import com.fr.concurrent.NamedThreadFactory; |
||||
import com.fr.design.dialog.UIDialog; |
||||
import com.fr.design.gui.ilable.UILabel; |
||||
import com.fr.design.layout.FRGUIPaneFactory; |
||||
import com.fr.module.ModuleContext; |
||||
|
||||
import javax.swing.BorderFactory; |
||||
import javax.swing.Icon; |
||||
import javax.swing.JPanel; |
||||
import javax.swing.SwingConstants; |
||||
import java.awt.BorderLayout; |
||||
import java.awt.Dimension; |
||||
import java.awt.Point; |
||||
import java.awt.Window; |
||||
import java.util.concurrent.ScheduledExecutorService; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
/** |
||||
* toast弹窗 |
||||
* */ |
||||
public class SimpleToast extends UIDialog { |
||||
private static final int MIN_HEIGHT = 36; |
||||
private static final String TOAST_MSG_TIMER = "TOAST_MSG_TIMER"; |
||||
private static final long DEFAULT_DISAPPEAR_DELAY = 5000; |
||||
private static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.MILLISECONDS; |
||||
|
||||
|
||||
private ScheduledExecutorService TIMER; |
||||
private int hide_height = 0; |
||||
private JPanel contentPane; |
||||
private boolean show = false; |
||||
private Window parent; |
||||
private boolean autoDisappear; |
||||
|
||||
public SimpleToast(Window parent, Icon icon, String text, boolean autoDisappear) { |
||||
super(parent); |
||||
this.parent = parent; |
||||
this.autoDisappear = autoDisappear; |
||||
JPanel panel = createContentPane(icon, text); |
||||
init(panel); |
||||
} |
||||
|
||||
private JPanel createContentPane(Icon icon, String text) { |
||||
JPanel pane = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
||||
|
||||
UILabel iconLabel = new UILabel(icon); |
||||
iconLabel.setVerticalAlignment(SwingConstants.TOP); |
||||
iconLabel.setBorder(BorderFactory.createEmptyBorder(3, 0, 0, 0)); |
||||
|
||||
|
||||
UILabel textLabel = new UILabel(text); |
||||
pane.add(iconLabel, BorderLayout.WEST); |
||||
pane.add(textLabel, BorderLayout.CENTER); |
||||
pane.setBorder(BorderFactory.createEmptyBorder(8, 15, 8, 15)); |
||||
|
||||
|
||||
return pane; |
||||
} |
||||
|
||||
|
||||
private void init(JPanel panel) { |
||||
setFocusable(false); |
||||
setAutoRequestFocus(false); |
||||
setUndecorated(true); |
||||
contentPane = panel; |
||||
initComponent(); |
||||
} |
||||
|
||||
private void initComponent() { |
||||
this.getContentPane().setLayout(null); |
||||
this.getContentPane().add(contentPane); |
||||
Dimension dimension = calculatePreferSize(); |
||||
hide_height = dimension.height; |
||||
setSize(new Dimension(dimension.width, 0)); |
||||
contentPane.setSize(dimension); |
||||
setRelativeLocation(dimension); |
||||
if (autoDisappear) { |
||||
disappear(contentPane); |
||||
} |
||||
} |
||||
|
||||
private void setRelativeLocation(Dimension dimension) { |
||||
int positionX = parent.getLocationOnScreen().x + (parent.getWidth() - dimension.width) / 2; |
||||
int positionY = parent.getLocationOnScreen().y + 10; |
||||
this.setLocation(positionX, positionY); |
||||
} |
||||
|
||||
private Dimension calculatePreferSize() { |
||||
Dimension contentDimension = contentPane.getPreferredSize(); |
||||
int height = Math.max(MIN_HEIGHT, contentDimension.height); |
||||
return new Dimension(contentDimension.width, height); |
||||
} |
||||
|
||||
|
||||
public void display(JPanel outerJPanel) { |
||||
show = true; |
||||
outerJPanel.setLocation(0, -hide_height); |
||||
ScheduledExecutorService TIP_TOOL_TIMER = createToastScheduleExecutorService(); |
||||
TIP_TOOL_TIMER.scheduleAtFixedRate(new Runnable() { |
||||
@Override |
||||
public void run() { |
||||
Point point = outerJPanel.getLocation(); |
||||
if (point.y >= 0) { |
||||
TIP_TOOL_TIMER.shutdown(); |
||||
} |
||||
int showDistance = 5 + point.y < 0 ? 5 : -point.y; |
||||
outerJPanel.setLocation(point.x, point.y + showDistance); |
||||
Dimension dimension = SimpleToast.this.getSize(); |
||||
SimpleToast.this.setSize(new Dimension(dimension.width, dimension.height + showDistance)); |
||||
} |
||||
}, 0, 50, TimeUnit.MILLISECONDS); |
||||
|
||||
} |
||||
|
||||
private void disappear(JPanel outerJPanel, long delay, TimeUnit timeUnit) { |
||||
TIMER = createToastScheduleExecutorService(); |
||||
TIMER.schedule(new DisappearMotion(outerJPanel), delay, timeUnit); |
||||
} |
||||
|
||||
/** |
||||
* toast消失的动画效果 |
||||
* */ |
||||
class DisappearMotion implements Runnable { |
||||
JPanel panel; |
||||
|
||||
DisappearMotion(JPanel panel) { |
||||
this.panel = panel; |
||||
} |
||||
|
||||
@Override |
||||
public void run() { |
||||
ScheduledExecutorService TIP_TOOL_TIMER = createToastScheduleExecutorService(); |
||||
TIP_TOOL_TIMER.scheduleAtFixedRate(new Runnable() { |
||||
@Override |
||||
public void run() { |
||||
Point point = panel.getLocation(); |
||||
if (point.y <= -hide_height) { |
||||
TIP_TOOL_TIMER.shutdown(); |
||||
SimpleToast.this.setVisible(false); |
||||
SimpleToast.this.dispose(); |
||||
SimpleToast.this.show = false; |
||||
} |
||||
panel.setLocation(point.x, point.y - 5); |
||||
Dimension dimension = SimpleToast.this.getSize(); |
||||
SimpleToast.this.setSize(new Dimension(dimension.width, dimension.height - 5)); |
||||
} |
||||
}, 0, 50, TimeUnit.MILLISECONDS); |
||||
} |
||||
} |
||||
|
||||
|
||||
private void disappear(JPanel outerJPanel) { |
||||
disappear(outerJPanel, DEFAULT_DISAPPEAR_DELAY, DEFAULT_TIME_UNIT); |
||||
} |
||||
|
||||
private ScheduledExecutorService createToastScheduleExecutorService() { |
||||
return ModuleContext.getExecutor().newSingleThreadScheduledExecutor(new NamedThreadFactory(TOAST_MSG_TIMER)); |
||||
} |
||||
|
||||
@Override |
||||
public void checkValid() throws Exception { |
||||
} |
||||
|
||||
public void setVisible(boolean visible) { |
||||
super.setVisible(visible); |
||||
if (visible) { |
||||
display(contentPane); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void dispose() { |
||||
super.dispose(); |
||||
} |
||||
|
||||
public boolean isShow() { |
||||
return show; |
||||
} |
||||
} |
@ -0,0 +1,170 @@
|
||||
package com.fr.design.mainframe.alphafine.download; |
||||
|
||||
import com.fr.base.svg.IconUtils; |
||||
import com.fr.common.util.Strings; |
||||
import com.fr.design.DesignerEnvManager; |
||||
import com.fr.design.extra.Process; |
||||
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.alphafine.AlphaFineHelper; |
||||
import com.fr.design.mainframe.alphafine.model.TemplateResource; |
||||
import com.fr.design.mainframe.alphafine.search.helper.FineMarketClientHelper; |
||||
import com.fr.design.mainframe.toast.SimpleToast; |
||||
import com.fr.file.FileFILE; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.third.jodd.io.ZipUtil; |
||||
import com.fr.workspace.WorkContext; |
||||
|
||||
import java.awt.Desktop; |
||||
import java.awt.Window; |
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.util.HashMap; |
||||
|
||||
public class FineMarketDownloadManager { |
||||
|
||||
private static final FineMarketDownloadManager INSTANCE = new FineMarketDownloadManager(); |
||||
public static final FineMarketDownloadManager getInstance() { |
||||
return INSTANCE; |
||||
} |
||||
|
||||
|
||||
public static final double PROCESS_SUCCESS = 1d; |
||||
public static final double PROCESS_FAILED = -1d; |
||||
public static final double OPENING_FILE = 2d; |
||||
|
||||
private static final String OPENING_PLEASE_WAIT = Toolkit.i18nText("Fine-Design_Report_AlphaFine_Template_Resource_Opening"); |
||||
private static final String DOWNLOAD_FAILED = Toolkit.i18nText("Fine-Design_Report_AlphaFine_Template_Resource_Download_Failed_Check_Network"); |
||||
private static final String REPORTLETS = "/reportlets"; |
||||
|
||||
private HashMap<String, DownloadProcess> processMap = new HashMap<>(); |
||||
|
||||
|
||||
/** |
||||
* 下载资源并解压, |
||||
* */ |
||||
public void downloadAndOpenTemplateResource(TemplateResource resource, Window parentWindow){ |
||||
// 验证登录
|
||||
String token = DesignerEnvManager.getEnvManager().getDesignerLoginRefreshToken(); |
||||
if (Strings.isEmpty(token)) { |
||||
DesignerLoginHelper.showLoginDialog(DesignerLoginSource.NORMAL, new HashMap<>(), AlphaFineHelper.getAlphaFineDialog()); |
||||
return; |
||||
} |
||||
downloadAndOpen(resource, parentWindow); |
||||
} |
||||
|
||||
private void downloadAndOpen(TemplateResource resource, Window parentWindow) { |
||||
// 获取报表录作为下载位置
|
||||
String workDir = WorkContext.getCurrent().getPath() + REPORTLETS; |
||||
File destDir = new File(workDir); |
||||
|
||||
DownloadProcess downloadProcess = new DownloadProcess(parentWindow); |
||||
String fileName = null; |
||||
try { |
||||
fileName = FineMarketClientHelper.getInstance().download(resource, destDir, downloadProcess); |
||||
open(fileName, downloadProcess); |
||||
} catch (Exception e) { |
||||
downloadProcess.process(FineMarketDownloadManager.PROCESS_FAILED); |
||||
FineLoggerFactory.getLogger().error(e, e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
void open(String fileName, DownloadProcess process) throws IOException { |
||||
process.process(OPENING_FILE); |
||||
if (Strings.isEmpty(fileName)) { |
||||
return; |
||||
} |
||||
File fileNeedOpen = new File(fileName); |
||||
if (fileName.endsWith(".zip")) { |
||||
File[] files = unzip(fileName); |
||||
fileNeedOpen = getFirstCptOrFrm(files); |
||||
} else if (fileName.endsWith(".rar")) { |
||||
// rar直接打开系统文件夹
|
||||
File parentDir = new File(fileName).getParentFile(); |
||||
Desktop.getDesktop().open(parentDir); |
||||
return; |
||||
} |
||||
openInDesigner(fileNeedOpen); |
||||
} |
||||
|
||||
private File getFirstCptOrFrm(File[] files) { |
||||
for (File f : files) { |
||||
if (f.getName().endsWith(".cpt") || f.getName().endsWith(".frm")) { |
||||
return f; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
File[] unzip(String fileName) throws IOException { |
||||
File file = new File(fileName); |
||||
File parentDir = file.getParentFile(); |
||||
ZipUtil.unzip(file, parentDir); |
||||
return parentDir.listFiles(); |
||||
} |
||||
|
||||
void openInDesigner(File file) { |
||||
DesignerContext.getDesignerFrame().openTemplate(new FileFILE(file)); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 下载流程控制,主要控制ui的显示 |
||||
* */ |
||||
class DownloadProcess implements Process<Double> { |
||||
|
||||
SimpleToast downloadingToast; |
||||
SimpleToast openingToast; |
||||
SimpleToast failedToast; |
||||
Window parent; |
||||
|
||||
public DownloadProcess(Window parentWindow) { |
||||
this.parent = parentWindow; |
||||
init(); |
||||
} |
||||
|
||||
void init() { |
||||
showLoadingToast(); |
||||
} |
||||
|
||||
@Override |
||||
public void process(Double aDouble) { |
||||
if (aDouble == PROCESS_FAILED) { |
||||
downloadFailed(); |
||||
} else if (aDouble == PROCESS_SUCCESS) { |
||||
downloadSuccess(); |
||||
} else if (aDouble == OPENING_FILE) { |
||||
openingFile(); |
||||
} |
||||
} |
||||
|
||||
public void downloadFailed() { |
||||
downloadingToast.setVisible(false); |
||||
showFailedToast(); |
||||
} |
||||
|
||||
public void downloadSuccess() { |
||||
downloadingToast.setVisible(false); |
||||
} |
||||
|
||||
|
||||
private void openingFile() { |
||||
downloadingToast.setVisible(false); |
||||
openingToast = new SimpleToast(AlphaFineHelper.getAlphaFineDialog(), IconUtils.readIcon("/com/fr/design/mainframe/alphafine/images/loading.svg"), OPENING_PLEASE_WAIT, true); |
||||
openingToast.setVisible(true); |
||||
} |
||||
|
||||
private void showLoadingToast() { |
||||
downloadingToast = new SimpleToast(AlphaFineHelper.getAlphaFineDialog(), IconUtils.readIcon("/com/fr/design/mainframe/alphafine/images/loading.svg"), OPENING_PLEASE_WAIT, false); |
||||
downloadingToast.setVisible(true); |
||||
} |
||||
|
||||
private void showFailedToast() { |
||||
failedToast = new SimpleToast(AlphaFineHelper.getAlphaFineDialog(), IconUtils.readIcon("/com/fr/design/mainframe/alphafine/images/caution.svg"), DOWNLOAD_FAILED, true); |
||||
failedToast.setVisible(true); |
||||
} |
||||
|
||||
} |
||||
} |
After Width: | Height: | Size: 820 B |
Loading…
Reference in new issue