home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / afc11 / JNotepad / src / JNoteSettings.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  11.4 KB  |  395 lines

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. /**
  5. *    JNoteSettings
  6. *
  7. *    JNotepad specific extension of the SettingsObject class. This only adds
  8. *    a method to load the SettingsObject with default values for JNotepad.
  9. *
  10. *    @version    1.0, 7/31/97
  11. *    @see        @SettingsObject
  12. */
  13.  
  14. import SettingsObject;
  15. import java.util.*;
  16. import com.ms.ui.*;
  17. import com.ms.fx.*;
  18.  
  19. public class JNoteSettings extends SettingsObject{
  20.     Hashtable strToCharTable;                    // table to convert key names into the characters they represent.
  21.     
  22.     /**
  23.     *    JNoteSettings constructor. Creates a new blank JNoteSettings object
  24.     *
  25.     *    @param filename File to keep settings information in.
  26.     */
  27.     public JNoteSettings(String filename)
  28.     {
  29.         super(filename);
  30.         init();
  31.     }
  32.     
  33.     /**
  34.     *    Private init function. Loads the strToCharTable with string names for
  35.     *    certain characters that cannot be easily represented in a text file.
  36.     */
  37.     private void init()
  38.     {
  39.         strToCharTable = new Hashtable(1); // !!! expand?
  40.         
  41.         strToCharTable.put("tab", new Character('\t'));
  42.     }
  43.     
  44.     /**
  45.     *    Loads the JNoteSettingsObject from the INI file. If the file could not
  46.     *    be loaded, load in the default settings.
  47.     *
  48.     *    @returns    true
  49.     */
  50.     public boolean load()
  51.     {
  52.         if (!super.load())
  53.         {
  54.             loadDefaults();
  55.         }
  56.         
  57.         return true;
  58.     }
  59.     
  60.     /**
  61.     *    Loads default JNotepad-specific values into the JNoteSettings object.
  62.     *    This loads the toolbars, feature lists, accelerators, and everything
  63.     *    else with factory defaults.
  64.     */
  65.     protected void loadDefaults()
  66.     {
  67.         // define the toolbar
  68.         put("Toolbar","0","CMD_New");
  69.         put("Toolbar","1","CMD_Open");
  70.         put("Toolbar","2","CMD_Save");
  71.         put("Toolbar","3","CMD_Find");
  72.         put("Toolbar","4","CMD_Cut");
  73.         put("Toolbar","5","CMD_Copy");
  74.         put("Toolbar","6","CMD_Paste");
  75.         put("Toolbar","7","CMD_Undo");
  76.         put("Toolbar","8","CMD_Redo");
  77.                 
  78.         // definitions for each button on the toolbar
  79.         put("CMD_New", "Image", "Images\\new.gif");
  80.         put("CMD_New", "CMD", "4021");
  81.         put("CMD_New", "Tip", "101");
  82.  
  83.         put("CMD_Open", "Image", "Images\\open.gif");
  84.         put("CMD_Open", "CMD", "4022");
  85.         put("CMD_Open", "Tip", "102");
  86.  
  87.         put("CMD_Save", "Image", "Images\\save.gif");
  88.         put("CMD_Save", "CMD", "4023");
  89.         put("CMD_Save", "Tip", "103");
  90.  
  91.         put("CMD_Find", "Image", "Images\\find.gif");
  92.         put("CMD_Find", "CMD", "4017");
  93.         put("CMD_Find", "Tip", "115");
  94.  
  95.         put("CMD_Cut", "Image", "Images\\cut.gif");
  96.         put("CMD_Cut", "CMD", "1001");
  97.         put("CMD_Cut", "Tip", "107");
  98.  
  99.         put("CMD_Copy", "Image", "Images\\copy.gif");
  100.         put("CMD_Copy", "CMD", "1002");
  101.         put("CMD_Copy", "Tip", "108");
  102.  
  103.         put("CMD_Paste", "Image", "Images\\paste.gif");
  104.         put("CMD_Paste", "CMD", "1003");
  105.         put("CMD_Paste", "Tip", "109");
  106.  
  107.         put("CMD_Undo", "Image", "Images\\undo.gif");
  108.         put("CMD_Undo", "CMD", "4026");
  109.         put("CMD_Undo", "Tip", "105");
  110.  
  111.         put("CMD_Redo", "Image", "Images\\redo.gif");
  112.         put("CMD_Redo", "CMD", "4004");
  113.         put("CMD_Redo", "Tip", "106");
  114.  
  115.         // we have no GIF for NextWindow, so we use the boring new.gif one
  116.         // we shouldn't need any gifs for it
  117.         put("CMD_NextWindow", "Image", "Images\\new.gif");
  118.         put("CMD_NextWindow", "CMD", "4015");
  119.         put("CMD_NextWindow", "Tip", "1");
  120.  
  121.         // we have no GIF for PrevWindow, so we use the boring new.gif one
  122.         // we shouldn't need any gifs for it
  123.         put("CMD_PrevWindow", "Image", "Images\\new.gif");
  124.         put("CMD_PrevWindow", "CMD", "4027");
  125.         put("CMD_PrevWindow", "Tip", "1");
  126.  
  127.         // we have no GIF for SelectAll, so we use the boring new.gif one
  128.         // we shouldn't need any gifs for it
  129.         put("CMD_SelectAll", "Image", "Images\\new.gif");
  130.         put("CMD_SelectAll", "CMD", "4013");
  131.         put("CMD_SelectAll", "Tip", "1");
  132.  
  133.         // we have no GIF for Replace, so we use the boring new.gif one        
  134.         // we shouldn't need any gifs for it
  135.         put("CMD_Replace", "Image", "Images\\new.gif");
  136.         put("CMD_Replace", "CMD", "4019");
  137.         put("CMD_Replace", "Tip", "1");
  138.  
  139.         // accelerator keys
  140.         put("Accelerators","ctrl-f", "CMD_Find");
  141.         put("Accelerators","ctrlshift-tab", "CMD_PrevWindow");
  142.         put("Accelerators","ctrl-a", "CMD_SelectAll");
  143.         put("Accelerators","ctrl-s", "CMD_Save");
  144.         put("Accelerators","ctrl-tab", "CMD_NextWindow");
  145.         put("Accelerators","ctrl-o", "CMD_Open");
  146.         put("Accelerators","ctrl-n", "CMD_New");
  147.         put("Accelerators","ctrl-h", "CMD_Replace");
  148.  
  149.         // colors
  150.         put("Colors", "Bright Red", "255 0 0");
  151.         put("Colors", "Red", "128 0 0");
  152.         put("Colors", "Dark Red", "64 0 0");
  153.         put("Colors", "Bright Blue", "0 0 255");
  154.         put("Colors", "Blue", "0 0 128");
  155.         put("Colors", "Dark Blue", "0 0 64");
  156.         put("Colors", "Bright Green", "0 255 0");
  157.         put("Colors", "Green", "0 128 0");
  158.         put("Colors", "Dark Green", "0 128 0");
  159.         put("Colors", "Yellow", "255 255 0");
  160.         put("Colors", "White", "255 255 255");
  161.         put("Colors", "Black", "0 0 0");
  162.     
  163.         // java files get these colors/fonts/features
  164.         put("JAVA", "Background", "Blue");
  165.         put("JAVA", "Foreground", "White");
  166.         put("JAVA", "Features", "BraceMatchFeature");            
  167.         
  168.         put("INI", "Background", "White");
  169.         put("INI", "Foreground", "Green");
  170.         put("INI", "Font", "Courier PLAIN 12");
  171.  
  172.         put("INF", "Background", "White");
  173.         put("INF", "Foreground", "Red");
  174.         put("INF", "Font", "Courier PLAIN 12");
  175.  
  176.         put("TXT", "Background", "White");
  177.         put("TXT", "Foreground", "Black");
  178.         put("TXT", "Font", "Arial PLAIN 14");
  179.  
  180.         put("DEFAULT", "Background", "White");
  181.         put("DEFAULT", "Foreground", "Black");
  182.         put("DEFAULT", "Font", "Arial PLAIN 14");
  183.         
  184.     }
  185.     
  186.     /**
  187.     *    Loads accelerators from the "Accelerators" section of the JNoteSettings
  188.     *    object. Adds them to the passed <a href="AcceleratorFeature.htm">AcceleratorFeature</a>
  189.     *
  190.     *    @param accel AcceleratorFeature to load with accelerator keys.
  191.     */
  192.     public void loadAccelerators(AcceleratorFeature accel)
  193.     {
  194.         Enumeration e = getKeys("Accelerators");
  195.         
  196.         if (e == null)
  197.         {
  198.             return;
  199.         }
  200.         
  201.         while (e.hasMoreElements())
  202.         {
  203.             String keyStr = (String)e.nextElement();
  204.             // grab the name of the section which holds the definition for this command
  205.             String sectionName = get("Accelerators", keyStr, "");
  206.     
  207.             // pull out the command ID number from this section
  208.             String valueStr = get(sectionName, "CMD", null);
  209.  
  210.             String accelType = null;
  211.             char cAccelKey;
  212.             
  213.             keyStr.toLowerCase();
  214.             
  215.             int iIndex = keyStr.indexOf('-');
  216.             if ((iIndex == -1) || (valueStr == null))
  217.             {
  218.                 // ini accel error
  219.                 UIMessageBox box = new UIMessageBox(new UIFrame(), 
  220.                     JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  221.                     JNotePad.loadString(ResourceIDs.IDS_MSGINIACCELERR),
  222.                     box.STOP, UIButtonBar.OK);
  223.                 box.doModal();
  224.                 
  225.                 return;
  226.             }
  227.             
  228.             // parse out the type of accelerator key from the key itself
  229.             accelType = keyStr.substring(0, iIndex);
  230.             cAccelKey = keyStr.charAt(iIndex+1);
  231.             
  232.             if (iIndex != keyStr.length()-2)
  233.             {
  234.                 Character c = (Character)strToCharTable.get(keyStr.substring(iIndex+1));
  235.                 if (c != null)
  236.                 {
  237.                     cAccelKey = c.charValue();
  238.                 }
  239.                 else
  240.                 {
  241.                     cAccelKey = 0;
  242.                 }
  243.             }
  244.             
  245.             // check if we're using ctrl-key combos or ctrl-shift-key.
  246.             boolean bUseShift = (accelType.equals("ctrlshift") ? true : false);
  247.             
  248.             accel.addAccelerator(cAccelKey, valueStr, bUseShift);
  249.         }
  250.     }
  251.     
  252.     /**
  253.     *    Retrieves an FxColor from the JNoteSettings object. The FxColor is
  254.     *    stored as a string of three integers separated by spaces which 
  255.     *    encode the RGB value of the color. For example, bright blue 
  256.     *    is "255 0 0". This function takes the name of a color, looks it up,
  257.     *    and converts it to an FxColor.
  258.     *
  259.     *    @param    key    The key to retrieve
  260.     *    @param    def    The default value to return if the key isn't present in
  261.     *                the JNoteSettings object.
  262.     *
  263.     *    @returns    The FxColor of the color named in the string, or null
  264.     *                if the key was not in the registry.
  265.     */
  266.     public FxColor getColor(String key, FxColor def)
  267.     {
  268.         String colorValue = get("Colors", key, null);
  269.         
  270.         if (colorValue == null)
  271.         {
  272.             return def;
  273.         }
  274.         
  275.         // grab the positions of the spaces that separate the
  276.         // numbers. 
  277.         int iFirstSpace = colorValue.indexOf(' ');
  278.         int iSecondSpace = colorValue.lastIndexOf(' ');
  279.         
  280.         // quit if the string isn't correctly formatted
  281.         if ((iFirstSpace == -1) || (iSecondSpace == -1))
  282.         {
  283.             return null;
  284.         }
  285.         
  286.         // pull out color numbers and turn them into ints
  287.         int iRed = Integer.parseInt(colorValue.substring(0, iFirstSpace));
  288.         int iGreen = Integer.parseInt(colorValue.substring(iFirstSpace+1, iSecondSpace));
  289.         int iBlue = Integer.parseInt(colorValue.substring(iSecondSpace+1));
  290.         
  291.         return new FxColor(iRed, iGreen, iBlue);
  292.     }
  293.     
  294.     
  295.     /**
  296.     *    Retrieves an FxColor from the JNoteSettings object indirectly. 
  297.     *    The value at the key will be looked up in the color section of
  298.     *    the JNoteSettings object. The color there will be returned. This
  299.     *    lets one put a color name into the JNoteSettings object and have
  300.     *    an FxColor object returned.
  301.     *    
  302.     *
  303.     *    @param    section    The name of the section to retrieve the key from
  304.     *    @param    key    The key to retrieve
  305.     *    @param    def    The default value to return if the key isn't present in
  306.     *                the JNoteSettings object.
  307.     *
  308.     *    @returns    The FxColor of the color described in the string, or null
  309.     *                if the key was not in the registry.
  310.     */
  311.     public FxColor getColorIndirect(String section, String key, FxColor def)
  312.     {
  313.         String colorName = get(section, key, null);
  314.         
  315.         if (colorName == null)
  316.         {
  317.             return def;
  318.         }
  319.         
  320.         return getColor(colorName, def);
  321.     }
  322.     
  323.     
  324.     /**
  325.     *    Retrieves an FxFont from the JNoteSettings object. The FxFont is
  326.     *    stored in a string with the name of the font, the style (BOLD,
  327.     *    ITALIC, or PLAIN) and the size, separated by space. 
  328.     *    For example, "Dialog PLAIN 12" is a 12 point plain Dialog font. 
  329.     *    This function looks up a key in the JNoteSettings and converts 
  330.     *    it to an FxFont.
  331.     *
  332.     *    @param    section    The name of the section to retrieve the key from
  333.     *    @param    key    The key to retrieve
  334.     *    @param    def    The default value to return if the key isn't present in
  335.     *                the JNoteSettings object.
  336.     *
  337.     *    @returns    The FxFont of the font described in the string, or null
  338.     *                if the key was not in the registry.
  339.     */
  340.     public FxFont getFont(String section, String key, FxFont def)
  341.     {
  342.         String font = get(section, key, null);
  343.         
  344.         if (font == null)
  345.         {
  346.             return def;
  347.         }
  348.         
  349.         // grab the positions of the spaces that separate the
  350.         // numbers. 
  351.         int iFirstSpace = font.indexOf(' ');
  352.         int iSecondSpace = font.lastIndexOf(' ');
  353.         
  354.         // quit if the string isn't correctly formatted
  355.         if ((iFirstSpace == -1) || (iSecondSpace == -1))
  356.         {
  357.             return null;
  358.         }
  359.         
  360.         // pull out values from the string which we'll use to construct
  361.         // an FxFont object,
  362.         String fontName = font.substring(0, iFirstSpace);
  363.         String style = font.substring(iFirstSpace+1, iSecondSpace).toUpperCase();
  364.         int iSize = Integer.parseInt(font.substring(iSecondSpace+1));
  365.         int iStyle = 0;
  366.         
  367.         if (style.equals("PLAIN"))
  368.         {
  369.             iStyle = FxFont.PLAIN;
  370.         }
  371.         else if (style.equals("BOLD"))
  372.         {
  373.             iStyle = FxFont.BOLD;
  374.         }
  375.         else if (style.equals("ITALIC"))
  376.         {
  377.             iStyle = FxFont.ITALIC;
  378.         }
  379.         else
  380.         {
  381.             // ini font error
  382.             UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  383.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  384.                 JNotePad.loadString(ResourceIDs.IDS_MSGINIFONTERR),
  385.                 UIMessageBox.STOP, UIButtonBar.OK);
  386.             _box_.doModal();
  387.             
  388.         }
  389.         
  390.         return new FxFont(fontName, iStyle, iSize);
  391.     }
  392.     
  393. }
  394.  
  395.