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.graph.model.GraphModel; 31 import org.objectweb.fractal.gui.repository.api.Repository; 32 import org.objectweb.fractal.gui.repository.api.Storage; 33 import org.objectweb.fractal.gui.selection.model.Selection; 34 import org.objectweb.fractal.swing.WaitGlassPane; 35 import org.objectweb.fractal.swing.AbstractAction; 36 37 import java.net.URL ; 38 import java.awt.event.ActionEvent ; 39 import java.io.File ; 40 41 import javax.swing.ImageIcon ; 42 import javax.swing.JFileChooser ; 43 import javax.swing.KeyStroke ; 44 import javax.swing.JComponent ; 45 import javax.swing.JOptionPane ; 46 import javax.swing.JRootPane ; 47 import javax.swing.JPopupMenu ; 48 import javax.swing.JMenuItem ; 49 50 53 54 public class ImportAction extends AbstractAction implements BindingController { 55 56 61 62 public final static String CONFIGURATION_BINDING = "configuration"; 63 64 69 70 public final static String GRAPH_BINDING = "graph"; 71 72 75 76 public final static String SELECTION_BINDING = "selection"; 77 78 82 83 public final static String REPOSITORY_BINDING = "repository"; 84 85 89 90 public final static String STORAGE_BINDING = "storage"; 91 92 95 96 private Configuration configuration; 97 98 101 102 private GraphModel graph; 103 104 107 108 private Selection selection; 109 110 113 114 private Repository repository; 115 116 119 120 private Storage storage; 121 122 125 126 public ImportAction () { 127 putValue(NAME, "Import"); 128 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control I")); 129 putValue(SHORT_DESCRIPTION, "Import"); 130 URL url = getClass().getResource( 131 "/org/objectweb/fractal/gui/resources/fileopen.gif"); 132 putValue(SMALL_ICON, new ImageIcon (url)); 133 } 134 135 139 public String [] listFc () { 140 return new String [] { 141 CONFIGURATION_BINDING, 142 GRAPH_BINDING, 143 SELECTION_BINDING, 144 REPOSITORY_BINDING, 145 STORAGE_BINDING 146 }; 147 } 148 149 public Object lookupFc (final String clientItfName) { 150 if (CONFIGURATION_BINDING.equals(clientItfName)) { 151 return configuration; 152 } else if (GRAPH_BINDING.equals(clientItfName)) { 153 return graph; 154 } else if (SELECTION_BINDING.equals(clientItfName)) { 155 return selection; 156 } else if (REPOSITORY_BINDING.equals(clientItfName)) { 157 return repository; 158 } else if (STORAGE_BINDING.equals(clientItfName)) { 159 return storage; 160 } 161 return null; 162 } 163 164 public void bindFc ( 165 final String clientItfName, 166 final Object serverItf) 167 { 168 if (CONFIGURATION_BINDING.equals(clientItfName)) { 169 configuration = (Configuration)serverItf; 170 } else if (GRAPH_BINDING.equals(clientItfName)) { 171 graph = (GraphModel)serverItf; 172 } else if (SELECTION_BINDING.equals(clientItfName)) { 173 selection = (Selection)serverItf; 174 } else if (REPOSITORY_BINDING.equals(clientItfName)) { 175 repository = (Repository)serverItf; 176 } else if (STORAGE_BINDING.equals(clientItfName)) { 177 storage = (Storage)serverItf; 178 } 179 } 180 181 public void unbindFc (final String clientItfName) { 182 if (CONFIGURATION_BINDING.equals(clientItfName)) { 183 configuration = null; 184 } else if (GRAPH_BINDING.equals(clientItfName)) { 185 graph = null; 186 } else if (SELECTION_BINDING.equals(clientItfName)) { 187 selection = null; 188 } else if (REPOSITORY_BINDING.equals(clientItfName)) { 189 repository = null; 190 } else if (STORAGE_BINDING.equals(clientItfName)) { 191 storage = null; 192 } 193 } 194 195 199 public void actionPerformed (final ActionEvent e) { 200 try { 201 if (!(selection.getSelection() instanceof Component)) { 202 JOptionPane.showMessageDialog( 203 null, 204 "A component must be selected, to which the imported component will be added", 205 "Error", 206 JOptionPane.ERROR_MESSAGE); 207 return; 208 } 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 imported", 220 "Error", 221 JOptionPane.ERROR_MESSAGE); 222 return; 223 } 224 225 JFileChooser fileChooser = new JFileChooser (); 226 fileChooser.addChoosableFileFilter( 227 new SimpleFileFilter("fractal", "Fractal ADL files")); 228 if (fileChooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) { 229 return; 230 } 231 File f = fileChooser.getSelectedFile(); 232 233 File p = f; 234 String name = f.getName().substring(0, f.getName().indexOf('.')); 235 while (p.getParentFile() != null && !p.getParentFile().equals(storage)) { 236 name = p.getParentFile().getName() + "." + name; 237 p = p.getParentFile(); 238 } 239 if (!storage.equals(p.getParentFile())) { 240 JOptionPane.showMessageDialog( 241 null, 242 "Cannot open a file which is not in the storage directory. " + 243 "Change the storage directory first.", 244 "Error", 245 JOptionPane.ERROR_MESSAGE); 246 return; 247 } 248 249 new Thread (new Import(e, storage, name)).start(); 250 } catch (Exception ignored) { 251 ignored.printStackTrace(); 252 } 253 } 254 255 260 261 class Import implements Runnable { 262 263 266 267 private JRootPane rootPane; 268 269 272 273 private File storage; 274 275 278 279 private String name; 280 281 286 287 public Import (final ActionEvent e, final File storage, final String name) { 288 JComponent src = (JComponent )e.getSource(); 289 if (src instanceof JMenuItem ) { 290 src = (JComponent )src.getParent(); 291 } 292 if (src instanceof JPopupMenu ) { 293 src = (JComponent )((JPopupMenu )src).getInvoker(); 294 } 295 this.rootPane = src.getRootPane(); 296 this.storage = storage; 297 this.name = name; 298 } 299 300 public void run () { 301 java.awt.Component glassPane = rootPane.getGlassPane(); 302 rootPane.setGlassPane(new WaitGlassPane()); 303 rootPane.getGlassPane().setVisible(true); 304 305 try { 306 ImportAction.this.storage.open(storage.getAbsolutePath()); 307 try { 308 Component c = repository.loadComponent(name, graph); 309 Component p = (Component)selection.getSelection(); 310 p.addSubComponent(c); 311 selection.selectComponent(c); 312 } finally { 313 ImportAction.this.storage.close(); 314 } 315 } catch (Exception ignored) { 316 ignored.printStackTrace(); 317 } 318 319 rootPane.getGlassPane().setVisible(false); 320 rootPane.setGlassPane(glassPane); 321 } 322 } 323 } 324 | Popular Tags |