1 23 24 package org.objectweb.fractal.gui.menu.control; 25 26 import org.objectweb.fractal.api.control.LifeCycleController; 27 import org.objectweb.fractal.api.control.BindingController; 28 29 import org.objectweb.fractal.gui.model.Configuration; 30 import org.objectweb.fractal.gui.model.Component; 31 import org.objectweb.fractal.gui.graph.model.GraphModel; 32 import org.objectweb.fractal.gui.repository.api.Repository; 33 import org.objectweb.fractal.gui.repository.api.Storage; 34 import org.objectweb.fractal.gui.selection.model.Selection; 35 import org.objectweb.fractal.gui.UserData; 36 import org.objectweb.fractal.swing.WaitGlassPane; 37 import org.objectweb.fractal.swing.AbstractAction; 38 39 import java.net.URL ; 40 import java.awt.event.ActionEvent ; 41 import java.io.File ; 42 43 import javax.swing.ImageIcon ; 44 import javax.swing.JFileChooser ; 45 import javax.swing.KeyStroke ; 46 import javax.swing.JComponent ; 47 import javax.swing.JOptionPane ; 48 import javax.swing.Action ; 49 import javax.swing.JRootPane ; 50 import javax.swing.JPopupMenu ; 51 import javax.swing.JMenuItem ; 52 53 56 57 public class OpenAction extends AbstractAction implements 58 BindingController, LifeCycleController 59 { 60 61 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 SELECTION_BINDING = "selection"; 83 84 88 89 public final static String REPOSITORY_BINDING = "repository"; 90 91 95 96 public final static String STORAGE_BINDING = "storage"; 97 98 103 104 public final static String USER_DATA_BINDING = "user-data"; 105 106 111 112 public final static String SAVE_ACTION_BINDING = "save-action"; 113 114 117 118 private Configuration configuration; 119 120 123 124 private GraphModel graph; 125 126 129 130 private Selection selection; 131 132 135 136 private Repository repository; 137 138 141 142 private Storage storage; 143 144 147 148 private UserData userData; 149 150 153 154 private Action save; 155 156 159 160 public OpenAction () { 161 putValue(NAME, "Open"); 162 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control O")); 163 putValue(SHORT_DESCRIPTION, "Open"); 164 URL url = getClass().getResource( 165 "/org/objectweb/fractal/gui/resources/fileopen.gif"); 166 putValue(SMALL_ICON, new ImageIcon (url)); 167 } 168 169 173 public String [] listFc () { 174 return new String [] { 175 CONFIGURATION_BINDING, 176 GRAPH_BINDING, 177 SELECTION_BINDING, 178 REPOSITORY_BINDING, 179 STORAGE_BINDING, 180 USER_DATA_BINDING, 181 SAVE_ACTION_BINDING 182 }; 183 } 184 185 public Object lookupFc (final String clientItfName) { 186 if (CONFIGURATION_BINDING.equals(clientItfName)) { 187 return configuration; 188 } else if (GRAPH_BINDING.equals(clientItfName)) { 189 return graph; 190 } else if (SELECTION_BINDING.equals(clientItfName)) { 191 return selection; 192 } else if (REPOSITORY_BINDING.equals(clientItfName)) { 193 return repository; 194 } else if (STORAGE_BINDING.equals(clientItfName)) { 195 return storage; 196 } else if (USER_DATA_BINDING.equals(clientItfName)) { 197 return userData; 198 } else if (SAVE_ACTION_BINDING.equals(clientItfName)) { 199 return save; 200 } 201 return null; 202 } 203 204 public void bindFc ( 205 final String clientItfName, 206 final Object serverItf) 207 { 208 if (CONFIGURATION_BINDING.equals(clientItfName)) { 209 configuration = (Configuration)serverItf; 210 } else if (GRAPH_BINDING.equals(clientItfName)) { 211 graph = (GraphModel)serverItf; 212 } else if (SELECTION_BINDING.equals(clientItfName)) { 213 selection = (Selection)serverItf; 214 } else if (REPOSITORY_BINDING.equals(clientItfName)) { 215 repository = (Repository)serverItf; 216 } else if (STORAGE_BINDING.equals(clientItfName)) { 217 storage = (Storage)serverItf; 218 } else if (USER_DATA_BINDING.equals(clientItfName)) { 219 userData = (UserData)serverItf; 220 } else if (SAVE_ACTION_BINDING.equals(clientItfName)) { 221 save = (Action)serverItf; 222 } 223 } 224 225 public void unbindFc (final String clientItfName) { 226 if (CONFIGURATION_BINDING.equals(clientItfName)) { 227 configuration = null; 228 } else if (GRAPH_BINDING.equals(clientItfName)) { 229 graph = null; 230 } else if (SELECTION_BINDING.equals(clientItfName)) { 231 selection = null; 232 } else if (REPOSITORY_BINDING.equals(clientItfName)) { 233 repository = null; 234 } else if (STORAGE_BINDING.equals(clientItfName)) { 235 storage = null; 236 } else if (USER_DATA_BINDING.equals(clientItfName)) { 237 userData = null; 238 } else if (SAVE_ACTION_BINDING.equals(clientItfName)) { 239 save = null; 240 } 241 } 242 243 247 public String getFcState () { 248 return null; 249 } 250 251 public void startFc () { 252 288 } 289 290 public void stopFc () { 291 } 292 293 297 public void actionPerformed (final ActionEvent e) { 298 try { 299 File storage = null; 300 if (configuration.getStorage() != null) { 301 storage = new File (configuration.getStorage()); 302 if (!storage.exists() || !storage.isDirectory()) { 303 storage = null; 304 } 305 } 306 if (storage == null) { 307 JOptionPane.showMessageDialog( 308 null, 309 "A storage directory must be selected before files can be opened", 310 "Error", 311 JOptionPane.ERROR_MESSAGE); 312 return; 313 } 314 315 if (configuration.getChangeCount() > 0) { 316 Object [] options = {"Yes", "No", "Cancel" }; 317 int n = JOptionPane.showOptionDialog( 318 null, 319 "Do you want to save the current configuration " + 320 "before opening a new one ?", 321 "Warning", 322 JOptionPane.YES_NO_CANCEL_OPTION, 323 JOptionPane.QUESTION_MESSAGE, 324 null, 325 options, 326 options[0]); 327 if (n == 0) { 328 save.actionPerformed(e); 329 } 330 else if (n == 2) return; 331 } 332 JFileChooser fileChooser = new JFileChooser (); 333 String dir = null; 334 if (userData != null) { 335 dir = userData.getStringData(UserData.LAST_OPEN_DIR); 336 } 337 fileChooser.setCurrentDirectory(dir == null ? storage : new File (dir)); 338 String file = userData.getStringData(UserData.LAST_OPEN_FILE); 339 if (file != null) { 340 fileChooser.setSelectedFile(new File (file)); 341 } 342 fileChooser.addChoosableFileFilter( 343 new SimpleFileFilter("fractal", "Fractal ADL files")); 344 if (fileChooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) { 345 return; 346 } 347 File f = fileChooser.getSelectedFile(); 348 349 File p = f; 350 String name = f.getName().substring(0, f.getName().indexOf('.')); 351 while (p.getParentFile() != null && !p.getParentFile().equals(storage)) { 352 name = p.getParentFile().getName() + "." + name; 353 p = p.getParentFile(); 354 } 355 if (!storage.equals(p.getParentFile())) { 356 JOptionPane.showMessageDialog( 357 null, 358 "Cannot open a file which is not in the storage directory. " + 359 "Change the storage directory first.", 360 "Error", 361 JOptionPane.ERROR_MESSAGE); 362 return; 363 } 364 365 if (userData != null) { 366 userData.setStringData(UserData.LAST_OPEN_FILE, f.getAbsolutePath()); 367 userData.setStringData(UserData.LAST_OPEN_DIR, f.getParent()); 368 userData.setStringData(UserData.LAST_OPEN_CONF, name); 369 userData.save(); 370 } 371 372 new Thread (new Open(e, storage, name)).start(); 373 } catch (Exception ignored) { 374 ignored.printStackTrace(); 375 } 376 } 377 378 383 384 class Open implements Runnable { 385 386 389 390 private JRootPane rootPane; 391 392 395 396 private File storage; 397 398 401 402 private String name; 403 404 409 410 public Open (final ActionEvent e, final File storage, final String name) { 411 JComponent src = (JComponent )e.getSource(); 412 if (src instanceof JMenuItem ) { 413 src = (JComponent )src.getParent(); 414 } 415 if (src instanceof JPopupMenu ) { 416 src = (JComponent )((JPopupMenu )src).getInvoker(); 417 } 418 this.rootPane = src.getRootPane(); 419 this.storage = storage; 420 this.name = name; 421 } 422 423 public void run () { 424 java.awt.Component glassPane = rootPane.getGlassPane(); 425 rootPane.setGlassPane(new WaitGlassPane()); 426 rootPane.getGlassPane().setVisible(true); 427 428 try { 429 OpenAction.this.storage.open(storage.getAbsolutePath()); 430 try { 431 Component c = repository.loadComponent(name, graph); 432 configuration.setRootComponent(c); 433 selection.selectComponent(c); 434 } finally { 435 OpenAction.this.storage.close(); 436 } 437 } catch (Exception ignored) { 438 ignored.printStackTrace(); 439 } 440 441 rootPane.getGlassPane().setVisible(false); 442 rootPane.setGlassPane(glassPane); 443 } 444 } 445 } 446 | Popular Tags |