KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > apps > svgbrowser > Main


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.apps.svgbrowser;
19
20 import java.awt.Dimension JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.Font JavaDoc;
23 import java.io.BufferedReader JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.io.UnsupportedEncodingException JavaDoc;
30 import java.io.Writer JavaDoc;
31 import java.net.Authenticator JavaDoc;
32 import java.net.URLDecoder JavaDoc;
33 import java.net.URLEncoder JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.LinkedList JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Locale JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.ResourceBundle JavaDoc;
41 import java.util.StringTokenizer JavaDoc;
42 import java.util.Vector JavaDoc;
43
44 import javax.swing.AbstractAction JavaDoc;
45 import javax.swing.Action JavaDoc;
46 import javax.swing.ImageIcon JavaDoc;
47 import javax.swing.JOptionPane JavaDoc;
48 import javax.swing.JProgressBar JavaDoc;
49 import javax.swing.UIManager JavaDoc;
50 import javax.swing.plaf.FontUIResource JavaDoc;
51
52 import org.apache.batik.swing.JSVGCanvas;
53 import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
54 import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
55 import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
56 import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
57 import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
58 import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
59 import org.apache.batik.util.ApplicationSecurityEnforcer;
60 import org.apache.batik.util.ParsedURL;
61 import org.apache.batik.util.SVGConstants;
62 import org.apache.batik.util.XMLResourceDescriptor;
63 import org.apache.batik.util.gui.resource.ResourceManager;
64
65 /**
66  * This class contains the main method of an SVG viewer.
67  *
68  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
69  * @version $Id: Main.java,v 1.53 2005/03/29 10:48:02 deweese Exp $
70  */

71 public class Main implements Application {
72     /**
73      * Extension used in addition to the scriptType value
74      * to read from the PreferenceManager whether or not the
75      * scriptType can be loaded.
76      */

77     public static final String JavaDoc UNKNOWN_SCRIPT_TYPE_LOAD_KEY_EXTENSION
78         = ".load";
79
80     /**
81      * User home property
82      */

83     public static final String JavaDoc PROPERTY_USER_HOME = "user.home";
84
85     /**
86      * System property for specifying an additional policy file.
87      */

88     public static final String JavaDoc PROPERTY_JAVA_SECURITY_POLICY
89         = "java.security.policy";
90
91     /**
92      * Batik configuration sub-directory
93      */

94     public static final String JavaDoc BATIK_CONFIGURATION_SUBDIRECTORY = ".batik";
95
96     /**
97      * Name of the Squiggle configuration file
98      */

99     public static final String JavaDoc SQUIGGLE_CONFIGURATION_FILE = "preferences.xml";
100
101     /**
102      * Name of the Squiggle policy file
103      */

104     public static final String JavaDoc SQUIGGLE_POLICY_FILE = "__svgbrowser.policy";
105
106     /**
107      * Entry for granting network access to scripts
108      */

109     public static final String JavaDoc POLICY_GRANT_SCRIPT_NETWORK_ACCESS
110         = "grant {\n permission java.net.SocketPermission \"*\", \"listen, connect, resolve, accept\";\n};\n\n";
111
112     /**
113      * Entry for granting file system access to scripts
114      */

115     public static final String JavaDoc POLICY_GRANT_SCRIPT_FILE_ACCESS
116         = "grant {\n permission java.io.FilePermission \"<<ALL FILES>>\", \"read\";\n};\n\n";
117
118     /**
119      * Entry for the list of recently visited URI
120      */

121     public static final String JavaDoc PREFERENCE_KEY_VISITED_URI_LIST
122         = "preference.key.visited.uri.list";
123
124     /**
125      * Entry for the maximum number of last visited URIs
126      */

127     public static final String JavaDoc PREFERENCE_KEY_VISITED_URI_LIST_LENGTH
128         = "preference.key.visited.uri.list.length";
129
130     /**
131      * List of separators between URI values in the preference
132      * file
133      */

134     public static final String JavaDoc URI_SEPARATOR = " ";
135
136     /**
137      * Default font-family value.
138      */

139     public static final String JavaDoc DEFAULT_DEFAULT_FONT_FAMILY
140         = "Arial, Helvetica, sans-serif";
141
142     /**
143      * SVG initialization file, used to trigger loading of most of
144      * the Batik classes
145      */

146     public static final String JavaDoc SVG_INITIALIZATION = "resources/init.svg";
147
148     /**
149      * Stores the initialization file URI
150      */

151     protected String JavaDoc svgInitializationURI;
152
153     /**
154      * Creates a viewer frame and shows it..
155      * @param args The command-line arguments.
156      */

157     public static void main(String JavaDoc[] args) {
158         new Main(args);
159     }
160
161     /**
162      * The gui resources file name
163      */

164     public final static String JavaDoc RESOURCES =
165         "org.apache.batik.apps.svgbrowser.resources.Main";
166
167     /**
168      * URL for Squiggle's security policy file
169      */

170     public static final String JavaDoc SQUIGGLE_SECURITY_POLICY
171         = "org/apache/batik/apps/svgbrowser/resources/svgbrowser.policy";
172
173     /**
174      * The resource bundle
175      */

176     protected static ResourceBundle JavaDoc bundle;
177
178     /**
179      * The resource manager
180      */

181     protected static ResourceManager resources;
182     static {
183         bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault());
184         resources = new ResourceManager(bundle);
185     }
186
187     /**
188      * The frame's icon.
189      */

