1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.river.example.browser;
19
20 import org.apache.river.admin.JavaSpaceAdmin;
21 import org.apache.river.admin.AdminIterator;
22 import net.jini.core.entry.Entry;
23 import net.jini.core.entry.UnusableEntryException;
24 import net.jini.core.lease.Lease;
25 import net.jini.space.JavaSpace05;
26 import net.jini.space.MatchSet;
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.util.Collections;
33 import java.util.List;
34 import java.util.logging.Level;
35 import javax.swing.JFrame;
36 import javax.swing.JMenuBar;
37 import javax.swing.JMenu;
38 import javax.swing.JMenuItem;
39
40
41
42
43
44
45
46
47 class SpaceBrowser extends JFrame {
48
49 private final static int MINIMUM_WINDOW_WIDTH = 320;
50 private Browser browser;
51
52 public SpaceBrowser(Object proxy, Browser browser) {
53 super("SpaceBrowser");
54
55 this.browser = browser;
56
57 SpaceEntryPanel entryPanel = new SpaceEntryPanel(proxy);
58
59
60 getContentPane().setLayout(new BorderLayout());
61 getContentPane().add(new BrowserMenuBar(entryPanel), "North");
62 getContentPane().add(entryPanel, "Center");
63
64 validate();
65 pack();
66 setSize(((getSize().width < MINIMUM_WINDOW_WIDTH) ? MINIMUM_WINDOW_WIDTH : getSize().width),
67 getSize().height);
68
69
70 Rectangle bounds = browser.getBounds();
71 Dimension dialogSize = getPreferredSize();
72 int xpos = bounds.x + (bounds.width - dialogSize.width)/ 2;
73 int ypos = bounds.y + (bounds.height - dialogSize.height)/2;
74 setLocation((xpos < 0) ? 0 : xpos,
75 (ypos < 0) ? 0 : ypos);
76 }
77
78
79 class BrowserMenuBar extends JMenuBar {
80 private EntryTreePanel entryPanel;
81
82 public BrowserMenuBar(EntryTreePanel entryPanel) {
83 this.entryPanel = entryPanel;
84 JMenuItem mitem;
85
86
87 JMenu fileMenu = (JMenu) add(new JMenu("File"));
88 mitem = (JMenuItem) fileMenu.add(new JMenuItem("Refresh"));
89 mitem.addActionListener(browser.wrap(new ActionListener() {
90 public void actionPerformed(ActionEvent ev) {
91 BrowserMenuBar.this.entryPanel.refreshPanel();
92 }
93 }));
94 mitem = (JMenuItem) fileMenu.add(new JMenuItem("Close"));
95 mitem.addActionListener(browser.wrap(new ActionListener() {
96 public void actionPerformed(ActionEvent ev) {
97 SpaceBrowser.this.setVisible(false);
98 }
99 }));
100 }
101 }
102
103 class SpaceEntryPanel extends EntryTreePanel {
104 private Object proxy;
105
106 public SpaceEntryPanel(Object proxy) {
107 super(false);
108 this.proxy = proxy;
109
110 refreshPanel();
111 }
112
113 protected Entry[] getEntryArray() {
114 try {
115 List acc = new java.util.LinkedList();
116 if (proxy instanceof JavaSpace05) {
117 MatchSet set =
118 ((JavaSpace05) proxy).contents(Collections.singleton(null),
119 null, Lease.ANY, Integer.MAX_VALUE);
120 Lease lease = set.getLease();
121 if (lease != null) {
122 lease = (Lease) browser.leasePreparer.prepareProxy(lease);
123 browser.leaseMgr.renewUntil(lease, Lease.ANY, null);
124 }
125 try {
126 while (true) {
127 try {
128 Entry e = set.next();
129 if (e == null)
130 break;
131 acc.add(e);
132 } catch (UnusableEntryException e) {
133 Browser.logger.log(Level.INFO, "unusable entry", e);
134 }
135 }
136 } finally {
137 if (lease != null) {
138 try {
139 browser.leaseMgr.cancel(lease);
140 } catch (Exception e) {
141 }
142 }
143 }
144 } else {
145 AdminIterator iter =
146 ((JavaSpaceAdmin) proxy).contents(null, null, 128);
147 try {
148 while (true) {
149 try {
150 Entry e = iter.next();
151 if (e == null)
152 break;
153 acc.add(e);
154 } catch (UnusableEntryException e) {
155 Browser.logger.log(Level.INFO, "unusable entry", e);
156 }
157 }
158 } finally {
159 try {
160 iter.close();
161 } catch (Exception e) {
162 }
163 }
164 }
165 return (Entry[])acc.toArray(new Entry[acc.size()]);
166 } catch (Throwable t) {
167 Browser.logger.log(Level.INFO, "obtaining entries failed", t);
168 }
169
170 return null;
171 }
172 }
173 }
174