home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 February / PCpro_2005_02.ISO / files / opensource / Opcion_1.1.1 / Opcion_v1.1.1.exe / FontViewer / components / OtherFontsPanel.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-04-25  |  9.2 KB  |  283 lines

  1. package FontViewer.components;
  2.  
  3. import FontViewer.filters.FontFileFilter;
  4. import FontViewer.util.QuickSort;
  5. import FontViewer.windows.MainWindow;
  6. import java.awt.BorderLayout;
  7. import java.awt.Dimension;
  8. import java.awt.Font;
  9. import java.awt.FontFormatException;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.FocusEvent;
  12. import java.awt.event.KeyEvent;
  13. import java.awt.event.MouseEvent;
  14. import java.awt.event.WindowEvent;
  15. import java.io.File;
  16. import java.io.FileInputStream;
  17. import java.io.IOException;
  18. import java.lang.ref.WeakReference;
  19. import javax.swing.JButton;
  20. import javax.swing.JFileChooser;
  21. import javax.swing.JLabel;
  22. import javax.swing.JList;
  23. import javax.swing.JOptionPane;
  24. import javax.swing.JPanel;
  25. import javax.swing.JScrollPane;
  26. import javax.swing.JTextField;
  27. import javax.swing.border.Border;
  28.  
  29. public class OtherFontsPanel extends JPanel implements ListPanel {
  30.    private File currentDirectory;
  31.    private String[] fnames;
  32.    private String[] fontnames;
  33.    // $FF: renamed from: mw FontViewer.windows.MainWindow
  34.    private MainWindow field_0;
  35.    private JButton browseButton;
  36.    private JLabel locationLabel;
  37.    private JPanel locationPanel;
  38.    private JTextField locationTextField;
  39.    private JList otherFontsList;
  40.    private JScrollPane otherFontsScrollPane;
  41.  
  42.    public OtherFontsPanel(MainWindow mw) {
  43.       this.field_0 = mw;
  44.       this.initComponents();
  45.    }
  46.  
  47.    public String[] getItem(int itemNumber) {
  48.       String[] s = new String[3];
  49.       if (itemNumber >= 0 && itemNumber < this.fnames.length) {
  50.          s[0] = this.fnames[itemNumber];
  51.          s[1] = this.currentDirectory.toString();
  52.          s[2] = itemNumber + "";
  53.       }
  54.  
  55.       return s;
  56.    }
  57.  
  58.    public int getNumItems() {
  59.       int items = 0;
  60.       if (this.fnames != null) {
  61.          items = this.fnames.length;
  62.       }
  63.  
  64.       return items;
  65.    }
  66.  
  67.    public String[] getCurrentItem() {
  68.       String[] s = new String[3];
  69.       int p = this.otherFontsList.getSelectedIndex();
  70.       if (p >= 0) {
  71.          s[0] = this.fnames[p];
  72.          s[1] = this.currentDirectory.toString();
  73.          s[2] = p + "";
  74.       }
  75.  
  76.       return s;
  77.    }
  78.  
  79.    public int getCurrentItemNum() {
  80.       return this.otherFontsList.getSelectedIndex();
  81.    }
  82.  
  83.    private void updateFontNames() {
  84.       Font f = null;
  85.       WeakReference wrf = null;
  86.       String fontfile = "";
  87.       this.fontnames = new String[this.fnames.length];
  88.  
  89.       for(int i = 0; i < this.fnames.length; ++i) {
  90.          try {
  91.             fontfile = this.currentDirectory.toString() + File.separator + this.fnames[i];
  92.             System.out.println("FF: " + fontfile);
  93.             f = Font.createFont(0, new FileInputStream(fontfile));
  94.             wrf = new WeakReference(f);
  95.             this.fontnames[i] = ((Font)wrf.get()).getName();
  96.          } catch (IOException var6) {
  97.             this.fontnames[i] = "Cannot load " + this.fnames[i] + " [IOException]";
  98.          } catch (FontFormatException var7) {
  99.             this.fontnames[i] = "Cannot load " + this.fnames[i] + " [FontFormatException]";
  100.          }
  101.       }
  102.  
  103.    }
  104.  
  105.    private void updateDisplay() {
  106.       this.fnames = this.currentDirectory.list(new FontFileFilter());
  107.       QuickSort.sort(this.fnames, true);
  108.       if (this.fnames.length == 0) {
  109.          String[] message = new String[]{"This folder does not contain any fonts."};
  110.          this.otherFontsList.setListData(message);
  111.          this.otherFontsList.setEnabled(false);
  112.       } else {
  113.          this.otherFontsList.setListData(this.fnames);
  114.          this.otherFontsList.setEnabled(true);
  115.       }
  116.  
  117.       this.field_0.updateDisplay();
  118.    }
  119.  
  120.    public void selectItem(String name, String loc) {
  121.       this.otherFontsList.setSelectedValue(name, true);
  122.       int p = this.otherFontsList.getSelectedIndex();
  123.       if (p >= 0) {
  124.          this.field_0.setCurrentFont(this.fnames[p].toString(), this.currentDirectory.toString(), p);
  125.       }
  126.  
  127.    }
  128.  
  129.    public void selectNext() {
  130.       int i = this.otherFontsList.getSelectedIndex();
  131.       if (i >= 0) {
  132.          if (i + 1 < this.fnames.length) {
  133.             ++i;
  134.          }
  135.  
  136.          this.setCurrentItem(i, true);
  137.       } else {
  138.          this.setCurrentItem(0, true);
  139.       }
  140.  
  141.    }
  142.  
  143.    public void selectPrev() {
  144.       int i = this.otherFontsList.getSelectedIndex();
  145.       if (i >= 0) {
  146.          if (i - 1 >= 0) {
  147.             --i;
  148.          }
  149.  
  150.          this.setCurrentItem(i, true);
  151.       } else {
  152.          this.setCurrentItem(0, true);
  153.       }
  154.  
  155.    }
  156.  
  157.    private void setCurrentItem(int p, boolean internal) {
  158.       if (this.fnames != null) {
  159.          if (internal) {
  160.             this.otherFontsList.setSelectedIndex(p);
  161.             int spos = p * (this.otherFontsScrollPane.getVerticalScrollBar().getMaximum() / this.fnames.length);
  162.             spos -= this.otherFontsScrollPane.getSize().height / 2;
  163.             this.otherFontsScrollPane.getVerticalScrollBar().setValue(spos);
  164.          }
  165.  
  166.          if (p >= 0) {
  167.             this.field_0.setCurrentFont(this.fnames[p].toString(), this.currentDirectory.toString(), p);
  168.          }
  169.       }
  170.  
  171.    }
  172.  
  173.    private void initComponents() {
  174.       this.locationPanel = new JPanel();
  175.       this.locationLabel = new JLabel();
  176.       this.locationTextField = new JTextField();
  177.       this.browseButton = new JButton();
  178.       this.otherFontsScrollPane = new JScrollPane();
  179.       this.otherFontsList = new JList();
  180.       this.setLayout(new BorderLayout(2, 2));
  181.       this.locationPanel.setLayout(new BorderLayout(4, 0));
  182.       this.locationLabel.setText("Location:");
  183.       this.locationPanel.add(this.locationLabel, "West");
  184.       this.locationTextField.setToolTipText("Enter the location where fonts you wish to view are stored here");
  185.       this.locationTextField.setPreferredSize(new Dimension(100, 20));
  186.       this.locationTextField.addFocusListener(new 1(this));
  187.       this.locationTextField.addKeyListener(new 2(this));
  188.       this.locationPanel.add(this.locationTextField, "Center");
  189.       this.browseButton.setText("Browse");
  190.       this.browseButton.setToolTipText("Browse for font directory");
  191.       this.browseButton.setPreferredSize(new Dimension(81, 20));
  192.       this.browseButton.addActionListener(new 3(this));
  193.       this.locationPanel.add(this.browseButton, "East");
  194.       this.add(this.locationPanel, "North");
  195.       this.otherFontsScrollPane.setBorder((Border)null);
  196.       this.otherFontsList.addKeyListener(new 4(this));
  197.       this.otherFontsList.addMouseListener(new 5(this));
  198.       this.otherFontsScrollPane.setViewportView(this.otherFontsList);
  199.       this.add(this.otherFontsScrollPane, "Center");
  200.    }
  201.  
  202.    private void locationTextFieldFocusLost(FocusEvent evt) {
  203.       this.field_0.setTyping(false);
  204.    }
  205.  
  206.    private void locationTextFieldFocusGained(FocusEvent evt) {
  207.       this.field_0.setTyping(true);
  208.    }
  209.  
  210.    private void otherFontsListKeyReleased(KeyEvent evt) {
  211.       this.setCurrentItem(this.otherFontsList.getSelectedIndex(), false);
  212.    }
  213.  
  214.    private void browseButtonActionPerformed(ActionEvent evt) {
  215.       JFileChooser fc = new JFileChooser(new File(""));
  216.       fc.setFileSelectionMode(1);
  217.       fc.showOpenDialog(this);
  218.       if (fc.getSelectedFile() != null) {
  219.          this.currentDirectory = fc.getSelectedFile();
  220.          this.locationTextField.setText(this.currentDirectory.toString());
  221.          this.updateDisplay();
  222.       }
  223.  
  224.    }
  225.  
  226.    private void otherFontsListMouseClicked(MouseEvent evt) {
  227.       this.setCurrentItem(this.otherFontsList.getSelectedIndex(), false);
  228.    }
  229.  
  230.    private void locationTextFieldKeyReleased(KeyEvent evt) {
  231.       if (evt.getKeyCode() == 10) {
  232.          this.field_0.setTyping(false);
  233.          File f = new File(this.locationTextField.getText());
  234.          if (f.exists()) {
  235.             if (f.isDirectory()) {
  236.                this.currentDirectory = f;
  237.                this.updateDisplay();
  238.             } else {
  239.                new JOptionPane();
  240.                JOptionPane.showMessageDialog(this, "The location does not point to a directory.", "Error!", 0);
  241.             }
  242.          } else {
  243.             new JOptionPane();
  244.             JOptionPane.showMessageDialog(this, "Directory does not exist.", "Error!", 0);
  245.          }
  246.       }
  247.  
  248.    }
  249.  
  250.    private void exitForm(WindowEvent evt) {
  251.    }
  252.  
  253.    // $FF: synthetic method
  254.    static void access$000(OtherFontsPanel x0, FocusEvent x1) {
  255.       x0.locationTextFieldFocusGained(x1);
  256.    }
  257.  
  258.    // $FF: synthetic method
  259.    static void access$100(OtherFontsPanel x0, FocusEvent x1) {
  260.       x0.locationTextFieldFocusLost(x1);
  261.    }
  262.  
  263.    // $FF: synthetic method
  264.    static void access$200(OtherFontsPanel x0, KeyEvent x1) {
  265.       x0.locationTextFieldKeyReleased(x1);
  266.    }
  267.  
  268.    // $FF: synthetic method
  269.    static void access$300(OtherFontsPanel x0, ActionEvent x1) {
  270.       x0.browseButtonActionPerformed(x1);
  271.    }
  272.  
  273.    // $FF: synthetic method
  274.    static void access$400(OtherFontsPanel x0, KeyEvent x1) {
  275.       x0.otherFontsListKeyReleased(x1);
  276.    }
  277.  
  278.    // $FF: synthetic method
  279.    static void access$500(OtherFontsPanel x0, MouseEvent x1) {
  280.       x0.otherFontsListMouseClicked(x1);
  281.    }
  282. }
  283.