190     protected static ImageIcon JavaDoc frameIcon = new ImageIcon JavaDoc
191         (Main.class.getResource(resources.getString("Frame.icon")));
192
193     /**
194      * The preference manager.
195      */

196     protected XMLPreferenceManager preferenceManager;
197
198     /**
199      * Maximum number of recently visited URIs
200      */

201     public static final int MAX_VISITED_URIS = 10;
202
203     /**
204      * The array of last visited URIs
205      */

206     protected Vector JavaDoc lastVisited = new Vector JavaDoc();
207
208     /**
209      * The actual allowed maximum number of last visited URIs
210      */

211     protected int maxVisitedURIs = MAX_VISITED_URIS;
212
213     /**
214      * The arguments.
215      */

216     protected String JavaDoc[] arguments;
217
218     /**
219      * Controls whether the application can override the
220      * system security policy property. This is done when there
221      * was no initial security policy specified when the application
222      * stated, in which case Batik will use that property.
223      */

224     protected boolean overrideSecurityPolicy = false;
225
226     /**
227      * Script security enforcement is delegated to the
228      * security utility
229      */

230     protected ApplicationSecurityEnforcer securityEnforcer;
231
232     /**
233      * The option handlers.
234      */

235     protected Map JavaDoc handlers = new HashMap JavaDoc();
236     {
237         handlers.put("-font-size", new FontSizeHandler());
238     }
239
240     /**
241      * The viewer frames.
242      */

243     protected List JavaDoc viewerFrames = new LinkedList JavaDoc();
244
245     /**
246      * The preference dialog.
247      */

248     protected PreferenceDialog preferenceDialog;
249
250     /**
251      * Creates a new application.
252      * @param args The command-line arguments.
253      */

