KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > explorer > FcExplorerImpl


1 /*====================================================================
2  
3  Objectweb Browser Framework
4  Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5  Contact: openccm@objectweb.org
6  
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or any later version.
11  
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16  
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  USA
21  
22  Initial developer(s): Romain Rouvoy.
23  Contributor(s): ______________________________________.
24  
25  ---------------------------------------------------------------------
26  $Id: FcExplorerImpl.java,v 1.3 2004/11/15 16:44:25 moroy Exp $
27  ====================================================================*/

28
29 package org.objectweb.fractal.explorer;
30
31 import java.awt.BorderLayout JavaDoc;
32 import java.awt.Dimension JavaDoc;
33 import java.awt.Event JavaDoc;
34 import java.awt.event.KeyEvent JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40 import java.util.Vector JavaDoc;
41
42 import javax.swing.AbstractAction JavaDoc;
43 import javax.swing.Action JavaDoc;
44 import javax.swing.ImageIcon JavaDoc;
45 import javax.swing.JButton JavaDoc;
46 import javax.swing.JFrame JavaDoc;
47 import javax.swing.JMenu JavaDoc;
48 import javax.swing.JMenuBar JavaDoc;
49 import javax.swing.JMenuItem JavaDoc;
50 import javax.swing.JSplitPane JavaDoc;
51 import javax.swing.JToolBar JavaDoc;
52 import javax.swing.KeyStroke JavaDoc;
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 /**
72  * This Fractal component contains the explorer
73  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
74  * @version 0.1
75  */

76 public class FcExplorerImpl
77   implements BindingController,
78              FcExplorerAttributes
79 {
80     
81     // ==================================================================
82
//
83
// Internal states.
84
//
85
// ==================================================================
86

87     /** The string which represents the fcAppl client */
88     protected final String JavaDoc FC_APPL = "fcAppl";
89     
90     /** The explorer component. */
91     protected Component explorer_ = null;
92     
93     /** The tree interface to use. */
94     protected Tree tree_ = null;
95     
96     /** The explorer interface to use. */
97     protected Explorer explorerItf_ = null;
98     
99     /** The config files */
100     protected String JavaDoc configFiles_ = null;
101     
102     /** The list of Component */
103     protected Map JavaDoc elements_ = new HashMap JavaDoc();
104     
105     // ==================================================================
106
//
107
// No internal methods.
108
//
109
// ==================================================================
110

111     /** Returns an ImageIcon, or null if the path is invalid. */
112     protected static ImageIcon JavaDoc createIcon(String JavaDoc imageName) {
113         if (imageName != null) {
114             URL JavaDoc urlFile = null;
115             urlFile = Thread.currentThread().getContextClassLoader().getResource(imageName);
116             if(urlFile==null){
117                 try {
118                     urlFile = new URL JavaDoc(imageName);
119                 } catch (java.net.MalformedURLException JavaDoc e) {
120                     System.out.println(imageName + ": Malformed URL !");
121                 }
122             }
123             if(urlFile!=null){
124                 return new ImageIcon JavaDoc(urlFile);
125             }
126         }
127         return null;
128     }
129     
130     protected void createFileMenu(JMenuBar JavaDoc menu, JToolBar JavaDoc toolBar) {
131         JMenu JavaDoc fileMenu = new JMenu JavaDoc("Explorer");
132         fileMenu.setMnemonic(KeyEvent.VK_E);
133         
134         JMenuItem JavaDoc menuItem = null;
135         JButton JavaDoc button = null;
136         
137         Action JavaDoc refreshAction = new RefreshAction("Refresh the tree", createIcon("icons/Reload.png"), "Refreshes the tree", new Integer JavaDoc(KeyEvent.VK_R));
138         Action JavaDoc quitAction = new QuitAction("Quit", createIcon("icons/Power.png"), "Quit the application", new Integer JavaDoc(KeyEvent.VK_Q));
139         
140         // Add to the menubar
141

142         menuItem = new JMenuItem JavaDoc(refreshAction);
143         menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, Event.CTRL_MASK));
144         fileMenu.add(menuItem);
145         
146         menuItem = new JMenuItem JavaDoc(quitAction);
147         menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, Event.ALT_MASK));
148         fileMenu.add(menuItem);
149         
150         // Adding to the toolbar
151

