home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / AFC / JNotepad / src / JNotePad.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  7.8 KB  |  302 lines

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4.  
  5.  
  6. import java.applet.*;
  7. import java.awt.*;
  8. import com.ms.ui.*;
  9. import com.ms.fx.*;
  10. import JNotePadFrame;
  11. import com.ms.ui.resource.*;
  12. import java.io.InputStream;
  13.  
  14.  
  15. /** 
  16. * A text editor written in Java. A cross between Wordpad and Notepad
  17. * with extra features, many of them user customizable. 
  18. * <p>
  19. * This is the main class which starts JNotepad and gets everything else
  20. * running. This is a little different than traditional Java applets. This
  21. * is because we want to use the AFC UIApplet class, but browsers don't 
  22. * recognize this because they expect an Applet. Our solution is to use a
  23. * AwtUIApplet wrapper. AwtUIApplet acts like an Applet object, so browsers
  24. * can run it, and it supports AFC. Here we use a private UIApplet class, JNotePadImpl,
  25. * to do all of the work necessary to run the applet, and wrap it inside
  26. * the AwtUIApplet class JNotepad. We create a copy of JNotepadImpl and
  27. * send it to the AwtUIApplet constructor. This gives us a UIApplet which
  28. * will run in a browser. See UIApplet and AwtUIApplet for more detail.
  29. * <p>
  30. * Copyright (C) 1997, Microsoft Corp. 
  31. * <p>
  32. *
  33. * @version     1.0, 7/9/97
  34. */
  35.  
  36. public class JNotePad extends AwtUIApplet
  37. {
  38.     /**
  39.     *    Creates a new JNotepad object.
  40.     */
  41.     public JNotePad()
  42.     {
  43.         super(new JNotePadImpl());
  44.     }
  45.     
  46.     /**
  47.     *    Program entry point. Creates an instance of the real JNotePad application
  48.     *    object, and runs it the same way as the system runs an applet.
  49.     * 
  50.     * @param args[] Array of command line arguments
  51.     */
  52.     public static void main(String args[])
  53.     {
  54.         // set sFilename to filename to load into JNotepad        
  55.         
  56.         JNotePadImpl theApp = new JNotePadImpl();
  57.     
  58.         theApp.args=args;
  59.  
  60.         if (args.length > 0)
  61.         {
  62.             theApp.sFilename = args[0];                
  63.             theApp.isApplet = false;
  64.         }
  65.         
  66.         theApp.init();
  67.         theApp.start();
  68.     }
  69.     
  70.     private static Win32ResourceDecoder resources;
  71.     public static Win32ResourceDecoder getResources()
  72.     {
  73.         if(resources==null)
  74.         {
  75.             try
  76.             {
  77.                 WhereAmI here=new WhereAmI();
  78.                 Class cl=here.getClass();
  79.                 InputStream is = com.ms.fx.FxToolkit.getSystemInterface().getResourceAsStream(cl, "jnotepad.res" );
  80.  
  81.                 resources =new Win32ResourceDecoder( is );
  82.             }
  83.             catch(Exception e)
  84.             {
  85.                 // no resource file
  86.                 UIMessageBox box = new UIMessageBox(new UIFrame(), 
  87.                     JNotePad.loadString(ResourceIDs.IDS_MSGINSTALLERR),
  88.                     JNotePad.loadString(ResourceIDs.IDS_MSGNORES),
  89.                     box.STOP, UIButtonBar.OK);
  90.                 box.doModal();
  91.                 return null;
  92.             }
  93.         }
  94.         return resources;
  95.     }
  96.  
  97.     public static String loadString(int id)
  98.     {
  99.         Win32ResourceDecoder res=getResources();
  100.         try
  101.         {
  102.             return res.getString(id);
  103.         }
  104.         catch (Exception e)
  105.         {
  106.             return null;
  107.         }
  108.     }
  109.  
  110.     public static UIMenuItem loadMenuItem(int stringID, int cmdID)
  111.     {
  112.         UIMenuItem mi=new UIMenuItem(loadString(stringID));
  113.         mi.setID(cmdID);
  114.         return mi;
  115.     }
  116.  
  117.     public static UIMenuItem loadMenuItem(int id)
  118.     {
  119.         return loadMenuItem(id,id);
  120.     }
  121. }
  122.  
  123.  
  124. /**
  125. *    Class which implements JNotepad. 
  126. */
  127. class JNotePadImpl extends UIApplet 
  128. {        
  129.     String sFilename;    
  130.     boolean isApplet;
  131.     String args[];
  132.  
  133.     
  134.     public JNotePadImpl()
  135.     {
  136.         sFilename = null;
  137.         isApplet = false;
  138.     }
  139.     
  140.     public void init()
  141.     {
  142.         // we do nothing in the init() function
  143.     }
  144.     
  145.     public void start()
  146.     {
  147.         // Create Toplevel Window
  148.         JNotePadFrame frame = new JNotePadFrame("JNotePad", sFilename);
  149.  
  150.         frame.show();        
  151.     }
  152.     
  153.     /** 
  154.     * getAppletInfo() returns information about the applet.
  155.     */
  156.     public String getAppletInfo()
  157.     {
  158.         return "Name: JNotepad\r\n" +
  159.             "Copyright (C) 1997 Microsoft Corp.\r\n";
  160.     }    
  161. }
  162.  
  163. interface ResourceIDs
  164. {
  165.     static final int ID_HELP_ABOUT=4020;
  166.     static final int ID_PROPERTIES=4005;
  167.     static final int ID_TIMEDATE=4014;
  168.     static final int ID_EMACS_CUT=-1;
  169.     static final int ID_EMACS_PASTE=-2;
  170.  
  171.     static final int ID_FILE_NEW=4021;
  172.     static final int ID_FILE_OPEN=4022;
  173.     static final int ID_FILE_SAVE=4023;
  174.     static final int ID_FILE_SAVEAS=4024;
  175.     static final int ID_FILE_CLOSE=4003;
  176.     static final int ID_FILE_PRINT=-3;
  177.     static final int ID_FILE_EXIT=4007;
  178.         
  179.     static final int ID_EDIT_FIND=4017;
  180.     static final int ID_EDIT_REPLACE=4019;
  181.     static final int ID_EDIT_CUT=1001;
  182.     static final int ID_EDIT_COPY=1002;
  183.     static final int ID_EDIT_PASTE=1003;
  184.     static final int ID_EDIT_DELETE=4012;
  185.     static final int ID_EDIT_UNDO=4026;
  186.     static final int ID_EDIT_REDO=4004;
  187.     static final int ID_EDIT_SELECT_ALL=4013;
  188.  
  189.     static final int ID_WORDWRAPMENU=4006;
  190.     static final int ID_NOWORDWRAP=5000; // dynamically generated
  191.     static final int ID_WORDWRAP=5001;    // dynamically generated
  192.     static final int ID_WORDWRAP2=5002;    // dynamically generated
  193.  
  194.     static final int ID_FONT=4001;
  195.     static final int ID_TEXTCOLOR=4002;
  196.     static final int ID_BGCOLOR=4011;
  197.  
  198.     static final int ID_WINDOW_PREVIOUS=4027;
  199.     static final int ID_WINDOW_NEXT=4015;
  200.  
  201.     //
  202.     // Context menu goo.
  203.     //
  204.     static final int ID_LAUNCH=3000;
  205.  
  206.     //
  207.     // Strings
  208.     //
  209.     static final int IDS_NEW=101;
  210.     static final int IDS_OPEN=102;
  211.     static final int IDS_SAVE=103;
  212.     static final int IDS_CLOSE=104;
  213.  
  214.     static final int IDS_UNDO=105;
  215.     static final int IDS_REDO=106;
  216.     static final int IDS_CUT=107;
  217.     static final int IDS_COPY=108;
  218.     static final int IDS_PASTE=109;
  219.     static final int IDS_DELETE=110;
  220.     static final int IDS_SELECT_ALL=121;
  221.  
  222.     static final int IDS_FONT=111;
  223.     static final int IDS_TEXTCOLOR=112;
  224.     static final int IDS_BGCOLOR=113;
  225.  
  226.     static final int IDS_LAUNCHSELLINK=114;
  227.  
  228.     static final int IDS_FIND=115;
  229.     static final int IDS_REPLACE=116;
  230.  
  231.     static final int IDS_WORDWRAPMENU = 117;
  232.     static final int IDS_NOWORDWRAP = 118;
  233.     static final int IDS_WORDWRAP = 119;
  234.     static final int IDS_WORDWRAP2 = 120;
  235.  
  236.     // clipboard strings
  237.     static final int IDS_LOCALCLIPBOARD=124;
  238.     static final int IDS_CLIPBOARD=125;
  239.     static final int IDS_INVALIDCLIPBOARDDATA=126;
  240.     static final int IDS_EMPTYCLIPBOARD=127;
  241.     static final int IDS_CLIPBOARDIMG=128;
  242.  
  243.     // misc strings
  244.     static final int IDS_FILEIMG=129;
  245.     static final int IDS_COLORCHOICEFGTIP=130;
  246.     static final int IDS_COLORCHOICEBGTIP=131;
  247.     static final int IDS_JNOTEPADICON=132;
  248.     static final int IDS_NEWFILETITLE=133;
  249.     static final int IDS_UNTITLEDFILE=239;
  250.     static final int IDS_FINDREPLACE=238;
  251.  
  252.     // message box text
  253.  
  254.     static final int IDS_MSGTITLE=122;
  255.     static final int IDS_MSGREADONLY=123;
  256.     static final int IDS_MSGNOSAVE=200;
  257.     static final int IDS_MSGFILENOTFOUND=202;
  258.     static final int IDS_MSGFILEREAD=203;
  259.     static final int IDS_MSGSAVEERR=204;
  260.     static final int IDS_MSGNODIALOG=206;
  261.     static final int IDS_MSGFINDTEXT=207;
  262.     static final int IDS_MSGSEARCHDONE=208;
  263.     static final int IDS_MSGNOMATCH=211;
  264.     static final int IDS_MSGREPLACEDONE=210;
  265.     static final int IDS_MSGMENULOAD=213;
  266.     static final int IDS_MSGMENUINIT=212;
  267.     static final int IDS_MSGINTERNALERR=214;
  268.     static final int IDS_MSGINSTALLERR=215;
  269.     static final int IDS_MSGNORES=216;
  270.     static final int IDS_MSGINIACCELERR=217;
  271.     static final int IDS_MSGINIFONTERR=218;
  272.     static final int IDS_MSGNOFEATURE=219;
  273.     static final int IDS_MSGNOFEATUREINIT=220;
  274.     static final int IDS_MSGINITOOLBARERR=221;
  275.     static final int IDS_MSGINITIMGERR=221;
  276.     static final int IDS_MSGINICOLORERR=222;
  277.     static final int IDS_MSGLAUNCH=223;
  278.     static final int IDS_MSGLAUNCHANNOC=224;
  279.     static final int IDS_MSGLAUNCHERR=225;
  280.     static final int IDS_MSGNOTRUNNABLE=226;
  281.     static final int IDS_MSGFILERUNERR=227;
  282.     static final int IDS_MSGRUNERROR=228;
  283.     static final int IDS_MSGNOSETTINGS=229;
  284.     static final int IDS_MSGSETTINGSERR=230;
  285.     static final int IDS_MSGSETTINGSSAVEERR=231;
  286.     static final int IDS_MSGSAVEFILE=232;
  287.     static final int IDS_MSGSAVECONFIRM=233;    
  288.     static final int IDS_MSGREPLACETEXT=236;
  289.     static final int IDS_MSGLOADFILE=237;
  290.     static final int IDS_MSGOKCANCEL=240;
  291.     static final int IDS_MSGLINKTO=241;
  292.  
  293. }
  294.  
  295.  
  296. class WhereAmI
  297. {
  298.     public WhereAmI()
  299.     {
  300.     }
  301. }
  302.