254     public Main(String JavaDoc[] args) {
255         arguments = args;
256
257         //
258
// Preferences
259
//
260
Map JavaDoc defaults = new HashMap JavaDoc(11);
261
262         defaults.put(PreferenceDialog.PREFERENCE_KEY_LANGUAGES,
263                      Locale.getDefault().getLanguage());
264         defaults.put(PreferenceDialog.PREFERENCE_KEY_SHOW_RENDERING,
265                      Boolean.FALSE);
266         defaults.put(PreferenceDialog.PREFERENCE_KEY_AUTO_ADJUST_WINDOW,
267                      Boolean.TRUE);
268         defaults.put(PreferenceDialog.PREFERENCE_KEY_SELECTION_XOR_MODE,
269                      Boolean.FALSE);
270         defaults.put(PreferenceDialog.PREFERENCE_KEY_ENABLE_DOUBLE_BUFFERING,
271                      Boolean.TRUE);
272         defaults.put(PreferenceDialog.PREFERENCE_KEY_SHOW_DEBUG_TRACE,
273                      Boolean.FALSE);
274         defaults.put(PreferenceDialog.PREFERENCE_KEY_PROXY_HOST,
275                      "");
276         defaults.put(PreferenceDialog.PREFERENCE_KEY_PROXY_PORT,
277                      "");
278         defaults.put(PreferenceDialog.PREFERENCE_KEY_CSS_MEDIA,
279                      "screen");
280         defaults.put(PreferenceDialog.PREFERENCE_KEY_DEFAULT_FONT_FAMILY,
281                      DEFAULT_DEFAULT_FONT_FAMILY);
282         defaults.put(PreferenceDialog.PREFERENCE_KEY_IS_XML_PARSER_VALIDATING,
283                      Boolean.FALSE);
284         defaults.put(PreferenceDialog.PREFERENCE_KEY_ENFORCE_SECURE_SCRIPTING,
285                      Boolean.TRUE);
286         defaults.put(PreferenceDialog.PREFERENCE_KEY_GRANT_SCRIPT_FILE_ACCESS,
287                      Boolean.FALSE);
288         defaults.put(PreferenceDialog.PREFERENCE_KEY_GRANT_SCRIPT_NETWORK_ACCESS,
289                      Boolean.FALSE);
290         defaults.put(PreferenceDialog.PREFERENCE_KEY_LOAD_JAVA,
291                      Boolean.TRUE);
292         defaults.put(PreferenceDialog.PREFERENCE_KEY_LOAD_ECMASCRIPT,
293                      Boolean.TRUE);
294         defaults.put(PreferenceDialog.PREFERENCE_KEY_ALLOWED_SCRIPT_ORIGIN,
295                      new Integer JavaDoc(ResourceOrigin.DOCUMENT));
296         defaults.put(PreferenceDialog.PREFERENCE_KEY_ALLOWED_EXTERNAL_RESOURCE_ORIGIN,
297                      new Integer JavaDoc(ResourceOrigin.ANY));
298         defaults.put(PREFERENCE_KEY_VISITED_URI_LIST,
299                      "");
300         defaults.put(PREFERENCE_KEY_VISITED_URI_LIST_LENGTH,
301                      new Integer JavaDoc(MAX_VISITED_URIS));
302     
303         securityEnforcer
304             = new ApplicationSecurityEnforcer(this.getClass(),
305                                               SQUIGGLE_SECURITY_POLICY);
306
307         try {
308             preferenceManager = new XMLPreferenceManager(SQUIGGLE_CONFIGURATION_FILE,
309                                                          defaults);
310             String JavaDoc dir = System.getProperty(PROPERTY_USER_HOME);
311             File JavaDoc f = new File JavaDoc(dir, BATIK_CONFIGURATION_SUBDIRECTORY);
312             f.mkdir();
313             XMLPreferenceManager.setPreferenceDirectory(f.getCanonicalPath());
314             preferenceManager.load();
315             setPreferences();
316             initializeLastVisited();
317             Authenticator.setDefault(new JAuthenticator());
318         } catch (Exception JavaDoc e) {
319             e.printStackTrace();
320         }
321
322         //
323
// Initialization
324
//
325
final AboutDialog initDialog = new AboutDialog();
326         final JProgressBar JavaDoc pb = new JProgressBar JavaDoc(0, 3);
327         initDialog.getContentPane().add("South", pb);
328
329         // Work around pack() bug on some platforms
330
Dimension JavaDoc ss = initDialog.getToolkit().getScreenSize();
331         Dimension JavaDoc ds = initDialog.getPreferredSize();
332
333         initDialog.setLocation((ss.width - ds.width) / 2,
334                                (ss.height - ds.height) / 2);
335
336         initDialog.setSize(ds);
337         initDialog.setVisible(true);
338
339         final JSVGViewerFrame v = new JSVGViewerFrame(this);
340         JSVGCanvas c = v.getJSVGCanvas();
341         c.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
342             public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
343                 pb.setValue(1);
344             }
345             public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
346                 pb.setValue(2);
347             }
348         });
349         c.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
350             public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
351                 pb.setValue(3);
352             }
353         });
354         c.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
355             public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
356                 initDialog.dispose();
357                 v.dispose();
358                 System.gc();
359                 run();
360             }
361         });
362
363         c.setSize(100, 100);
364         svgInitializationURI = Main.class.getResource(SVG_INITIALIZATION).toString();
365         c.loadSVGDocument(svgInitializationURI);
366     }
367
368     /**
369      * Installs a custom policy file in the '.batik' directory. This is initialized
370      * with the content of the policy file coming with the distribution
371      */

