KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > BrowserImpl


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: BrowserImpl.java,v 1.1 2004/04/20 16:37:29 moroy Exp $
27  ====================================================================*/

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

69 public class BrowserImpl
70   implements BindingController,
71              BrowserAttributes
72 {
73     
74     // ==================================================================
75
//
76
// Internal states.
77
//
78
// ==================================================================
79

80     /** The string which represents the fcAppl client */
81     protected final String JavaDoc FC_APPL = "fcAppl";
82     
83     /** The associated tree */
84     protected DynamicTree tree_;
85     
86     /** The array of the config files */
87     protected List JavaDoc configFiles_ = new Vector JavaDoc();
88     
89     /** The list of Component */
90     protected Map JavaDoc elements_ = new HashMap JavaDoc();
91     
92     // ==================================================================
93
//
94
// No internal methods.
95
//
96
// ==================================================================
97

98     /** Returns an ImageIcon, or null if the path is invalid. */
99     protected static ImageIcon JavaDoc createIcon(String JavaDoc imageName) {
100         if (imageName != null) {
101             URL JavaDoc urlFile = null;
102             urlFile = Thread.currentThread().getContextClassLoader().getResource(imageName);
103             if(urlFile==null){
104                 try {
105                     urlFile = new URL JavaDoc(imageName);
106                 } catch (java.net.MalformedURLException JavaDoc e) {
107                     System.out.println(imageName + ": Malformed URL !");
108                 }
109             }
110             if(urlFile!=null){
111                 return new ImageIcon JavaDoc(urlFile);
112             }
113         }
114         return null;
115     }
116     
117     protected void createFileMenu(JMenuBar JavaDoc menu, JToolBar JavaDoc toolBar) {
118         JMenu JavaDoc fileMenu = new JMenu JavaDoc("File");
119         fileMenu.setMnemonic(KeyEvent.VK_F);
120         
121         JMenuItem JavaDoc menuItem = null;
122         JButton JavaDoc button = null;
123         
124         Action JavaDoc refreshAction = new RefreshAction("Refresh the tree", createIcon("Reload.png"), "Refreshes the tree", new Integer JavaDoc(KeyEvent.VK_R));
125         Action JavaDoc quitAction = new QuitAction("Quit", createIcon("Power.png"), "Quit the application", new Integer JavaDoc(KeyEvent.VK_Q));
126         
127         // Add to the menubar
128

129         menuItem = new JMenuItem JavaDoc(refreshAction);
130         menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, Event.CTRL_MASK));
131         fileMenu.add(menuItem);
132         
133         menuItem = new JMenuItem JavaDoc(quitAction);
134         menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, Event.ALT_MASK));
135         fileMenu.add(menuItem);
136         
137         // Adding to the toolbar
138

139         button = new JButton JavaDoc(quitAction);
140         button.setText("");
141         toolBar.add(button);
142         
143         button = new JButton JavaDoc(refreshAction);
144         button.setText("");
145         toolBar.add(button);
146         
147         menu.add(fileMenu);
148     }
149     
150     // ==================================================================
151
//
152
// Constructors.
153
//
154
// ==================================================================
155

156     /**
157      * Default constructor
158      */

159     public BrowserImpl(){
160         
161         JFrame JavaDoc frm = new JFrame JavaDoc("Fractal Browser");
162         
163         tree_ = new DynamicTree();
164         
165         DefaultViewPanel viewPanel = new DefaultViewPanel();
166         JMenuBar JavaDoc menuBar = new JMenuBar JavaDoc();
167         JToolBar JavaDoc toolBar = new JToolBar JavaDoc();
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 JavaDoc());
179         frm.getContentPane().add(toolBar,BorderLayout.NORTH);
180         frm.getContentPane().add(new JSplitPane JavaDoc(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         // Centers the frame
187
Dimension JavaDoc 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     // ==================================================================
194
//
195
// Public methods for interface BrowserAttributes.
196
//
197
// ==================================================================
198

199     public void setConfigFiles (String JavaDoc configFiles) {
200         if(configFiles != null){
201             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(configFiles,": ");
202             List JavaDoc l = new Vector JavaDoc();
203             while (st.hasMoreTokens()) {
204                 String JavaDoc theFile = st.nextToken();
205                 configFiles_.add(theFile);
206                 l.add(theFile);
207             }
208             tree_.addBrowserProperty((String JavaDoc[])l.toArray(new String JavaDoc[0]));
209             tree_.setCurrentRole(new String JavaDoc[]{"user"});
210         }
211     }
212     
213     public String JavaDoc getConfigFiles () {
214         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
215         Iterator JavaDoc it = configFiles_.iterator();
216         while(it.hasNext()){
217             sb.append((String JavaDoc)it.next() + " ");
218         }
219         return sb.toString();
220     }
221     
222     // ==================================================================
223
//
224
// Public methods for interface BindingController.
225
//
226
// ==================================================================
227

228     public void bindFc(String JavaDoc clientItfName, Object JavaDoc 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 JavaDoc[] listFc() {
240         return (String JavaDoc[])elements_.keySet().toArray(new String JavaDoc[elements_.size()]);
241     }
242     
243     public Object JavaDoc lookupFc(String JavaDoc 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 JavaDoc clientItfName)
252     throws
253     NoSuchInterfaceException,
254     IllegalBindingException,
255     IllegalLifeCycleException {
256         if (clientItfName.equals(FC_APPL)) {
257             elements_.remove(clientItfName);
258         }
259     }
260     
261     // ==================================================================
262
//
263
// Internal classes.
264
//
265
// ==================================================================
266

267     protected class RefreshAction extends AbstractAction JavaDoc {
268         
269         public RefreshAction(String JavaDoc nom, ImageIcon JavaDoc image, String JavaDoc desc, Integer JavaDoc 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 JavaDoc ae) {
276             BrowserImpl.this.tree_.refreshAll();
277         }
278     }
279     
280     protected class QuitAction extends AbstractAction JavaDoc {
281         
282         public QuitAction(String JavaDoc nom, ImageIcon JavaDoc image, String JavaDoc desc, Integer JavaDoc 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 JavaDoc ae) {
289             System.exit(0);
290         }
291     }
292     
293 }
Popular Tags