KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > DockableWindowFactory


1 /*
2  * DockableWindowFactory.java - loads dockables.xml, etc
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2005 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import java.io.IOException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Stack JavaDoc;
33
34 import javax.swing.JComponent JavaDoc;
35
36 import org.gjt.sp.jedit.ActionSet;
37 import org.gjt.sp.jedit.BeanShell;
38 import org.gjt.sp.jedit.EditAction;
39 import org.gjt.sp.jedit.MiscUtilities;
40 import org.gjt.sp.jedit.PluginJAR;
41 import org.gjt.sp.jedit.View;
42 import org.gjt.sp.jedit.jEdit;
43 import org.gjt.sp.util.Log;
44 import org.gjt.sp.util.XMLUtilities;
45 import org.xml.sax.Attributes JavaDoc;
46 import org.xml.sax.InputSource JavaDoc;
47 import org.xml.sax.helpers.DefaultHandler JavaDoc;
48
49 import bsh.NameSpace;
50 import bsh.UtilEvalError;
51 //}}}
52

53 /**
54  * This class loads <code>dockable.xml</code> files and manages creation
55  * of new dockable windows.
56  *
57  * @see DockableWindowManager
58  *
59  * @since jEdit 4.3pre2
60  */

61 public class DockableWindowFactory
62 {
63     //{{{ getInstance() method
64
public static synchronized DockableWindowFactory getInstance()
65     {
66         if(instance == null)
67             instance = new DockableWindowFactory();
68         return instance;
69     } //}}}
70

71     //{{{ DockableWindowFactory constructor
72
public DockableWindowFactory()
73     {
74         dockableWindowFactories = new HashMap JavaDoc<String JavaDoc, Window>();
75     } //}}}
76

77     //{{{ loadDockableWindows() method
78
/**
79      * Plugins shouldn't need to call this method.
80      * @since jEdit 4.2pre1
81      */

82     public void loadDockableWindows(PluginJAR plugin, URL JavaDoc uri,
83         PluginJAR.PluginCacheEntry cache)
84     {
85         try
86         {
87             Log.log(Log.DEBUG,DockableWindowManager.class,
88                 "Loading dockables from " + uri);
89             DockableListHandler dh = new DockableListHandler(plugin,uri);
90             boolean failure = XMLUtilities.parseXML(uri.openStream(), dh);
91
92             if (!failure && cache != null)
93             {
94                 cache.cachedDockableNames = dh.getCachedDockableNames();
95                 cache.cachedDockableActionFlags = dh.getCachedDockableActionFlags();
96             }
97         }
98         catch(IOException JavaDoc e)
99         {
100             Log.log(Log.ERROR,DockableWindowManager.class,e);
101         }
102     } //}}}
103

104     //{{{ unloadDockableWindows() method
105
/**
106      * Plugins shouldn't need to call this method.
107      * @since jEdit 4.2pre1
108      */

109     public void unloadDockableWindows(PluginJAR plugin)
110     {
111         Iterator JavaDoc entries = dockableWindowFactories.entrySet().iterator();
112         while(entries.hasNext())
113         {
114             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
115             Window factory = (Window)entry.getValue();
116             if(factory.plugin == plugin)
117                 entries.remove();
118         }
119     } //}}}
120

121     //{{{ cacheDockableWindows() method
122
/**
123      * @since jEdit 4.2pre1
124      */

125     public void cacheDockableWindows(PluginJAR plugin,
126         String JavaDoc[] name, boolean[] actions)
127     {
128         for(int i = 0; i < name.length; i++)
129         {
130             Window factory = new Window(plugin,
131                 name[i],null,actions[i]);
132             dockableWindowFactories.put(name[i],factory);
133         }
134     } //}}}
135

136     //{{{ registerDockableWindow() method
137
public void registerDockableWindow(PluginJAR plugin,
138         String JavaDoc name, String JavaDoc code, boolean actions)
139     {
140         Window factory = dockableWindowFactories.get(name);
141         if(factory != null)
142         {
143             factory.code = code;
144             factory.loaded = true;
145         }
146         else
147         {
148             factory = new Window(plugin,name,code,actions);
149             dockableWindowFactories.put(name,factory);
150         }
151     } //}}}
152

153     //{{{ getRegisteredDockableWindows() method
154
public String JavaDoc[] getRegisteredDockableWindows()
155     {
156         String JavaDoc[] retVal = new String JavaDoc[dockableWindowFactories.size()];
157         Iterator JavaDoc<Window> entries = dockableWindowFactories.values().iterator();
158         int i = 0;
159         while(entries.hasNext())
160         {
161             Window factory = entries.next();
162             retVal[i++] = factory.name;
163         }
164
165         return retVal;
166     } //}}}
167

168     //{{{ getDockableWindowIterator() method
169
Iterator JavaDoc<Window> getDockableWindowIterator()
170     {
171         return dockableWindowFactories.values().iterator();
172     } //}}}
173

174     //{{{ DockableListHandler class
175
class DockableListHandler extends DefaultHandler JavaDoc
176     {
177         //{{{ DockableListHandler constructor
178
/**
179          * @param plugin - the pluginJAR for which we are loading the dockables.xml
180          * @param uri - the uri of the dockables.xml file?
181          */

182         DockableListHandler(PluginJAR plugin, URL JavaDoc uri)
183         {
184             this.plugin = plugin;
185             this.uri = uri;
186             stateStack = new Stack JavaDoc();
187             actions = true;
188
189             code = new StringBuffer JavaDoc();
190             cachedDockableNames = new LinkedList JavaDoc<String JavaDoc>();
191             cachedDockableActionFlags = new LinkedList JavaDoc();
192         } //}}}
193

194         //{{{ resolveEntity() method
195
public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
196         {
197             return XMLUtilities.findEntity(systemId, "dockables.dtd", MiscUtilities.class);
198         } //}}}
199

200         //{{{ characters() method
201
public void characters(char[] c, int off, int len)
202         {
203             String JavaDoc tag = peekElement();
204             if (tag.equals("DOCKABLE"))
205                 code.append(c, off, len);
206         } //}}}
207

208         //{{{ startElement() method
209
public void startElement(String JavaDoc uri, String JavaDoc localName,
210                      String JavaDoc qName, Attributes JavaDoc attrs)
211         {
212             String JavaDoc tag = pushElement(qName);
213             if (tag.equals("DOCKABLE"))
214             {
215                 dockableName = attrs.getValue("NAME");
216                 actions = "FALSE".equals(attrs.getValue("NO_ACTIONS"));
217             }
218         } //}}}
219

220         //{{{ endElement() method
221
public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc name)
222         {
223             if(name == null)
224                 return;
225
226             String JavaDoc tag = peekElement();
227
228             if(name.equals(tag))
229             {
230                 if(tag.equals("DOCKABLE"))
231                 {
232                     registerDockableWindow(plugin,
233                         dockableName,code.toString(),actions);
234                     cachedDockableNames.add(dockableName);
235                     cachedDockableActionFlags.add(
236                         new Boolean JavaDoc(actions));
237                     // make default be true for the next
238
// action
239
actions = true;
240                     code.setLength(0);
241                 }
242
243                 popElement();
244             }
245             else
246             {
247                 // can't happen
248
throw new InternalError JavaDoc();
249             }
250         } //}}}
251

252         //{{{ startDocument() method
253
public void startDocument()
254         {
255             try
256             {
257                 pushElement(null);
258             }
259             catch (Exception JavaDoc e)
260             {
261                 e.printStackTrace();
262             }
263         } //}}}
264

265         //{{{ getCachedDockableNames() method
266
public String JavaDoc[] getCachedDockableNames()
267         {
268             return (String JavaDoc[])cachedDockableNames.toArray(new String JavaDoc[cachedDockableNames.size()]);
269         } //}}}
270

271         //{{{ getCachedDockableActionFlags() method
272
public boolean[] getCachedDockableActionFlags()
273         {
274             boolean[] returnValue = new boolean[
275                 cachedDockableActionFlags.size()];
276             Iterator JavaDoc iter = cachedDockableActionFlags.iterator();
277             int i = 0;
278             while(iter.hasNext())
279             {
280                 boolean flag = ((Boolean JavaDoc)iter.next())
281                     .booleanValue();
282                 returnValue[i++] = flag;
283             }
284
285             return returnValue;
286         } //}}}
287

288         //{{{ Private members
289

290         //{{{ Instance variables
291
private PluginJAR plugin;
292         // What is the purpose of this?
293
private URL JavaDoc uri;
294
295         private java.util.List JavaDoc<String JavaDoc> cachedDockableNames;
296         private java.util.List JavaDoc cachedDockableActionFlags;
297
298         private String JavaDoc dockableName;
299         private StringBuffer JavaDoc code;
300         private boolean actions;
301
302         private Stack JavaDoc stateStack;
303         //}}}
304

305         //{{{ pushElement() method
306
private String JavaDoc pushElement(String JavaDoc name)
307         {
308             name = (name == null) ? null : name.intern();
309
310             stateStack.push(name);
311
312             return name;
313         } //}}}
314

315         //{{{ peekElement() method
316
private String JavaDoc peekElement()
317         {
318             return (String JavaDoc) stateStack.peek();
319         } //}}}
320

321         //{{{ popElement() method
322
private String JavaDoc popElement()
323         {
324             return (String JavaDoc) stateStack.pop();
325         } //}}}
326

327         //}}}
328
} //}}}
329

