This is an automated email from the ASF dual-hosted git repository.

ntimofeev pushed a commit to branch STABLE-4.0
in repository https://gitbox.apache.org/repos/asf/cayenne.git

commit c53c5aeceef30d8d27bf03ae757603385061d3c4
Author: Nikita Timofeev <[email protected]>
AuthorDate: Thu Sep 12 11:37:17 2019 +0300

    CAY-2616 Modeler: Wrong handling of path with spaces
---
 RELEASE-NOTES.txt                                  |   3 +-
 .../cayenne/modeler/CayenneModelerController.java  | 105 +++++++--------------
 .../cayenne/modeler/ProjectFileChangeTracker.java  |  57 ++++++-----
 3 files changed, 68 insertions(+), 97 deletions(-)

diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index da9cb16..3c20f4e 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -1,7 +1,7 @@
 Apache Cayenne Release Notes
 ============================
 
-For the latest information visit project web site: 
+For the latest information visit project web site:
 http://cayenne.apache.org/
 
 To browse individual bug reports check out project issue tracker:
@@ -27,6 +27,7 @@ CAY-2573 DI field injection is triggered when creating sql 
Driver
 CAY-2582 Double insert of manyToMany relationship mapped to Set
 CAY-2584 Crypto: can't use ColumnSelect with encrypted columns
 CAY-2586 Char can't be materialized to EnumType in some cases
+CAY-2616 Modeler: Wrong handling of path with spaces
 
 ----------------------------------
 Release: 4.0.1
