1 56 package org.objectstyle.cayenne.modeler.action; 57 58 import java.awt.event.ActionEvent ; 59 import java.io.File ; 60 61 import org.objectstyle.cayenne.CayenneRuntimeException; 62 import org.objectstyle.cayenne.modeler.Application; 63 import org.objectstyle.cayenne.modeler.dialog.validator.ValidationDisplayHandler; 64 import org.objectstyle.cayenne.modeler.dialog.validator.ValidatorDialog; 65 import org.objectstyle.cayenne.modeler.util.CayenneAction; 66 import org.objectstyle.cayenne.pref.Domain; 67 import org.objectstyle.cayenne.project.Project; 68 import org.objectstyle.cayenne.project.ProjectPath; 69 import org.objectstyle.cayenne.project.validator.Validator; 70 71 76 public class SaveAsAction extends CayenneAction { 77 78 protected ProjectOpener fileChooser; 79 80 public static String getActionName() { 81 return "Save As..."; 82 } 83 84 public SaveAsAction(Application application) { 85 this(getActionName(), application); 86 } 87 88 protected SaveAsAction(String name, Application application) { 89 super(name, application); 90 this.fileChooser = new ProjectOpener(); 91 } 92 93 97 protected boolean saveAll() throws Exception { 98 Project p = getCurrentProject(); 99 100 Domain preference = getProjectController().getPreferenceDomainForProject(); 102 103 if (!chooseDestination(p)) { 104 return false; 105 } 106 107 p.save(); 108 109 preference.rename(p.getMainFile().getAbsolutePath()); 111 112 getApplication().getFrameController().addToLastProjListAction( 113 p.getMainFile().getAbsolutePath()); 114 return true; 115 } 116 117 protected boolean chooseDestination(Project p) { 118 File projectDir = fileChooser.newProjectDir(Application.getFrame(), p); 119 if (projectDir == null) { 120 return false; 121 } 122 123 p.setProjectDirectory(projectDir); 124 return true; 125 } 126 127 130 public synchronized void performAction(ActionEvent e) { 131 performAction(ValidationDisplayHandler.WARNING); 132 } 133 134 public synchronized void performAction(int warningLevel) { 135 Validator val = getCurrentProject().getValidator(); 136 int validationCode = val.validate(); 137 138 if (validationCode < ValidationDisplayHandler.ERROR) { 140 try { 141 if (!saveAll()) { 142 return; 143 } 144 } 145 catch (Exception ex) { 146 throw new CayenneRuntimeException("Error on save", ex); 147 } 148 149 getApplication().getFrameController().projectSavedAction(); 150 } 151 152 if (validationCode >= warningLevel) { 154 ValidatorDialog.showDialog(Application.getFrame(), val); 155 } 156 } 157 158 162 public boolean enableForPath(ProjectPath path) { 163 if (path == null) { 164 return false; 165 } 166 167 Project project = (Project) path.firstInstanceOf(Project.class); 168 return project != null && project.isModified(); 169 } 170 } | Popular Tags |