From 9d57c1a74dc8e702491b34105fd52f628bfb5e99 Mon Sep 17 00:00:00 2001 From: Fangjie Hu Date: Thu, 1 Sep 2016 14:29:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=86=E5=A4=87=E6=8F=92=E4=BB=B6=E8=B5=84?= =?UTF-8?q?=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/fr/design/extra/PluginHelper.java | 1 + .../fr/design/extra/PluginManagerPane.java | 6 +- .../com/fr/design/extra/PluginWebBridge.java | 26 ++ .../extra/pre4plugin/InstallPhantomJs.java | 280 ++++++++++++++++++ .../extra/pre4plugin/NoneEnv4Plugin.java | 16 + .../extra/pre4plugin/PluginFactory.java | 34 +++ .../extra/pre4plugin/PreEnv4Plugin.java | 10 + .../design/mainframe/chart/ChartEditPane.java | 10 + 8 files changed, 380 insertions(+), 3 deletions(-) create mode 100644 designer_base/src/com/fr/design/extra/pre4plugin/InstallPhantomJs.java create mode 100644 designer_base/src/com/fr/design/extra/pre4plugin/NoneEnv4Plugin.java create mode 100644 designer_base/src/com/fr/design/extra/pre4plugin/PluginFactory.java create mode 100644 designer_base/src/com/fr/design/extra/pre4plugin/PreEnv4Plugin.java diff --git a/designer_base/src/com/fr/design/extra/PluginHelper.java b/designer_base/src/com/fr/design/extra/PluginHelper.java index 8bab17e68..0b4257885 100644 --- a/designer_base/src/com/fr/design/extra/PluginHelper.java +++ b/designer_base/src/com/fr/design/extra/PluginHelper.java @@ -30,6 +30,7 @@ public class PluginHelper { private static final String TEMP_PATH = System.getProperty("user.dir") + "/tmp"; public static final String DOWNLOAD_PATH = System.getProperty("user.dir") + "/download"; public static final String TEMP_FILE = "temp.zip"; + public static final String PHANTOM_PATH = "http://ocrpz63ed.bkt.clouddn.com/phantomjs.zip"; /** * 下载插件 diff --git a/designer_base/src/com/fr/design/extra/PluginManagerPane.java b/designer_base/src/com/fr/design/extra/PluginManagerPane.java index 1c9f7aafa..effa66f57 100644 --- a/designer_base/src/com/fr/design/extra/PluginManagerPane.java +++ b/designer_base/src/com/fr/design/extra/PluginManagerPane.java @@ -39,11 +39,11 @@ public class PluginManagerPane extends BasicPane { setLayout(new BorderLayout()); if (StableUtils.getMajorJavaVersion() == 8) { String installHome; - if (StableUtils.isDebug()) { + /*if (StableUtils.isDebug()) { URL url = ClassLoader.getSystemResource(""); installHome = url.getPath(); addPane(installHome); - } else { + } else {*/ installHome = StableUtils.getInstallHome(); File file = new File(StableUtils.pathJoin(installHome, "scripts")); if (!file.exists()) { @@ -61,7 +61,7 @@ public class PluginManagerPane extends BasicPane { addPane(installHome); updateShopScripts(); } - } + /*}*/ } else { initTraditionalStore(); } diff --git a/designer_base/src/com/fr/design/extra/PluginWebBridge.java b/designer_base/src/com/fr/design/extra/PluginWebBridge.java index 82dd4cf35..0e03a6e70 100644 --- a/designer_base/src/com/fr/design/extra/PluginWebBridge.java +++ b/designer_base/src/com/fr/design/extra/PluginWebBridge.java @@ -4,6 +4,9 @@ import com.fr.base.FRContext; import com.fr.design.RestartHelper; import com.fr.design.dialog.UIDialog; import com.fr.design.extra.exe.*; +import com.fr.design.extra.pre4plugin.InstallPhantomJs; +import com.fr.design.extra.pre4plugin.PluginFactory; +import com.fr.design.extra.pre4plugin.PreEnv4Plugin; import com.fr.general.FRLogger; import com.fr.general.Inter; import com.fr.general.SiteCenter; @@ -147,6 +150,29 @@ public class PluginWebBridge { new Thread(task).start(); } + + + + /** + * + * @param pluginID 插件类型 + * @return 如果不需要准备或者已经准备,返回true + */ + public boolean checkVanChartPluginOnline(final String pluginID) { + PreEnv4Plugin preEnv4Plugin = PluginFactory.createPreEnv(pluginID); + return preEnv4Plugin.checkEnv(); + } + + /** + * + * @param pluginID 插件类型 + * @return 是否准备好了,是或者不需要准备,返回true, 否则返回false + */ + public boolean preEnv4PluginOnline(final String pluginID) { + PreEnv4Plugin preEnv4Plugin = PluginFactory.createPreEnv(pluginID); + return preEnv4Plugin.preOnline(); + } + /** * 从磁盘上选择插件安装包进行安装 * diff --git a/designer_base/src/com/fr/design/extra/pre4plugin/InstallPhantomJs.java b/designer_base/src/com/fr/design/extra/pre4plugin/InstallPhantomJs.java new file mode 100644 index 000000000..4d2d0a486 --- /dev/null +++ b/designer_base/src/com/fr/design/extra/pre4plugin/InstallPhantomJs.java @@ -0,0 +1,280 @@ +package com.fr.design.extra.pre4plugin; + +/** + * Created by hufan on 2016/8/31. + */ +import com.fr.base.FRContext; +import com.fr.design.extra.PluginConstants; +import com.fr.design.extra.PluginHelper; +import com.fr.design.mainframe.DesignerContext; +import com.fr.general.IOUtils; +import com.fr.general.Inter; +import com.fr.general.http.HttpClient; +import com.fr.stable.StableUtils; +import com.fr.stable.StringUtils; + +import java.awt.*; + +import java.awt.event.ActionEvent; + + + +import java.awt.event.ActionListener; + +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.*; +import java.net.HttpURLConnection; +import javax.swing.*; + + +import javax.swing.event.ChangeEvent; + +import javax.swing.event.ChangeListener; + + + +public class InstallPhantomJs implements ActionListener, ChangeListener, PreEnv4Plugin { + private static final String PHANTOM_PATH = "http://ocrpz63ed.bkt.clouddn.com/phantomjs.zip"; + + //链接服务器的客户端 + private HttpClient httpClient; + + //已读文件字节数 + private int totalBytesRead = 0; + + //文件总长度 + private int totalSize = 0; + + //进度显示界面 + private JDialog frame = null; + + //进度条 + private JProgressBar progressbar; + + + private JLabel label; + + //进度条更新时钟 + private Timer timer; + + //文件路径 + private String filePath = StringUtils.EMPTY; + + //是否继续下载 + private boolean flag = true; + + //安装结果 + boolean result = false; + + private static final String WEB_INFO = FRContext.getCurrentEnv().getPath(); + private static final String WEB_REPORT = new File(WEB_INFO).getParent(); + public static String PHANTOM_ENV = WEB_REPORT + File.separator + "phantomjs"; + + public String getFilePath() { + return filePath; + } + + public InstallPhantomJs() { + } + + //是否可以连接服务器 + public boolean serverReached(){ + return totalSize != -1; + } + + private void init(){ + frame = new JDialog(DesignerContext.getDesignerFrame(), true); + frame.setTitle("在线安装phantomjs"); + frame.setSize(400, 130); + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + frame.setLocation(screenSize.width/2-400/2,screenSize.height/2-130/2); + frame.setResizable(false); + frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); + frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + //取消下载 + flag = false; + frame.dispose(); + } + }); + + Container contentPanel = frame.getContentPane(); + label = new JLabel("", JLabel.CENTER); + + progressbar = new JProgressBar(); + progressbar.setOrientation(JProgressBar.HORIZONTAL); + progressbar.setMinimum(0); + progressbar.setMaximum(totalSize); + progressbar.setValue(0); + progressbar.setStringPainted(true); + progressbar.addChangeListener(this); + progressbar.setPreferredSize(new Dimension(300, 20)); + progressbar.setBorderPainted(true); + progressbar.setBackground(Color.pink); + + timer = new Timer(100, this); + + contentPanel.add(label, BorderLayout.CENTER); + contentPanel.add(progressbar, BorderLayout.SOUTH); + } + + private int connectToServer(){ + httpClient = new HttpClient(PHANTOM_PATH); + if (httpClient.getResponseCode() == HttpURLConnection.HTTP_OK) { + return httpClient.getContentLength(); + } + return -1; + } + + //安装 + public boolean install() { + //初始化安装进度界面 + init(); + //连接服务器 + totalSize = connectToServer(); + + //开始时钟 + timer.start(); + //开始下载 + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + installPhantomJsOnline(httpClient); + } + }); + thread.start(); + + frame.setVisible(true); + //等待下载线程处理结束 + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + return false; + } + //停止时钟 + timer.stop(); + return result; + } + + private boolean downloadPluginPhantomJSFile(HttpClient httpClient) throws Exception { + if (httpClient.getResponseCode() == HttpURLConnection.HTTP_OK) { + InputStream reader = httpClient.getResponseStream(); + String temp = StableUtils.pathJoin(PluginHelper.DOWNLOAD_PATH, PluginHelper.TEMP_FILE); + StableUtils.makesureFileExist(new File(temp)); + FileOutputStream writer = new FileOutputStream(temp); + byte[] buffer = new byte[PluginConstants.BYTES_NUM]; + int bytesRead = 0; + totalBytesRead = 0; + + while ((bytesRead = reader.read(buffer)) > 0 && flag) { + writer.write(buffer, 0, bytesRead); + buffer = new byte[PluginConstants.BYTES_NUM]; + totalBytesRead += bytesRead; + } + reader.close(); + writer.flush(); + writer.close(); + filePath = temp; + + //下载被取消 + if (flag == false){ + result = false; + return false; + } + + //准备安装 + + } else { + result = false; + throw new com.fr.plugin.PluginVerifyException(Inter.getLocText("FR-Designer-Plugin_Connect_Server_Error")); + } + return true; + } + + public void installPhantomJsOnline(HttpClient httpClient){ + try { + if (downloadPluginPhantomJSFile(httpClient)){ + //安装文件 + installPluginPhantomJsFile(filePath); + result = true; + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + //安装已经下载好的文件 + private void installPluginPhantomJsFile(String filePath){ + IOUtils.unzip(new File(filePath), PHANTOM_ENV); + } + + public void actionPerformed(ActionEvent e) { + if (e.getSource() == timer) { + int value = progressbar.getValue(); + if (value < totalSize) { + progressbar.setValue(totalBytesRead); + } + else { + timer.stop(); + frame.dispose(); + } + } + } + + + + public void stateChanged(ChangeEvent e1) { + + double value = (double)progressbar.getValue() / 1000000.0; + + if (e1.getSource() == progressbar) { + + label.setText("已下载:" + Double.toString(value) + " m"); + + label.setForeground(Color.blue); + + } + + } + + + @Override + public boolean preOnline() { + int choose = JOptionPane.showConfirmDialog(null, "新图表需要phantomjs支持。是否需要安装phantomjs(" + totalSize/1000000 + " m)?", "install tooltip", JOptionPane.YES_NO_OPTION); + if (choose == 0){//下载安装 + InstallPhantomJs installPhantomJs = new InstallPhantomJs(); + if (!installPhantomJs.serverReached()){ + JOptionPane.showMessageDialog(null, "无法连接远程服务器!!", "警告", JOptionPane.ERROR_MESSAGE); + return false; + } + //安装phantomJs + if (installPhantomJs.install()){ + JOptionPane.showMessageDialog(null, "安装成功!!"); + return true; + }else { + JOptionPane.showMessageDialog(null, "安装失败!!", "警告", JOptionPane.ERROR_MESSAGE); + return false; + } + }else {//不安装。无需为用户准备环境 + return true; + } + } + + @Override + public boolean checkEnv() { + //只有图表插件在下载前需要准备环境 + String web_info = FRContext.getCurrentEnv().getPath(); + String web_report = new File(web_info).getParent(); + //检测路径下有没有需要的环境 + return checkPath(web_report, "phantomjs"); + } + + private boolean checkPath(String rootPath, String targetFileName) { + //创建server环境路径 + String serverPath = rootPath + File.separator + targetFileName; + File phantom = new File(serverPath); + return phantom.exists() && phantom.isDirectory(); + } +} diff --git a/designer_base/src/com/fr/design/extra/pre4plugin/NoneEnv4Plugin.java b/designer_base/src/com/fr/design/extra/pre4plugin/NoneEnv4Plugin.java new file mode 100644 index 000000000..6d222d90a --- /dev/null +++ b/designer_base/src/com/fr/design/extra/pre4plugin/NoneEnv4Plugin.java @@ -0,0 +1,16 @@ +package com.fr.design.extra.pre4plugin; + +/** + * Created by hufan on 2016/9/1. + */ +public class NoneEnv4Plugin implements PreEnv4Plugin { + @Override + public boolean preOnline() { + return true; + } + + @Override + public boolean checkEnv() { + return true; + } +} diff --git a/designer_base/src/com/fr/design/extra/pre4plugin/PluginFactory.java b/designer_base/src/com/fr/design/extra/pre4plugin/PluginFactory.java new file mode 100644 index 000000000..550cc2ae2 --- /dev/null +++ b/designer_base/src/com/fr/design/extra/pre4plugin/PluginFactory.java @@ -0,0 +1,34 @@ +package com.fr.design.extra.pre4plugin; + +import com.fr.general.FRLogger; + +import java.lang.reflect.Constructor; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by hufan on 2016/9/1. + */ +public class PluginFactory { + /** + * + * 标签Map + */ + private static Map> pluginMap = new HashMap>(); + static { + pluginMap.put("com.fr.plugin.chart.vancharts", InstallPhantomJs.class); + } + + public static PreEnv4Plugin createPreEnv(String pluginID) { + if(pluginMap.containsKey(pluginID)){ + try{ + Class cl = pluginMap.get(pluginID); + Constructor constructor = cl.getConstructor(); + return constructor.newInstance(); + } catch (Exception e){ + FRLogger.getLogger().error(e.getMessage()); + } + } + return new NoneEnv4Plugin(); + } +} diff --git a/designer_base/src/com/fr/design/extra/pre4plugin/PreEnv4Plugin.java b/designer_base/src/com/fr/design/extra/pre4plugin/PreEnv4Plugin.java new file mode 100644 index 000000000..9eccffb3a --- /dev/null +++ b/designer_base/src/com/fr/design/extra/pre4plugin/PreEnv4Plugin.java @@ -0,0 +1,10 @@ +package com.fr.design.extra.pre4plugin; + +/** + * Created by hufan on 2016/9/1. + */ +public interface PreEnv4Plugin { + public boolean preOnline(); + //是否已经准备好环境 + public boolean checkEnv(); +} diff --git a/designer_chart/src/com/fr/design/mainframe/chart/ChartEditPane.java b/designer_chart/src/com/fr/design/mainframe/chart/ChartEditPane.java index c3a2e1d50..7cbc87ca9 100644 --- a/designer_chart/src/com/fr/design/mainframe/chart/ChartEditPane.java +++ b/designer_chart/src/com/fr/design/mainframe/chart/ChartEditPane.java @@ -254,6 +254,16 @@ public class ChartEditPane extends BasicPane implements AttributeChange,Prepare4 } } + /** + * 响应事件. + */ + public void repaint() { + if (container != null && container.getEPane() != null) { + container.getEPane().validate(); + container.getEPane().repaint(); + } + } + public int getSelectedChartIndex(Chart chart){ int index = 0; if(typePane != null){