1 28 29 package org.objectweb.fractal.explorer; 30 31 import java.awt.BorderLayout ; 32 import java.awt.Dimension ; 33 import java.awt.Event ; 34 import java.awt.event.KeyEvent ; 35 import java.net.URL ; 36 import java.util.HashMap ; 37 import java.util.List ; 38 import java.util.Map ; 39 import java.util.StringTokenizer ; 40 import java.util.Vector ; 41 42 import javax.swing.AbstractAction ; 43 import javax.swing.Action ; 44 import javax.swing.ImageIcon ; 45 import javax.swing.JButton ; 46 import javax.swing.JFrame ; 47 import javax.swing.JMenu ; 48 import javax.swing.JMenuBar ; 49 import javax.swing.JMenuItem ; 50 import javax.swing.JSplitPane ; 51 import javax.swing.JToolBar ; 52 import javax.swing.KeyStroke ; 53 54 import org.objectweb.fractal.adl.ADLException; 55 import org.objectweb.fractal.adl.Factory; 56 import org.objectweb.fractal.adl.FactoryFactory; 57 import org.objectweb.fractal.api.Component; 58 import org.objectweb.fractal.api.NoSuchInterfaceException; 59 import org.objectweb.fractal.api.control.BindingController; 60 import org.objectweb.fractal.api.control.IllegalBindingException; 61 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 62 import org.objectweb.fractal.util.Fractal; 63 import org.objectweb.util.explorer.api.Tree; 64 import org.objectweb.util.explorer.parser.api.ParserConfiguration; 65 import org.objectweb.util.explorer.swing.api.Explorer; 66 import org.objectweb.util.explorer.swing.api.StatusBar; 67 import org.objectweb.util.explorer.swing.api.ViewPanel; 68 import org.objectweb.util.explorer.swing.lib.DefaultTreePanel; 69 import org.objectweb.util.trace.TraceSystem; 70 71 76 public class FcExplorerImpl 77 implements BindingController, 78 FcExplorerAttributes 79 { 80 81 87 88 protected final String FC_APPL = "fcAppl"; 89 90 91 protected Component explorer_ = null; 92 93 94 protected Tree tree_ = null; 95 96 97 protected Explorer explorerItf_ = null; 98 99 100 protected String configFiles_ = null; 101 102 103 protected Map elements_ = new HashMap (); 104 105 111 112 protected static ImageIcon createIcon(String imageName) { 113 if (imageName != null) { 114 URL urlFile = null; 115 urlFile = Thread.currentThread().getContextClassLoader().getResource(imageName); 116 if(urlFile==null){ 117 try { 118 urlFile = new URL (imageName); 119 } catch (java.net.MalformedURLException e) { 120 System.out.println(imageName + ": Malformed URL !"); 121 } 122 } 123 if(urlFile!=null){ 124 return new ImageIcon (urlFile); 125 } 126 } 127 return null; 128 } 129 130 protected void createFileMenu(JMenuBar menu, JToolBar toolBar) { 131 JMenu fileMenu = new JMenu ("Explorer"); 132 fileMenu.setMnemonic(KeyEvent.VK_E); 133 134 JMenuItem menuItem = null; 135 JButton button = null; 136 137 Action refreshAction = new RefreshAction("Refresh the tree", createIcon("icons/Reload.png"), "Refreshes the tree", new Integer (KeyEvent.VK_R)); 138 Action quitAction = new QuitAction("Quit", createIcon("icons/Power.png"), "Quit the application", new Integer (KeyEvent.VK_Q)); 139 140 142 menuItem = new JMenuItem (refreshAction); 143 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, Event.CTRL_MASK)); 144 fileMenu.add(menuItem); 145 146 menuItem = new JMenuItem (quitAction); 147 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, Event.ALT_MASK)); 148 fileMenu.add(menuItem); 149 150 152 button = new JButton (quitAction); 153 button.setText(""); 154 toolBar.add(button); 155 156 button = new JButton (refreshAction); 157 button.setText(""); 158 toolBar.add(button); 159 160 menu.add(fileMenu); 161 } 162 163 169 172 public FcExplorerImpl(){ 173 TraceSystem.setLevel("debug"); 174 try { 175 Factory f = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND); 177 explorer_ = (Component)f.newComponent("org.objectweb.fractal.explorer.SwingFractalExplorer",null); 178 Fractal.getLifeCycleController(explorer_).startFc(); 179 180 Tree treeItf = (Tree)explorer_.getFcInterface(Tree.TREE); 182 183 setConfigFiles("config/fractalProperties.xml"); 185 186 tree_ = (Tree)explorer_.getFcInterface(Tree.TREE); 188 189 JMenuBar menuBar = new JMenuBar (); 190 JToolBar toolBar = new JToolBar (); 191 createFileMenu(menuBar, toolBar); 192 193 explorerItf_ = (Explorer)explorer_.getFcInterface(Explorer.EXPLORER); 194 explorerItf_.setMenuBar(menuBar); 195 explorerItf_.setToolBar(toolBar); 196 197 explorerItf_ = (Explorer)explorer_.getFcInterface(Explorer.EXPLORER); 199 ViewPanel viewPanelItf = (ViewPanel)explorer_.getFcInterface(ViewPanel.VIEW_PANEL); 200 StatusBar statusBar = (StatusBar)explorer_.getFcInterface(StatusBar.STATUS_BAR); 201 JFrame frame = new JFrame ("Fractal Explorer"); 202 frame.setJMenuBar(menuBar); 203 frame.getContentPane().setLayout(new BorderLayout ()); 204 frame.getContentPane().add(toolBar,BorderLayout.NORTH); 205 frame.getContentPane().add(new JSplitPane ( 206 JSplitPane.HORIZONTAL_SPLIT, 207 true, 208 new DefaultTreePanel(explorerItf_.getTree()), 209 viewPanelItf.getViewPanel()), BorderLayout.CENTER); 210 frame.getContentPane().add(statusBar.getStatusBar(), BorderLayout.SOUTH); 211 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 212 frame.pack(); 213 214 Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 216 frame.setLocation((screenSize.width - frame.getWidth()) / 2, (screenSize.height - frame.getHeight()) / 2); 217 218 frame.setVisible(true); 219 220 } catch (ADLException e) { 221 e.printStackTrace(); 222 } catch (IllegalLifeCycleException e) { 223 e.printStackTrace(); 224 } catch (NoSuchInterfaceException e) { 225 e.printStackTrace(); 226 } 227 } 228 229 235 public void setConfigFiles (String configFiles) { 236 if(configFiles != null){ 237 try { 238 configFiles_ = configFiles; 239 ParserConfiguration parser = (ParserConfiguration)explorer_.getFcInterface(ParserConfiguration.PARSER_CONFIGURATION); 240 StringTokenizer st = new StringTokenizer (configFiles,"; "); 241 List l = new Vector (); 242 while (st.hasMoreTokens()) { 243 String theFile = st.nextToken(); 244 parser.addPropertyFile(theFile); 245 } 246 parser.parse(); 247 248 Explorer explorerItf = (Explorer)explorer_.getFcInterface(Explorer.EXPLORER); 249 explorerItf.setCurrentRoles(new String []{"administrator"}); 250 } catch (NoSuchInterfaceException e) { 251 e.printStackTrace(); 252 } 253 } 254 } 255 256 public String getConfigFiles () { 257 return configFiles_; 258 } 259 260 266 public void bindFc(String clientItfName, Object sItf) 267 throws 268 NoSuchInterfaceException, 269 IllegalBindingException, 270 IllegalLifeCycleException { 271 if (clientItfName.startsWith(FC_APPL)) { 272 elements_.put(clientItfName, sItf); 273 Tree treeItf = (Tree)explorer_.getFcInterface(Tree.TREE); 274 treeItf.addEntry(FcExplorer.getName((Component)sItf), sItf,1); 275 } 276 } 277 278 public String [] listFc() { 279 return (String [])elements_.keySet().toArray(new String [elements_.size()]); 280 } 281 282 public Object lookupFc(String clientItfName) 283 throws NoSuchInterfaceException { 284 if (clientItfName.startsWith(FC_APPL)) { 285 return elements_.get(clientItfName); 286 } 287 return null; 288 } 289 290 public void unbindFc(String clientItfName) 291 throws 292 NoSuchInterfaceException, 293 IllegalBindingException, 294 IllegalLifeCycleException { 295 if (clientItfName.equals(FC_APPL)) { 296 elements_.remove(clientItfName); 297 } 298 } 299 300 306 protected class RefreshAction extends AbstractAction { 307 308 public RefreshAction(String nom, ImageIcon image, String desc, Integer mnemonic) { 309 super(nom, image); 310 putValue(SHORT_DESCRIPTION, desc); 311 putValue(MNEMONIC_KEY, mnemonic); 312 } 313 314 public void actionPerformed(java.awt.event.ActionEvent ae) { 315 FcExplorerImpl.this.tree_.refreshAll(); 316 } 317 } 318 319 protected class QuitAction extends AbstractAction { 320 321 public QuitAction(String nom, ImageIcon image, String desc, Integer mnemonic) { 322 super(nom, image); 323 putValue(SHORT_DESCRIPTION, desc); 324 putValue(MNEMONIC_KEY, mnemonic); 325 } 326 327 public void actionPerformed(java.awt.event.ActionEvent ae) { 328 System.exit(0); 329 } 330 } 331 332 } | Popular Tags |