Source code: com/sunwheeltech/sirius/GenericBrowserPanel.java
1 package com.sunwheeltech.sirius;
2 /* GPL
3 ulunum java libraries for complex simulation modelling,
4 3d graphics, peer-to-peer networking and other purposes
5
6 version 0.1 released December 2001
7 see the file contents.html for a quick description of whats in
8 each package, and what you can expect to do with it
9
10 Copyright (C) December 2001 Dave Crane dave@cranepeople.co.uk
11
12
13 Find the GNU public license at:
14
15 http://www.gnu.org/copyleft/gpl.html
16
17 This program is free software; you can redistribute it and/or
18 modify it under the terms of the GNU General Public License
19 as published by the Free Software Foundation; either version 2
20 of the License, or (at your option) any later version.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 */
31
32
33 import ulu.view.*;
34 import ulu.view.io.*;
35 import ulu.view.ui.*;
36 import ulu.view.sys.fs.*;
37 import ulu.view.sys.refl.*;
38 import ulu.view.ui.sirius.*;
39
40 import ulu.ut.*;
41 import dog.gui.*;
42 import java.awt.*;
43 import java.awt.event.*;
44 import java.beans.*;
45 import java.io.*;
46 import java.net.*;
47 import java.util.*;
48 /**
49 * <p>Generic top-level browser pane used as a common base for browser beans. It is abstract because
50 * it doesn't define a RootName to be used wen defining groups of coordinated components
51 * @see{UncContaner}
52 *
53 * The panel is assigned root pane status in the dog.gui system, allowing it to host popup windows
54 * when embedded inside Swing components
55 *
56 * @author Dave Crane Sunwheel Technologies Ltd April 2003
57 */
58 public abstract class GenericBrowserPanel
59 extends DRootPanel{
60
61 /** */
62 private UncContainer body=null;
63
64 /** root view shown by this panel */
65 View root;
66 /** combination list/detail GUI view of panel */
67 CombiPane cpane;
68 /** get the root view shown by this panel
69 @return root view
70 */
71 public View getRoot(){ return root; }
72
73 /** the event proxy that handles notification of selection changes */
74 private EventProxy evprox;
75
76 /** constructor sets up the gui components */
77 public GenericBrowserPanel(){
78 super();
79 setPreferredSize(new Dimension(400,300));
80 body=new UncContainer(){
81 public String getRootName(){ return GenericBrowserPanel.this.getRootName(); }
82 };
83 DComponent.setPreloadImages(true);
84 root=null;
85 cpane=new CombiPane();
86
87 DContainer topbar=new DContainer();
88 topbar.setLayout(new BoxLayout(BoxLayout.HORIZONTAL,0,0));
89 addToolbars(topbar);
90
91 body.setLayout(new BorderLayout());
92 body.add(BorderLayout.NORTH,topbar);
93 body.add(BorderLayout.CENTER,cpane);
94
95 setLayout(new GridLayout(1,1));
96 add(body);
97
98 evprox=new EventProxy();
99 }
100
101 /** return a standard root name used for deriving a name for all coordinated components
102 * contained in this group. (typically a name based on the widget e.g. a file browser bean may
103 * return "filebrowser", resulting in group names like 'filebrowser-1'
104 */
105 public abstract String getRootName();
106
107 /** add custom toolbar widgets - override to do something useful
108 @param c container to add tools to
109 */
110 public void addToolbars(Container c){
111 }
112
113
114 //NB: setting view may make getRoot() incorrect! Should we allow to setView
115 //to a different root's child, or allow ability to restrict the option??
116 /** get the current view
117 @return view being displayed at present
118 */
119 public View getView(){ return cpane.getView(); }
120 /** set the current view
121 @param v view to display
122 */
123 public void setView(View v){
124 if (root==null){ root=v; }
125 if (cpane!=null){ cpane.setView(v); }
126 }
127
128
129 /** get the scrapbook view for this client
130 @return view to use as a scrapbook
131 */
132 public View getScrapbook(){
133 //work out what to do if not null later!!
134 User user=User.getUserManager().getUser(null);
135 return ScrapBook.get(user);
136 }
137
138 /** invoke a python script on this container (typically to populate the scrapbook)
139 *
140 * @param scriptfile the path to the script to execute - may be filesystem or inside jar
141 * @param refclass reference class used by classloader to find scripts inside jar files
142 * @param init if true will clear the scrapbook out before running script
143 */
144 protected void execScript(String scriptfile,Class refclass,boolean init){
145 InputStream scrstr=FlexiResourceLoader.getStream(refclass,scriptfile);
146 execScript(scrstr,init);
147 }
148
149 /** invoke a python script on this container (typically to populate the scrapbook)
150 *
151 * @param instr input stream to read script from
152 * @param init if true will clear the scrapbook out before running script
153 */
154 protected void execScript(InputStream instr,boolean init){
155 if (init){
156 //launch python interpreter and read startup script as way of populating scrapbook
157 UserManager umgr=User.getUserManager();
158 User user=umgr.getUser(null);
159 View scrapbk=ScrapBook.get(user);
160 scrapbk.clear();
161 Scripter.set("browser",this);
162 }
163 Scripter.execStream(instr);
164 }
165
166 /** add an object to the scripting environment
167 @param name name to assign to object in the scripting environment
168 @param obj object to expose to scripts
169 */
170 public void addToScript(String name,Object obj){
171 Scripter.set(name,obj);
172 }
173
174
175 //--- bound property support for beans ------------------------------
176 /** bean propery change support helper object */
177 protected PropertyChangeSupport changes=new PropertyChangeSupport(this);
178
179 /**
180 * Adds a property change listener to receive notification of changes to this component.
181 * @param l the listener
182 */
183 public void addPropertyChangeListener(PropertyChangeListener l){
184 changes.addPropertyChangeListener(l);
185 }
186
187 /**
188 * Removes a property change listener so it no longer receives notification of changes to this component.
189 * @param l the listener
190 */
191 public void removePropertyChangeListener(PropertyChangeListener l){
192 changes.removePropertyChangeListener(l);
193 }
194
195
196 /** delegate that listens for selection changes in the ViewPane, and reports to listeners
197 registered to this component */
198 private class EventProxy implements ActionListener,ItemListener{
199 EventProxy(){
200 ViewPane vp=cpane.viewp;
201 vp.addActionListener(EventProxy.this);
202 vp.addItemListener(EventProxy.this);
203 }
204 public void itemStateChanged(ItemEvent ev){
205 fireEvent(cpane.viewp.getSelected(),ev);
206 }
207 public void actionPerformed(ActionEvent ev){
208 fireEvent(cpane.viewp.getSelected(),ev);
209 }
210 }
211
212 /** fire a TreeBrowserEvent to all interested listeners */
213 protected void fireEvent(Item it,EventObject obj){
214 if (it==null){ return; }
215 TreeBrowserEvent event=new TreeBrowserEvent(it,obj);
216 for(Enumeration enu=listeners.elements();enu.hasMoreElements();){
217 TreeBrowserListener lsnr=(TreeBrowserListener)(enu.nextElement());
218 lsnr.respond(event);
219 }
220 }
221
222 /** list of registered listeners */
223 protected Vector listeners=new Vector();
224
225 /** add a listener */
226 public void addTreeBrowserListener(TreeBrowserListener lsnr){
227 if (!(listeners.contains(lsnr))){ listeners.addElement(lsnr); }
228 }
229
230 /** remove a listener */
231 public void removeTreeBrowserListener(TreeBrowserListener lsnr){
232 listeners.remove(lsnr);
233 }
234
235 }