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

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