372     public void installCustomPolicyFile() throws IOException JavaDoc {
373         String JavaDoc securityPolicyProperty
374             = System.getProperty(PROPERTY_JAVA_SECURITY_POLICY);
375
376         if (overrideSecurityPolicy
377             ||
378             securityPolicyProperty == null
379             ||
380             "".equals(securityPolicyProperty)) {
381             // Access default policy file
382
ParsedURL policyURL = new ParsedURL(securityEnforcer.getPolicyURL());
383             
384             // Override the user policy
385
String JavaDoc dir = System.getProperty(PROPERTY_USER_HOME);
386             File JavaDoc batikConfigDir = new File JavaDoc(dir, BATIK_CONFIGURATION_SUBDIRECTORY);
387             File JavaDoc policyFile = new File JavaDoc(batikConfigDir, SQUIGGLE_POLICY_FILE);
388             
389             // Copy original policy file into local policy file
390
Reader JavaDoc r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(policyURL.openStream()));
391             Writer JavaDoc w = new FileWriter JavaDoc(policyFile);
392             
393             char[] buf = new char[1024];
394             int n = 0;
395             while ( (n=r.read(buf, 0, buf.length)) != -1 ) {
396                 w.write(buf, 0, n);
397             }
398             
399             r.close();
400             
401             // Now, append additional grants depending on the security
402
// settings
403
boolean grantScriptNetworkAccess
404                 = preferenceManager.getBoolean
405                 (PreferenceDialog.PREFERENCE_KEY_GRANT_SCRIPT_NETWORK_ACCESS);
406             boolean grantScriptFileAccess
407                 = preferenceManager.getBoolean
408                 (PreferenceDialog.PREFERENCE_KEY_GRANT_SCRIPT_FILE_ACCESS);
409             
410             if (grantScriptNetworkAccess) {
411                 w.write(POLICY_GRANT_SCRIPT_NETWORK_ACCESS);
412             }
413             
414             if (grantScriptFileAccess) {
415                 w.write(POLICY_GRANT_SCRIPT_FILE_ACCESS);
416             }
417             
418             w.close();
419             
420             // We now use the JAVA_SECURITY_POLICY property, so
421
// we allow override on subsequent calls.
422
overrideSecurityPolicy = true;
423             
424             System.setProperty(PROPERTY_JAVA_SECURITY_POLICY,
425                                policyFile.toURL().toString());
426             
427         }
428     }
429
430     /**
431      * Runs the application.
432      */