152         button = new JButton JavaDoc(quitAction);
153         button.setText("");
154         toolBar.add(button);
155         
156         button = new JButton JavaDoc(refreshAction);
157         button.setText("");
158         toolBar.add(button);
159         
160         menu.add(fileMenu);
161     }
162     
163     // ==================================================================
164
//
165
// Constructors.
166
//
167
// ==================================================================
168

169     /**
170      * Default constructor
171      */

172     public FcExplorerImpl(){
173         TraceSystem.setLevel("debug");
174         try {
175             // Creates a Fractal Explorer instance.
176
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             // Browses the structure of the Explorer component.
181
Tree treeItf = (Tree)explorer_.getFcInterface(Tree.TREE);
182             
183             // Loads the Explorer Framework configuration file from the fractal-explorer Java archive.
184
setConfigFiles("config/fractalProperties.xml");
185         
186             // Populates the tree.
187
tree_ = (Tree)explorer_.getFcInterface(Tree.TREE);
188
189             JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
190             JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
191             createFileMenu(menuBar, toolBar);
192             
193             explorerItf_ = (Explorer)explorer_.getFcInterface(Explorer.EXPLORER);
194             explorerItf_.setMenuBar(menuBar);
195             explorerItf_.setToolBar(toolBar);
196             
197             // Configures the main frame.
198
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 JavaDoc frame = new JFrame JavaDoc("Fractal Explorer");
202             frame.setJMenuBar(menuBar);
203             frame.getContentPane().setLayout(new BorderLayout JavaDoc());
204             frame.getContentPane().add(toolBar,BorderLayout.NORTH);
205             frame.getContentPane().add(new JSplitPane JavaDoc(
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             // Centers the frame
215
Dimension JavaDoc 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     // ==================================================================
230
//
231
// Public methods for interface ExplorerAttributes.
232
//
233
// ==================================================================
234

235     public void setConfigFiles (String JavaDoc configFiles) {
236         if(configFiles != null){
237             try {
238                 configFiles_ = configFiles;
239                 ParserConfiguration parser = (ParserConfiguration)explorer_.getFcInterface(ParserConfiguration.PARSER_CONFIGURATION);
240                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(configFiles,"; ");
241                 List JavaDoc l = new Vector JavaDoc();
242                 while (st.hasMoreTokens()) {
243                     String JavaDoc 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 JavaDoc[]{"administrator"});
250             } catch (NoSuchInterfaceException e) {
251                 e.printStackTrace();
252             }
253         }
254     }
255     
256     public String JavaDoc getConfigFiles () {
257         return configFiles_;
258     }
259     
260     // ==================================================================
261
//
262
// Public methods for interface BindingController.
263
//
264
// ==================================================================
265

266     public void bindFc(String JavaDoc clientItfName, Object JavaDoc 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 JavaDoc[] listFc() {
279         return (String JavaDoc[])elements_.keySet().toArray(new String JavaDoc[elements_.size()]);
280     }
281     
282     public Object JavaDoc lookupFc(String JavaDoc 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 JavaDoc clientItfName)
291     throws
292     NoSuchInterfaceException,
293     IllegalBindingException,
294     IllegalLifeCycleException {
295         if (clientItfName.equals(FC_APPL)) {
296             elements_.remove(clientItfName);
297         }
298     }
299     
300     // ==================================================================
301
//
302
// Internal classes.
303
//
304
// ==================================================================
305

306     protected class RefreshAction extends AbstractAction JavaDoc {
307         
308         public RefreshAction(String JavaDoc nom, ImageIcon JavaDoc image, String JavaDoc desc, Integer JavaDoc 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 JavaDoc ae) {
315             FcExplorerImpl.this.tree_.refreshAll();
316         }
317     }
318     
319     protected class QuitAction extends AbstractAction JavaDoc {
320         
321         public QuitAction(String JavaDoc nom, ImageIcon JavaDoc image, String JavaDoc desc, Integer JavaDoc 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 JavaDoc ae) {
328             System.exit(0);
329         }
330     }
331     
332 }
Popular Tags