1 28 29 import java.awt.BorderLayout ; 30 import java.awt.Dimension ; 31 import java.awt.Event ; 32 import java.awt.event.KeyEvent ; 33 import java.net.URL ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.StringTokenizer ; 39 import java.util.Vector ; 40 41 import javax.swing.AbstractAction ; 42 import javax.swing.Action ; 43 import javax.swing.ImageIcon ; 44 import javax.swing.JButton ; 45 import javax.swing.JFrame ; 46 import javax.swing.JMenu ; 47 import javax.swing.JMenuBar ; 48 import javax.swing.JMenuItem ; 49 import javax.swing.JSplitPane ; 50 import javax.swing.JToolBar ; 51 import javax.swing.KeyStroke ; 52 53 import org.objectweb.fractal.api.Component; 54 import org.objectweb.fractal.api.NoSuchInterfaceException; 55 import org.objectweb.fractal.api.control.BindingController; 56 import org.objectweb.fractal.api.control.IllegalBindingException; 57 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 58 import org.objectweb.util.browser.core.common.DefaultStatusBar; 59 import org.objectweb.util.browser.core.common.DefaultTreePanel; 60 import org.objectweb.util.browser.core.common.DefaultViewPanel; 61 import org.objectweb.util.browser.core.common.DynamicTree; 62 import org.objectweb.util.browser.plugins.fractal.FcBrowser; 63 64 69 public class BrowserImpl 70 implements BindingController, 71 BrowserAttributes 72 { 73 74 80 81 protected final String FC_APPL = "fcAppl"; 82 83 84 protected DynamicTree tree_; 85 86 87 protected List configFiles_ = new Vector (); 88 89 90 protected Map elements_ = new HashMap (); 91 92 98 99 protected static ImageIcon createIcon(String imageName) { 100 if (imageName != null) { 101 URL urlFile = null; 102 urlFile = Thread.currentThread().getContextClassLoader().getResource(imageName); 103 if(urlFile==null){ 104 try { 105 urlFile = new URL (imageName); 106 } catch (java.net.MalformedURLException e) { 107 System.out.println(imageName + ": Malformed URL !"); 108 } 109 } 110 if(urlFile!=null){ 111 return new ImageIcon (urlFile); 112 } 113 } 114 return null; 115 } 116 117 protected void createFileMenu(JMenuBar menu, JToolBar toolBar) { 118 JMenu fileMenu = new JMenu ("File"); 119 fileMenu.setMnemonic(KeyEvent.VK_F); 120 121 JMenuItem menuItem = null; 122 JButton button = null; 123 124 Action refreshAction = new RefreshAction("Refresh the tree", createIcon("Reload.png"), "Refreshes the tree", new Integer (KeyEvent.VK_R)); 125 Action quitAction = new QuitAction("Quit", createIcon("Power.png"), "Quit the application", new Integer (KeyEvent.VK_Q)); 126 127 129 menuItem = new JMenuItem (refreshAction); 130 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, Event.CTRL_MASK)); 131 fileMenu.add(menuItem); 132 133 menuItem = new JMenuItem (quitAction); 134 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, Event.ALT_MASK)); 135 fileMenu.add(menuItem); 136 137 139 button = new JButton (quitAction); 140 button.setText(""); 141 toolBar.add(button); 142 143 button = new JButton (refreshAction); 144 button.setText(""); 145 toolBar.add(button); 146 147 menu.add(fileMenu); 148 } 149 150 156 159 public BrowserImpl(){ 160 161 JFrame frm = new JFrame ("Fractal Browser"); 162 163 tree_ = new DynamicTree(); 164 165 DefaultViewPanel viewPanel = new DefaultViewPanel(); 166 JMenuBar menuBar = new JMenuBar (); 167 JToolBar toolBar = new JToolBar (); 168 createFileMenu(menuBar, toolBar); 169 170 tree_.setTargetPanel(viewPanel); 171 tree_.setJMenuBar(menuBar); 172 tree_.setJToolBar(toolBar); 173 174 DefaultStatusBar statusBar = new DefaultStatusBar(); 175 tree_.setStatusBar(statusBar); 176 177 frm.setJMenuBar(menuBar); 178 frm.getContentPane().setLayout(new BorderLayout ()); 179 frm.getContentPane().add(toolBar,BorderLayout.NORTH); 180 frm.getContentPane().add(new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, true, new DefaultTreePanel(tree_), viewPanel), BorderLayout.CENTER); 181 frm.getContentPane().add(statusBar,BorderLayout.SOUTH); 182 183 frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 184 frm.pack(); 185 186 Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 188 frm.setLocation((screenSize.width - frm.getWidth()) / 2, (screenSize.height - frm.getHeight()) / 2); 189 190 frm.setVisible(true); 191 } 192 193 199 public void setConfigFiles (String configFiles) { 200 if(configFiles != null){ 201 StringTokenizer st = new StringTokenizer (configFiles,": "); 202 List l = new Vector (); 203 while (st.hasMoreTokens()) { 204 String theFile = st.nextToken(); 205 configFiles_.add(theFile); 206 l.add(theFile); 207 } 208 tree_.addBrowserProperty((String [])l.toArray(new String [0])); 209 tree_.setCurrentRole(new String []{"user"}); 210 } 211 } 212 213 public String getConfigFiles () { 214 StringBuffer sb = new StringBuffer (); 215 Iterator it = configFiles_.iterator(); 216 while(it.hasNext()){ 217 sb.append((String )it.next() + " "); 218 } 219 return sb.toString(); 220 } 221 222 228 public void bindFc(String clientItfName, Object sItf) 229 throws 230 NoSuchInterfaceException, 231 IllegalBindingException, 232 IllegalLifeCycleException { 233 if (clientItfName.startsWith(FC_APPL)) { 234 elements_.put(clientItfName, sItf); 235 tree_.addEntry(FcBrowser.getName((Component)sItf), sItf,1); 236 } 237 } 238 239 public String [] listFc() { 240 return (String [])elements_.keySet().toArray(new String [elements_.size()]); 241 } 242 243 public Object lookupFc(String clientItfName) 244 throws NoSuchInterfaceException { 245 if (clientItfName.startsWith(FC_APPL)) { 246 return elements_.get(clientItfName); 247 } 248 return null; 249 } 250 251 public void unbindFc(String clientItfName) 252 throws 253 NoSuchInterfaceException, 254 IllegalBindingException, 255 IllegalLifeCycleException { 256 if (clientItfName.equals(FC_APPL)) { 257 elements_.remove(clientItfName); 258 } 259 } 260 261 267 protected class RefreshAction extends AbstractAction { 268 269 public RefreshAction(String nom, ImageIcon image, String desc, Integer mnemonic) { 270 super(nom, image); 271 putValue(SHORT_DESCRIPTION, desc); 272 putValue(MNEMONIC_KEY, mnemonic); 273 } 274 275 public void actionPerformed(java.awt.event.ActionEvent ae) { 276 BrowserImpl.this.tree_.refreshAll(); 277 } 278 } 279 280 protected class QuitAction extends AbstractAction { 281 282 public QuitAction(String nom, ImageIcon image, String desc, Integer mnemonic) { 283 super(nom, image); 284 putValue(SHORT_DESCRIPTION, desc); 285 putValue(MNEMONIC_KEY, mnemonic); 286 } 287 288 public void actionPerformed(java.awt.event.ActionEvent ae) { 289 System.exit(0); 290 } 291 } 292 293 } | Popular Tags |