View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.river.example.browser;
19  
20  import net.jini.core.lookup.ServiceItem;
21  import net.jini.core.lookup.ServiceRegistrar;
22  import net.jini.core.lookup.ServiceMatches;
23  import net.jini.core.lookup.ServiceTemplate;
24  import net.jini.core.entry.Entry;
25  import net.jini.lookup.ui.factory.JFrameFactory;
26  import net.jini.lookup.entry.UIDescriptor;
27  import java.awt.BorderLayout;
28  import java.awt.Dimension;
29  import java.awt.Rectangle;
30  import java.awt.event.ActionListener;
31  import java.awt.event.ActionEvent;
32  import java.awt.event.MouseAdapter;
33  import java.awt.event.MouseEvent;
34  import java.util.logging.Level;
35  import javax.swing.JFrame;
36  import javax.swing.JMenu;
37  import javax.swing.JMenuBar;
38  import javax.swing.JMenuItem;
39  import javax.swing.JPopupMenu;
40  import javax.swing.event.PopupMenuListener;
41  import javax.swing.event.PopupMenuEvent;
42  
43  /**
44   * A browser utility to browse entries in a specified space.
45   *
46   * @author Sun Microsystems, Inc.
47   *
48   * @version 0.2 06/04/98
49   *
50   */
51  class ServiceBrowser extends JFrame {
52    private Browser browser;
53    private AttributePanel attrPanel;
54    private final static int MINIMUM_WINDOW_WIDTH = 320;
55  
56    public ServiceBrowser(ServiceItem item,
57  			ServiceRegistrar registrar,
58  			Browser browser)
59    {
60      super("ServiceItem Browser");
61  
62      this.browser = browser;
63      // init main components
64      attrPanel = new AttributePanel(item, registrar);
65  
66      // add menu and attr panel
67      getContentPane().setLayout(new BorderLayout());
68      getContentPane().add(new BrowserMenuBar(), "North");
69      getContentPane().add(attrPanel, "Center");
70  
71      validate();
72      pack();
73      setSize(((getSize().width < MINIMUM_WINDOW_WIDTH) ? MINIMUM_WINDOW_WIDTH : getSize().width),
74  	    getSize().height);
75  
76      // center in parent frame
77      Rectangle bounds = browser.getBounds();
78      Dimension dialogSize = getPreferredSize();
79      int xpos = bounds.x + (bounds.width - dialogSize.width)/ 2;
80      int ypos = bounds.y + (bounds.height - dialogSize.height)/2;
81      setLocation((xpos < 0) ? 0 : xpos,
82  		(ypos < 0) ? 0 : ypos);
83    }
84  
85  
86    class BrowserMenuBar extends JMenuBar {
87      public BrowserMenuBar() {
88        JMenuItem mitem;
89  
90        // "File" Menu
91        JMenu fileMenu = (JMenu) add(new JMenu("File"));
92        mitem = (JMenuItem) fileMenu.add(new JMenuItem("Refresh"));
93        mitem.addActionListener(browser.wrap(new ActionListener() {
94  	public void actionPerformed(ActionEvent ev) {
95  	  attrPanel.refreshPanel();
96  	}
97        }));
98        mitem = (JMenuItem) fileMenu.add(new JMenuItem("Close"));
99        mitem.addActionListener(browser.wrap(new ActionListener() {
100 	public void actionPerformed(ActionEvent ev) {
101 	  ServiceBrowser.this.setVisible(false);
102 	}
103       }));
104     }
105   }
106 
107   class AttributePanel extends EntryTreePanel {
108     private ServiceItem item;
109     private ServiceRegistrar registrar;
110 
111     public AttributePanel(ServiceItem item, ServiceRegistrar registrar) {
112       super(false);	// Entries are not editable.
113 
114       this.item = item;
115       this.registrar = registrar;
116 
117       tree.addMouseListener(browser.wrap(new ServiceBrowser.MouseReceiver(item,
118 					 uiDescriptorPopup())));
119 
120       refreshPanel();
121     }
122 
123     protected Entry[] getEntryArray() {
124       try{
125 	ServiceMatches matches = registrar.lookup(new ServiceTemplate(item.serviceID,
126 								      new Class[] { item.service.getClass() },
127 								      new Entry[] {}),
128 						  10);
129 	if(matches.totalMatches != 1)
130 	  Browser.logger.log(Level.INFO, "unexpected lookup matches: {0}",
131 			     Integer.valueOf(matches.totalMatches));
132 	else
133 	  return matches.items[0].attributeSets;
134       } catch (Throwable t) {
135 	Browser.logger.log(Level.INFO, "lookup failed", t);
136       }
137       return null;
138     }
139   }
140 
141     // provides support for ServiceUI
142     public class UIDescriptorPopup extends JPopupMenu implements ActionListener,
143 	PopupMenuListener {
144 
145 	protected transient JMenuItem showUIItem;
146 	protected transient ServiceItem serviceItem;
147 
148 	public UIDescriptorPopup() {
149 	    super();
150 
151 	    showUIItem = new JMenuItem("Show UI");
152 
153 	    showUIItem.addActionListener(this);
154 	    showUIItem.setActionCommand("showUI");
155 	    add(showUIItem);
156 
157 	    addPopupMenuListener(this);
158 	    setOpaque(true);
159 	    setLightWeightPopupEnabled(true);
160 	}
161 
162 	public void actionPerformed(ActionEvent anEvent) {
163 
164 	    UIDescriptor uiDescriptor = getSelectedUIDescriptor();
165 
166 	    if (uiDescriptor == null) {
167 		return;
168 	    }
169 
170 	    try {
171 		JFrameFactory uiFactory = (JFrameFactory)
172 		    uiDescriptor.getUIFactory(Thread.currentThread().getContextClassLoader());
173 		JFrame frame = uiFactory.getJFrame(serviceItem);
174 
175 		frame.validate();
176 		frame.setVisible(true);
177 	    }
178 	    catch (Exception e) {
179 		e.printStackTrace();
180 
181 		return;
182 	    }
183 	}
184 
185 	public void popupMenuWillBecomeVisible(PopupMenuEvent ev) {
186 	}
187 
188 	public void popupMenuWillBecomeInvisible(PopupMenuEvent ev) {
189 	}
190 
191 	public void popupMenuCanceled(PopupMenuEvent ev) {
192 	}
193 
194 	public void setServiceItem(ServiceItem anItem) {
195 	    serviceItem = anItem;
196 	}
197     }
198 
199     class MouseReceiver extends MouseAdapter {
200 
201 	private ServiceBrowser.UIDescriptorPopup popup;
202 	private ServiceItem serviceItem;
203 
204 	public MouseReceiver(ServiceItem aServiceItem,
205 			ServiceBrowser.UIDescriptorPopup popup) {
206 		this.popup = popup;
207 		serviceItem = aServiceItem;
208 	}
209 
210 	public void mouseReleased(MouseEvent ev) {
211 
212 	    higlightSelection(ev);
213 
214 	    if (!ev.isPopupTrigger()) {
215 		return;
216 	    }
217 
218 	    UIDescriptor selectedDescriptor = getSelectedUIDescriptor();
219 
220 	    if (selectedDescriptor == null) {
221 		return;
222 	    }
223 
224 	    if (!"javax.swing".equals(selectedDescriptor.toolkit)) {
225 		return;
226 	    }
227 
228 	    popup.setServiceItem(serviceItem);
229 	    popup.show(ev.getComponent(), ev.getX(), ev.getY());
230 	}
231 
232 	public void mousePressed(MouseEvent ev) {
233 
234 	    higlightSelection(ev);
235 
236 	    if (!ev.isPopupTrigger()) {
237 		return;
238 	    }
239 
240 	    UIDescriptor selectedDescriptor = getSelectedUIDescriptor();
241 
242 	    if (selectedDescriptor == null) {
243 		return;
244 	    }
245 
246 	    if (!"javax.swing".equals(selectedDescriptor.toolkit)) {
247 		return;
248 	    }
249 
250 	    popup.setServiceItem(serviceItem);
251 	    popup.show(ev.getComponent(), ev.getX(), ev.getY());
252 	}
253     }
254 
255     private UIDescriptor getSelectedUIDescriptor() {
256 
257 	ObjectNode selectedNode =
258 		(ObjectNode) attrPanel.tree.getLastSelectedPathComponent();
259 
260 	if (selectedNode == null) {
261 	    return null;
262 	}
263 
264 	Object selectedObject = selectedNode.getObject();
265 
266 	try {
267 	    return (UIDescriptor) selectedObject;
268 	}
269 	catch (ClassCastException e) {
270 	    return null;
271 	}
272     }
273 
274     private void higlightSelection(MouseEvent anEvent) {
275 	attrPanel.tree.setSelectionPath(attrPanel.tree.getPathForLocation(
276 	    anEvent.getX(), anEvent.getY()));
277     }
278 
279     private ServiceBrowser.UIDescriptorPopup uiDescriptorPopup() {
280 	return new ServiceBrowser.UIDescriptorPopup();
281     }
282 }