1 56 package org.objectstyle.cayenne.modeler.action; 57 58 import java.awt.Frame ; 59 import java.awt.event.ActionEvent ; 60 import java.io.File ; 61 62 import javax.swing.JFileChooser ; 63 import javax.swing.JOptionPane ; 64 65 import org.apache.log4j.Logger; 66 import org.objectstyle.cayenne.access.DataDomain; 67 import org.objectstyle.cayenne.map.DataMap; 68 import org.objectstyle.cayenne.map.DataMapException; 69 import org.objectstyle.cayenne.map.MapLoader; 70 import org.objectstyle.cayenne.modeler.Application; 71 import org.objectstyle.cayenne.modeler.pref.FSPath; 72 import org.objectstyle.cayenne.modeler.util.CayenneAction; 73 import org.objectstyle.cayenne.modeler.util.FileFilters; 74 import org.objectstyle.cayenne.project.NamedObjectFactory; 75 import org.objectstyle.cayenne.util.ResourceLocator; 76 77 83 public class ImportDataMapAction extends CayenneAction { 84 85 private static Logger logObj = Logger.getLogger(ImportDataMapAction.class); 86 87 public static String getActionName() { 88 return "Import DataMap"; 89 } 90 91 public ImportDataMapAction(Application application) { 92 super(getActionName(), application); 93 } 94 95 public void performAction(ActionEvent e) { 96 importDataMap(); 97 } 98 99 protected void importDataMap() { 100 File dataMapFile = selectDataMap(Application.getFrame()); 101 if (dataMapFile == null) { 102 return; 103 } 104 105 try { 106 MapLoader mapLoader = new MapLoader() { 108 109 protected ResourceLocator configLocator() { 110 ResourceLocator locator = new ResourceLocator(); 111 locator.setSkipAbsolutePath(false); 112 locator.setSkipClasspath(true); 113 locator.setSkipCurrentDirectory(true); 114 locator.setSkipHomeDirectory(true); 115 return locator; 116 } 117 }; 118 119 DataMap newMap = mapLoader.loadDataMap(dataMapFile.getAbsolutePath()); 120 DataDomain domain = getProjectController().getCurrentDataDomain(); 121 122 if (newMap.getName() != null) { 123 newMap.setName(NamedObjectFactory.createName( 124 DataMap.class, 125 domain, 126 newMap.getName())); 127 } 128 else { 129 newMap.setName(NamedObjectFactory.createName(DataMap.class, domain)); 130 } 131 132 getProjectController().addDataMap(this, newMap); 133 } 134 catch (DataMapException ex) { 135 logObj.info("Error importing DataMap.", ex); 136 JOptionPane.showMessageDialog( 137 Application.getFrame(), 138 "Error reading DataMap: " + ex.getMessage(), 139 "Can't Open DataMap", 140 JOptionPane.OK_OPTION); 141 } 142 } 143 144 protected File selectDataMap(Frame f) { 145 146 FSPath lastDir = getApplication().getFrameController().getLastDirectory(); 148 149 JFileChooser chooser = new JFileChooser (); 151 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 152 lastDir.updateChooser(chooser); 153 154 chooser.addChoosableFileFilter(FileFilters.getDataMapFilter()); 155 156 int status = chooser.showDialog(f, "Select DataMap"); 157 if (status == JFileChooser.APPROVE_OPTION) { 158 File file = chooser.getSelectedFile(); 159 160 lastDir.updateFromChooser(chooser); 162 163 return file; 164 } 165 166 return null; 167 } 168 } | Popular Tags |