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

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. /**
  5. *    JNoteToolbar
  6. *
  7. *    Toolbar class. Operates on objects that implement the IFileOperationTarget
  8. *    interface, which contains common file operations. Also implements changing
  9. *    application settings.
  10. *
  11. *    The IFileOperationTarget will have certain methods called when the user
  12. *    clicks file-related buttons on the toolbar.
  13. *
  14. *    @version    1.0, 7/10/97
  15. *    @see        IFileOperationTarget
  16. *    @see        SettingsObject
  17. */
  18.  
  19. import com.ms.ui.*;
  20. import java.awt.*;
  21. import com.ms.fx.*;
  22. import java.io.File;
  23. import java.io.*;
  24. import java.util.*;
  25. import java.awt.image.*;
  26. import java.net.URL;
  27. import com.ms.ui.resource.*;
  28. import com.ms.util.*;
  29. import com.ms.awt.image.StreamedImageSource;
  30. public class JNoteToolbar extends UIBand implements ICommandFeedback{
  31.     /**
  32.     *    The hashtable that holds the buttons in the toolbar
  33.     */
  34.     protected Hashtable buttonTable;
  35.     /**
  36.     *    Foreground color chooser control.    
  37.     */
  38.     JNoteColorChoice colorChoiceFg;
  39.     /**
  40.     *    The applet that we're running from. If we're in standalone mode, this
  41.     *    is null.
  42.     */
  43.     UIApplet theApplet;
  44.     /**
  45.     *    Background color chooser control.
  46.     */
  47.     JNoteColorChoice colorChoiceBg;
  48.     /**
  49.     *    Creates a JNoteToolbar.        
  50.     */
  51.     /**
  52.     *    Creates a JNoteToolbar.        
  53.     *
  54.     *    @param applet The applet which this is running from. This is null if we're running standalone
  55.     */
  56.     public JNoteToolbar(UIApplet applet){
  57.         //super(IFxGraphicsConstants.EDGE_RAISED);
  58.         super("", BREAK);
  59.         colorChoiceFg=null;
  60.         colorChoiceBg=null;
  61.         
  62.         init(applet);
  63.     }
  64.     /**
  65.     *    Private init function. Initializes the button hash table and other
  66.     *    variables.
  67.     *
  68.     *    @param applet The applet which this is running from. This is null if we're running standalone.
  69.     */
  70.     private void init(UIApplet applet){            
  71.         theApplet = applet;
  72.         buttonTable = new Hashtable(11);
  73.     }
  74.     /**
  75.     *    Loads buttons and their from the settings object
  76.     *    and places the button objects into the button table. Also adds JNoteColorChoice
  77.     *    controls to the toolbar.
  78.     *
  79.     *    @param     settings    Object which holds application settings information
  80.     */
  81.     public void loadButtons(Win32ResourceDecoder res){
  82.         int i = 0;
  83.         boolean bLoadedOK = true;
  84.         MediaTracker mt = new MediaTracker(FxToolkit.getHelperFrame());
  85.         MediaTracker grayMT = new MediaTracker(FxToolkit.getHelperFrame());
  86.         JNoteSettings settings = JNoteAppSettings.getSettingsObj();
  87.         String cmdSection=null;
  88.         while ((cmdSection = settings.get("Toolbar", String.valueOf(i), null)) != null){                    
  89.             if (cmdSection.length() == 0){
  90.                 // toolbar load error
  91.                 UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  92.                     JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  93.                     JNotePad.loadString(ResourceIDs.IDS_MSGINITOOLBARERR),
  94.                     UIMessageBox.STOP, UIButtonBar.OK);
  95.                 _box_.doModal();
  96.                 cmdSection = "Default";
  97.             }
  98.             //
  99.             // get the filename for the image
  100.             // Get the resource ID for the tooltip
  101.             // get the CMD id for the button.
  102.             //
  103.             String imageName = settings.get(cmdSection, "Image", "");
  104.             String nameID = settings.get(cmdSection, "Tip", "");
  105.             String name=null;
  106.             try{
  107.                 name = res.getString( Integer.parseInt(nameID));
  108.             }
  109.             catch (Exception e){name="";}
  110.             String cmd = settings.get(cmdSection,"CMD","");
  111.             int cmdID= Integer.parseInt(cmd);
  112.             JNotePushButton button = null;
  113.             Image im = null;
  114.             UIGraphic grayGraphic = null;
  115.             if (imageName != null)
  116.                 im = loadImage(imageName);                            
  117.             if ((imageName == null) || (im == null)){
  118.                 // if button image doesn't exist, pop up an error
  119.                 // box at the end of the button loading.
  120.                 button = new JNotePushButton(name);
  121.  
  122.                 bLoadedOK = false;
  123.             }else{
  124.                 //
  125.                 //
  126.                 //
  127.                 int iIndex = imageName.indexOf('.');
  128.                 if (iIndex > -1){
  129.                     // the grayed out bitmap has a _ appended to the end of the normal bitmap's name
  130.                     String grayImageName = imageName.substring(0, iIndex)+"_"+imageName.substring(iIndex);
  131.                     File grayFile = new File(grayImageName);
  132.                     if (grayFile.exists()){
  133.                         Image grayIm;
  134.                         grayIm = loadImage(grayImageName);
  135.                         grayGraphic = new UIGraphic(grayIm);
  136.                         grayMT.addImage(grayIm, i);
  137.                     }
  138.                 }
  139.                 button = new JNotePushButton(new UIGraphic(im), grayGraphic, 0);
  140.                 button.setName(name);
  141.                 mt.addImage(im, i);
  142.             }
  143.             button.setID(cmdID);
  144.             add(button);
  145.             buttonTable.put(name, button);
  146.             i++;
  147.         }
  148.         try{
  149.             mt.waitForAll();
  150.             grayMT.waitForAll();
  151.         }catch (InterruptedException e){}
  152.         // add a color choice dropdown for selecting the text color
  153.         UIPanel panel = new UIPanel();
  154.         colorChoiceFg = new JNoteColorChoice(JNoteColorChoice.TEXTCOLOR);
  155.         panel.add(colorChoiceFg);
  156.         add(panel);
  157.         // add a color choice dropdown for selecting the background color
  158.         panel = new UIPanel();
  159.         colorChoiceBg = new JNoteColorChoice(JNoteColorChoice.BACKCOLOR);
  160.         panel.add(colorChoiceBg);
  161.         add(panel);
  162.         if (!bLoadedOK){
  163.             // ini graphic load error
  164.             UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  165.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  166.                 JNotePad.loadString(ResourceIDs.IDS_MSGINITIMGERR),
  167.                 UIMessageBox.STOP, UIButtonBar.OK);
  168.             _box_.doModal();
  169.         }
  170.         repaint();
  171.     }
  172.     // ICommandFeedback methods
  173.     /**
  174.     *    Part of ICommandFeedback. Called when a command button needs to be enabled or disabled.
  175.     *    Disables the button and grays it out.
  176.     *    
  177.     *    @see    ICommandFeedback
  178.     *    @param    command    Name of button to enable or disable
  179.     *    @param    on    true if button is to be enabled, false if it is to be disabled.
  180.     */
  181.     public void enableCommand(String command, boolean on){
  182.         JNotePushButton button = (JNotePushButton)buttonTable.get(command);
  183.         if (button != null){
  184.             button.setEnabled(on);
  185.             button.repaint();
  186.         }
  187.     }
  188.     /**
  189.     *    Part of ICommandFeedback. Checks the enabled state of a button.
  190.     *
  191.     *    @param    command    Name of button to check
  192.     *    @returns    true if button is enabled, false if it is not.
  193.     */
  194.     public boolean isEnabled(String command){
  195.         JNotePushButton button = (JNotePushButton)buttonTable.get(command);
  196.         if (button != null)
  197.             return button.isEnabled();                    
  198.         else
  199.             return true;
  200.     }
  201.     /**
  202.     *    Part of ICommandFeedback. Called when a command button needs to be checked or unchecked.
  203.     *    Sets the checkmark accordingly. This works only on UICheckButtons -- it has no effect
  204.     *    on other buttons.
  205.     *    
  206.     *    @see    ICommandFeedback
  207.     *    @param    command    Name of button to set the check on
  208.     *    @param    on    true if button is to be checked, false if it is to be unchecked.
  209.     */
  210.     public void setChecked(String command, boolean on){
  211.         Object obj = buttonTable.get(command);
  212.         if ((obj != null) && (obj instanceof UICheckButton)){
  213.             UICheckButton button = (UICheckButton)obj;
  214.             button.setChecked(on);
  215.             button.repaint();
  216.         }
  217.     }
  218.     /**
  219.     *    Called when a file is loaded. Adds filename to Most Recently Used (MRU) list.
  220.     *    Part of ICommandFeedback. Does nothing in this class cause there's no 
  221.     *    MRU list to add to.
  222.     *
  223.     *    @param    command    Name of file to add to MRU list
  224.     *    @see    ICommandFeedback
  225.     */
  226.     public void addToMRUList(String filename){}
  227.     /**
  228.     *    Called when a control in the system changes color. Changes the colors
  229.     *    in the JNoteColorChoice controls to reflect this change if the component
  230.     *    being changed is the edit control.
  231.     *
  232.     *    @param    comp    Component who had a color change
  233.     *    @param    color    Color that the component was changed to
  234.     *    @param    whichPart    Which part of the component had a color change. This can
  235.     *                        be either FOREGROUND or BACKGROUND.
  236.     */        
  237.     public void    notifyColorChange(Object comp, 
  238.                                   FxColor color, 
  239.                                   int whichPart){            
  240.         if (!(comp instanceof ITextOperationTargetExt))
  241.             return;
  242.         JNoteColorChoice colorChooser = null;
  243.         if (whichPart == COLORCHANGE_FOREGROUND)
  244.             colorChooser = colorChoiceFg;
  245.         else
  246.             colorChooser = colorChoiceBg;
  247.         colorChooser.setSelectedColor(color);            
  248.     }
  249.     /**
  250.     *    Loads an image file. Loads from the Toolkit if we're standalone or over
  251.     *    the net if we're an applet.
  252.     *    
  253.     *    @param    imageName    Name of image to load
  254.     */
  255.     protected Image loadImage(String imageName){
  256.         if (!JNoteAppletObject.isApplet()){
  257.             // standalone
  258.             ClassLoader cl=this.getClass().getClassLoader();
  259.             InputStream is;
  260.             if(cl!=null)
  261.                 is=cl.getResourceAsStream(imageName);
  262.             else
  263.                 is=ClassLoader.getSystemResourceAsStream(imageName);
  264.             if(is!=null)
  265.                 return Toolkit.getDefaultToolkit().createImage(new StreamedImageSource(is));
  266.             return null;
  267.         }else{
  268.             // applet
  269.             URL url = theApplet.getCodeBase();
  270.             return theApplet.getImage(url, imageName);
  271.         }
  272.     }
  273. }
  274. /**
  275.  *    JNotePushButton
  276.  *
  277.  *  JNoteToolbar private class. This implements Tooltips in a standard UIPushButton.
  278.  *  The Tooltip displayed is the name of the control.
  279.  *
  280.  *    @version    1.0, 7/31/97
  281.  *    @see        JNoteToolbar
  282.  */
  283. class JNotePushButton extends UIPushButton{
  284.     private UIGraphic grayedImage = null;
  285.     private UIGraphic normalImage = null;
  286.     /**
  287.     *    Creates a JNotePushButton with a text field.
  288.     *
  289.     *    @param text Text to display on button.
  290.     */
  291.     public JNotePushButton(String text){
  292.         super(text);
  293.         normalImage = null;
  294.         grayedImage = null;        
  295.     }
  296.     /**
  297.     *    Creates a JNotePushButton with a particular style and text.
  298.     *
  299.     *    @param text Text to display on button
  300.     *    @param style Button style -- constants in <a href="com.ms.ui.UIPushButton">UIPushButton</a>.
  301.     */
  302.     public JNotePushButton(String text, int style){
  303.         super(text, style);
  304.         normalImage = null;
  305.         grayedImage = null;
  306.     }
  307.     /**
  308.     *    Creates a JNotePushButton which displays a IUIComponent, such as UIGraphic
  309.     *    or UIStatic.
  310.     *
  311.     *    @param comp IUIComponent to display on button
  312.     */
  313.     public JNotePushButton(IUIComponent comp){
  314.         super(comp);
  315.         init(comp, null);
  316.     }
  317.     /**
  318.     *    Creates JNotePushButton with a particular style and displaying a given IUIComponent.
  319.     *
  320.     *    @param comp IUIComponent to display
  321.     *    @param style Style of button - constants in <a href="com.ms.ui.UIPushButton">UIPushButton</a>.
  322.     */
  323.     public JNotePushButton(IUIComponent comp, int style){
  324.         super(comp, style);
  325.         init(comp, null);
  326.     }
  327.     /**
  328.     *    Creates JNotePushButton with a particular style and displaying a given IUIComponent.
  329.     *    When disabled, the button displays the given image.
  330.     *
  331.     *    @param comp IUIComponent to display
  332.     *    @param grayComp    IUIComponent to display when grayed out (disabled)
  333.     *    @param style Style of button - constants in <a href="com.ms.ui.UIPushButton">UIPushButton</a>.
  334.     */
  335.     public JNotePushButton(IUIComponent comp, 
  336.                            IUIComponent grayComp, 
  337.                            int style){
  338.         super(comp, style);
  339.         init(comp, grayComp);
  340.     }
  341.     /**
  342.     *    Private init method. Initializes the button with the given
  343.     *    component and style. Also constructs a grayed out image for display
  344.     *    when the button is disabled.
  345.     *    
  346.     *    @param    comp    Component to display on button
  347.     *    @param    style    Style of button - constants constants in <a href="com.ms.ui.UIPushButton">UIPushButton</a>
  348.     */
  349.     private void init(IUIComponent comp, IUIComponent grayComp){
  350.         if (comp instanceof UIGraphic){
  351.             normalImage = (UIGraphic)comp;
  352.             if ((grayComp != null) && (grayComp instanceof UIGraphic))
  353.                 grayedImage = (UIGraphic)grayComp;
  354.             else
  355.                 makeGrayedImage(normalImage);
  356.         }
  357.     }            
  358.     /**
  359.     *    Implements the getHelp() method for Tooltips. The text displayed
  360.     *    by a Tooltip is whatever is returned from the getHelp() method.
  361.     *    JNotePushButtons return the name of the button (getName()).
  362.     */
  363.     public String getHelp(){
  364.         return getName();
  365.     }
  366.     /**
  367.     *    enables or disables the button. If there's an image, changes it to
  368.     *    a grayed out version.
  369.     *
  370.     *    @param    on    true if the button is to be enabled, false if it is to be disabled.
  371.     *    @returns    true
  372.     */
  373.     public void setEnabled(boolean on){
  374.         if (isEnabled() == on)
  375.             return;
  376.         super.setEnabled(on);
  377.         if (normalImage != null){                    
  378.             UIGraphic image = null;
  379.             if (on == false)
  380.                 // set the current image to a grayed out image.
  381.                 image = grayedImage;
  382.             else
  383.                 // reset the original image
  384.                 image = normalImage;                        
  385.             replace(image, 0);
  386.             repaint();
  387.         }
  388.     } 
  389.     /**
  390.     *    Private method to create a grayed version of the component and store
  391.     *    it in grayedImage. Currently works only with UIGraphic objects. 
  392.     *    Text strings gray automatically when disabled.
  393.     *
  394.     *    @param    comp    Component to gray out
  395.     */     
  396.     private void makeGrayedImage(UIGraphic image){
  397.         // use a gray filter to gray out the image
  398.         grayedImage = new UIGraphic( createImage(new FilteredImageSource(image.getImage().getSource(),
  399.             new GrayImageFilter())) );    
  400.     }    
  401. }
  402. /**
  403.  *    JNoteColorChoice
  404.  *
  405.  *    This is a private class to JNoteToolbar. It creates a dropdown color choice
  406.  *    list.
  407.  *
  408.  *    @version    1.0, 8/12/97
  409.  *
  410.  *    @see    JNoteToolbar
  411.  */
  412. class JNoteColorChoice extends UIChoice{        
  413.     /**
  414.     *    Maps colors to items in the list
  415.     */
  416.     private Hashtable colorTable;                
  417.     /**
  418.     *    whether this control displays foreground or background colors        
  419.     */
  420.     private int iStyle;                
  421.     /**
  422.     *    foreground color style
  423.     */
  424.     public static final int TEXTCOLOR = 1;
  425.     /**
  426.     *    background color style    
  427.     */
  428.     public static final int BACKCOLOR = 2;
  429. /**
  430.  *    Creates a new JNoteColorChoice.
  431.  *
  432.  *    @param    style    Style of the control. TEXTCOLOR indicates the control will
  433.  *                    display foreground colors, and BACKCOLOR indicates it will 
  434.  *                    display background colors.
  435.  */
  436.     public JNoteColorChoice(int style){
  437.         colorTable = new Hashtable(10);
  438.         init(style);
  439.     }
  440. /**
  441.  *    Private init method. Creates the UIChoice control and adds color bars to it.
  442.  *
  443.  *    @param    style    Style of the control. See <a href="JNoteColorChoice">JNoteColorChoice</a> 
  444.  *                    for details.
  445.  */
  446.     private void init(int style){
  447.         iStyle = style;
  448.         // grab settings object
  449.         JNoteSettings settings = JNoteAppSettings.getSettingsObj();
  450.         // fill vector with the color names
  451.         Vector svector = new Vector(10);
  452.         Enumeration e = settings.getKeys("Colors");
  453.         while (e.hasMoreElements())
  454.             svector.addElement(e.nextElement());
  455.         // sort the vector so we get the colors in alpha order
  456.         VectorSort.sort(svector, new StringComparison());
  457.         e = svector.elements();
  458.         while (e.hasMoreElements()){
  459.             String colorName = (String)e.nextElement();
  460.             FxColor color = settings.getColor(colorName, null);
  461.             if (color == null){
  462.                 // ini color load error
  463.                 UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  464.                     JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  465.                     JNotePad.loadString(ResourceIDs.IDS_MSGINICOLORERR),
  466.                     UIMessageBox.STOP, UIButtonBar.OK);
  467.                 _box_.doModal();
  468.                 return;
  469.             }
  470.             addColorBar(colorName, color);                        
  471.         }
  472.         // add a box for custom color. This usually launches a color dialog.
  473.         UIStatic custom;
  474.         if (iStyle == TEXTCOLOR){
  475.             custom = new UIText("Custom...", UIText.LEFT);
  476.             custom.setForeground(FxColor.getExtendedColor(Color.black));
  477.         }else{
  478.             custom = new UIText("Custom...", UIText.LEFT);
  479.             custom.setBackground(FxColor.getExtendedColor(Color.lightGray));
  480.         }
  481.         //choiceControl.add(custom);
  482.         add(custom);
  483.         //add(choiceControl);
  484.         //choiceControl.repaint();
  485.         repaint();
  486.     }
  487. /**
  488.  *    Adds a component to the end of the list. Also adds its color value
  489.  *    to the hashtable so you can do a lookup of components by color.
  490.  *
  491.  *    @param    comp    Component to be added
  492.  */
  493.     public IUIComponent add(IUIComponent comp){
  494.         super.add(comp);
  495.         FxColor color = null;
  496.         if (iStyle == TEXTCOLOR)
  497.             color = FxColor.getExtendedColor(comp.getForeground());
  498.         else
  499.             color = FxColor.getExtendedColor(comp.getBackground());
  500.         // if the item we're adding is the Custom... item (which isn't
  501.         // really a color - it launches a color menu), add its name
  502.         // as the key instead of its color. We use this later.
  503.         if (!(comp.getName().equals("Custom...")))
  504.              colorTable.put(color, comp);                        
  505.          else
  506.              colorTable.put(comp.getName(), comp);
  507.          return comp;
  508.     }
  509. /**
  510.  *    adds a color bar.  
  511.  *
  512.  *    @param    colorname    Name of the color
  513.  *    @param    color    Color object
  514.  */
  515.     public void addColorBar(String colorname, FxColor color){    
  516.         UIStatic s = new UIText(colorname, UIText.LEFT);
  517.         // create an inverse color
  518.         int red = color.getRed() - 255;
  519.         int green = color.getGreen() - 255;
  520.         int blue = color.getBlue() - 255;
  521.         if (red < 0) red = -red;
  522.         if (green < 0) green = -green;
  523.         if (blue < 0) blue = -blue;    
  524.         FxColor inverse = new FxColor((red << 16) | (green << 8) | (blue));
  525.         if (iStyle == TEXTCOLOR){
  526.             s.setForeground(color);                            
  527.             //s.setForeground(color.getShadow());
  528.         }else{
  529.             s.setBackground(color);
  530.             //s.setForeground(color.getDarkShadow());
  531.             s.setForeground(inverse);
  532.         }
  533.         //choiceControl.add(s);
  534.         add(s);
  535.     }    
  536. /**
  537.  *    Sets the selection to an element in the list which is of the
  538.  *    given color.
  539.  *
  540.  *    @param    color    Color to set the UIChoice to
  541.  */
  542.     public void setSelectedColor(FxColor color){                
  543.         IUIComponent comp = (IUIComponent)colorTable.get(color);
  544.         if (comp != null)
  545.             setSelectedItem(comp);
  546.         else{
  547.             comp = (IUIComponent)colorTable.get("Custom...");
  548.             if (comp != null)
  549.                 setSelectedItem(comp);
  550.         }
  551.     }
  552.     public String getHelp(){
  553.         if (iStyle == TEXTCOLOR)
  554.             return "Change Text Color";
  555.         else
  556.             return "Change Background Color";
  557.     }
  558. }
  559. /**
  560.  *    Private class to be used with SortedVectors. Compares two colors
  561.  *    
  562.  *    @version    1.0, 7/28/97
  563.  */
  564. class StringComparison implements Comparison{
  565.     /**
  566.     *    Compares two objects.    
  567.     *
  568.     *    @returns    negative if a < b, 0 if a == b, positive if a > b
  569.     */
  570.     public int compare(Object p, Object q){
  571.         if ((p instanceof String) || (q instanceof String))
  572.             return ((String)p).compareTo((String)q);
  573.         return 0;
  574.     }
  575. }
  576.  
  577.