XML
  Home arrow XML arrow Page 4 - 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: Save


    (Page 4 of 4 )

    Next is the very similar save file dialog, which is basically a different implantation of the same underlying structure used in the open file dialog.  Generating an instance of the save file dialog is almost identical to the open dialog. All that is different is the string displayed in the dialog's title bar and the interface mode:

    function saveDialog() {
      const CC = Components.classes, CI = Components.interfaces;
      var fp = CC["@mozilla.org/filepicker;1"].createInstance(CI.nsIFilePicker);
      fp.init(window, "Pick a location to save to",
    CI.nsIFilePicker.modeSave);
      fp.appendFilters(CI.nsIFilePicker.filterAll | CI.nsIFilePicker.filterText | CI.nsIFilePicker.filterXUL |
    CI.nsIFilePicker.filterXML | CI.nsIFilePicker.filterHTML);
      var rv = fp.show();

    We still encase the remainder of the function in an if loop so that it is executed on the OK button click, and we still get the file from fp.file.  This however is where the similarities end:

    if (rv == CI.nsIFilePicker.returnOK) {
        var file = fp.file;
        var foStream = CC["@mozilla.org/network/file-output-
    stream;1"].createInstance(CI.nsIFileOutputStream);

    This time, we implement the NSIFileOutputStream interface from the ["@mozilla.org/network/fileOutputStream;1"] component.  In order to save data to a file, you need to create an output stream and pass the data to be saved into it.  Once we have set the interface to a variable, we call the initialize method:

    foStream.init(file, 0x02 | 0x08, 0664, null);

    The first parameter is what we want to initialize, in this case the file.  The next parameter states that the file should be in write mode, or if it does not exist, it should be created.  The third parameter deals with permissions, and the final parameter is always set to null as it is not yet supported.

    Next, we want to get the content of our editor. To do this, you first need to get the editor itself, then the contents of the editor:

        var text = document.getElementById('mainContent');
        var text = data.contentDocument.body.textContent;

    The text in your editor is actually HTML, so to get the actual words that are typed to save correctly, you need to get them from the <body> element, which the second line above will do.

    All you need to do now is write the data and close the output stream:

        foStream.write(text, text.length);
       foStream.close();

    You now have a way of opening files, displaying (and modifying) their content, and saving files.  Our File menu is now completely scripted with all of the required behaviors, although there is still more work to be done. We still need to code the Edit menuitems, again using XPCOM to handle the clipboard actions, and we still need to create our help window and script the calls to open it.  All of this and more will be discussed in the next article.   


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 4 hosted by Hostway