@@ -70,6 +70,8 @@ public class Main extends JFrame{
70
70
JComboBox <String > sourceDensityComboBox ; //list of available densities
71
71
LinkedHashMap <String , JCheckBox > densitiesCheckBox ; //HashMap that stores the available density checkboxes
72
72
JPanel mainPanel , densitiesPanel ; //panel containing checkboxes
73
+ private File lastUsedSourceDirectory ;
74
+ private File lastUsedProjectDirectory ;
73
75
//JProgressBar progressBar;
74
76
75
77
File sourceImg ; //source Image File object
@@ -173,6 +175,9 @@ private void initListeners()
173
175
public void mouseClicked (MouseEvent event )
174
176
{
175
177
JFileChooser imageChooser = new JFileChooser ();
178
+ if (lastUsedSourceDirectory != null ) {
179
+ imageChooser .setCurrentDirectory (lastUsedSourceDirectory );
180
+ }
176
181
imageChooser .setDialogTitle ("Select an image" );
177
182
imageChooser .setFileSelectionMode (JFileChooser .FILES_ONLY );
178
183
imageChooser .setFileFilter (new FileFilter (){
@@ -199,6 +204,7 @@ public String getDescription() {
199
204
case (JFileChooser .APPROVE_OPTION ):
200
205
try {
201
206
sourceImg = new File (imageChooser .getSelectedFile ().getPath ());
207
+ lastUsedSourceDirectory = sourceImg .getParentFile ();
202
208
sourceFileName = sourceImg .getName ();
203
209
bufferedSource = ImageIO .read (sourceImg );
204
210
Image sourceResized = ImageUtils .resizeImage (bufferedSource , 80 , 80 );
@@ -225,15 +231,16 @@ public void mouseReleased(MouseEvent arg0){}
225
231
public void actionPerformed (ActionEvent event )
226
232
{
227
233
projectPathChooser = new JFileChooser ();
228
- projectPathChooser .setDialogTitle ("Select you app's project path" );
234
+ if (lastUsedProjectDirectory != null ) {
235
+ projectPathChooser .setCurrentDirectory (lastUsedProjectDirectory );
236
+ }
237
+ projectPathChooser .setDialogTitle ("Project root directory of your app" );
229
238
projectPathChooser .setAcceptAllFileFilterUsed (false );
230
239
projectPathChooser .setFileSelectionMode (JFileChooser .DIRECTORIES_ONLY );
231
- switch (projectPathChooser .showOpenDialog (projectPathButton ))
232
- {
233
- case JFileChooser .APPROVE_OPTION :
234
- projectPathField .setText (projectPathChooser .getSelectedFile ().getPath ());
235
- break ;
236
- }
240
+ if (projectPathChooser .showOpenDialog (projectPathButton ) == JFileChooser .APPROVE_OPTION ) {
241
+ projectPathField .setText (projectPathChooser .getSelectedFile ().getPath ());
242
+ lastUsedProjectDirectory = projectPathChooser .getSelectedFile ();
243
+ }
237
244
}
238
245
});
239
246
//"Make" button action listener
@@ -276,6 +283,11 @@ public void run()
276
283
for (Map .Entry <String , Double > e : densityMap .entrySet ())
277
284
{
278
285
JCheckBox singleDensity = densitiesCheckBox .get (e .getKey ());
286
+ String projectPath = projectPathField .getText ();
287
+ File projectResourceRoot = new File (projectPath );
288
+ if (!"res" .equals (projectResourceRoot .getName ())) {
289
+ projectResourceRoot = new File (projectResourceRoot , "res" );
290
+ }
279
291
if (singleDensity .isSelected ())
280
292
{
281
293
String folderName = "drawable-" + e .getKey ();
@@ -286,10 +298,10 @@ public void run()
286
298
287
299
try {
288
300
Image newImg = ImageUtils .resizeImage (bufferedSource , newWidth , newHeight );
289
- File targetDir = new File (projectPathField . getText ()+ File . separator + folderName );
301
+ File targetDir = new File (projectResourceRoot , folderName );
290
302
boolean dirExists = false ;
291
303
//check if project dir exists, if not create it
292
- dirExists = targetDir .exists () ? true : targetDir .mkdir ();
304
+ dirExists = targetDir .exists () || targetDir .mkdir ();
293
305
if (dirExists )
294
306
{
295
307
BufferedImage bufImg = new BufferedImage (newImg .getWidth (null ), newImg .getHeight (null ), BufferedImage .TYPE_INT_ARGB );
@@ -305,8 +317,13 @@ public void run()
305
317
}
306
318
307
319
}
308
- JOptionPane .showMessageDialog (getContentPane (), "Resize Completed!" , "Completed" , JOptionPane .INFORMATION_MESSAGE );
309
- createButton .setEnabled (true );
320
+ javax .swing .SwingUtilities .invokeLater (new Runnable () {
321
+ @ Override
322
+ public void run () {
323
+ JOptionPane .showMessageDialog (getContentPane (), "Resize Completed!" , "Completed" , JOptionPane .INFORMATION_MESSAGE );
324
+ createButton .setEnabled (true );
325
+ }
326
+ });
310
327
}
311
328
};
312
329
}
0 commit comments