330     //{{{ Window class
331
class Window
332     {
333         PluginJAR plugin;
334         String JavaDoc name;
335         String JavaDoc code;
336         boolean loaded;
337
338         //{{{ Window constructor
339
Window(PluginJAR plugin, String JavaDoc name, String JavaDoc code,
340             boolean actions)
341         {
342             this.plugin = plugin;
343             this.name = name;
344             this.code = code;
345
346             if(code != null)
347                 loaded = true;
348
349             if(actions)
350             {
351                 ActionSet actionSet = (plugin == null
352                     ? jEdit.getBuiltInActionSet()
353                     : plugin.getActionSet());
354                 actionSet.addAction(new OpenAction(name));
355                 actionSet.addAction(new ToggleAction(name));
356                 actionSet.addAction(new FloatAction(name));
357
358                 String JavaDoc label = jEdit.getProperty(name
359                     + ".label");
360                 if(label == null)
361                     label = "NO LABEL PROPERTY: " + name;
362
363                 String JavaDoc[] args = { label };
364                 jEdit.setTemporaryProperty(name + ".label",
365                     label);
366                 jEdit.setTemporaryProperty(name
367                     + "-toggle.label",
368                     jEdit.getProperty(
369                     "view.docking.toggle.label",args));
370                 jEdit.setTemporaryProperty(name
371                     + "-toggle.toggle","true");
372                 jEdit.setTemporaryProperty(name
373                     + "-float.label",
374                     jEdit.getProperty(
375                     "view.docking.float.label",args));
376             }
377         } //}}}
378

379         //{{{ load() method
380
void load()
381         {
382             if(loaded)
383                 return;
384
385             loadDockableWindows(plugin,plugin.getDockablesURI(),null);
386         } //}}}
387

388         //{{{ createDockableWindow() method
389
JComponent JavaDoc createDockableWindow(View view, String JavaDoc position)
390         {
391             load();
392
393             if(!loaded)
394             {
395                 Log.log(Log.WARNING,this,"Outdated cache");
396                 return null;
397             }
398
399             NameSpace nameSpace = new NameSpace(
400                 BeanShell.getNameSpace(),
401                 "DockableWindowManager.Factory"
402                 + ".createDockableWindow()");
403             try
404             {
405                 nameSpace.setVariable(
406                     "position",position);
407             }
408             catch(UtilEvalError e)
409             {
410                 Log.log(Log.ERROR,this,e);
411             }
412             JComponent JavaDoc win = (JComponent JavaDoc)BeanShell.eval(view,
413                 nameSpace,code);
414             return win;
415         } //}}}
416

417         //{{{ OpenAction class
418
class OpenAction extends EditAction
419         {
420             private String JavaDoc dockable;
421
422             //{{{ OpenAction constructor
423
OpenAction(String JavaDoc name)
424             {
425                 super(name);
426                 this.dockable = name;
427             } //}}}
428

429             //{{{ invoke() method
430
public void invoke(View view)
431             {
432                 view.getDockableWindowManager()
433                     .showDockableWindow(dockable);
434             } //}}}
435

436             //{{{ getCode() method
437
public String JavaDoc getCode()
438             {
439                 return "view.getDockableWindowManager()"
440                     + ".showDockableWindow(\"" + dockable + "\");";
441             } //}}}
442
} //}}}
443

444         //{{{ ToggleAction class
445
class ToggleAction extends EditAction
446         {
447             private String JavaDoc dockable;
448
449             //{{{ ToggleAction constructor
450
ToggleAction(String JavaDoc name)
451             {
452                 super(name + "-toggle");
453                 this.dockable = name;
454             } //}}}
455

456             //{{{ invoke() method
457
public void invoke(View view)
458             {
459                 view.getDockableWindowManager()
460                     .toggleDockableWindow(dockable);
461             } //}}}
462

463             //{{{ isSelected() method
464
public boolean isSelected(View view)
465             {
466                 return view.getDockableWindowManager()
467                     .isDockableWindowVisible(dockable);
468             } //}}}
469

470             //{{{ getCode() method
471
public String JavaDoc getCode()
472             {
473                 return "view.getDockableWindowManager()"
474                     + ".toggleDockableWindow(\"" + dockable + "\");";
475             } //}}}
476
} //}}}
477

478         //{{{ FloatAction class
479
class FloatAction extends EditAction
480         {
481             private String JavaDoc dockable;
482
483             //{{{ FloatAction constructor
484
FloatAction(String JavaDoc name)
485             {
486                 super(name + "-float");
487                 this.dockable = name;
488             } //}}}
489

490             //{{{ invoke() method
491
public void invoke(View view)
492             {
493                 view.getDockableWindowManager()
494                     .floatDockableWindow(dockable);
495             } //}}}
496

497             //{{{ getCode() method
498
public String JavaDoc getCode()
499             {
500                 return "view.getDockableWindowManager()"
501                     + ".floatDockableWindow(\"" + dockable + "\");";
502             } //}}}
503
} //}}}
504
} //}}}
505

506     private static DockableWindowFactory instance;
507     private HashMap JavaDoc<String JavaDoc, Window> dockableWindowFactories;
508 }
509
Popular Tags