XML
  Home arrow XML arrow Page 3 - Back to XUL: JavaScript Logic
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Back to XUL: JavaScript Logic
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2006-02-06

    Table of Contents:
  • Back to XUL: JavaScript Logic
  • Scripting the Menubar: Close, Exit and New
  • Scripting the Menubar: Open
  • Scripting the Menubar: Save

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Back to XUL: JavaScript Logic - Scripting the Menubar: Open


    (Page 3 of 4 )

    So now the File menu is fully coded except for the open and save actions.  As I said before, these actions will need to make use of XPCOM (the cross-platform component object model) in order to function correctly.  We are going to conjure up from nowhere an instance of the Windows OS specific file open or file save dialog box and allow the user to either pick a file for opening, or choose a location for saving files.  XPCOM consists of a series of Mozilla components, and each of these components use a variety of interfaces to perform certain functions.  We are going to need to generate instances of several interfaces to execute the desired function, and so will need to first implement the required components.

    We begin by opening the function definition and creating two constants:

    function openDialog()  {
      const CC = Components.classes, CI = Components.interfaces;

    Creating these constants is just a shortcut to save on typing and the size of the function.   Next, you need to set a variable to hold the component and interface instance:

      var fp = CC["@mozilla.org/filepicker;1"].createInstance(CI.nsIFilePicker);

    The fp is short for filepicker by the way.  We then call the init method to initialize the dialog box and provide some parameters that control its appearance:

      fp.init(window, "Pick a file to open", CI.nsIFilePicker.modeOpen);

    Filters can be used to tell the dialog which file types to use in the Files of Type listbox inherent in the dialog:

      fp.appendFilters(CI.nsIFilePicker.filterAll | CI.nsIFilePicker.filterText | CI.nsIFilePicker.filterXUL | CI.nsIFilePicker.filterXML | CI.nsIFilePicker.filterHTML);

    To actually display the dialog on screen, you simply set a new variable and call the show(); method:

      var rv = fp.show();

    This will render the open file dialog box.  We encase the next section within an if loop to ensure that the rest of the function is only executed if the user clicks OK in the open dialog:

      if (rv == CI.nsIFilePicker.returnOK) {

    The file that a user selects is held in the file function and is set to a variable simply called file:

      var file = fp.file;

    To handle the file that is selected, we now need to implement another of Mozilla’s components, the io-service component:

      var ios = CC["@mozilla.org/network/io-service;1"].getService(CI.nsIIOService);

    The ios variable will now hold the required component and interface, however, we will also need another interface to open a file: the nsIFileProtocolHandler.  This interface provides the method that we need to convert the file that was selected into a URL string, which is used to open the file within the application.  We can get this using the QueryInterface method, specifying which interface we want:

      var fileHandler = ios.getProtocolHandler("file").QueryInterface(CI.nsIFileProtocolHandler);

    To get the URL, call the getURLSpecFromFile method on the variable holding the file:

      var URLSpec = fileHandler.getURLSpecFromFile(file);

    We now need to specify where we are going to put the file in this case, it is into the editor:

      var editor = document.getElementById("mainContent");

    Finally, we use the loadURI method to open the file and close the if statement and function:

      editor.webNavigation.loadURI(URLSpec, 0, null, null, null);
      }
    }

    The webNavigation function provides the method we need and is available to all interfaces.  Add the caller to the Open  menuitem:

    Oncommand="openDialog()"

    More XML Articles
    More By Dan Wellman


       · Hi everyone,This article is where things start to get interesting because it...
     

    XML ARTICLES

    - Datatypes and More in RELAX NG
    - Providing Options in RELAX NG
    - An Introduction to RELAX NG
    - Path, Predicates, and XQuery
    - Using Predicates with XQuery
    - Navigating Input Documents Using Paths
    - XML Basics
    - Introduction to XPath
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway