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

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4. import java.util.*;
  5. import java.io.*;
  6. import java.awt.*;
  7.  
  8. import com.ms.awt.*;
  9. import com.ms.ui.*;
  10. import com.ms.fx.*;
  11. import com.ms.ui.event.*;
  12. import com.ms.fx.FxFont;
  13.  
  14. //
  15. //
  16. // TabFileViewer
  17. //
  18. //
  19. public class TabFileViewer extends UITabViewer implements IFileOperationTarget, ITextOperationTarget
  20. {
  21.     protected Vector loadedFiles;
  22.     protected int iNumFiles;
  23.     private   IFileOperationTargetExt currentFile;
  24.     protected UIFrame parentFrame;
  25.     private   int iNumNewFiles;
  26.     private   ClipboardControl clipboardControl;
  27.     static    private Image clipboardImage;
  28.     
  29.  
  30.     public IFileOperationTargetExt getFileTarget()
  31.     {
  32.         return currentFile;
  33.     }
  34.  
  35.     protected TabFileViewer()
  36.     {
  37.         this(null);
  38.     }
  39.     
  40.     public TabFileViewer(UIFrame parentFrame)
  41.     {
  42.         super();
  43.  
  44.         loadedFiles = new Vector(5);
  45.         this.parentFrame = parentFrame;
  46.         iNumFiles = 0;
  47.         iNumNewFiles = 0;    
  48.         currentFile = null;
  49.  
  50.         addClipboardTab();
  51.     }
  52.     
  53.     private void addToVector(IFileOperationTargetExt fc)
  54.     {
  55.         loadedFiles.addElement(fc);
  56.         iNumFiles++;
  57.     }
  58.  
  59.     protected Image getClipboardImage()
  60.     {
  61.         if(clipboardImage!=null)
  62.             return clipboardImage;
  63.  
  64.         clipboardImage = Toolkit.getDefaultToolkit().getImage(clipboardControl.getImageName());
  65.         // create an MediaTracker, which will wait until the image is
  66.         // entirely loaded before displaying it
  67.         MediaTracker mt = new MediaTracker(FxToolkit.getHelperFrame());
  68.         mt.addImage(clipboardImage,0);
  69.         try
  70.         {
  71.             mt.waitForAll();
  72.         }
  73.         catch (InterruptedException e)
  74.         {
  75.             // we don't care if we're interrupted...the worst that
  76.             // could happen is that the image loads gradually, which
  77.             // isn't a big deal. This rarely. if ever, happens.
  78.         }
  79.  
  80.         return clipboardImage;
  81.     }
  82.  
  83.     public void addClipboardTab()
  84.     {
  85.         // create a new edit control inside a new scroll viewer. This
  86.         // automatically gives us scrolling inside the edit control.
  87.  
  88.         JNoteUIEdit edit = new JNoteUIEdit(parentFrame);
  89.         UIScrollViewer sv = new UIScrollViewer(edit);
  90.         
  91.         // Create a clipboard control which will encapsulate this edit control 
  92.         // as a file object (of class FileControl).
  93.         clipboardControl = new ClipboardControl((ITextOperationTargetExt)edit, parentFrame);
  94.         
  95.         // add to vector of loaded files
  96.         addToVector(clipboardControl);
  97.         
  98.         currentFile = clipboardControl;
  99.  
  100.         initNewTab();
  101.         
  102.         // add the UIItem glyph and the ScrollViewer to the tab viewer.
  103.         // The UIItem is displayed on the tab itself, and the ScrollViewer is the
  104.         // content displayed inside the tab body.
  105.         addTab(new JNoteUIItem(getClipboardImage(), clipboardControl.getDisplayFileName()), sv);
  106.     }
  107.  
  108.     //
  109.     //
  110.     //
  111.     public UIPanel addNewFileTab(String path, String name)
  112.     {    
  113.         // create a new edit control inside a new scroll viewer
  114.         JNoteUIEdit edit = new JNoteUIEdit(parentFrame);
  115.         UIScrollViewer sv = new UIScrollViewer(edit);
  116.         UIPanel p = new UIPanel();
  117.         p.setLayout(new UIBorderLayout());
  118.         p.add(sv, "center");
  119.         
  120.         DiskFileControl dfc = new DiskFileControl((ITextOperationTargetExt)edit, parentFrame);
  121.         if (dfc.init(path, name))
  122.         {
  123.             // add to vector of loaded files
  124.             addToVector(dfc);
  125.             
  126.             Image im = Toolkit.getDefaultToolkit().getImage(dfc.getImageName());                            
  127.             JNoteUIItem glyph = null;
  128.             
  129.             // add tab to viewer
  130.             if (name == null)
  131.             {
  132.                 ++iNumNewFiles;
  133.                 
  134.                 // create a glyph with "New File" followed by the number of new files in this
  135.                 // session
  136.                 glyph = new JNoteUIItem(im, JNotePad.loadString(ResourceIDs.IDS_NEWFILETITLE)+iNumNewFiles);
  137.             }
  138.             else
  139.             {
  140.                 glyph = new JNoteUIItem(im, dfc.getDisplayFileName());                            
  141.             }
  142.             
  143.             removeTab(getChildCount()-1);
  144.  
  145.             addTab(glyph, p);
  146.             
  147.             addClipboardTab();
  148.             
  149.             // set current file
  150.             currentFile = dfc;
  151.  
  152.             // init the new tab
  153.             initNewTab();
  154.             
  155.             // set tab selection to this new file
  156.             setSelectedIndex(iNumFiles-2);
  157.             
  158.             edit.requestFocus();
  159.             
  160.             return p;
  161.         }
  162.         
  163.         return null;
  164.     }
  165.  
  166.     /**
  167.     *    Called when a new tab is created but before it is displayed.
  168.     */
  169.     protected void initNewTab()
  170.     {
  171.         // does nothing here
  172.     }
  173.     
  174.     protected void refreshTab(UITab tab, IFileOperationTargetExt fc)
  175.     {
  176.         if (fc.getFileName() != null)
  177.         {
  178.             tab.setName(fc.getDisplayFileName());
  179.         }
  180.         tab.invalidateAll();
  181.     }
  182.     
  183.     public void refreshCurrentTab()
  184.     {
  185.         refreshTab((UITab)getSelectedItem(), currentFile);
  186.         currentFile.gotFocus();        
  187.     }
  188.     
  189.     public void removeTab(int iIndex)
  190.     {
  191.         super.removeTab(iIndex);
  192.         loadedFiles.removeElementAt(iIndex);
  193.         --iNumFiles;
  194.     }
  195.     
  196.     public boolean handleEvent(Event e)
  197.     {
  198.         if ((e.arg instanceof UITab) && (e.id == Event.LIST_SELECT))
  199.         {
  200.             currentFile = (IFileOperationTargetExt )(loadedFiles.elementAt(getSelectedIndex()));
  201.             refreshCurrentTab();
  202.         }
  203.         else if ((e.id == Event.KEY_PRESS) && (e.key == '\t'))
  204.         {
  205.             // do nothing. We want the key listener to handle
  206.             // tab switching.
  207.             return(true);
  208.         }
  209.         
  210.         return(super.handleEvent(e));
  211.     }
  212.     
  213.     
  214.     private boolean closeFile(IFileOperationTargetExt fc)
  215.     {
  216.         String filename = fc.getFileName();
  217.         
  218.         if (filename == null)
  219.         {
  220.             filename = JNotePad.loadString(ResourceIDs.IDS_UNTITLEDFILE);
  221.         }
  222.         
  223.         if (currentFile.isDirty())
  224.         {
  225.             // save confirmation
  226.             UIMessageBox msgbox = new UIMessageBox(parentFrame, 
  227.                 JNotePad.loadString(ResourceIDs.IDS_MSGSAVEFILE),
  228.                 "Do you want to save "+filename+"?",
  229.                 UIMessageBox.QUESTION, UIButtonBar.YESNOCANCEL);
  230.             
  231.             int iRet = ((UIButton)msgbox.doModal()).getID();
  232.             
  233.             if (iRet == UIButtonBar.ID_YES)
  234.             {
  235.                 fc.saveFile();
  236.             }
  237.             else if (iRet == UIButtonBar.ID_CANCEL)
  238.             {
  239.                 return false;
  240.             }
  241.         }
  242.         
  243.         return true;
  244.     }
  245.     
  246.     
  247.     public boolean closingApp()
  248.     {
  249.         Enumeration e = loadedFiles.elements();
  250.         while (e.hasMoreElements())
  251.         {
  252.             IFileOperationTargetExt fc = (IFileOperationTargetExt )e.nextElement();
  253.             if (fc.isDirty())
  254.             {                            
  255.                 if (!closeFile(fc))
  256.                 {
  257.                     return false;
  258.                 }
  259.             }
  260.         }        
  261.         
  262.         return true;
  263.     }
  264.     
  265.     public IFileOperationTargetExt getCurrentFile()
  266.     {
  267.         return currentFile;
  268.     }
  269.     
  270.     //
  271.     // IFileOperationTarget methods
  272.     //
  273.     
  274.     public void newFile()
  275.     {
  276.         addNewFileTab(null, null);            
  277.     }
  278.     
  279.     public boolean saveFile()
  280.     {
  281.         boolean ret = false;
  282.         
  283.         if (currentFile.getFileName() == null)
  284.         {
  285.             ret = saveFileAs();
  286.             
  287.             String curFileName = currentFile.getFileName();
  288.             if (curFileName != null)
  289.             {                            
  290.                 getSelectedItem().setName(curFileName);
  291.             }                                        
  292.         }
  293.         else
  294.         {
  295.             ret = currentFile.saveFile();
  296.         }
  297.         
  298.         return ret;
  299.     }
  300.     
  301.     public boolean saveFileAs()
  302.     {
  303.         if (!currentFile.shouldSave())
  304.         {
  305.             return true;
  306.         }
  307.         
  308.         // open a save dialog
  309.         FileDialog fdialog = new FileDialog(parentFrame.getFrame(), 
  310.                                     JNotePad.loadString(ResourceIDs.IDS_MSGSAVEFILE), 
  311.                                     FileDialog.SAVE);
  312.         fdialog.show();
  313.         
  314.         String fileName = fdialog.getFile();
  315.         String filePath = fdialog.getDirectory();
  316.         
  317.         if (fileName == null)
  318.         {
  319.             return true; // ?? correct behavior to return true if no filename?
  320.         }        
  321.         
  322.         currentFile.setFile(filePath, fileName);
  323.         return currentFile.saveFile();
  324.     }
  325.     
  326.     public boolean openFile()
  327.     {
  328.         // open an open file dialog
  329.         FileDialog fdialog = new FileDialog(parentFrame.getFrame(), 
  330.                                     JNotePad.loadString(ResourceIDs.IDS_MSGLOADFILE), 
  331.                                     FileDialog.LOAD);
  332.         fdialog.show();
  333.         
  334.         String fileName = fdialog.getFile();
  335.         String filePath = fdialog.getDirectory();
  336.         
  337.         if ((fileName == null) || (addNewFileTab(filePath, fileName) == null))
  338.         {
  339.             return false;
  340.         }
  341.         else
  342.         {            
  343.             return true;
  344.         }
  345.     }
  346.     
  347.     
  348.     public boolean closeFile()
  349.     {            
  350.         if (iNumFiles == 1)
  351.         {
  352.             return false;
  353.         }
  354.         
  355.         if (currentFile.shouldSave() && closeFile(currentFile))
  356.         {
  357.             int iIndex = getSelectedIndex();
  358.             
  359.             removeTab(iIndex);
  360.             
  361.             if (--iIndex < 0) 
  362.             {
  363.                 iIndex = 0;
  364.             }
  365.             setSelectedIndex(iIndex);
  366.             
  367.             repaint();
  368.             
  369.             return true;
  370.         }
  371.         
  372.         return false;
  373.     }            
  374.     
  375.     public void searchReplace(boolean isFind)
  376.     {
  377.         currentFile.searchReplace(isFind);
  378.     }
  379.     
  380.     public void printFile()
  381.     {    
  382.         //PrintJob pjob = Toolkit.getDefaultToolkit().getPrintJob(null, "Test", null);
  383.         WPrintJob pjob = null;
  384.         
  385.         try
  386.         {
  387.             Toolkit tools = parentFrame.getToolkit();
  388.             pjob = (WPrintJob)tools.getPrintJob(null, "JNotepad", null);
  389.             //pjob = parentFrame.getToolkit().getPrintJob(parentFrame.getFrame(), "Print", null);
  390.         }
  391.         catch (Exception e)
  392.         {
  393.             System.out.println("Exception thrown in print");
  394.             PrintStream fos = null;
  395.             try
  396.             {
  397.                 fos = new PrintStream(new FileOutputStream("jnotepad.log"));
  398.             }
  399.             catch (Exception e1)
  400.             {
  401.                 System.out.println("Exception in exception");
  402.             }                        
  403.             
  404.             e.printStackTrace(fos);
  405.             
  406.             fos.close();
  407.         }
  408.         
  409.         if (pjob != null)
  410.         {
  411.             Graphics g = pjob.getGraphics();
  412.             if (g != null)
  413.             {
  414.                 parentFrame.getFrame().paintAll(g);
  415.                 g.dispose();
  416.             }
  417.             
  418.             pjob.end();
  419.         }
  420.         else
  421.         {
  422.             System.out.println("Printjob is null");
  423.         }
  424.         
  425.         //pjob.end();
  426.     }
  427.     
  428.     public boolean otherCommand(String command, UIActionEvent evt)
  429.     {
  430.         boolean ret = false;
  431.         
  432.         IUIComponent comp=evt.getActionItem();
  433.         switch( comp.getID() )
  434.         { 
  435.         case ResourceIDs.ID_WINDOW_NEXT:
  436.             return doWindowNext();
  437.         case ResourceIDs.ID_WINDOW_PREVIOUS:
  438.             return doWindowPrevious();
  439.         case ResourceIDs.ID_FILE_EXIT:
  440.             return doFileExit();
  441.         }
  442.         
  443.         // !!
  444.         //if (Character.isDigit(command.charAt(0)))
  445.         //{
  446.         //    ret = processNumberCommands(command, evt);                                        
  447.         //}
  448.         //else                    
  449.         {
  450.             ret = currentFile.getEditControl().otherCommand(command, evt);
  451.         }
  452.         
  453.         return ret;
  454.     }
  455.     
  456.     protected boolean doWindowNext()
  457.     {
  458.         // # tabs is one less than # of components in the UITabViewer
  459.         int iIndex = getSelectedIndex();
  460.         int iMaxIndex = getComponentCount() - 1; 
  461.  
  462.         setSelectedIndex((iIndex+1) % iMaxIndex);
  463.         return true;
  464.     }
  465.  
  466.     protected boolean doWindowPrevious()
  467.     {
  468.         // # tabs is one less than # of components in the UITabViewer
  469.         int iIndex = getSelectedIndex();
  470.         int iMaxIndex = getComponentCount() - 1; 
  471.  
  472.         setSelectedIndex((iIndex-1+iMaxIndex) % iMaxIndex);
  473.         return true;
  474.     }
  475.  
  476.     protected boolean doFileExit()
  477.     {
  478.         // send a window destroy message to the parent. This handles closing up
  479.         // the app, cancelling if necessary, and destroys the parent window.
  480.         Event e = new Event(parentFrame, Event.WINDOW_DESTROY, this);
  481.         parentFrame.postEvent(e);                    
  482.         return true;
  483.     }
  484.  
  485.     private boolean processNumberCommands(String command, UIActionEvent evt)
  486.     {
  487.         if (command.length() < 3)
  488.         {
  489.             return true; // !!! should be false??
  490.         }
  491.         
  492.         boolean ret = false;
  493.         
  494.         if (command.charAt(1) == ' ')                
  495.         {
  496.             // blank line after the # means a MRU selection
  497.             File f = new File(command.substring(2));
  498.             if (f.exists())
  499.             {
  500.                 addNewFileTab(null, f.getPath());
  501.                 ret = true;
  502.             }
  503.             else
  504.             {
  505.                 // file not found
  506.                 UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  507.                     JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  508.                     JNotePad.loadString(ResourceIDs.IDS_MSGFILENOTFOUND)+command.substring(2),
  509.                     UIMessageBox.STOP, UIButtonBar.OK);
  510.                 _box_.doModal();
  511.             }
  512.         }
  513.         else
  514.         {
  515.             // internal error
  516.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  517.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  518.                 JNotePad.loadString(ResourceIDs.IDS_MSGINTERNALERR),
  519.                 box.STOP, UIButtonBar.OK);
  520.             box.doModal();
  521.         }        
  522.         
  523.         return ret;
  524.     }
  525.     
  526.     //
  527.     // ITextOperationTarget methods
  528.     // These simply route the command to the current file or the current
  529.     // edit control window.
  530.     //
  531.     
  532.     public boolean undo()
  533.     {
  534.         return currentFile.getEditControl().undo();
  535.     }
  536.     
  537.     public boolean redo()
  538.     {
  539.         return currentFile.getEditControl().redo();
  540.     }
  541.     
  542.     public void cut()
  543.     {
  544.         currentFile.getEditControl().cut();    
  545.     }
  546.     
  547.     public void copy()
  548.     {
  549.         currentFile.getEditControl().copy();
  550.     }
  551.     
  552.     public void paste()
  553.     {
  554.         currentFile.getEditControl().paste();
  555.     }
  556.     
  557.     public void delete()
  558.     {
  559.         currentFile.getEditControl().delete();
  560.     }
  561.     
  562.     public void selectAll()
  563.     {
  564.         currentFile.getEditControl().selectAll();
  565.     }
  566.     
  567.     public void changeTextColor()
  568.     {
  569.         currentFile.getEditControl().changeTextColor();        
  570.     }
  571.     
  572.     public void changeBackColor()
  573.     {
  574.         currentFile.getEditControl().changeBackColor();        
  575.     }
  576.     
  577.     public void changeFont()
  578.     {
  579.         currentFile.getEditControl().changeFont();
  580.     }
  581.     
  582.     public void changeFont(FxFont font)
  583.     {
  584.         currentFile.getEditControl().changeFont(font);
  585.     }
  586.     
  587.     public void setHorizAlign(int h)
  588.     {
  589.         currentFile.getEditControl().setHorizAlign(h);
  590.     }
  591.  
  592. }
  593.  
  594.  
  595. /**
  596. *    JNoteUIItem
  597. *
  598. *    Extends UIItem to add tooltips. This class is used to display file names
  599. *    on a tab in a TabFileViewer. If the name is long, we truncate it
  600. *    and have the tooltip display the real name.
  601. *
  602. *    @see    #TabFileViewer
  603. *
  604. *    @version    1.0, 8/16/97
  605. */
  606. class JNoteUIItem extends UIItem
  607. {
  608.     private String sLongname;
  609.     private int iMaxNameLength;
  610.     public static final int DEFAULT_NAME_LENGTH = 20;
  611.     
  612.     public JNoteUIItem(Image image, String text)
  613.     {
  614.         super(image, text, UIText.HOTTRACK);            
  615.     }
  616.     
  617.     private void init(String text)
  618.     {
  619.         iMaxNameLength = DEFAULT_NAME_LENGTH;
  620.         
  621.         sLongname = text;
  622.         if (text.length() > iMaxNameLength)
  623.         {
  624.             text = text.substring(0, iMaxNameLength) + "...";
  625.         }            
  626.         super.setName(text);
  627.     }
  628.     
  629.     public void setMaxNameLength(int maxlen)
  630.     {
  631.         iMaxNameLength = maxlen;
  632.         init(sLongname);
  633.     }
  634.     
  635.     public void setName(String name)
  636.     {
  637.         init(name);
  638.     }
  639.     
  640.     public String getHelp()
  641.     {
  642.         return sLongname;
  643.     }
  644.     
  645. }
  646.