diff --git 
a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/CayenneModelerController.java
 
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/CayenneModelerController.java
index 98f0bd4..fadb295 100644
--- 
a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/CayenneModelerController.java
+++ 
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/CayenneModelerController.java
@@ -46,6 +46,7 @@ import java.awt.event.ActionEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.io.File;
+import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -144,8 +145,7 @@ public class CayenneModelerController extends 
CayenneController {
         }
 
         if (fileList != null) {
-
-        File transferFile = fileList.get(0);
+            File transferFile = fileList.get(0);
             if (transferFile.isFile()) {
                 FileFilter filter = FileFilters.getApplicationFilter();
                 if (filter.accept(transferFile)) {
@@ -220,13 +220,14 @@ public class CayenneModelerController extends 
CayenneController {
             frame.setTitle("[New Project]");
         } else {
             updateStatus("Project opened...");
-            
frame.setTitle(project.getConfigurationResource().getURL().getPath());
-        }
-
-        // update preferences
-        if (project.getConfigurationResource() != null) {
-            getLastDirectory().setDirectory(new 
File(project.getConfigurationResource().getURL().getPath()));
-            frame.fireRecentFileListChanged();
+            try {
+                File file = new 
File(project.getConfigurationResource().getURL().toURI());
+                frame.setTitle(file.toString());
+                // update preferences
+                getLastDirectory().setDirectory(file);
+                frame.fireRecentFileListChanged();
+            } catch (URISyntaxException ignore) {
+            }
         }
 
         PROJECT_STATE_UTIL.fireLastState(projectController);
@@ -258,15 +259,11 @@ public class CayenneModelerController extends 
CayenneController {
 
        /** Adds path to the list of last opened projects in preferences. */
     public void addToLastProjListAction(File file) {
-
         Preferences prefLastProjFiles = 
ModelerPreferences.getLastProjFilesPref();
         List<File> arr = ModelerPreferences.getLastProjFiles();
-        // Add proj path to the preferences
-        // Prevent duplicate entries.
-        if (arr.contains(file)) {
-            arr.remove(file);
-        }
 
+        // Add proj path to the preferences
+        arr.remove(file);
         arr.add(0, file);
         while (arr.size() > ModelerPreferences.LAST_PROJ_FILES_SIZE) {
             arr.remove(arr.size() - 1);
@@ -284,68 +281,36 @@ public class CayenneModelerController extends 
CayenneController {
         }
     }
 
+    public void changePathInLastProjListAction(File oldFile, File newFile) {
+        ModelerPreferences.getLastProjFiles().remove(oldFile);
+
+        addToLastProjListAction(newFile);
+
+        getLastDirectory().setDirectory(newFile);
+        frame.fireRecentFileListChanged();
+    }
+
     /**
-     * Performs status bar update with a message. Message will dissappear in 6 
seconds.
+     * Performs status bar update with a message. Message will disappear in 6 
seconds.
      */
-    public void updateStatus(String message) {
+    public void updateStatus(final String message) {
         frame.getStatus().setText(message);
 
         // start message cleanup thread that would remove the message after X 
seconds
         if (message != null && message.trim().length() > 0) {
-            Thread cleanup = new ExpireThread(message, 6);
-            cleanup.start();
-        }
-    }
-
-    class ExpireThread extends Thread {
-
-        int seconds;
-        protected String message;
-
-        ExpireThread(String message, int seconds) {
-            this.seconds = seconds;
-            this.message = message;
-        }
-
-        @Override
-        public void run() {
-            try {
-                sleep(seconds * 1000);
-            } catch (InterruptedException e) {
-                // ignore exception
-            }
-
-            if (message.equals(frame.getStatus().getText())) {
-                updateStatus(null);
-            }
+            new Thread(new Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        Thread.sleep(6 * 10000);
+                    } catch (InterruptedException ignore) {
+                    }
+                    if (message.equals(frame.getStatus().getText())) {
+                        CayenneModelerController.this.updateStatus(null);
+                    }
+                }
+            }).start();
         }
     }
 
-    public void changePathInLastProjListAction(File oldFile, File newFile) {
-        Preferences frefLastProjFiles = 
ModelerPreferences.getLastProjFilesPref();
-        List<File> arr = ModelerPreferences.getLastProjFiles();
-
-        // Add proj path to the preferences
-        arr.remove(oldFile);
-        arr.remove(newFile);
-        arr.add(0, newFile);
-        while (arr.size() > ModelerPreferences.LAST_PROJ_FILES_SIZE) {
-            arr.remove(arr.size() - 1);
-        }
-
-        try {
-            frefLastProjFiles.clear();
-        } catch (BackingStoreException e) {
-            // ignore exception
-        }
-
-        int size = arr.size();
-        for (int i = 0; i < size; i++) {
-            frefLastProjFiles.put(String.valueOf(i), 
arr.get(i).getAbsolutePath());
-        }
-
-        getLastDirectory().setDirectory(newFile);
-        frame.fireRecentFileListChanged();
-    }
-       
 }
diff --git 
a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ProjectFileChangeTracker.java
 
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ProjectFileChangeTracker.java
index 99866d2..5889ec3 100644
--- 
a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ProjectFileChangeTracker.java
+++ 
b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ProjectFileChangeTracker.java
@@ -19,6 +19,8 @@
 package org.apache.cayenne.modeler;
 
 import java.io.File;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -26,6 +28,7 @@ import java.util.concurrent.ConcurrentHashMap;
 import javax.swing.JOptionPane;
 import javax.swing.SwingUtilities;
 
+import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.configuration.DataChannelDescriptor;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.modeler.action.OpenProjectAction;
@@ -42,7 +45,7 @@ import org.slf4j.LoggerFactory;
  */
 public class ProjectFileChangeTracker extends Thread {
 
-    private static final Logger log = 
LoggerFactory.getLogger(ProjectFileChangeTracker.class);
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(ProjectFileChangeTracker.class);
 
     /**
      * The default delay between every file modification check
@@ -52,16 +55,16 @@ public class ProjectFileChangeTracker extends Thread {
     /**
      * The names of the files to observe for changes.
      */
-    protected Map<String, FileInfo> files;
+    protected final Map<URI, FileInfo> files;
+    protected final ProjectController mediator;
+
     protected boolean paused;
     protected boolean isShownChangeDialog;
     protected boolean isShownRemoveDialog;
-    protected ProjectController mediator;
-    public ProjectFileChangeTracker(ProjectController mediator) {
 
+    public ProjectFileChangeTracker(ProjectController mediator) {
         this.files = new ConcurrentHashMap<>();
         this.mediator = mediator;
-
         setName("cayenne-modeler-file-change-tracker");
     }
 
@@ -78,15 +81,18 @@ public class ProjectFileChangeTracker extends Thread {
 
         // check if project exists and has been saved at least once.
         if (project != null && project.getConfigurationResource() != null) {
-            String projectPath = 
project.getConfigurationResource().getURL().getPath() + File.separator;
-            addFile(projectPath);
+            try {
+                addFile(project.getConfigurationResource().getURL().toURI());
 
-            Iterator<DataMap> it = ((DataChannelDescriptor) 
project.getRootNode()).getDataMaps().iterator();
-            while (it.hasNext()) {
-                DataMap dm = it.next();
-                addFile(dm.getConfigurationSource().getURL().getPath());
+                for (DataMap dm : ((DataChannelDescriptor) 
project.getRootNode()).getDataMaps()) {
+                    if (dm.getConfigurationSource() != null) {
+                        // if DataMap is in separate file, monitor it
+                        addFile(dm.getConfigurationSource().getURL().toURI());
+                    }
+                }
+            } catch (URISyntaxException ex) {
+                throw new CayenneRuntimeException("Unable to start change 
tracker", ex);
             }
-
         }
 
         resumeWatching();
@@ -154,11 +160,11 @@ public class ProjectFileChangeTracker extends Thread {
      * @param location
      *            path of file
      */
-    public void addFile(String location) {
+    public void addFile(URI location) {
         try {
             files.put(location, new FileInfo(location));
         } catch (SecurityException e) {
-            log.error("SecurityException adding file " + location, e);
+            LOGGER.error("SecurityException adding file " + location, e);
         }
     }
 
@@ -169,7 +175,7 @@ public class ProjectFileChangeTracker extends Thread {
      *            path of file
      */
     public void removeFile(String location) {
-        files.remove(location);
+        files.remove(URI.create(location));
     }
 
     /**
@@ -194,7 +200,7 @@ public class ProjectFileChangeTracker extends Thread {
             try {
                 fileExists = fi.getFile().exists();
             } catch (SecurityException e) {
-                log.error("SecurityException checking file " + 
fi.getFile().getPath(), e);
+                LOGGER.error("SecurityException checking file " + 
fi.getFile().getPath(), e);
 
                 // we still process with other files
                 continue;
@@ -208,9 +214,8 @@ public class ProjectFileChangeTracker extends Thread {
                     fi.setLastModified(l);
                     hasChanges = true;
                 }
-            }
-            // the file has been removed
-            else if (fi.getLastModified() != -1) {
+            } else if (fi.getLastModified() != -1) {
+                // the file has been removed
                 hasDeletions = true;
                 it.remove(); // no point to watch the file now
             }
@@ -254,17 +259,17 @@ public class ProjectFileChangeTracker extends Thread {
      * Class to store information about files (last modification time & File
      * pointer)
      */
-    protected class FileInfo {
+    protected static class FileInfo {
 
         /**
          * Exact java.io.File object, may not be null
          */
-        File file;
+        private final File file;
 
         /**
          * Time the file was modified
          */
-        long lastModified;
+        private long lastModified;
 
         /**
          * Creates new object
@@ -272,20 +277,20 @@ public class ProjectFileChangeTracker extends Thread {
          * @param location
          *            the file path
          */
-        public FileInfo(String location) {
+        protected FileInfo(URI location) {
             file = new File(location);
             lastModified = file.exists() ? file.lastModified() : -1;
         }
 
-        public File getFile() {
+        protected File getFile() {
             return file;
         }
 
-        public long getLastModified() {
+        protected long getLastModified() {
             return lastModified;
         }
 
-        public void setLastModified(long l) {
+        protected void setLastModified(long l) {
             lastModified = l;
         }
     }

Reply via email to