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.Component; 29 import org.objectweb.fractal.gui.model.Configuration; 30 import org.objectweb.fractal.gui.model.Factory; 31 import org.objectweb.fractal.gui.selection.model.Selection; 32 import org.objectweb.fractal.gui.UserData; 33 import org.objectweb.fractal.swing.AbstractAction; 34 35 import java.awt.event.ActionEvent ; 36 import java.net.URL ; 37 38 import javax.swing.ImageIcon ; 39 import javax.swing.KeyStroke ; 40 import javax.swing.JOptionPane ; 41 import javax.swing.Action ; 42 43 46 47 public class NewAction extends AbstractAction implements 48 BindingController 49 { 50 51 55 56 public final static String CONFIGURATION_BINDING = "configuration"; 57 58 62 63 public final static String SELECTION_BINDING = "selection"; 64 65 70 71 public final static String FACTORY_BINDING = "configuration-factory"; 72 73 78 79 public final static String USER_DATA_BINDING = "user-data"; 80 81 86 87 public final static String SAVE_ACTION_BINDING = "save-action"; 88 89 92 93 private Configuration configuration; 94 95 98 99 private Selection selection; 100 101 104 105 private Factory factory; 106 107 110 111 private UserData userData; 112 113 116 117 private Action save; 118 119 122 123 public NewAction () { 124 putValue(NAME, "New"); 125 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control N")); 126 putValue(SHORT_DESCRIPTION, "New"); 127 URL url = getClass().getResource( 128 "/org/objectweb/fractal/gui/resources/filenew.gif"); 129 putValue(SMALL_ICON, new ImageIcon (url)); 130 } 131 132 136 public String [] listFc () { 137 return new String [] { 138 CONFIGURATION_BINDING, 139 SELECTION_BINDING, 140 FACTORY_BINDING, 141 USER_DATA_BINDING, 142 SAVE_ACTION_BINDING 143 }; 144 } 145 146 public Object lookupFc (final String clientItfName) { 147 if (CONFIGURATION_BINDING.equals(clientItfName)) { 148 return configuration; 149 } else if (SELECTION_BINDING.equals(clientItfName)) { 150 return selection; 151 } else if (FACTORY_BINDING.equals(clientItfName)) { 152 return factory; 153 } else if (USER_DATA_BINDING.equals(clientItfName)) { 154 return userData; 155 } else if (SAVE_ACTION_BINDING.equals(clientItfName)) { 156 return save; 157 } 158 return null; 159 } 160 161 public void bindFc ( 162 final String clientItfName, 163 final Object serverItf) 164 { 165 if (CONFIGURATION_BINDING.equals(clientItfName)) { 166 configuration = (Configuration)serverItf; 167 } else if (SELECTION_BINDING.equals(clientItfName)) { 168 selection = (Selection)serverItf; 169 } else if (FACTORY_BINDING.equals(clientItfName)) { 170 factory = (Factory)serverItf; 171 } else if (USER_DATA_BINDING.equals(clientItfName)) { 172 userData = (UserData)serverItf; 173 } else if (SAVE_ACTION_BINDING.equals(clientItfName)) { 174 save = (Action)serverItf; 175 } 176 } 177 178 public void unbindFc (final String clientItfName) { 179 if (CONFIGURATION_BINDING.equals(clientItfName)) { 180 configuration = null; 181 } else if (SELECTION_BINDING.equals(clientItfName)) { 182 selection = null; 183 } else if (FACTORY_BINDING.equals(clientItfName)) { 184 factory = null; 185 } else if (USER_DATA_BINDING.equals(clientItfName)) { 186 userData = null; 187 } else if (SAVE_ACTION_BINDING.equals(clientItfName)) { 188 save = null; 189 } 190 } 191 192 196 public void actionPerformed (final ActionEvent e) { 197 try { 198 if (configuration.getChangeCount() > 0) { 199 Object [] options = {"Yes", "No", "Cancel" }; 200 int n = JOptionPane.showOptionDialog( 201 null, 202 "Do you want to save the current configuration " + 203 "before creating a new one ?", 204 "Warning", 205 JOptionPane.YES_NO_CANCEL_OPTION, 206 JOptionPane.QUESTION_MESSAGE, 207 null, 208 options, 209 options[0]); 210 if (n == 0) { 211 save.actionPerformed(e); 212 } 213 else if (n == 2) return; 214 } 215 if (userData != null) { 216 userData.setStringData(UserData.LAST_OPEN_FILE, null); 217 userData.setStringData(UserData.LAST_SAVE_FILE, null); 218 userData.setStringData(UserData.LAST_OPEN_CONF, null); 219 userData.save(); 220 } 221 } catch (Exception ex) { 222 } 223 Component c = factory.createComponent(); 224 configuration.setRootComponent(c); 225 selection.selectComponent(c); 226 } 227 } 228 | Popular Tags |