433     public void run() {
434         try {
435             int i = 0;
436
437             for (; i < arguments.length; i++) {
438                 OptionHandler oh = (OptionHandler)handlers.get(arguments[i]);
439                 if (oh == null) {
440                     break;
441                 }
442                 i = oh.handleOption(i);
443             }
444
445             JSVGViewerFrame frame = createAndShowJSVGViewerFrame();
446             while (i < arguments.length) {
447                 if (arguments[i].length() == 0) {
448                     i++;
449                     continue;
450                 }
451
452                 File JavaDoc file = new File JavaDoc(arguments[i]);
453                 String JavaDoc uri = null;
454
455                 try{
456                     if (file.canRead()) {
457                         uri = file.toURL().toString();
458                     }
459                 }catch(SecurityException JavaDoc se){
460                     // Cannot access files.
461
}
462                 
463                 if(uri == null){
464                     uri = arguments[i];
465                     ParsedURL purl = null;
466                     purl = new ParsedURL(arguments[i]);
467
468                     if (!purl.complete())
469                         // This is not a valid uri
470
uri = null;
471                 }
472
473                 if (uri != null) {
474                     if (frame == null)
475                         frame = createAndShowJSVGViewerFrame();
476
477                     frame.showSVGDocument(uri);
478                     frame = null;
479                 } else {
480                     // Let the user know that we are
481
// skipping this file...
482

483                     // Note that frame may be null, which is
484
// a valid argument for showMessageDialog
485

486                     // NOTE: Need to revisit Resources/Messages usage to
487
// have a single entry point. Should have a
488
// formated message here instead of a + ...
489
JOptionPane.showMessageDialog
490                         (frame,
491                          resources.getString("Error.skipping.file")
492                          + arguments[i]);
493                 }
494                 i++;
495             }
496         } catch (Exception JavaDoc e) {
497             e.printStackTrace();
498             printUsage();
499         }
500     }
501
502     /**
503      * Prints the command line usage.
504      */

505     protected void printUsage() {
506         System.out.println();
507
508         System.out.println(resources.getString("Command.header"));
509         System.out.println(resources.getString("Command.syntax"));
510         System.out.println();
511         System.out.println(resources.getString("Command.options"));
512         Iterator JavaDoc it = handlers.keySet().iterator();
513         while (it.hasNext()) {
514             String JavaDoc s = (String JavaDoc)it.next();
515             System.out.println(((OptionHandler)handlers.get(s)).getDescription());
516         }
517     }
518
519     /**
520      * This interface represents an option handler.
521      */

522     protected interface OptionHandler {
523         /**
524          * Handles the current option.
525          * @return the index of argument just before the next one to handle.
526          */

527         int handleOption(int i);
528
529         /**
530          * Returns the option description.
531          */

532         String JavaDoc getDescription();
533     }
534
535     /**
536      * To handle the '-font-size' option.
537      */

538     protected class FontSizeHandler implements OptionHandler {
539         public int handleOption(int i) {
540             int size = Integer.parseInt(arguments[++i]);
541
542             Font JavaDoc font = new Font JavaDoc("Dialog", Font.PLAIN, size);
543             FontUIResource JavaDoc fontRes = new FontUIResource JavaDoc(font);
544             UIManager.put("CheckBox.font", fontRes);
545             UIManager.put("PopupMenu.font", fontRes);
546             UIManager.put("TextPane.font", fontRes);
547             UIManager.put("MenuItem.font", fontRes);
548             UIManager.put("ComboBox.font", fontRes);
549             UIManager.put("Button.font", fontRes);
550             UIManager.put("Tree.font", fontRes);
551             UIManager.put("ScrollPane.font", fontRes);
552             UIManager.put("TabbedPane.font", fontRes);
553             UIManager.put("EditorPane.font", fontRes);
554             UIManager.put("TitledBorder.font", fontRes);
555             UIManager.put("Menu.font", fontRes);
556             UIManager.put("TextArea.font", fontRes);
557             UIManager.put("OptionPane.font", fontRes);
558             UIManager.put("DesktopIcon.font", fontRes);
559             UIManager.put("MenuBar.font", fontRes);
560             UIManager.put("ToolBar.font", fontRes);
561             UIManager.put("RadioButton.font", fontRes);
562             UIManager.put("RadioButtonMenuItem.font", fontRes);
563             UIManager.put("ToggleButton.font", fontRes);
564             UIManager.put("ToolTip.font", fontRes);
565             UIManager.put("ProgressBar.font", fontRes);
566             UIManager.put("TableHeader.font", fontRes);
567             UIManager.put("Panel.font", fontRes);
568             UIManager.put("List.font", fontRes);
569             UIManager.put("ColorChooser.font", fontRes);
570             UIManager.put("PasswordField.font", fontRes);
571             UIManager.put("TextField.font", fontRes);
572             UIManager.put("Table.font", fontRes);
573             UIManager.put("Label.font", fontRes);
574             UIManager.put("InternalFrameTitlePane.font", fontRes);
575             UIManager.put("CheckBoxMenuItem.font", fontRes);
576
577             return i;
578         }
579         public String JavaDoc getDescription() {
580             return resources.getString("Command.font-size");
581         }
582     }
583
584     // Application ///////////////////////////////////////////////
585

