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

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4. /** 
  5. *    JNoteUIEdit
  6. *
  7. *    JNotepad extension of UIEdit. Supports ITextOperationTargetExt interface,
  8. *    which allows other classes to control the JNoteUIEdit without knowing
  9. *    anything about it. Adds a right-click context menu and support for HTML links.
  10. *
  11. *    @version    1.0, 7/13/97
  12. */
  13.  
  14. import com.ms.ui.*;
  15. import com.ms.ui.event.*;
  16. import com.ms.ui.resource.*;
  17. import java.awt.*;
  18. import java.io.*;
  19. import com.ms.fx.*;
  20. import java.awt.datatransfer.*;
  21.  
  22.  
  23. public class JNoteUIEdit extends UIEdit implements ITextOperationTargetExt, IConstants, IUIFocusListener
  24. {
  25.     protected ICommandFeedback commandFeedback;
  26.     protected IComponentFeature compFeature; 
  27.     protected UIContextMenu popupMenu;
  28.     
  29.     protected Clipboard clipBoard;
  30.     protected UIFrame parentFrame;
  31.     
  32.     /**
  33.     * Default constructor. Constructs a JNoteUIEdit.
  34.     */
  35.     public JNoteUIEdit(UIFrame parentframe)
  36.     {
  37.         super();
  38.         init(parentframe);
  39.     }
  40.     
  41.     /**
  42.     *    Constructs a JNoteUIEdit with the given text.
  43.     *
  44.     *    @param text Text to fill the control with.
  45.     */
  46.     public JNoteUIEdit(UIFrame parentframe, String text)
  47.     {
  48.         super();
  49.         
  50.         setText(text);
  51.         init(parentframe);
  52.     }
  53.     
  54.     /**
  55.     *    Constructs a JNoteUIEdit with the given text.
  56.     *
  57.     *    @param text    Text to fill the control with.
  58.     */
  59.     public JNoteUIEdit(UIFrame parentframe, char[] text)
  60.     {
  61.         super();
  62.         
  63.         setText(new String(text));
  64.         init(parentframe);
  65.     }
  66.     
  67.     // private init method. Initializes variables and sets colors and styles.
  68.     private void init(UIFrame parentframe)
  69.     {
  70.         parentFrame = parentframe;
  71.         commandFeedback = null;
  72.         
  73.         setSingleLine(false);
  74.         
  75.         clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
  76.         
  77.         loadPopupMenu();        
  78.  
  79.         // set the initial word wrap. We enable/disable the appropriate menu
  80.         // items in the gotFocus() method.
  81.         setWordWrap(wwWrap);
  82.  
  83.         // add a focus listener, so we can detect when we gain focus. This class
  84.         // implements IUIFocusListener, so we can set a listener on ourselves. We
  85.         // change the word wrap menu radio buttons to reflect the current word
  86.         // wrapping style.
  87.         addFocusListener(this);
  88.     }
  89.     
  90.     public void setFeedbackObject(ICommandFeedback comfeedback)
  91.     {
  92.         commandFeedback = comfeedback;        
  93.     }
  94.     
  95.     public void setCommandObject(IComponentFeature compfeature)
  96.     {
  97.         compFeature = compfeature;
  98.         
  99.         compFeature.addTalker(popupMenu);        
  100.     }
  101.     
  102.     // private method which loads the right-click popup menu.
  103.     private void loadPopupMenu()
  104.     {
  105.         UIMenuList ml = new UIMenuList();
  106.         ml.add( JNotePad.loadMenuItem( ResourceIDs.IDS_UNDO, ResourceIDs.ID_EDIT_UNDO ));
  107.         ml.add( JNotePad.loadMenuItem( ResourceIDs.IDS_REDO, ResourceIDs.ID_EDIT_REDO ));
  108.         ml.add(new UILine());                    
  109.         ml.add( JNotePad.loadMenuItem( ResourceIDs.IDS_CUT,  ResourceIDs.ID_EDIT_CUT ));
  110.         ml.add( JNotePad.loadMenuItem( ResourceIDs.IDS_COPY, ResourceIDs.ID_EDIT_COPY ));
  111.         ml.add( JNotePad.loadMenuItem( ResourceIDs.IDS_PASTE, ResourceIDs.ID_EDIT_PASTE ));
  112.         ml.add( JNotePad.loadMenuItem( ResourceIDs.IDS_DELETE, ResourceIDs.ID_EDIT_DELETE ));
  113.         ml.add(new UILine());
  114.         ml.add( JNotePad.loadMenuItem( ResourceIDs.IDS_FONT, ResourceIDs.ID_FONT ));
  115.         ml.add( JNotePad.loadMenuItem( ResourceIDs.IDS_TEXTCOLOR, ResourceIDs.ID_TEXTCOLOR ));
  116.         ml.add( JNotePad.loadMenuItem( ResourceIDs.IDS_BGCOLOR, ResourceIDs.ID_BGCOLOR ));
  117.         ml.add(new UILine());
  118.         ml.add( JNotePad.loadMenuItem( ResourceIDs.IDS_LAUNCHSELLINK, ResourceIDs.ID_LAUNCH ));
  119.             
  120.         popupMenu = new JNoteContextMenu(this, ml);
  121.     }
  122.     
  123.     
  124.     protected void enableCommand(String command, boolean on)
  125.     {
  126.         if (commandFeedback != null)
  127.         {
  128.             commandFeedback.enableCommand(command, on);
  129.         }
  130.     }
  131.     
  132.     
  133.     protected void setChecked(String command, boolean on)
  134.     {
  135.         if (commandFeedback != null)
  136.         {
  137.             commandFeedback.setChecked(command, on);
  138.         }
  139.     }
  140.     
  141.     
  142.     public boolean handleEvent(Event e)
  143.     {
  144.         // check for right button click
  145.         if ((e.id == Event.MOUSE_UP) && (e.modifiers == Event.META_MASK))
  146.         {
  147.             //Dimension d = TabFileViewer.getSize();
  148.             Point pt = parentFrame.getLocation();
  149.             Point edit = getLocation(parentFrame);
  150.             popupMenu.launchAt(e.x+pt.x+edit.x, e.y+pt.y+edit.y, parentFrame);                        
  151.             
  152.             return true;
  153.         }
  154.         else if ((e.id == Event.MOUSE_DOWN) && (e.modifiers == Event.META_MASK))
  155.         {
  156.             // do nothing. We swallow this event and don't give it to the superclass
  157.             // to avoid losing focus for the context menu
  158.             return true;
  159.         }
  160.         else
  161.         {                    
  162.             return super.handleEvent(e);
  163.         }
  164.     }
  165.     
  166.     /**
  167.     *    Sets the word wrapping for this control. Makes sure that the    
  168.     *    word wrap radio buttons are properly updated.
  169.     *
  170.     *    @param    iStyle    Word wrap style. See the AFC UIEdit class for more information.
  171.     *    @returns    true
  172.     */
  173.     protected boolean setWordWrapStyle(int iStyle)
  174.     {
  175.         boolean b1 = false;
  176.         boolean b2 = false;
  177.         boolean b3 = false;
  178.  
  179.         // decide which radio buttons should be selected and which not
  180.         if (iStyle == wwNone)
  181.         {
  182.             b2=b3=false;
  183.             b1=true;
  184.         }
  185.         else if (iStyle == wwWrap)
  186.         {
  187.             b1=b3=false;
  188.             b2=true;
  189.         }
  190.         else if (iStyle == wwKeepWordIntact)
  191.         {
  192.             b1=b2=false;
  193.             b3=true;
  194.         }
  195.  
  196.         // set the word wrap menu radio buttons accordingly
  197.         setChecked(JNotePad.loadString(ResourceIDs.IDS_NOWORDWRAP), b1);
  198.         setChecked(JNotePad.loadString(ResourceIDs.IDS_WORDWRAP), b2);
  199.         setChecked(JNotePad.loadString(ResourceIDs.IDS_WORDWRAP2), b3);
  200.             
  201.         setWordWrap(iStyle);
  202.  
  203.         invalidate();
  204.  
  205.         return true;
  206.     }
  207.  
  208.     public boolean otherCommand(String command, UIActionEvent evt)
  209.     {
  210.         boolean ret = false;
  211.         
  212.         IUIComponent comp=evt.getActionItem();
  213.         switch( comp.getID() )
  214.         { 
  215.         case ResourceIDs.ID_LAUNCH:
  216.             return launchApp();
  217.         case ResourceIDs.ID_NOWORDWRAP:
  218.             return setWordWrapStyle(wwNone);
  219.         case ResourceIDs.ID_WORDWRAP:
  220.             return setWordWrapStyle(wwWrap);
  221.         case ResourceIDs.ID_WORDWRAP2:
  222.             return setWordWrapStyle(wwKeepWordIntact);
  223.         }
  224.  
  225.         if (evt.getActionItem() instanceof UIStatic)
  226.         {
  227.             UIStatic s = (UIStatic)evt.getActionItem();                    
  228.             Object bgcolor = s.getBackground();
  229.             Object fgcolor = s.getForeground();
  230.             
  231.             if (bgcolor instanceof FxColor)
  232.             {
  233.                 if (s.getName().startsWith("Custom"))
  234.                 {
  235.                     changeBackColor();
  236.                 }
  237.                 else
  238.                 {
  239.                     setBackground((FxColor)bgcolor);
  240.                 }
  241.                 commandFeedback.notifyColorChange(this, getBackColor(), 
  242.                         commandFeedback.COLORCHANGE_BACKGROUND);
  243.             }
  244.             else if (fgcolor instanceof FxColor)
  245.             {
  246.                 if (s.getName().startsWith("Custom"))
  247.                 {
  248.                     changeTextColor();
  249.                 }
  250.                 else
  251.                 {
  252.                     setForeground((FxColor)fgcolor);
  253.                 }                            
  254.                 commandFeedback.notifyColorChange(this, getTextColor(), 
  255.                         commandFeedback.COLORCHANGE_FOREGROUND);
  256.             }
  257.             
  258.             ret = true;
  259.         }
  260.         
  261.         return ret;
  262.     }
  263.     
  264.     /**
  265.     *    Tries to run the selected text as a file or HTML link.    
  266.     *
  267.     *    @returns    true
  268.     */
  269.     protected boolean launchApp()
  270.     {
  271.         String linkText = getSelectedText();
  272.         
  273.         if (linkText.length() == 0)
  274.         {
  275.             int iIndex = getCaretIndex();
  276.             linkText = getWord(iIndex);
  277.             if (linkText == null)
  278.             {
  279.                 // file could not be run
  280.                 UIMessageBox box = new UIMessageBox(new UIFrame(), 
  281.                     JNotePad.loadString(ResourceIDs.IDS_MSGLAUNCHERR),
  282.                     JNotePad.loadString(ResourceIDs.IDS_MSGNOTRUNNABLE),
  283.                     box.STOP, UIButtonBar.OK);
  284.                 box.doModal();
  285.                 return true;
  286.             }
  287.         }
  288.         
  289.         // load in the text which links the two strings, depending on whether you're
  290.         // launching an HTML link or a file.
  291.         int linkID = (linkText.startsWith("http://") ? ResourceIDs.IDS_MSGLINKTO : ResourceIDs.IDS_MSGLAUNCH);
  292.         String linkOrFileText = JNotePad.loadString(linkID);
  293.         
  294.         File fileToRun = new File(linkText);
  295.         
  296.         if (fileToRun.exists() || linkID == ResourceIDs.IDS_MSGLINKTO)
  297.         {
  298.             // confirm that user wants to launch this
  299.             UIMessageBox msgbox = new UIMessageBox(parentFrame, 
  300.                 JNotePad.loadString(ResourceIDs.IDS_MSGLAUNCH),
  301.                 JNotePad.loadString(ResourceIDs.IDS_MSGLAUNCHANNOC)+linkOrFileText+linkText+
  302.                 JNotePad.loadString(ResourceIDs.IDS_MSGOKCANCEL), 
  303.                 UIMessageBox.INFORMATION, UIButtonBar.OKCANCEL);
  304.             
  305.             UIButton ret = (UIButton)msgbox.doModal();
  306.             
  307.             if (ret.getID() != UIButtonBar.ID_OK)
  308.             {
  309.                 return true;
  310.             }
  311.             
  312.             
  313.             ExeRunnerThread exerunner = new ExeRunnerThread(linkText);
  314.             exerunner.start();
  315.         }
  316.         else
  317.         {
  318.             // file could not be run
  319.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  320.                 JNotePad.loadString(ResourceIDs.IDS_MSGLAUNCHERR),
  321.                 JNotePad.loadString(ResourceIDs.IDS_MSGNOTRUNNABLE),
  322.                 box.STOP, UIButtonBar.OK);
  323.             box.doModal();
  324.         }
  325.         
  326.         return true;
  327.     }
  328.     
  329.     
  330.     // returns the word at the given character index.
  331.     protected String getWord(int iIndex)
  332.     {
  333.         String editText = getText();
  334.         
  335.         // if we're clicking on valid text
  336.         if ((iIndex >= 0) && (iIndex < editText.length()))
  337.         {
  338.             // try to grab the word. 
  339.             // getWordEdge definition:
  340.             //        getWordEdge(int iPos, boolean pleaseMoveLeft, 
  341.             //                    boolean givePrevious)
  342.             // the ifs test for spaces - if we click right before
  343.             //        or after a space, we have to make sure we
  344.             //        grab the edge of the current word and not the
  345.             //        edge of the next (or prev) word.
  346.             
  347.             int iWordLeftEdge = 0;
  348.             int iWordRightEdge = 0;
  349.             
  350.             if ((iIndex > 0) && (editText.charAt(iIndex-1) == ' '))
  351.             {
  352.                 iWordLeftEdge = getWordEdge(iIndex, true, false);
  353.             }
  354.             else
  355.             {
  356.                 iWordLeftEdge = getWordEdge(iIndex, true, true);
  357.             }
  358.             
  359.             if ((iIndex < editText.length()-1) && (editText.charAt(iIndex) == ' '))
  360.             {
  361.                 iWordRightEdge = getWordEdge(iIndex+1, false, true);
  362.             }
  363.             else
  364.             {
  365.                 iWordRightEdge = getWordEdge(iIndex, false, false);
  366.             }
  367.             
  368.             return editText.substring(iWordLeftEdge, iWordRightEdge-1);
  369.         }
  370.         
  371.         return null;
  372.     }
  373.     
  374.     // protected method. Copies given text to clipboard.
  375.     protected void copyToClipboard(String text)
  376.     {
  377.         StringSelection cbdata = new StringSelection(text);
  378.         clipBoard.setContents(cbdata, cbdata);
  379.     }
  380.     
  381.     
  382.     protected String getClipboardContents()
  383.     {
  384.         Transferable cbdata = clipBoard.getContents(this);
  385.         if (cbdata != null)
  386.         {
  387.             try 
  388.             {
  389.                 return (String)cbdata.getTransferData(DataFlavor.stringFlavor);                            
  390.             }
  391.             catch (Exception e)
  392.             {
  393.                 System.out.println("Data not string flavor");
  394.             }                        
  395.         }
  396.         
  397.         return null;
  398.     }
  399.     
  400.     //
  401.     // ITextOperation methods
  402.     //
  403.     
  404.     
  405.     /**
  406.     *    IFileOperationTarget method. Called when the user wants to perform a cut
  407.     *    operation. Does the usual thing.
  408.     */
  409.     public void cut()
  410.     {            
  411.         int iSelStart = getSelectionStart();
  412.         int iSelEnd = getSelectionEnd();
  413.         
  414.         if (iSelStart != iSelEnd)
  415.         {
  416.             String selText = getSelectedText();
  417.             
  418.             copyToClipboard(selText);
  419.             
  420.             remove(iSelStart, iSelEnd - iSelStart);
  421.             
  422.             setSelection(iSelStart, iSelStart);
  423.             
  424.             moveTheCaret(iSelStart);
  425.             
  426.             requestFocus();
  427.         }
  428.     }
  429.     
  430.     /**
  431.     *    IFileOperationTarget method. Called when the user wants to do a copy
  432.     *    operation. Does the usual thing.
  433.     */
  434.     public void copy()
  435.     {            
  436.         if (getSelectionStart() != getSelectionEnd())
  437.         {
  438.             copyToClipboard(getSelectedText());
  439.         }
  440.     }
  441.     
  442.     /**
  443.     *    IFileOperationTarget method. Called when the user wants to do a paste
  444.     *    operation. Does the usual thing.
  445.     */
  446.     public void paste()
  447.     {
  448.         String data = getClipboardContents();
  449.         paste(data);                        
  450.     }
  451.     
  452.     /**
  453.     *    IFileOperationTarget method. Called when the user wants to change the
  454.     *    font. Pops up a AwtUIFontDialog and does the usual thing.
  455.     */    
  456.     public void changeFont()
  457.     {
  458.         UIFontDialog dialog = new UIFontDialog(parentFrame);
  459.         dialog.show();
  460.         
  461.         FxFont font = dialog.getFxFont();
  462.         
  463.         if (font != null)
  464.         {
  465.             setFont(font);
  466.         }
  467.     }
  468.     
  469.     /**
  470.     *    IFileOperationTarget method. Called when the user wants to change the
  471.     *    font to a certain font. Does the usual thing.
  472.     *
  473.     *    @param font    Font to change to
  474.     */
  475.     public void changeFont(FxFont font)
  476.     {
  477.         setFont(font);
  478.     }
  479.     
  480.     /**
  481.     *    IFileOperationTarget method. Called when the user wants to change the
  482.     *    text color. Pops up a AwtUIColorDialog and does the usual thing.
  483.     */    
  484.     public void changeTextColor()
  485.     {
  486.         UIColorDialog dialog = new UIColorDialog(parentFrame);            
  487.         dialog.show();
  488.         
  489.         FxColor c = dialog.getFxColor();
  490.         if (c != null)
  491.         {
  492.             setForeground(c);
  493.             repaint();
  494.         }                
  495.     }
  496.     
  497.     public void changeBackColor()
  498.     {
  499.         UIColorDialog dialog = new UIColorDialog(parentFrame);
  500.         dialog.show();
  501.         
  502.         FxColor c = dialog.getFxColor();
  503.         if (c != null)
  504.         {
  505.             setBackground(c);
  506.             repaint();
  507.         }                
  508.     }
  509.  
  510.     public FxColor getTextColor()
  511.     {
  512.         Color c=getForeground();
  513.         if(c!=null)
  514.             return FxColor.getExtendedColor(c);
  515.         return null;
  516.     }
  517.     
  518.     public FxColor getBackColor()
  519.     {
  520.         Color c=getBackground();
  521.         if(c!=null)
  522.             return FxColor.getExtendedColor(c);
  523.         return null;
  524.     }
  525.     
  526.     public void setTextColor(FxColor color)
  527.     {
  528.         setForeground((Color)color);            
  529.     }
  530.     
  531.     public void setBackColor(FxColor color)
  532.     {
  533.         setBackground((Color)color);            
  534.     }
  535.     
  536.     public void selectAll()
  537.     {
  538.         setSelection(0, getText().length());
  539.     }
  540.     
  541.     //
  542.     // ITextOperationTargetExt functions
  543.     //
  544.     
  545.     public void paste(String text)
  546.     {
  547.         int iSelStart = getSelectionStart();
  548.         int iSelEnd = getSelectionEnd();
  549.         
  550.         // if no selection, insert at current caret position
  551.         // if selection, paste over hilited text
  552.         if (iSelStart == iSelEnd)
  553.         {
  554.             iSelStart = iSelEnd = getCaretIndex();
  555.         }
  556.         
  557.         String str = getText();
  558.         str = str.substring(0, iSelStart) + text + str.substring(iSelEnd);
  559.         setText(str);
  560.         moveTheCaret(iSelStart + text.length());
  561.     }
  562.     
  563.     /**
  564.     *    Inserts text at the given character position in the control.
  565.     *    If iPos < 0, insert at caret position.
  566.     *
  567.     *    @param text String to insert
  568.     *    @parem iPos Position to insert at. If < 0, insert at caret.
  569.     */
  570.     public void insertText(String text, int iPos)
  571.     {
  572.         if (iPos < 0)
  573.         {
  574.             insert(text, getCurrIndex());
  575.         }
  576.         else
  577.         {
  578.             insert(text, iPos);
  579.         }
  580.     }
  581.     
  582.     public void moveCaret(int idx)
  583.     {
  584.         setCurrIndex(idx);
  585.     }
  586.  
  587.     public void appendText(String text)
  588.     {
  589.         append(text);
  590.     }
  591.     
  592.     public void delete()
  593.     {    
  594.         int iSelStart = getSelectionStart();
  595.         int iSelEnd = getSelectionEnd();
  596.         
  597.         if (iSelStart != iSelEnd)
  598.         {
  599.             remove(iSelStart, iSelEnd - iSelStart);
  600.             
  601.             setSelection(iSelStart, iSelStart);
  602.             
  603.             moveTheCaret(iSelStart);
  604.             
  605.             requestFocus();
  606.         }
  607.     }
  608.     
  609.     public int getCaretIndex()
  610.     {
  611.         return getCurrIndex();
  612.     }
  613.     
  614.     public void moveTheCaret(int idx)
  615.     {
  616.         setCurrIndex(idx);
  617.     }
  618.     
  619.     public void setText(String text)
  620.     {
  621.         setValueText(text);
  622.     }
  623.     
  624.     public String getText()
  625.     {
  626.         return getValueText();
  627.     }
  628.  
  629.     // part of IUIFocusListener interface.
  630.  
  631.     /**
  632.     *    Called when this control gets focus, via the IUIFocusListener interface.    
  633.     *    Sets the current word wrap style.
  634.     *
  635.     *    @param    evt    The UIFocusEvent we are handling
  636.     */
  637.     public void focusGained(UIFocusEvent evt)
  638.     {
  639.         setWordWrapStyle(getWordWrap());
  640.     }
  641.     
  642.     /**
  643.     *    Called when this control loses focus, via the IUIFocusListener interface.    
  644.     *    Does nothing.
  645.     *
  646.     *    @param    evt    The UIFocusEvent we are handling
  647.     */
  648.     public void focusLost(UIFocusEvent evt)
  649.     {
  650.         // we don't do anything if focus is lost
  651.     }
  652.     
  653. }
  654.  
  655. /**
  656. *    Thread to start processes. We run external programs using a thread
  657. *    so we can continue to use the app while the program loads.
  658. *    Used by JNoteUIEdit.
  659. *
  660. *    @version    1.0, 8/2/97
  661. *    @see        JNoteUIEdit
  662. */
  663. class ExeRunnerThread extends Thread
  664. {
  665. /**
  666. *    Name of program to run.
  667.     */
  668.     String exeFile;
  669.     
  670.     /**
  671.     *    Creates a new ExeRunnerThread.
  672.     *    
  673.     *    @param    exeFile    Name of program to run.
  674.     */
  675.     public ExeRunnerThread(String exefile)
  676.     {
  677.         super();
  678.         exeFile = exefile;
  679.     }
  680.     
  681.     /**
  682.     *    Called when thread is started. Overrides Thread.start(). Runs
  683.     *    the program using Runtime.exec() and then quits.
  684.     */
  685.     public void start()
  686.     {                        
  687.         try
  688.         {
  689.             Runtime.getRuntime().exec(exeFile);
  690.         }
  691.         catch (IOException e1)
  692.         {
  693.             // error trying to run file
  694.             UIMessageBox mb = new UIMessageBox(new UIFrame(), 
  695.                 JNotePad.loadString(ResourceIDs.IDS_MSGFILERUNERR),
  696.                 JNotePad.loadString(ResourceIDs.IDS_MSGRUNERROR)+exeFile,
  697.                 UIMessageBox.INFORMATION, UIButtonBar.OK);
  698.             mb.doModal();
  699.         }
  700.     }
  701. }
  702.