1 56 package org.objectstyle.cayenne.modeler; 57 58 import java.awt.Component ; 59 import java.awt.event.WindowAdapter ; 60 import java.awt.event.WindowEvent ; 61 import java.io.File ; 62 import java.util.Iterator ; 63 import java.util.Vector ; 64 65 import javax.swing.JFrame ; 66 67 import org.objectstyle.cayenne.access.DataDomain; 68 import org.objectstyle.cayenne.modeler.action.ExitAction; 69 import org.objectstyle.cayenne.modeler.action.OpenProjectAction; 70 import org.objectstyle.cayenne.modeler.dialog.validator.ValidatorDialog; 71 import org.objectstyle.cayenne.modeler.editor.EditorView; 72 import org.objectstyle.cayenne.modeler.pref.ComponentGeometry; 73 import org.objectstyle.cayenne.modeler.pref.FSPath; 74 import org.objectstyle.cayenne.modeler.util.CayenneController; 75 import org.objectstyle.cayenne.modeler.util.RecentFileMenu; 76 import org.objectstyle.cayenne.pref.Domain; 77 import org.objectstyle.cayenne.project.Project; 78 import org.objectstyle.cayenne.project.validator.Validator; 79 80 85 public class CayenneModelerController extends CayenneController { 86 87 protected ProjectController projectController; 88 protected ActionController actionController; 89 90 protected CayenneModelerFrame frame; 91 protected File initialProject; 92 93 public CayenneModelerController(Application application, File initialProject) { 94 super(application); 95 96 this.initialProject = initialProject; 97 this.frame = new CayenneModelerFrame(this); 98 99 projectController = new ProjectController(this); 100 actionController = new ActionController(application); 101 } 102 103 public Component getView() { 104 return frame; 105 } 106 107 public ProjectController getProjectController() { 108 return projectController; 109 } 110 111 112 113 public FSPath getLastEOModelDirectory() { 114 116 FSPath path = (FSPath) getViewDomain() 117 .getDetail("lastEOMDir", FSPath.class, true); 118 119 if (path.getPath() == null) { 120 path.setPath(getLastDirectory().getPath()); 121 } 122 123 return path; 124 } 125 126 protected void initBindings() { 127 frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 128 129 frame.addWindowListener(new WindowAdapter () { 130 131 public void windowClosing(WindowEvent e) { 132 ((ExitAction) getApplication().getAction(ExitAction.getActionName())) 133 .exit(); 134 } 135 }); 136 137 Domain prefDomain = application.getPreferenceDomain().getSubdomain( 138 frame.getClass()); 139 ComponentGeometry geometry = ComponentGeometry.getPreference(prefDomain); 140 geometry.bind(frame, 650, 550, 30); 141 } 142 143 public void dataDomainSelectedAction(DataDomain domain) { 144 actionController.domainSelected(domain); 145 } 146 147 public void startupAction() { 148 initBindings(); 149 frame.setVisible(true); 150 151 if (initialProject != null) { 153 OpenProjectAction openAction = (OpenProjectAction) getApplication() 154 .getAction(OpenProjectAction.getActionName()); 155 openAction.openProject(initialProject); 156 } 157 } 158 159 public void projectModifiedAction() { 160 String title = (projectController.getProject().isLocationUndefined()) 161 ? "[New]" 162 : projectController.getProject().getMainFile().getAbsolutePath(); 163 164 frame.setTitle("* - " + ModelerConstants.TITLE + " - " + title); 165 } 166 167 public void projectSavedAction() { 168 projectController.setDirty(false); 169 updateStatus("Project saved..."); 170 frame.setTitle(ModelerConstants.TITLE 171 + " - " 172 + projectController.getProject().getMainFile().getAbsolutePath()); 173 } 174 175 178 public void projectClosedAction() { 179 RecentFileMenu recentFileMenu = frame.getRecentFileMenu(); 181 recentFileMenu.rebuildFromPreferences(); 182 recentFileMenu.setEnabled(recentFileMenu.getMenuComponentCount() > 0); 183 184 frame.setView(null); 185 186 frame.repaint(); 189 frame.setTitle(ModelerConstants.TITLE); 190 191 projectController.setProject(null); 192 193 projectController.reset(); 194 actionController.projectClosed(); 195 196 updateStatus("Project Closed..."); 197 } 198 199 203 public void projectOpenedAction(Project project) { 204 205 projectController.setProject(project); 206 207 frame.setView(new EditorView(projectController)); 208 209 projectController.projectOpened(); 210 actionController.projectOpened(); 211 212 if (project.isLocationUndefined()) { 214 updateStatus("New project created..."); 215 frame.setTitle(ModelerConstants.TITLE + "- [New]"); 216 } 217 else { 218 updateStatus("Project opened..."); 219 frame.setTitle(ModelerConstants.TITLE 220 + " - " 221 + project.getMainFile().getAbsolutePath()); 222 } 223 224 if (!project.isLocationUndefined()) { 226 getLastDirectory().setDirectory(project.getProjectDirectory()); 227 } 228 229 if (project.getLoadStatus().hasFailures()) { 231 project.setModified(true); 233 projectController.setDirty(true); 234 235 ValidatorDialog.showDialog(frame, new Validator( 237 project, 238 project.getLoadStatus())); 239 } 240 241 } 242 243 244 public void addToLastProjListAction(String path) { 245 ModelerPreferences pref = ModelerPreferences.getPreferences(); 246 Vector arr = pref.getVector(ModelerPreferences.LAST_PROJ_FILES); 247 if (arr.contains(path)) { 250 arr.remove(path); 251 } 252 253 arr.insertElementAt(path, 0); 254 while (arr.size() > 4) { 255 arr.remove(arr.size() - 1); 256 } 257 258 pref.remove(ModelerPreferences.LAST_PROJ_FILES); 259 Iterator iter = arr.iterator(); 260 while (iter.hasNext()) { 261 pref.addProperty(ModelerPreferences.LAST_PROJ_FILES, iter.next()); 262 } 263 } 264 265 268 public ActionController getActionController() { 269 return actionController; 270 } 271 272 275 public void updateStatus(String message) { 276 frame.getStatus().setText(message); 277 278 if (message != null && message.trim().length() > 0) { 280 Thread cleanup = new ExpireThread(message, 6); 281 cleanup.start(); 282 } 283 } 284 285 class ExpireThread extends Thread { 286 287 protected int seconds; 288 protected String message; 289 290 public ExpireThread(String message, int seconds) { 291 this.seconds = seconds; 292 this.message = message; 293 } 294 295 public void run() { 296 try { 297 sleep(seconds * 1000); 298 } 299 catch (InterruptedException e) { 300 } 302 303 if (message.equals(frame.getStatus().getText())) { 304 updateStatus(null); 305 } 306 } 307 } 308 } | Popular Tags |