586     /**
587      * Creates and shows a new viewer frame.
588      */

589     public JSVGViewerFrame createAndShowJSVGViewerFrame() {
590         JSVGViewerFrame mainFrame = new JSVGViewerFrame(this);
591         mainFrame.setSize(resources.getInteger("Frame.width"),
592                           resources.getInteger("Frame.height"));
593         mainFrame.setIconImage(frameIcon.getImage());
594         mainFrame.setTitle(resources.getString("Frame.title"));
595         mainFrame.setVisible(true);
596         viewerFrames.add(mainFrame);
597         setPreferences(mainFrame);
598         return mainFrame;
599     }
600
601     /**
602      * Closes the given viewer frame.
603      */

604     public void closeJSVGViewerFrame(JSVGViewerFrame f) {
605         f.getJSVGCanvas().stopProcessing();
606         viewerFrames.remove(f);
607         if (viewerFrames.size() == 0) {
608             System.exit(0);
609         }
610         f.dispose();
611     }
612
613     /**
614      * Creates a new application exit action.
615      */

616     public Action JavaDoc createExitAction(JSVGViewerFrame vf) {
617         return new AbstractAction JavaDoc() {
618                 public void actionPerformed(ActionEvent JavaDoc e) {
619                     System.exit(0);
620                 }
621             };
622     }
623
624     /**
625      * Opens the given link in a new window.
626      */

627     public void openLink(String JavaDoc url) {
628         JSVGViewerFrame f = createAndShowJSVGViewerFrame();
629         f.getJSVGCanvas().loadSVGDocument(url);
630     }
631
632     /**
633      * Returns the XML parser class name.
634      */

635     public String JavaDoc getXMLParserClassName() {
636         return XMLResourceDescriptor.getXMLParserClassName();
637     }
638
639     /**
640      * Returns true if the XML parser must be in validation mode, false
641      * otherwise.
642      */

643     public boolean isXMLParserValidating() {
644         return preferenceManager.getBoolean
645             (PreferenceDialog.PREFERENCE_KEY_IS_XML_PARSER_VALIDATING);
646     }
647
648     /**
649      * Shows the preference dialog.
650      */

