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

  1. //
  2. // (C) Copyright 1995 - 1999 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, 
  20.     ITextOperationTarget{
  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.     private boolean closeFile(IFileOperationTargetExt fc){
  214.         String filename = fc.getFileName();
  215.         if (filename == null){
  216.             filename =
  217.                 JNotePad.loadString(ResourceIDs.IDS_UNTITLEDFILE);
  218.             fc.setFile(".",filename);
  219.         }
  220.         if (currentFile.isDirty()){
  221.             // save confirmation
  222.             UIMessageBox msgbox = new UIMessageBox(parentFrame, 
  223.                 JNotePad.loadString(ResourceIDs.IDS_MSGSAVEFILE),
  224.                 "Do you want to save "+filename+"?",
  225.                 UIMessageBox.QUESTION, UIButtonBar.YESNOCANCEL);
  226.             int iRet = ((UIButton)msgbox.doModal()).getID();
  227.             if (iRet == UIButtonBar.ID_YES){
  228.                 fc.saveFile();
  229.                 //this.saveFileAs(); //Use this to bring up a saveas
  230.                                     //dialog.
  231.             }else if (iRet == UIButtonBar.ID_CANCEL)
  232.                 return false;
  233.         }
  234.         return true;
  235.     }
  236.     public boolean closingApp()
  237.     {
  238.         Enumeration e = loadedFiles.elements();
  239.         while (e.hasMoreElements())
  240.         {
  241.             IFileOperationTargetExt fc = (IFileOperationTargetExt )e.nextElement();
  242.             if (fc.isDirty())
  243.             {                            
  244.                 if (!closeFile(fc))
  245.                 {
  246.                     return false;
  247.                 }
  248.             }
  249.         }        
  250.         
  251.         return true;
  252.     }
  253.     
  254.     public IFileOperationTargetExt getCurrentFile()
  255.     {
  256.         return currentFile;
  257.     }
  258.     
  259.     //
  260.     // IFileOperationTarget methods
  261.     //
  262.     
  263.     public void newFile()
  264.     {
  265.         addNewFileTab(null, null);            
  266.     }
  267.     
  268.     public boolean saveFile()
  269.     {
  270.         boolean ret = false;
  271.         
  272.         if (currentFile.getFileName() == null)
  273.         {
  274.             ret = saveFileAs();
  275.             
  276.             String curFileName = currentFile.getFileName();
  277.             if (curFileName != null)
  278.             {                            
  279.                 getSelectedItem().setName(curFileName);
  280.             }                                        
  281.         }
  282.         else
  283.         {
  284.             ret = currentFile.saveFile();
  285.         }
  286.         
  287.         return ret;
  288.     }
  289.     
  290.     public boolean saveFileAs()
  291.     {
  292.         if (!currentFile.shouldSave())
  293.         {
  294.             return true;
  295.         }
  296.         
  297.         // open a save dialog
  298.         FileDialog fdialog = new FileDialog(parentFrame.getFrame(), 
  299.                                     JNotePad.loadString(ResourceIDs.IDS_MSGSAVEFILE), 
  300.                                     FileDialog.SAVE);
  301.         fdialog.show();
  302.         
  303.         String fileName = fdialog.getFile();
  304.         String filePath = fdialog.getDirectory();
  305.         
  306.         if (fileName == null)
  307.         {
  308.             return true; // ?? correct behavior to return true if no filename?
  309.         }        
  310.         
  311.         currentFile.setFile(filePath, fileName);
  312.         return currentFile.saveFile();
  313.     }
  314.     
  315.     public boolean openFile()
  316.     {
  317.         // open an open file dialog
  318.         FileDialog fdialog = new FileDialog(parentFrame.getFrame(), 
  319.                                     JNotePad.loadString(ResourceIDs.IDS_MSGLOADFILE), 
  320.                                     FileDialog.LOAD);
  321.         fdialog.show();
  322.         
  323.         String fileName = fdialog.getFile();
  324.         String filePath = fdialog.getDirectory();
  325.         
  326.         if ((fileName == null) || (addNewFileTab(filePath, fileName) == null))
  327.         {
  328.             return false;
  329.         }
  330.         else
  331.         {            
  332.             return true;
  333.         }
  334.     }
  335.     
  336.     
  337.     public boolean closeFile()
  338.     {            
  339.         if (iNumFiles == 1)
  340.         {
  341.             return false;
  342.         }
  343.         
  344.         if (currentFile.shouldSave() && closeFile(currentFile))
  345.         {
  346.             int iIndex = getSelectedIndex();
  347.             
  348.             removeTab(iIndex);
  349.             
  350.             if (--iIndex < 0) 
  351.             {
  352.                 iIndex = 0;
  353.             }
  354.             setSelectedIndex(iIndex);
  355.             
  356.             repaint();
  357.             
  358.             return true;
  359.         }
  360.         
  361.         return false;
  362.     }            
  363.     
  364.     public void searchReplace(boolean isFind)
  365.     {
  366.         currentFile.searchReplace(isFind);
  367.     }
  368.     
  369.     public void printFile()
  370.     {    
  371.         //PrintJob pjob = Toolkit.getDefaultToolkit().getPrintJob(null, "Test", null);
  372.         WPrintJob pjob = null;
  373.         
  374.         try
  375.         {
  376.             Toolkit tools = parentFrame.getToolkit();
  377.             pjob = (WPrintJob)tools.getPrintJob(null, "JNotepad", null);
  378.             //pjob = parentFrame.getToolkit().getPrintJob(parentFrame.getFrame(), "Print", null);
  379.         }
  380.         catch (Exception e)
  381.         {
  382.             System.out.println("Exception thrown in print");
  383.             PrintStream fos = null;
  384.             try
  385.             {
  386.                 fos = new PrintStream(new FileOutputStream("jnotepad.log"));
  387.             }
  388.             catch (Exception e1)
  389.             {
  390.                 System.out.println("Exception in exception");
  391.             }                        
  392.             
  393.             e.printStackTrace(fos);
  394.             
  395.             fos.close();
  396.         }
  397.         
  398.         if (pjob != null)
  399.         {
  400.             Graphics g = pjob.getGraphics();
  401.             if (g != null)
  402.             {
  403.                 parentFrame.getFrame().paintAll(g);
  404.                 g.dispose();
  405.             }
  406.             
  407.             pjob.end();
  408.         }
  409.         else
  410.         {
  411.             System.out.println("Printjob is null");
  412.         }
  413.         
  414.         //pjob.end();
  415.     }
  416.     
  417.     public boolean otherCommand(String command, UIActionEvent evt)
  418.     {
  419.         boolean ret = false;
  420.         
  421.         IUIComponent comp=evt.getActionItem();
  422.         switch( comp.getID() )
  423.         { 
  424.         case ResourceIDs.ID_WINDOW_NEXT:
  425.             return doWindowNext();
  426.         case ResourceIDs.ID_WINDOW_PREVIOUS:
  427.             return doWindowPrevious();
  428.         case ResourceIDs.ID_FILE_EXIT:
  429.             return doFileExit();
  430.         }
  431.         
  432.         // !!
  433.         //if (Character.isDigit(command.charAt(0)))
  434.         //{
  435.         //    ret = processNumberCommands(command, evt);                                        
  436.         //}
  437.         //else                    
  438.         {
  439.             ret = currentFile.getEditControl().otherCommand(command, evt);
  440.         }
  441.         
  442.         return ret;
  443.     }
  444.     
  445.     protected boolean doWindowNext()
  446.     {
  447.         // # tabs is one less than # of components in the UITabViewer
  448.         int iIndex = getSelectedIndex();
  449.         int iMaxIndex = getComponentCount() - 1; 
  450.  
  451.         setSelectedIndex((iIndex+1) % iMaxIndex);
  452.         return true;
  453.     }
  454.  
  455.     protected boolean doWindowPrevious()
  456.     {
  457.         // # tabs is one less than # of components in the UITabViewer
  458.         int iIndex = getSelectedIndex();
  459.         int iMaxIndex = getComponentCount() - 1; 
  460.  
  461.         setSelectedIndex((iIndex-1+iMaxIndex) % iMaxIndex);
  462.         return true;
  463.     }
  464.  
  465.     protected boolean doFileExit()
  466.     {
  467.         // send a window destroy message to the parent. This handles closing up
  468.         // the app, cancelling if necessary, and destroys the parent window.
  469.         Event e = new Event(parentFrame, Event.WINDOW_DESTROY, this);
  470.         parentFrame.postEvent(e);                    
  471.         return true;
  472.     }
  473.  
  474.     private boolean processNumberCommands(String command, UIActionEvent evt)
  475.     {
  476.         if (command.length() < 3)
  477.         {
  478.             return true; // !!! should be false??
  479.         }
  480.         
  481.         boolean ret = false;
  482.         
  483.         if (command.charAt(1) == ' ')                
  484.         {
  485.             // blank line after the # means a MRU selection
  486.             File f = new File(command.substring(2));
  487.             if (f.exists())
  488.             {
  489.                 addNewFileTab(null, f.getPath());
  490.                 ret = true;
  491.             }
  492.             else
  493.             {
  494.                 // file not found
  495.                 UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  496.                     JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  497.                     JNotePad.loadString(ResourceIDs.IDS_MSGFILENOTFOUND)+command.substring(2),
  498.                     UIMessageBox.STOP, UIButtonBar.OK);
  499.                 _box_.doModal();
  500.             }
  501.         }
  502.         else
  503.         {
  504.             // internal error
  505.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  506.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  507.                 JNotePad.loadString(ResourceIDs.IDS_MSGINTERNALERR),
  508.                 box.STOP, UIButtonBar.OK);
  509.             box.doModal();
  510.         }        
  511.         
  512.         return ret;
  513.     }
  514.     
  515.     //
  516.     // ITextOperationTarget methods
  517.     // These simply route the command to the current file or the current
  518.     // edit control window.
  519.     //
  520.     
  521.     public boolean undo()
  522.     {
  523.         return currentFile.getEditControl().undo();
  524.     }
  525.     
  526.     public boolean redo()
  527.     {
  528.         return currentFile.getEditControl().redo();
  529.     }
  530.     
  531.     public void cut()
  532.     {
  533.         currentFile.getEditControl().cut();    
  534.     }
  535.     
  536.     public void copy()
  537.     {
  538.         currentFile.getEditControl().copy();
  539.     }
  540.     
  541.     public void paste()
  542.     {
  543.         currentFile.getEditControl().paste();
  544.     }
  545.     
  546.     public void delete()
  547.     {
  548.         currentFile.getEditControl().delete();
  549.     }
  550.     
  551.     public void selectAll()
  552.     {
  553.         currentFile.getEditControl().selectAll();
  554.     }
  555.     
  556.     public void changeTextColor()
  557.     {
  558.         currentFile.getEditControl().changeTextColor();        
  559.     }
  560.     
  561.     public void changeBackColor()
  562.     {
  563.         currentFile.getEditControl().changeBackColor();        
  564.     }
  565.     
  566.     public void changeFont()
  567.     {
  568.         currentFile.getEditControl().changeFont();
  569.     }
  570.     
  571.     public void changeFont(FxFont font)
  572.     {
  573.         currentFile.getEditControl().changeFont(font);
  574.     }
  575.     
  576.     public void setHorizAlign(int h)
  577.     {
  578.         currentFile.getEditControl().setHorizAlign(h);
  579.     }
  580.  
  581. }
  582.  
  583.  
  584. /**
  585. *    JNoteUIItem
  586. *
  587. *    Extends UIItem to add tooltips. This class is used to display file names
  588. *    on a tab in a TabFileViewer. If the name is long, we truncate it
  589. *    and have the tooltip display the real name.
  590. *
  591. *    @see    #TabFileViewer
  592. *
  593. *    @version    1.0, 8/16/97
  594. */
  595. class JNoteUIItem extends UIItem{
  596.     private String sLongname;
  597.     private int iMaxNameLength;
  598.     public static final int DEFAULT_NAME_LENGTH = 20;
  599.     
  600.     public JNoteUIItem(Image image, String text){
  601.         super(image, text, UIText.HOTTRACK);            
  602.     }
  603.     
  604.     private void init(String text){
  605.         iMaxNameLength = DEFAULT_NAME_LENGTH;
  606.         sLongname = text;
  607.         if (text.length() > iMaxNameLength)
  608.             text = text.substring(0, iMaxNameLength) + "...";
  609.         super.setName(text);
  610.     }
  611.     
  612.     public void setMaxNameLength(int maxlen){
  613.         iMaxNameLength = maxlen;
  614.         init(sLongname);
  615.     }
  616.     
  617.     public void setName(String name){
  618.         init(name);
  619.     }
  620.     
  621.     public String getHelp(){
  622.         return sLongname;
  623.     }    
  624. }