1 56 package org.objectstyle.cayenne.modeler.dialog.datamap; 57 58 import java.util.Iterator ; 59 60 import org.objectstyle.cayenne.map.DataMap; 61 import org.objectstyle.cayenne.map.ObjEntity; 62 import org.objectstyle.cayenne.map.event.EntityEvent; 63 import org.objectstyle.cayenne.modeler.ProjectController; 64 import org.objectstyle.cayenne.util.Util; 65 import org.scopemvc.core.Control; 66 import org.scopemvc.core.ControlException; 67 import org.scopemvc.view.swing.SPanel; 68 69 72 public class PackageUpdateController extends DefaultsPreferencesController { 73 74 public static final String ALL_CONTROL = "cayenne.modeler.datamap.defaultprefs.package.radio"; 75 public static final String UNINIT_CONTROL = "cayenne.modeler.datamap.defaultprefs.packagenull.radio"; 76 77 public PackageUpdateController(ProjectController mediator, DataMap dataMap) { 78 super(mediator, dataMap); 79 } 80 81 84 public void startup() { 85 SPanel view = new DefaultsPreferencesDialog(ALL_CONTROL, UNINIT_CONTROL); 86 view.setTitle("Update ObjEntities Java Package"); 87 setView(view); 88 super.startup(); 89 } 90 91 protected void doHandleControl(Control control) throws ControlException { 92 if (control.matchesID(UPDATE_CONTROL)) { 93 updatePackage(); 94 } 95 else { 96 super.doHandleControl(control); 97 } 98 } 99 100 protected void updatePackage() { 101 boolean doAll = ((DefaultsPreferencesModel) getModel()).isAllEntities(); 102 String defaultPackage = dataMap.getDefaultPackage(); 103 if (Util.isEmptyString(defaultPackage)) { 104 defaultPackage = ""; 105 } 106 else if (!defaultPackage.endsWith(".")) { 107 defaultPackage = defaultPackage + '.'; 108 } 109 110 Iterator it = dataMap.getObjEntities().iterator(); 111 while (it.hasNext()) { 112 ObjEntity entity = (ObjEntity) it.next(); 113 if (doAll || Util.isEmptyString(entity.getClassName())) { 114 String oldName = entity.getClassName(); 115 String className = extractClassName(Util.isEmptyString(oldName) ? entity 116 .getName() : oldName); 117 String newName = defaultPackage + className; 118 119 if (!Util.nullSafeEquals(newName, oldName)) { 120 entity.setClassName(newName); 121 122 mediator.fireDbEntityEvent(new EntityEvent(this, entity)); 125 } 126 } 127 } 128 129 shutdown(); 130 } 131 132 protected String extractClassName(String name) { 133 if (name == null) { 134 return ""; 135 } 136 137 int dot = name.lastIndexOf('.'); 138 return (dot < 0) ? name : (dot + 1 < name.length()) 139 ? name.substring(dot + 1) 140 : ""; 141 } 142 } | Popular Tags |