1 23 24 package org.objectweb.fractal.gui.menu.control; 25 26 import org.objectweb.fractal.api.control.BindingController; 27 28 import org.objectweb.fractal.gui.model.Configuration; 29 import org.objectweb.fractal.gui.model.Component; 30 import org.objectweb.fractal.gui.repository.api.Repository; 31 import org.objectweb.fractal.gui.repository.api.Storage; 32 import org.objectweb.fractal.gui.graph.model.GraphModel; 33 import org.objectweb.fractal.gui.UserData; 34 import org.objectweb.fractal.swing.AbstractAction; 35 36 import org.objectweb.fractal.gui.model.Interface; 37 38 import java.net.URL ; 39 import java.awt.event.ActionEvent ; 40 import java.io.File ; 41 import java.util.List ; 42 import java.util.HashMap ; 43 44 import javax.swing.ImageIcon ; 45 import javax.swing.KeyStroke ; 46 import javax.swing.JOptionPane ; 47 48 51 52 public class SaveAction extends AbstractAction implements 53 BindingController 54 { 55 56 static String LS = new String (System.getProperty("line.separator")); 57 private static int OK = 0; 58 private static int WARNING = 1; 59 private static int ERROR = 2; 60 private HashMap hmt = new HashMap (); 61 62 66 67 public final static String CONFIGURATION_BINDING = "configuration"; 68 69 74 75 public final static String GRAPH_BINDING = "graph"; 76 77 81 82 public final static String REPOSITORY_BINDING = "repository"; 83 84 88 89 public final static String STORAGE_BINDING = "storage"; 90 91 96 97 public final static String USER_DATA_BINDING = "user-data"; 98 99 102 103 private Configuration configuration; 104 105 108 109 private GraphModel graph; 110 111 114 115 private Repository repository; 116 117 120 121 private Storage storage; 122 123 126 127 private UserData userData; 128 129 132 133 public SaveAction () { 134 putValue(NAME, "Save"); 135 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control S")); 136 putValue(SHORT_DESCRIPTION, "Save"); 137 URL url = getClass().getResource( 138 "/org/objectweb/fractal/gui/resources/filesave.gif"); 139 putValue(SMALL_ICON, new ImageIcon (url)); 140 } 141 142 146 147 public String [] listFc () { 148 return new String [] { 149 CONFIGURATION_BINDING, 150 GRAPH_BINDING, 151 REPOSITORY_BINDING, 152 STORAGE_BINDING, 153 USER_DATA_BINDING 154 }; 155 } 156 157 public Object lookupFc (final String clientItfName) { 158 if (CONFIGURATION_BINDING.equals(clientItfName)) { 159 return configuration; 160 } else if (GRAPH_BINDING.equals(clientItfName)) { 161 return graph; 162 } else if (REPOSITORY_BINDING.equals(clientItfName)) { 163 return repository; 164 } else if (STORAGE_BINDING.equals(clientItfName)) { 165 return storage; 166 } else if (USER_DATA_BINDING.equals(clientItfName)) { 167 return userData; 168 } 169 return null; 170 } 171 172 public void bindFc ( 173 final String clientItfName, 174 final Object serverItf) 175 { 176 if (CONFIGURATION_BINDING.equals(clientItfName)) { 177 configuration = (Configuration)serverItf; 178 } else if (GRAPH_BINDING.equals(clientItfName)) { 179 graph = (GraphModel)serverItf; 180 } else if (REPOSITORY_BINDING.equals(clientItfName)) { 181 repository = (Repository)serverItf; 182 } else if (STORAGE_BINDING.equals(clientItfName)) { 183 storage = (Storage)serverItf; 184 } else if (USER_DATA_BINDING.equals(clientItfName)) { 185 userData = (UserData)serverItf; 186 } 187 } 188 189 public void unbindFc (final String clientItfName) { 190 if (CONFIGURATION_BINDING.equals(clientItfName)) { 191 configuration = null; 192 } else if (GRAPH_BINDING.equals(clientItfName)) { 193 graph = null; 194 } else if (REPOSITORY_BINDING.equals(clientItfName)) { 195 repository = null; 196 } else if (STORAGE_BINDING.equals(clientItfName)) { 197 storage = null; 198 } else if (USER_DATA_BINDING.equals(clientItfName)) { 199 userData = null; 200 } 201 } 202 203 207 public void actionPerformed (final ActionEvent e) { 208 try { 209 File storage = null; 210 if (configuration.getStorage() != null) { 211 storage = new File (configuration.getStorage()); 212 if (!storage.exists() || !storage.isDirectory()) { 213 storage = null; 214 } 215 } 216 if (storage == null) { 217 JOptionPane.showMessageDialog( 218 null, 219 "A storage directory must be selected before files can be saved", 220 "Error", 221 JOptionPane.ERROR_MESSAGE); 222 } 223 224 int ind = verify (configuration.getRootComponent()); 226 227 if (ind == WARNING) { 228 Object [] options = { "Yes", "No" }; 229 int n = JOptionPane.showOptionDialog (null, 230 "Configuration not completed\nDo you want to save it anyway ?", "Warning", 231 JOptionPane.YES_NO_CANCEL_OPTION, 232 JOptionPane.QUESTION_MESSAGE, null, 233 options, 234 options[0]); 235 if (n == 1) return; 236 } 237 else if (ind == ERROR) return; 238 239 273 274 try { 275 this.storage.open(storage.getAbsolutePath()); 276 Component c = configuration.getRootComponent(); 277 String root = repository.storeComponent(c, graph, null); 278 configuration.setChangeCount(0); 279 } finally { 280 try { 281 this.storage.close(); 282 } catch (Exception ex) { 283 JOptionPane.showMessageDialog (null, 284 ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 285 } 286 } 287 } catch (Exception ignored) { 288 ignored.printStackTrace(); 289 } 290 } 291 292 294 private int verify (Component c) { 295 List subComponents = c.getSubComponents(); 296 int avert = 0; 297 HashMap hm = new HashMap (); 298 HashMap hmn = new HashMap (); 299 300 for (int i = 0; i < subComponents.size(); i++) { 301 Component subC = (Component)subComponents.get(i); 302 if (subC.getStatus() != Component.OK) avert = WARNING; 303 304 if (hmn.get(subC.getName()) != null) { 305 JOptionPane.showMessageDialog (null, 306 "Error in configuration : two components\nwith the same name ("+subC.getName() 307 +")\nin component '"+c.getName()+"'", "Error", 308 JOptionPane.ERROR_MESSAGE); 309 return ERROR; 310 } 311 else hmn.put(subC.getName(), "z"); 312 313 String typ = subC.getType(); 314 if ((typ != null) && (typ.length() > 0)) { 315 String cpsit = (String )hmt.get(typ); 316 if (cpsit != null) { 317 327 } 328 else hmt.put(typ, (subC.isComposite() ? "c" : "p")); 329 } 330 hm.clear(); 331 332 List itfc = subC.getClientInterfaces(); 333 for (int j = 0; j < itfc.size(); ++j) { 334 Interface itf = (Interface)itfc.get(j); 335 if (itf.getName().length() < 1) continue; 337 if (itf.getStatus() != Interface.OK) avert = WARNING; 338 if (hm.get(itf.getName()) != null) { 339 JOptionPane.showMessageDialog (null, 340 " Error in configuration : two client\ninterfaces with the same name ("+itf.getName() 341 +")\n in component '"+subC.getName()+"'", "Error", 342 JOptionPane.ERROR_MESSAGE); 343 return ERROR; 344 } 345 else hm.put(itf.getName(), "z"); 346 347 } 348 hm.clear(); 349 350 List itfs = subC.getServerInterfaces(); 351 for (int k = 0; k < itfs.size(); ++k) { 352 Interface itf = (Interface)itfs.get(k); 353 if (itf.getName().length() < 1) continue; 355 if (itf.getStatus() != Interface.OK) avert = WARNING; 356 if (hm.get(itf.getName()) != null) { 357 JOptionPane.showMessageDialog (null, 358 " Error in configuration : two server\ninterfaces with the same name ("+itf.getName() 359 +")\nin component '"+subC.getName()+"'", "Error", 360 JOptionPane.ERROR_MESSAGE); 361 return ERROR; 362 } 363 else hm.put(itf.getName(), "z"); 364 } 365 366 int ind = verify (subC); 367 if (ind == ERROR) return ind; 368 } 369 hmn.clear(); 370 return avert; 371 } 372 } 373 | Popular Tags |