Search in sources :

Example 1 with ProjectSaver

use of org.apache.cayenne.project.ProjectSaver in project cayenne by apache.

the class SaveAction method saveAll.

@Override
protected boolean saveAll() throws Exception {
    Project p = getCurrentProject();
    if (p == null || p.getConfigurationResource() == null) {
        return super.saveAll();
    }
    String oldPath = p.getConfigurationResource().getURL().getPath();
    File oldProjectFile = new File(p.getConfigurationResource().getURL().toURI());
    getProjectController().getFileChangeTracker().pauseWatching();
    ProjectSaver saver = getApplication().getInjector().getInstance(ProjectSaver.class);
    saver.save(p);
    RenamedPreferences.removeOldPreferences();
    // if change DataChanelDescriptor name - as result change name of xml file
    // we will need change preferences path
    String[] path = oldPath.split("/");
    String[] newPath = p.getConfigurationResource().getURL().getPath().split("/");
    if (!path[path.length - 1].equals(newPath[newPath.length - 1])) {
        String newName = newPath[newPath.length - 1].replace(".xml", "");
        RenamedPreferences.copyPreferences(newName, getProjectController().getPreferenceForProject());
        RenamedPreferences.removeOldPreferences();
    }
    File newProjectFile = new File(p.getConfigurationResource().getURL().toURI());
    getApplication().getFrameController().changePathInLastProjListAction(oldProjectFile, newProjectFile);
    Application.getFrame().fireRecentFileListChanged();
    // Reset the watcher now
    getProjectController().getFileChangeTracker().reconfigure();
    return true;
}
Also used : Project(org.apache.cayenne.project.Project) ProjectSaver(org.apache.cayenne.project.ProjectSaver) File(java.io.File)

Example 2 with ProjectSaver

use of org.apache.cayenne.project.ProjectSaver in project cayenne by apache.

the class SaveAsAction method saveAll.

/**
 * Saves project and related files. Saving is done to temporary files, and
 * only on successful save, master files are replaced with new versions.
 */
protected boolean saveAll() throws Exception {
    Project p = getCurrentProject();
    String oldPath = null;
    if (p.getConfigurationResource() != null) {
        oldPath = p.getConfigurationResource().getURL().getPath();
    }
    File projectDir = fileChooser.newProjectDir(Application.getFrame(), p);
    if (projectDir == null) {
        return false;
    }
    if (projectDir.exists() && !projectDir.canWrite()) {
        JOptionPane.showMessageDialog(Application.getFrame(), "Can't save project - unable to write to file \"" + projectDir.getPath() + "\"", "Can't Save Project", JOptionPane.OK_OPTION);
        return false;
    }
    getProjectController().getFileChangeTracker().pauseWatching();
    URLResource res = new URLResource(projectDir.toURI().toURL());
    ProjectSaver saver = getApplication().getInjector().getInstance(ProjectSaver.class);
    boolean isNewProject = p.getConfigurationResource() == null;
    Preferences tempOldPref = null;
    if (isNewProject) {
        tempOldPref = getApplication().getMainPreferenceForProject();
    }
    saver.saveAs(p, res);
    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);
        RenamedPreferences.copyPreferences(newPref, getProjectController().getPreferenceForProject(), false);
    } else if (isNewProject) {
        if (tempOldPref != null) {
            String newProjectName = getApplication().getNewProjectTemporaryName();
            if (tempOldPref.absolutePath().contains(newProjectName)) {
                String projPath = tempOldPref.absolutePath().replace("/" + newProjectName, "");
                String newName = p.getConfigurationResource().getURL().getPath().replace(".xml", "");
                Preferences newPref = getApplication().getMainPreferenceForProject().node(projPath + newName);
                RenamedPreferences.copyPreferences(newPref, tempOldPref, false);
                tempOldPref.removeNode();
            }
        }
    }
    RenamedPreferences.removeNewPreferences();
    File file = new File(p.getConfigurationResource().getURL().toURI());
    getApplication().getFrameController().addToLastProjListAction(file);
    Application.getFrame().fireRecentFileListChanged();
    // Reset the watcher now
    getProjectController().getFileChangeTracker().reconfigure();
    return true;
}
Also used : Project(org.apache.cayenne.project.Project) URLResource(org.apache.cayenne.resource.URLResource) ProjectSaver(org.apache.cayenne.project.ProjectSaver) Preferences(java.util.prefs.Preferences) RenamedPreferences(org.apache.cayenne.pref.RenamedPreferences) File(java.io.File)

Aggregations

File (java.io.File)2 Project (org.apache.cayenne.project.Project)2 ProjectSaver (org.apache.cayenne.project.ProjectSaver)2 Preferences (java.util.prefs.Preferences)1 RenamedPreferences (org.apache.cayenne.pref.RenamedPreferences)1 URLResource (org.apache.cayenne.resource.URLResource)1