home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / TextPreview.java < prev    next >
Text File  |  1998-02-26  |  5KB  |  174 lines

  1. /*
  2.  * @(#)TextPreview.java    1.5 01/28/98
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not disclose
  8.  * such Confidential Information and shall use it only in accordance with the
  9.  * terms of the license agreement you entered into with Sun.
  10.  * 
  11.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  12.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  14.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  15.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  16.  * ITS DERIVATIVES.
  17.  * 
  18.  */
  19.  
  20. package com.sun.java.swing;
  21.  
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import java.io.*;
  25. import com.sun.java.swing.event.*;
  26.  
  27. /** A component to preview a text file, intended to fit into the
  28.  *  preview slot of a FileChooser.  In normal usage, only the
  29.  *  ask method is of interest: it pops up a dialog box with
  30.  *  a FileChooser containing an instance of this previewer and
  31.  *  filtering for appropriate files.
  32.  *  @author James Gosling
  33.  */
  34. public class TextPreview extends Component implements FilePreview {
  35.     private String txt[] = new String[10];
  36.     private boolean blank;
  37.  
  38.  
  39.     private static FileChooserFilter dfcf;
  40.  
  41.     /** Convenience method to prompt for the name of a text file.
  42.     For example:
  43.     <pre>File f = ImagePreview.ask(null,"Open file",previousFile,
  44.                     null,new FileChooserFilter("Config file (cfg)"));
  45.     </pre>
  46.     @param fparent the parent frame for the dialog box.
  47.         fparent may be null if a default parent has
  48.         been established with StandardDialog
  49.     @param description a description string that will be shown
  50.         to the user to indicate what is being requested
  51.     @param initial the initial value for the string
  52.     @param columns the default width (in 'm's) of the TextField
  53.     @param target the ChangeListener that will be informed if the
  54.             OK or Apply buttons are hit.
  55.     @param fcf    a FileChooserFilter for selecting the
  56.             file names to be presented in the dialog box.
  57.     @return If target is null,
  58.         the dialog box will be modal and the method
  59.         will return the final result, or null if canceled.
  60.         Otherwise, the
  61.         dialog box is non-modal, the method returns null
  62.         immediatly, and the listener is informed when
  63.         appropriate.  The source of the change event will
  64.         be a FileChooser.
  65.     @see StandardDialog
  66.      */
  67.     public static File ask(Component parent, String title, File dfc,
  68.                     ChangeListener target,
  69.                     FileChooserFilter fcf) {
  70.     StandardDialog d;
  71.     FileChooser c;
  72.     if (dfcf == null && fcf == null) {
  73.         dfcf = new FileChooserFilter();
  74.         dfcf.add("Text file (txt,exc,dic)");
  75.         dfcf.add("Hypertext file (html,htm)");
  76.         dfcf.add("Java source (java)");
  77.         dfcf.add("System file (ini,cfg,dat)");
  78.         dfcf.add("C program (c,cpp)");
  79.     }
  80.     c = new FileChooser();
  81.     c.setPreview(new TextPreview ());
  82.     d = new StandardDialog(parent,
  83.                    c, target == null);
  84.     c.setFilter(fcf != null ? fcf : dfcf);
  85.     if (dfc != null)
  86.         c.getModel().setFile(dfc);
  87.     if (title != null)
  88.         d.setTitle(title);
  89.     if (target != null)
  90.         d.addChangeListener(target);
  91.     d.start();
  92.     return d.isCanceled() || target != null ? null
  93.         : c.getModel().getFile();
  94.     }
  95.  
  96.     public TextPreview () {
  97.     }
  98.     public void setPreviewFile(File f) {
  99.     if (f == null) {
  100.         if (!blank) {
  101.         blank = true;
  102.         repaint();
  103.         }
  104.         return;
  105.     }
  106.     blank = false;
  107.     for (int i = txt.length; --i >= 0;)
  108.         txt[i] = null;
  109.     try {
  110.         Reader in = new BufferedReader(new FileReader(f));
  111.         char [] ln = new char[40];
  112.         int c;
  113.         int nc = 2000;
  114.         int l = 0;
  115.         int p = 0;
  116.         while ((c = in.read()) >= 0 && --nc >= 0) {
  117.         if (c == '\t')
  118.             c = ' ';
  119.         if ((c == '\n' || c == '\r') && p > 0) {
  120.             if (p > ln.length)
  121.             p = ln.length;
  122.             String s = new String(ln, 0, p);
  123.             if (!s.startsWith("//") && !s.startsWith("import")) {
  124.             txt[l++] = s;
  125.             if (l >= txt.length)
  126.                 break;
  127.             }
  128.             p = 0;
  129.             continue;
  130.         }
  131.         if (c >= 0177 || c == 0) {
  132.             blank = true;
  133.             break;
  134.         }
  135.         if (c > 040 || c == 040 && p > 0)
  136.             if (p < ln.length)
  137.             ln[p++] = (char) c;
  138.         }
  139.         in.close();
  140.     } catch(Throwable e) {
  141.         txt[0] = "Error: " + e;
  142.     }
  143.     repaint();
  144.     }
  145.     private Dimension isize = new Dimension(120, 120);
  146.     public void setSize(int w, int h) {
  147.     if (w != isize.width || h != isize.height) {
  148.         isize.width = w;
  149.         isize.height = h;
  150.         invalidate();
  151.     }
  152.     }
  153.     public void setBounds(int x, int y, int width, int height) {
  154.     super.setBounds(x, y, width, height);
  155.     setSize(width, height);
  156.     }
  157.     public Dimension getPreferredSize() {
  158.     return isize;
  159.     }
  160.     public Dimension getMinimumSize() {
  161.     return getPreferredSize();
  162.     }
  163.     public void paint(Graphics g) {
  164.     if (!blank) {
  165.         FontMetrics fm = g.getFontMetrics();
  166.         int y0 = fm.getAscent();
  167.         int dy = fm.getHeight();
  168.         for (int i = 0; i < txt.length; i++)
  169.         if (txt[i] != null)
  170.             g.drawString(txt[i], 0, i * dy + y0);
  171.     }
  172.     }
  173. }
  174.