11package com .pistacium .modcheck ;
22
3+ import com .google .gson .JsonObject ;
4+ import com .google .gson .JsonParser ;
35import com .pistacium .modcheck .mod .ModData ;
46import com .pistacium .modcheck .mod .resource .ModResource ;
57import com .pistacium .modcheck .mod .version .ModVersion ;
68import com .pistacium .modcheck .util .ModCheckStatus ;
9+ import com .pistacium .modcheck .util .ModCheckUtils ;
710
811import javax .swing .*;
912import javax .swing .border .EmptyBorder ;
13+ import javax .swing .plaf .FontUIResource ;
1014import java .awt .*;
1115import java .io .File ;
1216import java .net .URI ;
13- import java .nio .file .InvalidPathException ;
1417import java .nio .file .Path ;
1518import java .nio .file .Paths ;
16- import java .util .ArrayList ;
17- import java .util .HashMap ;
18- import java .util .Map ;
19- import java .util .Objects ;
19+ import java .util .*;
2020
21+ @ SuppressWarnings ("ResultOfMethodCallIgnored" )
2122public class ModCheckFrame extends JFrame {
2223
24+ private static final FontUIResource font = new FontUIResource ("SansSerif" , Font .BOLD , 15 );
25+
2326 private final JPanel mainPanel ;
2427 private JProgressBar progressBar ;
2528 private JComboBox <ModVersion > versionSelection ;
@@ -35,6 +38,7 @@ public JProgressBar getProgressBar() {
3538
3639 ModCheckFrame () {
3740 super ("ModCheck v" + ModCheckConstants .APPLICATION_VERSION + " by RedLime" );
41+ setUIFont ();
3842
3943 mainPanel = new JPanel (new BorderLayout ());
4044
@@ -48,13 +52,23 @@ public JProgressBar getProgressBar() {
4852
4953 getContentPane ().add (mainPanel );
5054
51- setSize (800 , 550 );
55+ setSize (1100 , 700 );
5256 setResizable (false );
5357 setVisible (true );
5458 setLocationRelativeTo (null );
5559 setDefaultCloseOperation (EXIT_ON_CLOSE );
5660 }
5761
62+ private static void setUIFont (){
63+ Enumeration <?> keys = UIManager .getLookAndFeelDefaults ().keys ();
64+ while (keys .hasMoreElements ()) {
65+ Object key = keys .nextElement ();
66+ Object value = UIManager .get (key );
67+ if (value instanceof FontUIResource )
68+ UIManager .put (key , ModCheckFrame .font );
69+ }
70+ }
71+
5872 private void initHeaderLayout () {
5973 JPanel instanceSelectPanel = new JPanel ();
6074
@@ -82,52 +96,71 @@ private void initBottomLayout() {
8296 progressBar = new JProgressBar ();
8397 progressBar .setStringPainted (true );
8498 progressBar .setString ("Idle..." );
85- progressBar .setPreferredSize (new Dimension (300 , 20 ));
99+ progressBar .setPreferredSize (new Dimension (500 , 30 ));
86100
87101 JPanel buttonPanel = new JPanel (new GridLayout (2 , 1 ));
88102 JCheckBox jCheckBox = new JCheckBox ("Delete all old .jar files" );
89103 downloadButton = new JButton ("Download" );
90104 downloadButton .addActionListener (e -> {
91105 downloadButton .setEnabled (false );
92106
93- Path instancePath ;
107+ Stack < Path > instancePathStack = new Stack <>() ;
94108 try {
95- instancePath = Paths .get (pathField .getText ());
96- } catch (InvalidPathException exception ) {
109+ String [] pathArr = pathField .getText ().split (String .format ("\\ %s" , File .separator ));
110+ String lastPath = pathArr [pathArr .length - 1 ];
111+ if (lastPath .contains ("*" ) && lastPath .chars ().filter (c -> c == '*' ).count () == 1 ) {
112+ pathArr [pathArr .length - 1 ] = "" ;
113+ File [] pathFiles = Paths .get (String .join (File .separator , pathArr )).toFile ().listFiles ();
114+ if (pathFiles == null ) {
115+ throw new IllegalAccessException ();
116+ }
117+
118+ if (lastPath .equals ("*" )) {
119+ for (File pathFile : pathFiles ) {
120+ if (pathFile .isDirectory ()) instancePathStack .push (pathFile .toPath ());
121+ }
122+ } else if (lastPath .startsWith ("*" )) {
123+ for (File pathFile : pathFiles ) {
124+ if (pathFile .isDirectory () && pathFile .getName ().endsWith (lastPath .replace ("*" , "" ))) instancePathStack .push (pathFile .toPath ());
125+ }
126+ } else if (lastPath .endsWith ("*" )) {
127+ for (File pathFile : pathFiles ) {
128+ if (pathFile .isDirectory () && pathFile .getName ().startsWith (lastPath .replace ("*" , "" ))) instancePathStack .push (pathFile .toPath ());
129+ }
130+ } else {
131+ String [] split = lastPath .split ("\\ *" );
132+ for (File pathFile : pathFiles ) {
133+ if (pathFile .isDirectory () && pathFile .getName ().startsWith (split [0 ]) && pathFile .getName ().endsWith (split [1 ])) instancePathStack .push (pathFile .toPath ());
134+ }
135+ }
136+ } else {
137+ instancePathStack .push (Paths .get (String .join (File .separator , pathArr )));
138+ }
139+ } catch (Exception exception ) {
140+ exception .printStackTrace ();
97141 JOptionPane .showMessageDialog (this , "Failed to parsing Instance path!" , "Please try again" , JOptionPane .ERROR_MESSAGE );
98142 downloadButton .setEnabled (true );
99143 return ;
100144 }
101- if (!instancePath .toString ().endsWith ("mods" )) instancePath = instancePath .resolve ("mods" );
102- File instanceDir = instancePath .toFile ();
103- if (!instanceDir .isDirectory ()) {
104- JOptionPane .showMessageDialog (this , "Please select a instance path(directory)!" , "Please try again" , JOptionPane .ERROR_MESSAGE );
105- downloadButton .setEnabled (true );
106- return ;
107- }
108145
109- if (jCheckBox .isSelected ()) {
110- File [] modFiles = instanceDir .listFiles ();
111- if (modFiles == null ) return ;
112- for (File file : modFiles ) {
113- if (file .getName ().endsWith (".jar" ))
114- //noinspection ResultOfMethodCallIgnored
115- file .delete ();
146+ Stack <File > modsFileStack = new Stack <>();
147+ for (Path instancePath : instancePathStack ) {
148+ Path modsPath = instancePath .resolve ("mods" );
149+ File instanceDir = modsPath .toFile ();
150+ if (!instanceDir .isDirectory ()) {
151+ JOptionPane .showMessageDialog (this , "Please select a instance path(directory)!" , "Please try again" , JOptionPane .ERROR_MESSAGE );
152+ downloadButton .setEnabled (true );
153+ return ;
116154 }
155+ modsFileStack .push (instanceDir );
117156 }
118157
119-
120158 if (this .versionSelection .getSelectedItem () == null ) {
121159 JOptionPane .showMessageDialog (this , "??? What are you doing" );
122160 downloadButton .setEnabled (true );
123161 return ;
124162 }
125163
126- ModVersion mcVersion = (ModVersion ) this .versionSelection .getSelectedItem ();
127- this .progressBar .setValue (0 );
128- ModCheck .setStatus (ModCheckStatus .DOWNLOADING_MOD_FILE );
129-
130- Path finalInstancePath = instancePath ;
131164 ArrayList <ModData > targetMods = new ArrayList <>();
132165 int maxCount = 0 ;
133166 for (Map .Entry <ModData , JCheckBox > modEntry : modCheckBoxes .entrySet ()) {
@@ -136,14 +169,39 @@ private void initBottomLayout() {
136169 maxCount ++;
137170 }
138171 }
172+ ModVersion mcVersion = (ModVersion ) this .versionSelection .getSelectedItem ();
173+
174+ for (File instanceDir : modsFileStack ) {
175+ File [] modFiles = instanceDir .listFiles ();
176+ if (modFiles == null ) return ;
177+ for (File file : modFiles ) {
178+ if (file .getName ().endsWith (".jar" )) {
179+ if (jCheckBox .isSelected ()) {
180+ file .delete ();
181+ } else {
182+ String modFileName = file .getName ().split (ModVersion .versionRegex .pattern ())[0 ]
183+ .split (ModVersion .snapshotRegex .pattern ())[0 ];
184+ for (ModData targetMod : targetMods ) {
185+ String targetModFileName = targetMod .getLatestVersionResource (mcVersion ).getFileName ();
186+ if (targetModFileName .startsWith (modFileName )) {
187+ file .delete ();
188+ }
189+ }
190+ }
191+ }
192+ }
193+ }
194+
195+ this .progressBar .setValue (0 );
196+ ModCheck .setStatus (ModCheckStatus .DOWNLOADING_MOD_FILE );
139197
140198 int finalMaxCount = maxCount ;
141199 ModCheck .THREAD_EXECUTOR .submit (() -> {
142200 int count = 0 ;
143201 ArrayList <ModData > failedMods = new ArrayList <>();
144202 for (ModData targetMod : targetMods ) {
145203 this .progressBar .setString ("Downloading '" + targetMod .getName () + "'" );
146- if (!targetMod .downloadModJarFile (mcVersion , finalInstancePath )) {
204+ if (!targetMod .downloadModJarFile (mcVersion , modsFileStack )) {
147205 failedMods .add (targetMod );
148206 }
149207 this .progressBar .setValue ((int ) ((++count / (finalMaxCount * 1f )) * 100 ));
@@ -179,7 +237,7 @@ private void initCenterLayout() {
179237 versionJPanel = new JPanel ();
180238 versionJPanel .setLayout (new BoxLayout (versionJPanel , BoxLayout .Y_AXIS ));
181239 versionScrollPane = new JScrollPane (versionJPanel , JScrollPane .VERTICAL_SCROLLBAR_ALWAYS , JScrollPane .HORIZONTAL_SCROLLBAR_NEVER );
182- versionScrollPane .setPreferredSize (new Dimension (600 , 300 ));
240+ versionScrollPane .setPreferredSize (new Dimension (1000 , 500 ));
183241 versionScrollPane .getVerticalScrollBar ().setUnitIncrement (16 );
184242
185243 JPanel selectButtonPanel = new JPanel (new FlowLayout (FlowLayout .LEADING ));
@@ -206,21 +264,59 @@ private void initCenterLayout() {
206264 centerPanel .add (versionSelectPanel );
207265 centerPanel .add (selectButtonPanel );
208266 centerPanel .add (versionScrollPane );
267+ centerPanel .setBorder (BorderFactory .createEmptyBorder (0 , 30 , 0 , 30 ));
209268 mainPanel .add (centerPanel , BorderLayout .CENTER );
210269 }
211270
212271 public void initMenuBar () {
213272 JMenuBar menuBar = new JMenuBar ();
214273
215- JMenu source = new JMenu ("Source..." );
216- JMenuItem githubSource = new JMenuItem ("Github" );
274+ JMenu source = new JMenu ("Info" );
275+
276+ JMenuItem githubSource = new JMenuItem ("Github..." );
217277 githubSource .addActionListener (e -> {
218278 try {
219279 Desktop .getDesktop ().browse (new URI ("https://github.com/RedLime/ModCheck" ));
220280 } catch (Exception ignored ) {
221281 }
222282 });
223283 source .add (githubSource );
284+
285+ JMenuItem donateSource = new JMenuItem ("Support..." );
286+ donateSource .addActionListener (e -> {
287+ try {
288+ Desktop .getDesktop ().browse (new URI ("https://ko-fi.com/redlimerl" ));
289+ } catch (Exception ignored ) {
290+ }
291+ });
292+ source .add (donateSource );
293+
294+ JMenuItem checkChangeLogSource = new JMenuItem ("Changelog..." );
295+ checkChangeLogSource .addActionListener (e -> {
296+ try {
297+ Desktop .getDesktop ().browse (new URI ("https://github.com/RedLime/ModCheck/releases/tag/" +ModCheckConstants .APPLICATION_VERSION ));
298+ } catch (Exception ignored ) {
299+ }
300+ });
301+ source .add (checkChangeLogSource );
302+
303+ JMenuItem updateCheckSource = new JMenuItem ("Check new update" );
304+ updateCheckSource .addActionListener (e -> {
305+ try {
306+ JsonObject jsonObject = JsonParser .parseString (ModCheckUtils .getUrlRequest ("https://api.github.com/repos/RedLime/ModCheck/releases/latest" )).getAsJsonObject ();
307+ if (ModVersion .of (jsonObject .get ("tag_name" ).getAsString ()).compareTo (ModVersion .of (ModCheckConstants .APPLICATION_VERSION )) > 0 ) {
308+ int result = JOptionPane .showOptionDialog (null , "<html><body>Found new ModCheck update!<br><br>Current Version : " + ModCheckConstants .APPLICATION_VERSION + "<br>Updated Version : " + jsonObject .get ("tag_name" ).getAsString () + "</body></html>" ,"Update Checker" , JOptionPane .YES_NO_CANCEL_OPTION , JOptionPane .PLAIN_MESSAGE , null , new String [] { "Download" , "Cancel" }, "Download" );
309+ if (result == 0 ) {
310+ Desktop .getDesktop ().browse (new URI ("https://github.com/RedLime/ModCheck/releases/latest" ));
311+ }
312+ } else {
313+ JOptionPane .showMessageDialog (this , "You are using latest version!" );
314+ }
315+ } catch (Exception ignored ) {
316+ }
317+ });
318+ source .add (updateCheckSource );
319+
224320 menuBar .add (source );
225321
226322 this .setJMenuBar (menuBar );
@@ -266,14 +362,14 @@ public void updateModList() {
266362 });
267363
268364 JLabel description = new JLabel ("<html><body>" + modData .getDescription ().replace ("\n " , "<br>" ) + "</body></html>" );
269- description .setMaximumSize (new Dimension (500 , 60 ));
365+ description .setMaximumSize (new Dimension (800 , 60 ));
270366 description .setBorder (new EmptyBorder (0 , 15 ,0 , 0 ));
271367 Font f = description .getFont ();
272368 description .setFont (f .deriveFont (f .getStyle () & ~Font .BOLD ));
273369
274370 modPanel .add (checkBox );
275371 modPanel .add (description );
276- modPanel .setMaximumSize (new Dimension (600 , 60 ));
372+ modPanel .setMaximumSize (new Dimension (950 , 60 ));
277373 modPanel .setBorder (new EmptyBorder (0 , 10 ,10 , 0 ));
278374
279375 versionJPanel .add (modPanel );
0 commit comments