1 56 package org.objectstyle.cayenne.modeler.action; 57 58 import java.awt.Frame ; 59 import java.io.File ; 60 61 import javax.swing.JFileChooser ; 62 import javax.swing.filechooser.FileFilter ; 63 64 import org.apache.log4j.Logger; 65 import org.objectstyle.cayenne.CayenneRuntimeException; 66 import org.objectstyle.cayenne.conf.Configuration; 67 import org.objectstyle.cayenne.modeler.Application; 68 import org.objectstyle.cayenne.modeler.dialog.OverwriteDialog; 69 import org.objectstyle.cayenne.modeler.util.FileFilters; 70 import org.objectstyle.cayenne.project.ApplicationProject; 71 import org.objectstyle.cayenne.project.DataMapProject; 72 import org.objectstyle.cayenne.project.Project; 73 import org.objectstyle.cayenne.project.ProjectFile; 74 75 80 class ProjectOpener extends JFileChooser { 81 82 private static final Logger logObj = Logger.getLogger(ProjectOpener.class); 83 84 87 File newProjectDir(Frame f, Project p) { 88 if (p instanceof ApplicationProject) { 89 return newProjectDir(f, Configuration.DEFAULT_DOMAIN_FILE, FileFilters 91 .getApplicationFilter()); 92 } 93 else if (p instanceof DataMapProject) { 94 ProjectFile projFileWrapper = p.projectFileForObject(p); 96 return newProjectDir(f, projFileWrapper.getLocation(), FileFilters 97 .getDataMapFilter()); 98 } 99 else { 100 String message = (p == null) 101 ? "Null project." 102 : "Unrecognized project class: " + p.getClass().getName(); 103 throw new CayenneRuntimeException(message); 104 } 105 } 106 107 File newProjectDir(Frame f, String location, FileFilter filter) { 108 setDialogTitle("Select Project Directory"); 110 setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 111 setCurrentDirectory(getDefaultStartDir()); 112 113 if (getCurrentDirectory() != null) { 115 setSelectedFile(getCurrentDirectory()); 116 } 117 118 resetChoosableFileFilters(); 120 addChoosableFileFilter(filter); 122 123 File selectedDir; 124 125 while (true) { 126 int status = showDialog(f, "Select"); 127 selectedDir = getSelectedFile(); 128 if (status != JFileChooser.APPROVE_OPTION || selectedDir == null) { 129 logObj.info("Save canceled."); 130 return null; 131 } 132 133 logObj.info("Selected: " + selectedDir); 135 if (!selectedDir.isDirectory()) { 136 selectedDir = getSelectedFile().getParentFile(); 137 } 138 139 File projectFile = new File (selectedDir, location); 141 if (projectFile.exists()) { 142 OverwriteDialog dialog = new OverwriteDialog(projectFile, f); 143 dialog.show(); 144 145 if (dialog.shouldOverwrite()) { 146 break; 147 } 148 else if (dialog.shouldSelectAnother()) { 149 continue; 150 } 151 else { 152 return null; 154 } 155 } 156 else { 157 break; 158 } 159 } 160 161 return selectedDir; 162 } 163 164 167 File openProjectFile(Frame f) { 168 169 setDialogTitle("Select Project File"); 171 setFileSelectionMode(JFileChooser.FILES_ONLY); 172 setCurrentDirectory(getDefaultStartDir()); 173 174 resetChoosableFileFilters(); 176 addChoosableFileFilter(FileFilters.getApplicationFilter()); 177 addChoosableFileFilter(FileFilters.getDataMapFilter()); 178 179 setFileFilter(FileFilters.getApplicationFilter()); 181 182 int status = showOpenDialog(f); 183 if (status != JFileChooser.APPROVE_OPTION) { 184 return null; 185 } 186 187 return getSelectedFile(); 188 } 189 190 194 File getDefaultStartDir() { 195 File existingDir = Application 197 .getInstance() 198 .getFrameController() 199 .getLastDirectory() 200 .getExistingDirectory(false); 201 202 if (existingDir == null) { 203 existingDir = new File (System.getProperty("user.dir")); 205 } 206 207 return existingDir; 208 } 209 } | Popular Tags |