1
1
package com .simtechdata ;
2
2
3
+ import com .simtechdata .build .Job ;
4
+ import com .simtechdata .build .Selections ;
3
5
import javafx .scene .image .Image ;
4
- import javafx .scene .image .WritableImage ;
5
- import javafx .scene .paint .Color ;
6
6
import org .apache .commons .io .FileUtils ;
7
7
import org .apache .commons .io .FilenameUtils ;
8
- import org .glavo .png .javafx .PNGJavaFXUtils ;
9
8
10
9
import java .io .File ;
11
10
import java .io .IOException ;
17
16
18
17
public class ProcessFile {
19
18
20
- public ProcessFile (Path selectedImageFile , boolean inWindow ) {
19
+ public ProcessFile (Path selectedImageFile , Selections selections ) {
21
20
this .selectedImageFile = selectedImageFile ;
22
- this .inWindow = inWindow ;
21
+ this .selections = selections ;
23
22
}
24
23
25
24
private final Path selectedImageFile ;
26
- private final boolean inWindow ;
25
+ private final Selections selections ;
27
26
28
27
public Response run () {
29
- return processFile ();
28
+ if (selections == null )
29
+ return processFileShell ();
30
+ return processFileGUI ();
30
31
}
31
32
32
33
private static String icnsFilePath ;
@@ -35,24 +36,75 @@ public static String getIcnsFilePath() {
35
36
return icnsFilePath ;
36
37
}
37
38
38
- private Response processFile () {
39
+ private Response processFileGUI () {
40
+ Response response = new Response ();
41
+ if (selectedImageFile != null ) {
42
+ Map <Integer , Job > jobMap = selections .getJobMap ();
43
+ if (jobMap == null ) {
44
+ System .out .println ("NULL" );
45
+ System .exit (0 );
46
+ }
47
+ try {
48
+ File file = selectedImageFile .toFile ();
49
+ Image original = new Image (file .toURI ().toString ());
50
+ double width = original .getWidth ();
51
+ double height = original .getHeight ();
52
+ String msg ;
53
+ if (!ImageChecker .isValid (file )) {
54
+ msg = "Specified file is not a valid image type.\n Must be: PNG, JPEG, GIF, TIFF, BMP or SVG" ;
55
+ response .set (false , msg );
56
+ return response ;
57
+ }
58
+ if (width != 1024 || height != 1024 ) {
59
+ msg = "Specified image is not 1024 x 1024 in size" ;
60
+ response .set (false , msg );
61
+ return response ;
62
+ }
63
+ if (selections .makeParentFolder ()) {
64
+ for (int index : jobMap .keySet ()) {
65
+ jobMap .get (index ).saveFile ();
66
+ }
67
+ int exitCode = new Run (selections .getIconFolder (), selections .getIcnsFilePath ()).run ();
68
+ if (exitCode != 0 ) {
69
+ response .set (false , "Error occurred while running the icon creation process" );
70
+ }
71
+ else {
72
+ FileUtils .deleteDirectory (selections .getIconFolder ().toFile ());
73
+ response .set (true , "" );
74
+ }
75
+ }
76
+ else {
77
+ msg = "Could not create the icon workspace folder, permission issue?" ;
78
+ response .set (false , msg );
79
+ }
80
+ }
81
+ catch (IOException e ) {
82
+ throw new RuntimeException (e );
83
+ }
84
+ }
85
+ return response ;
86
+ }
87
+
88
+ private Response processFileShell () {
39
89
Response response = new Response ();
40
90
if (selectedImageFile != null ) {
41
91
String folder = selectedImageFile .toFile ().getParentFile ().getAbsolutePath ();
42
92
Path iconFolder = Paths .get (folder , "icon.iconset" );
43
93
String iconFilename = FilenameUtils .getBaseName (selectedImageFile .toString ()) + ".icns" ;
44
94
Path icnsFile = Paths .get (folder , iconFilename );
45
95
icnsFilePath = icnsFile .toAbsolutePath ().toString ();
46
- Path Image01 = Paths .get (iconFolder .toString (), "icon_512x512@2x.png" );
47
- Path Image02 = Paths .get (iconFolder .toString (), "icon_512x512.png" );
48
- Path Image03 = Paths .get (iconFolder .toString (), "icon_256x256@2x.png" );
49
- Path Image04 = Paths .get (iconFolder .toString (), "icon_256x256.png" );
50
- Path Image05 = Paths .get (iconFolder .toString (), "icon_128x128@2x.png" );
51
- Path Image06 = Paths .get (iconFolder .toString (), "icon_128x128.png" );
52
- Path Image07 = Paths .get (iconFolder .toString (), "icon_32x32@2x.png" );
53
- Path Image08 = Paths .get (iconFolder .toString (), "icon_32x32.png" );
54
- Path Image09 = Paths .get (iconFolder .toString (), "icon_16x16@2x.png" );
55
- Path Image10 = Paths .get (iconFolder .toString (), "icon_16x16.png" );
96
+ Path Image01 = Paths .get (iconFolder .toString (), "icon_512x512@2x.png" ); //01
97
+ Path Image02 = Paths .get (iconFolder .toString (), "icon_512x512.png" ); //02
98
+ Path Image03 = Paths .get (iconFolder .toString (), "icon_256x256@2x.png" ); //03
99
+ Path Image04 = Paths .get (iconFolder .toString (), "icon_256x256.png" ); //04
100
+ Path Image05 = Paths .get (iconFolder .toString (), "icon_128x128@2x.png" ); //05
101
+ Path Image06 = Paths .get (iconFolder .toString (), "icon_128x128.png" ); //06
102
+ Path Image07 = Paths .get (iconFolder .toString (), "icon_64x64@2x.png" ); //07
103
+ Path Image08 = Paths .get (iconFolder .toString (), "icon_64x64.png" ); //08
104
+ Path Image09 = Paths .get (iconFolder .toString (), "icon_32x32@2x.png" ); //09
105
+ Path Image10 = Paths .get (iconFolder .toString (), "icon_32x32.png" ); //10
106
+ Path Image11 = Paths .get (iconFolder .toString (), "icon_16x16@2x.png" ); //11
107
+ Path Image12 = Paths .get (iconFolder .toString (), "icon_16x16.png" ); //12
56
108
57
109
Map <Integer , Job > jobMap = new HashMap <>();
58
110
@@ -64,21 +116,13 @@ private Response processFile() {
64
116
String msg ;
65
117
if (!ImageChecker .isValid (file )) {
66
118
msg = "Specified file is not a valid image type.\n Must be: PNG, JPEG, GIF, TIFF, BMP or SVG" ;
67
- if (!inWindow ) {
68
- System .out .println (msg );
69
- System .exit (0 );
70
- }
71
- response .set (false , msg );
72
- return response ;
119
+ System .out .println (msg );
120
+ System .exit (0 );
73
121
}
74
122
if (width != 1024 || height != 1024 ) {
75
123
msg = "Specified image is not 1024 x 1024 in size" ;
76
- if (!inWindow ) {
77
- System .out .println (msg );
78
- System .exit (0 );
79
- }
80
- response .set (false , msg );
81
- return response ;
124
+ System .out .println (msg );
125
+ System .exit (0 );
82
126
}
83
127
if (FileUtils .createParentDirectories (Image01 .toFile ()).exists ()) {
84
128
jobMap .put (1 , new Job (original , Image01 , 1024 ));
@@ -87,10 +131,12 @@ private Response processFile() {
87
131
jobMap .put (4 , new Job (original , Image04 , 256 ));
88
132
jobMap .put (5 , new Job (original , Image05 , 256 ));
89
133
jobMap .put (6 , new Job (original , Image06 , 128 ));
90
- jobMap .put (7 , new Job (original , Image07 , 64 ));
91
- jobMap .put (8 , new Job (original , Image08 , 32 ));
92
- jobMap .put (9 , new Job (original , Image09 , 32 ));
93
- jobMap .put (10 , new Job (original , Image10 , 16 ));
134
+ jobMap .put (7 , new Job (original , Image07 , 128 ));
135
+ jobMap .put (8 , new Job (original , Image08 , 64 ));
136
+ jobMap .put (9 , new Job (original , Image09 , 64 ));
137
+ jobMap .put (10 , new Job (original , Image10 , 32 ));
138
+ jobMap .put (11 , new Job (original , Image11 , 32 ));
139
+ jobMap .put (12 , new Job (original , Image12 , 16 ));
94
140
95
141
for (int index : jobMap .keySet ()) {
96
142
jobMap .get (index ).saveFile ();
@@ -102,88 +148,23 @@ private Response processFile() {
102
148
}
103
149
else {
104
150
FileUtils .forceDeleteOnExit (iconFolder .toFile ());
105
- if (!inWindow ) {
106
- System .out .println ("Icon file created: " + icnsFile );
107
- System .exit (0 );
108
- }
151
+ System .out .println ("Icon file created: " + icnsFile );
152
+ System .exit (0 );
109
153
response .set (true , "" );
110
154
}
111
155
}
112
156
else {
113
157
msg = "Could not create the icon workspace folder, permission issue?" ;
114
- response .set (false , msg );
115
- if (!inWindow ) {
116
- System .out .println (msg );
117
- System .exit (0 );
118
- }
158
+ System .out .println (msg );
159
+ System .exit (0 );
119
160
}
120
- } catch (IOException e ) {
121
- System .err .println ("There was an error. If the following information does not help you figure out the problem, copy and paste the text below the line and create an issue on https://github.com/EasyG0ing1/MacIcns\n ---------------------------------------------------------------\n ProcessFile.processFile()\n \n " );
122
- throw new RuntimeException (e );
123
- }
124
- }
125
- return response ;
126
- }
127
-
128
- private static class Job {
129
- public Job (Image original , Path path , int size ) {
130
- this .original = original ;
131
- this .path = path ;
132
- this .size = size ;
133
- }
134
-
135
- public static WritableImage lastImage ;
136
- private final Image original ;
137
- private final Path path ;
138
- private final int size ;
139
- private int lastSize = 0 ;
140
-
141
- public void saveFile () {
142
- try {
143
- PNGJavaFXUtils .writeImage (getImage (), path );
144
- } catch (IOException e ) {
145
- throw new RuntimeException (e );
146
161
}
147
- }
148
-
149
- private WritableImage getImage () {
150
- try {
151
- WritableImage writableImage ;
152
- if (lastImage == null ) {
153
- writableImage = resizeImage (original , size , size );
154
- lastImage = writableImage ;
155
- lastSize = size ;
156
- }
157
- else {
158
- if (size == lastSize ) {
159
- writableImage = lastImage ;
160
- }
161
- else {
162
- writableImage = resizeImage (lastImage , size , size );
163
- lastSize = size ;
164
- }
165
- }
166
- return writableImage ;
167
- } catch (Exception e ) {
162
+ catch (IOException e ) {
163
+ System .err .println ("There was an error. If the following information does not help you figure out the problem, copy and paste the text below the line and create an issue on https://github.com/EasyG0ing1/MacIcns\n ---------------------------------------------------------------\n ProcessFile.processFileShell()\n \n " );
168
164
throw new RuntimeException (e );
169
165
}
170
166
}
171
-
172
- private WritableImage resizeImage (Image originalImage , int targetWidth , int targetHeight ) {
173
- int width = (int ) originalImage .getWidth ();
174
- int height = (int ) originalImage .getHeight ();
175
-
176
- WritableImage resizedImage = new WritableImage (targetWidth , targetHeight );
177
- for (int x = 0 ; x < targetWidth ; x ++) {
178
- for (int y = 0 ; y < targetHeight ; y ++) {
179
- double sourceX = x * width / (double ) targetWidth ;
180
- double sourceY = y * height / (double ) targetHeight ;
181
- Color color = originalImage .getPixelReader ().getColor ((int ) sourceX , (int ) sourceY );
182
- resizedImage .getPixelWriter ().setColor (x , y , color );
183
- }
184
- }
185
- return resizedImage ;
186
- }
167
+ return response ;
187
168
}
188
169
189
170
}
0 commit comments