651     public void showPreferenceDialog(JSVGViewerFrame f) {
652         if (preferenceDialog == null) {
653             preferenceDialog = new PreferenceDialog(preferenceManager);
654         }
655         if (preferenceDialog.showDialog() == PreferenceDialog.OK_OPTION) {
656             try {
657                 preferenceManager.save();
658                 setPreferences();
659             } catch (Exception JavaDoc e) {
660             }
661         }
662     }
663
664     private void setPreferences() throws IOException JavaDoc {
665         Iterator JavaDoc it = viewerFrames.iterator();
666         while (it.hasNext()) {
667             setPreferences((JSVGViewerFrame)it.next());
668         }
669
670         System.setProperty("proxyHost", preferenceManager.getString
671                            (PreferenceDialog.PREFERENCE_KEY_PROXY_HOST));
672         System.setProperty("proxyPort", preferenceManager.getString
673                            (PreferenceDialog.PREFERENCE_KEY_PROXY_PORT));
674
675         installCustomPolicyFile();
676
677         securityEnforcer.enforceSecurity
678             (preferenceManager.getBoolean
679              (PreferenceDialog.PREFERENCE_KEY_ENFORCE_SECURE_SCRIPTING)
680              );
681
682     }
683
684     private void setPreferences(JSVGViewerFrame vf) {
685         boolean db = preferenceManager.getBoolean
686             (PreferenceDialog.PREFERENCE_KEY_ENABLE_DOUBLE_BUFFERING);
687         vf.getJSVGCanvas().setDoubleBufferedRendering(db);
688         boolean sr = preferenceManager.getBoolean
689             (PreferenceDialog.PREFERENCE_KEY_SHOW_RENDERING);
690         vf.getJSVGCanvas().setProgressivePaint(sr);
691         boolean d = preferenceManager.getBoolean
692             (PreferenceDialog.PREFERENCE_KEY_SHOW_DEBUG_TRACE);
693         vf.setDebug(d);
694         boolean aa = preferenceManager.getBoolean
695             (PreferenceDialog.PREFERENCE_KEY_AUTO_ADJUST_WINDOW);
696         vf.setAutoAdjust(aa);
697         boolean dd = preferenceManager.getBoolean
698             (PreferenceDialog.PREFERENCE_KEY_SELECTION_XOR_MODE);
699     vf.getJSVGCanvas().setSelectionOverlayXORMode(dd);
700     }
701
702     /**
703      * Returns the user languages.
704      */

705     public String JavaDoc getLanguages() {
706         String JavaDoc s = preferenceManager.getString
707             (PreferenceDialog.PREFERENCE_KEY_LANGUAGES);
708         return (s == null)
709             ? Locale.getDefault().getLanguage()
710             : s;
711     }
712
713     /**
714      * Returns the user stylesheet uri.
715      * @return null if no user style sheet was specified.
716      */

717     public String JavaDoc getUserStyleSheetURI() {
718         return preferenceManager.getString
719             (PreferenceDialog.PREFERENCE_KEY_USER_STYLESHEET);
720     }
721
722     /**
723      * Returns the default value for the CSS
724      * "font-family" property
725      */

726     public String JavaDoc getDefaultFontFamily() {
727         return preferenceManager.getString
728             (PreferenceDialog.PREFERENCE_KEY_DEFAULT_FONT_FAMILY);
729     }
730
731     /**
732      * Returns the CSS media to use.
733      * @return empty string if no CSS media was specified.
734      */

735     public String JavaDoc getMedia() {
736         String JavaDoc s = preferenceManager.getString
737             (PreferenceDialog.PREFERENCE_KEY_CSS_MEDIA);
738         return (s == null) ? "screen" : s;
739     }
740
741     /**
742      * Returns true if the selection overlay is painted in XOR mode, false
743      * otherwise.
744      */

745     public boolean isSelectionOverlayXORMode() {
746         return preferenceManager.getBoolean
747             (PreferenceDialog.PREFERENCE_KEY_SELECTION_XOR_MODE);
748     }
749
750     /**
751      * Returns true if the input scriptType can be loaded in
752      * this application.
753      */

