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

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. import FeatureTabFileViewer;
  5.  
  6. /**
  7. *    JNoteTabFileViewer
  8. *    
  9. *    JNotepad specific implementation of FeatureTabFileViewer.
  10. *    We extend this class and add JNotepad specific functionality.
  11. *    The following functionality is added:
  12. *        - Time/Date stamp
  13. *        - Properties dialog
  14. *        - About JNotepad dialog
  15. *
  16. *    @version    1.0, 8/17/97
  17. *
  18. *    @see    #FeatureTabFileViewer
  19. */
  20.  
  21. import com.ms.ui.*;
  22. import com.ms.fx.*;
  23. import com.ms.ui.event.*;
  24. import java.util.*;
  25. import java.awt.*;
  26.  
  27. public class JNoteTabFileViewer extends FeatureTabFileViewer
  28. {    
  29.     public JNoteTabFileViewer(UIFrame parFrame, ICommandFeedback feedback, IComponentFeature compfeature)
  30.     {
  31.         this(parFrame, null, feedback, compfeature);
  32.     }
  33.     
  34.     public JNoteTabFileViewer(UIFrame parFrame, UIBand menubar, ICommandFeedback feedback, IComponentFeature compfeature)
  35.     {
  36.         super(parFrame, menubar, feedback, compfeature);
  37.         setWindowAppearance("Default");
  38.     }
  39.     
  40.     public boolean otherCommand(String command, UIActionEvent evt)
  41.     {
  42.         boolean ret = false;
  43.         IUIComponent comp=evt.getActionItem();
  44.         switch( comp.getID() )
  45.         { 
  46.         case ResourceIDs.ID_HELP_ABOUT:
  47.             ret = doHelpAbout();
  48.             break;
  49.         case ResourceIDs.ID_PROPERTIES:
  50.             ret = doProperties();
  51.             break;
  52.         case ResourceIDs.ID_TIMEDATE:
  53.             ret = doTimeDate();
  54.             break;
  55.         case ResourceIDs.ID_EMACS_CUT:
  56.             ret = doEmacsCut();
  57.             break;
  58.         case ResourceIDs.ID_EMACS_PASTE:
  59.             ret = doEmacsPaste();
  60.             break;
  61.         default:
  62.             ret = super.otherCommand(command, evt);
  63.         }
  64.         return ret;
  65.     }
  66.     
  67.     protected void insertTimeDate()
  68.     {
  69.         Date curDateObj = new Date(System.currentTimeMillis());
  70.         String curDate = (curDateObj.getMonth()+1) + "/" + curDateObj.getDate() + 
  71.             "/" + curDateObj.getYear();
  72.         
  73.         int iHours = curDateObj.getHours();
  74.         String ampm = null;
  75.         
  76.         if (iHours > 11)
  77.         {
  78.             ampm = "PM";
  79.         }
  80.         else
  81.         {
  82.             ampm = "AM";
  83.         }
  84.         
  85.         if (iHours > 12)
  86.         {
  87.             iHours -= 12;
  88.         }
  89.         
  90.         String sMinutes = null;
  91.         int iMinutes = curDateObj.getMinutes();
  92.         if (iMinutes < 10)
  93.         {
  94.             sMinutes = "0" + iMinutes;
  95.         }
  96.         else
  97.         {
  98.             sMinutes = String.valueOf(iMinutes);
  99.         }
  100.         
  101.         ITextOperationTargetExt edit = getFileTarget().getEditControl();
  102.         
  103.         String curTime = iHours + ":" + sMinutes + " " + ampm;
  104.         String dateAndTime = curTime + " " + curDate;
  105.         int iCaretIndex = edit.getCaretIndex();
  106.         
  107.         if (edit.getSelectionStart() == edit.getSelectionEnd())
  108.         {
  109.             edit.insertText(dateAndTime, iCaretIndex);
  110.         }
  111.         else
  112.         {
  113.             edit.paste(dateAndTime);
  114.         }
  115.  
  116.         edit.moveTheCaret(iCaretIndex+dateAndTime.length());
  117.     }
  118.     
  119.     public void emacsCut()
  120.     {
  121.         ITextOperationTargetExt edit = getFileTarget().getEditControl();
  122.         String text = edit.getText()+"\n";
  123.         
  124.         int iPos = edit.getCaretIndex();
  125.         int iEndOfLine = text.indexOf('\n', iPos);
  126.         
  127.         if (iEndOfLine >= 0)
  128.         {
  129.             edit.setSelection(iPos, iEndOfLine);
  130.             cut();
  131.         }
  132.     }
  133.     
  134.     public void emacsPaste()
  135.     {
  136.     }
  137.     
  138.     protected void initNewTab()
  139.     {            
  140.         super.initNewTab();
  141.  
  142.         String sectionName = getFileTarget().getFileExtension();
  143.         
  144.         if ((sectionName == null) || (sectionName.length() == 0))
  145.         {
  146.             sectionName = "DEFAULT";
  147.         }
  148.         
  149.         sectionName = sectionName.toUpperCase();
  150.         
  151.         setWindowAppearance(sectionName);
  152.         
  153.         loadFileFeatures(sectionName);
  154.     }
  155.     
  156.     protected void updateColors()
  157.     {
  158.         ITextOperationTargetExt tTarget = getFileTarget().getEditControl();
  159.         
  160.         FxColor bgColor = tTarget.getBackColor();
  161.         FxColor fgColor = tTarget.getTextColor();
  162.         
  163.         if (feedbackObj != null)
  164.         {
  165.             feedbackObj.notifyColorChange(tTarget, bgColor, 
  166.                 ICommandFeedback.COLORCHANGE_BACKGROUND);
  167.             
  168.             feedbackObj.notifyColorChange(tTarget, fgColor, 
  169.                 ICommandFeedback.COLORCHANGE_FOREGROUND);            
  170.         }                    
  171.     }
  172.  
  173.     public void refreshCurrentTab()
  174.     {                    
  175.         updateColors();
  176.         super.refreshCurrentTab();
  177.     }
  178.     
  179.     protected void setWindowAppearance(String sectionName)
  180.     {
  181.         JNoteSettings settingsObj = JNoteAppSettings.getSettingsObj();
  182.         
  183.         FxColor bgColor = settingsObj.getColorIndirect(sectionName, "Background", FxColor.getExtendedColor(Color.white));
  184.         FxColor fgColor = settingsObj.getColorIndirect(sectionName, "Foreground", FxColor.getExtendedColor(Color.blue));
  185.         FxFont font = settingsObj.getFont(sectionName, "Font", new FxFont("Arial", 0, 14));
  186.         
  187.         if(getFileTarget() == null )
  188.         {
  189.             System.out.println("No file");
  190.             return;
  191.         }
  192.  
  193.         ITextOperationTargetExt tTarget = getFileTarget().getEditControl();
  194.         
  195.         tTarget.setBackColor(bgColor);
  196.         tTarget.setTextColor(fgColor);
  197.         tTarget.changeFont(font);            
  198.  
  199.         tTarget.repaint();
  200.     }
  201.     
  202.     protected boolean loadFileFeatures(String sectionName)
  203.     {
  204.         String features = JNoteAppSettings.getSettingsObj().get(sectionName, "Features", "");
  205.         StringTokenizer st = new StringTokenizer(features);
  206.         
  207.         while (st.hasMoreTokens())
  208.         {
  209.             loadFeature(st.nextToken());                    
  210.         }
  211.         
  212.         return true;
  213.     }
  214.     
  215.     protected boolean loadFeature(String featureName)
  216.     {
  217.         boolean ret = false;
  218.         
  219.         try 
  220.         {                    
  221.             Class newFeatureClass = Class.forName(featureName);
  222.             Object newFeatureInstance = newFeatureClass.newInstance();
  223.             
  224.             IFeature fl = (IFeature)newFeatureInstance;
  225.             
  226.             if (fl instanceof ITextFeature)
  227.             {
  228.                 registerListener(fl);
  229.                 ((ITextFeature)fl).init(getFileTarget().getEditControl());
  230.                 fl.register();                                    
  231.             }
  232.             
  233.             ret = true;
  234.         }
  235.         catch (ClassNotFoundException e)
  236.         {
  237.             // plug in feature could not be found
  238.             UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  239.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  240.                 JNotePad.loadString(ResourceIDs.IDS_MSGNOFEATURE)+featureName,
  241.                 UIMessageBox.STOP, UIButtonBar.OK);
  242.             _box_.doModal();
  243.             
  244.         }
  245.         catch (InstantiationException e1)
  246.         {
  247.             // plug in feature could not be loaded/created
  248.             UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  249.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  250.                 JNotePad.loadString(ResourceIDs.IDS_MSGNOFEATUREINIT)+featureName,
  251.                 UIMessageBox.STOP, UIButtonBar.OK);
  252.             _box_.doModal();
  253.             
  254.         }
  255.         catch (IllegalAccessException e2)
  256.         {
  257.             // plug in feature could not be accessed
  258.             UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  259.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  260.                 JNotePad.loadString(ResourceIDs.IDS_MSGNOFEATUREINIT)+featureName,
  261.                 UIMessageBox.STOP, UIButtonBar.OK);
  262.             _box_.doModal();
  263.             
  264.         }
  265.         catch (NullPointerException e3)
  266.         {
  267.             // can't load/init plug in feature
  268.             UIMessageBox _box_ = new UIMessageBox(new UIFrame(), 
  269.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  270.                 JNotePad.loadString(ResourceIDs.IDS_MSGNOFEATUREINIT)+featureName,
  271.                 UIMessageBox.STOP, UIButtonBar.OK);
  272.             _box_.doModal();
  273.             
  274.         }
  275.         
  276.         return ret;
  277.     }
  278.  
  279.     protected boolean doHelpAbout()
  280.     {
  281.         // Create an About JNotepad dialog.
  282.         JNoteDialog dialog = new JNoteDialog(parentFrame, "JNotepad", true);
  283.         // Load the dialog box from the resource file
  284.         dialog.loadDialog( JNotePad.getResources(), IDR_DIALOG_ABOUT);
  285.         dialog.display();
  286.         return true;
  287.     }
  288.  
  289.     protected boolean doProperties()
  290.     {
  291.         // Create and display a properties dialog, which displays file statistics.
  292.         JNoteStatsDialog dialog = new JNoteStatsDialog(parentFrame, getFileTarget());
  293.         dialog.display();
  294.         return true;
  295.     }
  296.  
  297.     protected boolean doTimeDate()
  298.     {
  299.         // insert a time/date stamp, just like Notepad does
  300.         insertTimeDate();
  301.         return true;
  302.     }
  303.  
  304.     protected boolean doEmacsCut()
  305.     {
  306.         emacsCut();
  307.         return true;
  308.     }
  309.  
  310.     protected boolean doEmacsPaste()
  311.     {
  312.         emacsPaste();
  313.         return true;
  314.     }
  315. }
  316.  
  317.  
  318.