You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by ol...@apache.org on 2010/02/11 15:43:31 UTC

svn commit: r909003 - in /cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne: modeler/action/SaveAsAction.java pref/CayennePreferenceForProject.java

Author: oltka
Date: Thu Feb 11 14:43:27 2010
New Revision: 909003

URL: http://svn.apache.org/viewvc?rev=909003&view=rev
Log:
CAY-1327 Migrate HSQLDB modeler preferences to Java preferences API

* when save Project as "Save as" need to save preference for new path to project

Modified:
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/SaveAsAction.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/pref/CayennePreferenceForProject.java

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/SaveAsAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/SaveAsAction.java?rev=909003&r1=909002&r2=909003&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/SaveAsAction.java (original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/action/SaveAsAction.java Thu Feb 11 14:43:27 2010
@@ -25,6 +25,7 @@
 import java.io.File;
 import java.net.URL;
 import java.util.List;
+import java.util.prefs.Preferences;
 
 import javax.swing.JOptionPane;
 import javax.swing.KeyStroke;
@@ -79,7 +80,7 @@
         Project p = getCurrentProject();
 
         String oldPath = null;
-        if(p.getConfigurationResource()!=null){
+        if (p.getConfigurationResource() != null) {
             oldPath = p.getConfigurationResource().getURL().getPath();
         }
 
@@ -114,18 +115,29 @@
 
         // update preferences domain key
         preference.rename(projectDir.getPath());
-        
-        CayennePreferenceForProject.removeOldPreferences();
 
-        if (oldPath != null && oldPath.length() != 0) {
-            getApplication().getFrameController().changePathInLastProjListAction(
-                    oldPath,
-                    p.getConfigurationResource().getURL().getPath());
-        }
-        else {
-            getApplication().getFrameController().addToLastProjListAction(
-                    p.getConfigurationResource().getURL().getPath());
+        if (oldPath != null
+                && oldPath.length() != 0
+                && !oldPath.equals(p.getConfigurationResource().getURL().getPath())) {
+
+            String newName = p.getConfigurationResource().getURL().getPath().replace(
+                    ".xml",
+                    "");
+            String oldName = oldPath.replace(".xml", "");
+            
+            Preferences oldPref = getProjectController().getPreferenceForProject();
+            String projPath = oldPref.absolutePath().replace(oldName, "");
+            Preferences newPref = getProjectController().getPreferenceForProject().node(
+                    projPath + newName);
+            CayennePreferenceForProject.copyPreferences(newPref, getProjectController()
+                    .getPreferenceForProject(), false);
         }
+
+        CayennePreferenceForProject.removeNewPreferences();
+
+        getApplication().getFrameController().addToLastProjListAction(
+                p.getConfigurationResource().getURL().getPath());
+
         Application.getFrame().fireRecentFileListChanged();
 
         /**

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/pref/CayennePreferenceForProject.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/pref/CayennePreferenceForProject.java?rev=909003&r1=909002&r2=909003&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/pref/CayennePreferenceForProject.java (original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/pref/CayennePreferenceForProject.java Thu Feb 11 14:43:27 2010
@@ -28,7 +28,7 @@
 
 public abstract class CayennePreferenceForProject extends CayennePreference {
 
-    private static List<Preferences> newNode; 
+    private static List<Preferences> newNode;
     private static List<Preferences> oldNode;
 
     public CayennePreferenceForProject(Preferences pref) {
@@ -41,57 +41,15 @@
 
     public static Preferences copyPreferences(String newName, Preferences oldPref) {
 
-        try {
-            
-            // copy all preferences in this node
-            String[] names = oldPref.keys();
-            Preferences parent = oldPref.parent();
-            Preferences newPref = parent.node(newName);
-            for (int i = 0; i < names.length; i++) {
-                newPref.put(names[i], oldPref.get(names[i], ""));
-            }
-
-            String oldPath = oldPref.absolutePath();
-            String newPath = newPref.absolutePath();
-
-            // copy children nodes and its preferences
-            ArrayList<Preferences> childrenOldPref = childrenCopy(oldPref, oldPath, newPath);
-
-            while (childrenOldPref.size() > 0) {
-
-                ArrayList<Preferences> childrenPrefTemp = new ArrayList<Preferences>();
-
-                Iterator<Preferences> it = childrenOldPref.iterator();
-                while (it.hasNext()) {
-                    Preferences child = it.next();
-                    ArrayList<Preferences> childArray = childrenCopy(child, oldPath, newPath);
-
-                    childrenPrefTemp.addAll(childArray);
-                }
-
-                childrenOldPref.clear();
-                childrenOldPref.addAll(childrenPrefTemp);
-            }
-
-            if (newNode == null) {
-                newNode = new ArrayList<Preferences>();
-            }
-            if (oldNode == null) {
-                oldNode = new ArrayList<Preferences>();
-            }
-
-            newNode.add(newPref);
-            oldNode.add(oldPref);
-            
-            return newPref;
-        }
-        catch (BackingStoreException e) {
-            new CayenneRuntimeException("Error remane preferences");
-        }
-        return oldPref;
+        Preferences parent = oldPref.parent();
+        Preferences newPref = parent.node(newName);
+        return copyPreferences(newPref, oldPref, true);
     }
-      
-    private static ArrayList<Preferences> childrenCopy(Preferences pref, String oldPath, String newPath) {
+
+    private static ArrayList<Preferences> childrenCopy(
+            Preferences pref,
+            String oldPath,
+            String newPath) {
 
         try {
             String[] children = pref.childrenNames();
@@ -102,17 +60,19 @@
                 String child = children[j];
                 // get old preference
                 Preferences childNode = pref.node(child);
-                
-                // path to node
-                String path = childNode.absolutePath().replace(oldPath, newPath);
-                
-                // copy all preferences in this node
-                String[] names = childNode.keys();
-                Preferences newPref = Preferences.userRoot().node(path);
-                for (int i = 0; i < names.length; i++) {
-                    newPref.put(names[i], childNode.get(names[i], ""));
+
+                if (!oldNode.contains(childNode)) {
+                    // path to node
+                    String path = childNode.absolutePath().replace(oldPath, newPath);
+
+                    // copy all preferences in this node
+                    String[] names = childNode.keys();
+                    Preferences newPref = Preferences.userRoot().node(path);
+                    for (int i = 0; i < names.length; i++) {
+                        newPref.put(names[i], childNode.get(names[i], ""));
+                    }
+                    prefChild.add(childNode);
                 }
-                prefChild.add(childNode);
             }
 
             return prefChild;
@@ -134,8 +94,7 @@
                 catch (BackingStoreException e) {
                 }
             }
-            oldNode.clear();
-            newNode.clear();
+            clearPreferences();
         }
 
     }
@@ -152,8 +111,74 @@
                 catch (BackingStoreException e) {
                 }
             }
-            oldNode.clear();
-            newNode.clear();
+            clearPreferences();
         }
     }
+
+    public static void clearPreferences() {
+        oldNode.clear();
+        newNode.clear();
+    }
+
+    public static Preferences copyPreferences(
+            Preferences newPref,
+            Preferences oldPref,
+            boolean addToPreferenceList) {
+
+        try {
+            // copy all preferences in this node
+            String[] names = oldPref.keys();
+
+            for (int i = 0; i < names.length; i++) {
+                newPref.put(names[i], oldPref.get(names[i], ""));
+            }
+
+            String oldPath = oldPref.absolutePath();
+            String newPath = newPref.absolutePath();
+
+            // copy children nodes and its preferences
+            ArrayList<Preferences> childrenOldPref = childrenCopy(
+                    oldPref,
+                    oldPath,
+                    newPath);
+
+            while (childrenOldPref.size() > 0) {
+
+                ArrayList<Preferences> childrenPrefTemp = new ArrayList<Preferences>();
+
+                Iterator<Preferences> it = childrenOldPref.iterator();
+                while (it.hasNext()) {
+                    Preferences child = it.next();
+                    ArrayList<Preferences> childArray = childrenCopy(
+                            child,
+                            oldPath,
+                            newPath);
+
+                    childrenPrefTemp.addAll(childArray);
+                }
+
+                childrenOldPref.clear();
+                childrenOldPref.addAll(childrenPrefTemp);
+            }
+
+            if (newNode == null) {
+                newNode = new ArrayList<Preferences>();
+            }
+            if (oldNode == null) {
+                oldNode = new ArrayList<Preferences>();
+            }
+
+            if (addToPreferenceList) {
+                newNode.add(newPref);
+                oldNode.add(oldPref);
+            }
+
+            return newPref;
+        }
+        catch (BackingStoreException e) {
+            new CayenneRuntimeException("Error remane preferences");
+        }
+        return oldPref;
+    }
+
 }