754     public boolean canLoadScriptType(String JavaDoc scriptType){
755         if (SVGConstants.SVG_SCRIPT_TYPE_ECMASCRIPT.equals(scriptType)){
756             return preferenceManager.getBoolean
757                 (PreferenceDialog.PREFERENCE_KEY_LOAD_ECMASCRIPT);
758         } else if (SVGConstants.SVG_SCRIPT_TYPE_JAVA.equals(scriptType)){
759             return preferenceManager.getBoolean
760                 (PreferenceDialog.PREFERENCE_KEY_LOAD_JAVA);
761         } else {
762             return preferenceManager.getBoolean
763                 (scriptType + UNKNOWN_SCRIPT_TYPE_LOAD_KEY_EXTENSION);
764         }
765     }
766
767     /**
768      * Returns the allowed origins for scripts.
769      * @see ResourceOrigin
770      */

771     public int getAllowedScriptOrigin() {
772         int ret = preferenceManager.getInteger
773             (PreferenceDialog.PREFERENCE_KEY_ALLOWED_SCRIPT_ORIGIN);
774
775         return ret;
776     }
777
778     /**
779      * Returns the allowed origins for external
780      * resources.
781      * @see ResourceOrigin
782      */

783     public int getAllowedExternalResourceOrigin() {
784         int ret = preferenceManager.getInteger
785             (PreferenceDialog.PREFERENCE_KEY_ALLOWED_EXTERNAL_RESOURCE_ORIGIN);
786
787         return ret;
788     }
789
790     /**
791      * Notifies Application of recently visited URI
792      */

793     public void addVisitedURI(String JavaDoc uri) {
794         if(svgInitializationURI.equals(uri)) {
795             return;
796         }
797         
798         int maxVisitedURIs =
799             preferenceManager.getInteger
800             (PREFERENCE_KEY_VISITED_URI_LIST_LENGTH);
801         
802         if (maxVisitedURIs < 0) {
803             maxVisitedURIs = 0;
804         }
805
806         if (lastVisited.contains(uri)) {
807             lastVisited.removeElement(uri);
808         }
809
810         while (lastVisited.size() > 0 && lastVisited.size() > (maxVisitedURIs-1)) {
811             lastVisited.removeElementAt(0);
812         }
813
814         if (maxVisitedURIs > 0) {
815             lastVisited.addElement(uri);
816         }
817
818         // Now, save the list of visited URL into the preferences
819
StringBuffer JavaDoc lastVisitedBuffer = new StringBuffer JavaDoc();
820
821         for (int i=0; i<lastVisited.size(); i++) {
822             lastVisitedBuffer.append
823                 (URLEncoder.encode(lastVisited.elementAt(i).toString()));
824             lastVisitedBuffer.append(URI_SEPARATOR);
825         }
826         
827         preferenceManager.setString
828             (PREFERENCE_KEY_VISITED_URI_LIST,
829              lastVisitedBuffer.toString());
830
831         try {
832             preferenceManager.save();
833         } catch (Exception JavaDoc e) {
834             // As in other places. But this is ugly...
835
}
836     }
837
838     /**
839      * Asks Application for a list of recently visited URI.
840      */

841     public String JavaDoc[] getVisitedURIs() {
842         String JavaDoc[] visitedURIs = new String JavaDoc[lastVisited.size()];
843         lastVisited.copyInto(visitedURIs);
844         return visitedURIs;
845     }
846
847     /**
848      * Initializes the lastVisited array
849      */

850     protected void initializeLastVisited(){
851         String JavaDoc lastVisitedStr
852             = preferenceManager.getString(PREFERENCE_KEY_VISITED_URI_LIST);
853
854         StringTokenizer JavaDoc st
855             = new StringTokenizer JavaDoc(lastVisitedStr,
856                                   URI_SEPARATOR);
857
858         int n = st.countTokens();
859
860         int maxVisitedURIs
861             = preferenceManager.getInteger
862             (PREFERENCE_KEY_VISITED_URI_LIST_LENGTH);
863
864         if (n > maxVisitedURIs) {
865             n = maxVisitedURIs;
866         }
867
868         for (int i=0; i<n; i++) {
869                 lastVisited.addElement(URLDecoder.decode(st.nextToken()));
870         }
871     }
872 }
873
Popular Tags