Skip to content

Commit

Permalink
feat: 修改更新逻辑,通过比较config的版本号与GitHub版本号判断是否更新
Browse files Browse the repository at this point in the history
  • Loading branch information
yagol2020 committed Nov 24, 2021
1 parent 3356a4e commit 96a8619
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 68 deletions.
1 change: 1 addition & 0 deletions src/main/java/config/MyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
**/
@Data
public class MyConfig {
String version;
ChromeProperties chrome;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/gui/view/BaseGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
**/
public interface BaseGui {
/**
* 启动窗口,并显示
* 向组件传递数据
*
* @param data 数据
*/
void start(HashMap<String, Object> data);

/**
* 初始化窗口
* 初始化窗口,注意,不能被内部的构造函数调用,任何时候都应该由外部调用
*
* @return 窗口自身
*/
Expand All @@ -29,7 +29,7 @@ public interface BaseGui {
void show();

/**
* 初始化组件功能
* 初始化组件功能,应该由构造函数内部调用
*/
void initComponentFunctions();
}
17 changes: 13 additions & 4 deletions src/main/java/gui/view/MainGui.form
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="gui.view.MainGui">
<grid id="27dc6" binding="main" layout-manager="GridBagLayout">
<constraints>
<xy x="20" y="20" width="927" height="650"/>
<xy x="20" y="20" width="928" height="650"/>
</constraints>
<properties>
<preferredSize width="900" height="500"/>
Expand Down Expand Up @@ -31,7 +31,7 @@
<grid id="4fbaa" binding="content" layout-manager="GridLayoutManager" row-count="7" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="0.0"/>
</constraints>
<properties/>
Expand Down Expand Up @@ -406,7 +406,7 @@
</grid>
<scrollpane id="85cec" binding="logScrollPane">
<constraints>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="1.0"/>
</constraints>
<properties/>
Expand All @@ -423,13 +423,22 @@
</scrollpane>
<component id="8223f" class="javax.swing.JButton" binding="checkUpdateButton">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<text value="检查更新"/>
</properties>
</component>
<component id="e8507" class="javax.swing.JLabel" binding="versionLabel">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<text value="Label"/>
</properties>
</component>
</children>
</grid>
</form>
21 changes: 13 additions & 8 deletions src/main/java/gui/view/MainGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private void initComponent() {
if (StrUtil.isNotEmpty(config.getChrome().getDriverPath())) {
output.setText(config.getChrome().getDriverPath());
}
versionLabel.setText("版本号:" + config.getVersion());
}


Expand Down Expand Up @@ -227,12 +228,7 @@ public void actionPerformed(ActionEvent e) {
checkUpdateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (StrUtil.isNotEmpty(config.getChrome().getDriverPath())) {
updateGui.show();
} else {
log.info("更新前请选择浏览器驱动,否则无法更新");
}

updateGui.show();
}
});
}
Expand Down Expand Up @@ -374,6 +370,7 @@ public static void main(String[] args) {
private JProgressBar acmDetectorProgressBar;
private JLabel acmDetectorLabel;
private JButton checkUpdateButton;
private JLabel versionLabel;

/**
* Method generated by IntelliJ IDEA GUI Designer
Expand Down Expand Up @@ -410,7 +407,7 @@ public static void main(String[] args) {
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.gridwidth = 3;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.BOTH;
main.add(content, gbc);
Expand Down Expand Up @@ -548,7 +545,7 @@ public static void main(String[] args) {
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.gridwidth = 3;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
Expand All @@ -562,8 +559,16 @@ public static void main(String[] args) {
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
main.add(checkUpdateButton, gbc);
versionLabel = new JLabel();
versionLabel.setText("Label");
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
main.add(versionLabel, gbc);
}

/**
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/gui/view/UpdateGui.form
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,25 @@
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="1" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="c5c80" class="javax.swing.JTextArea" binding="updateLogTextArea">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties>
<editable value="false"/>
<stringPainted value="true"/>
</properties>
</component>
<scrollpane id="b5d2e">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="c5c80" class="javax.swing.JTextArea" binding="updateLogTextArea">
<constraints/>
<properties>
<editable value="false"/>
</properties>
</component>
</children>
</scrollpane>
<component id="14ec9" class="javax.swing.JLabel" binding="updateLogLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
Expand All @@ -44,7 +51,7 @@
</component>
<component id="fde5" class="javax.swing.JButton" binding="update">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="更新"/>
Expand Down
48 changes: 37 additions & 11 deletions src/main/java/gui/view/UpdateGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
**/
public class UpdateGui implements BaseGui {
private JFrame jFrame;
private String savePath;

public UpdateGui() {
initComponentFunctions();
}

@Override
public void start(HashMap<String, Object> data) {
Expand All @@ -30,6 +35,7 @@ public UpdateGui init() {
jFrame.setContentPane(updatePanel);
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jFrame.pack();
update.setEnabled(false);
return this;
}

Expand All @@ -39,25 +45,39 @@ public void show() {
ThreadUtil.execute(new Runnable() {
@Override
public void run() {
updateLogTextArea.append(DateUtil.now() + StrUtil.SPACE + "已开始搜索GitHub");
updateLogTextArea.append(DateUtil.now() + StrUtil.SPACE + "已开始搜索GitHub" + StrUtil.LF);
downloadUpdateFile();
}
});
}

private void downloadUpdateFile() {
try {
MyUpdateUtil myUpdateUtil = new MyUpdateUtil();
myUpdateUtil.checkUpdate(updateProgressBar, updateLogTextArea);
} catch (Exception e) {
updateLogTextArea.append(DateUtil.now() + StrUtil.SPACE + "下载更新文件时出现错误,错误信息为:" + e.getMessage());
e.printStackTrace();
}
ThreadUtil.execute(new Runnable() {
@Override
public void run() {
try {
savePath = MyUpdateUtil.checkUpdate(updateProgressBar, updateLogTextArea);
if (!StrUtil.equals(savePath, MyUpdateUtil.ALREADY_NEWEST_VERSION)) {
update.setEnabled(true);
} else {
JOptionPane.showMessageDialog(updatePanel, "已为最新版,无需升级!");
}
} catch (Exception e) {
updateLogTextArea.append(DateUtil.now() + StrUtil.SPACE + "下载更新文件时出现错误,错误信息为:" + e.getMessage() + StrUtil.LF);
e.printStackTrace();
}
}
});
}

@Override
public void initComponentFunctions() {

update.addActionListener(e -> {
if (StrUtil.isNotEmpty(savePath)) {
JOptionPane.showMessageDialog(updatePanel, "新版本的地址为" + StrUtil.LF + savePath + StrUtil.LF + StrUtil.LF
+ "在使用前请重新命名为PWC.exe");
}
});
}


Expand All @@ -84,19 +104,25 @@ public void initComponentFunctions() {
*/
private void $$$setupUI$$$() {
updatePanel = new JPanel();
updatePanel.setLayout(new GridLayoutManager(2, 2, new Insets(0, 0, 0, 0), -1, -1));
updatePanel.setLayout(new GridLayoutManager(2, 3, new Insets(0, 0, 0, 0), -1, -1));
updatePanel.setPreferredSize(new Dimension(600, 200));
updateLabel = new JLabel();
updateLabel.setText("下载进度");
updatePanel.add(updateLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 1, false));
updateProgressBar = new JProgressBar();
updateProgressBar.setStringPainted(true);
updatePanel.add(updateProgressBar, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 1, false));
final JScrollPane scrollPane1 = new JScrollPane();
updatePanel.add(scrollPane1, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
updateLogTextArea = new JTextArea();
updateLogTextArea.setEditable(false);
updatePanel.add(updateLogTextArea, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false));
scrollPane1.setViewportView(updateLogTextArea);
updateLogLabel = new JLabel();
updateLogLabel.setText("日志");
updatePanel.add(updateLogLabel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
update = new JButton();
update.setText("更新");
updatePanel.add(update, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/param/NormalParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class NormalParam {
public final static String ABOUT_FILE_PATH = "/gui_text_file/about.txt";
public final static String HELP_FILE_PATH = "/gui_text_file/help.txt";
public final static String CONFIG_FILE_NAME = "base.yaml";
public final static Integer ONE_HUNDRED = 100;
public final static Integer THIRTY = 30;
public final static Integer FIFTY = 50;
public final static Integer SEVENTY = 70;
public final static String QUESTION_MARK = "?";
/**
* 小、中、大括号内容匹配,注意需要循环遍历
*/
Expand Down
Loading

0 comments on commit 96a8619

Please sign in to comment.