⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thumblabel.java

📁 java写的图片浏览器,类似acds
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.net9.oops.jsee;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
//import java.util.*;

public class ThumbLabel extends JScrollPane implements Runnable {
    // ThumbLabel and FileListView should inherite from a class that has the popup menus,
    // that would make the code look better.
    ImageIcon view_fullscreen = new ImageIcon(getClass().getResource("/org/net9/oops/jsee/image/view.gif"));
    ImageIcon delete_icon = new ImageIcon(getClass().getResource("/org/net9/oops/jsee/image/delete.gif"));
    ImageIcon rename_icon = new ImageIcon(getClass().getResource("/org/net9/oops/jsee/image/rename.gif"));
    ImageIcon play_icon = new ImageIcon(getClass().getResource("/org/net9/oops/jsee/image/play.gif"));

    JPopupMenu popupMenu = new JPopupMenu();
    JPopupMenu dirPopupMenu = new JPopupMenu();
    JPopupMenu linkPopupMenu = new JPopupMenu();

    JMenuItem rename_item = new JMenuItem("Rename", rename_icon);
    JMenuItem rename_dir_item = new JMenuItem("Rename", rename_icon);
    JMenuItem delete_item = new JMenuItem("Delete", delete_icon);
    JMenuItem delete_dir_item = new JMenuItem("Delete", delete_icon);

    JMenuItem view_item = new JMenuItem("View", view_fullscreen);
    JMenuItem convert_item = new JMenuItem("Convert ...", view_fullscreen);
    JMenuItem browse_item = new JMenuItem("Browse");
    JMenuItem slide_item = new JMenuItem("Slide show",  play_icon);
    JMenuItem browse_link_item = new JMenuItem("Browse");
    JMenuItem slide_link_item = new JMenuItem("Slide show",  play_icon);

public class JThumbButton extends JPanel {
    BorderLayout borderLayout1 = new BorderLayout();
    JButton thumbImage;
    JTextField fileNameField = new JTextField();

    private String  imageFullFileName,
                    thumbFileName,
                    oldThumbFileName;

    private int thumbID,
                listItemNumber;

    private boolean isDirectory,
                    isLinkToUpperDir;

    public JThumbButton(String text, Icon icon, String iFileName, final boolean isDirectory, int fileListIndex, boolean isLink) {
    // text: the text to be display on thumb label
    // isLink: if this is the link to the upper directory
        try {
          thumbImage  = new JButton(icon);
          thumbFileName = text;
          imageFullFileName = iFileName;
          this.isDirectory = isDirectory;
          listItemNumber = fileListIndex;
          isLinkToUpperDir = isLink;
          jbInit();
        } catch(Exception ex) {
          ex.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        thumbImage.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(FocusEvent e) {
              thumbImage_focusGained(e);
            }
            public void focusLost(FocusEvent e) {
              thumbImage_focusLost(e);
            }
        });

        thumbImage.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
              thumbImage_mouseClicked(e);
            }
            public void mousePressed(MouseEvent e) {
              thumbImage_popupTriggered(e);
            }
            public void mouseReleased(MouseEvent e) {
              thumbImage_popupTriggered(e);
            }
        });
        this.setLayout(borderLayout1);
        fileNameField.setPreferredSize(new Dimension(52, 17));
        fileNameField.setEditable(false);
        fileNameField.setText(thumbFileName);
        if (isDirectory) {
            fileNameField.setFont(new java.awt.Font("Dialog", 1, 12));
            thumbImage.setBorder(null);
        } else {
            String lowercaseThumbName = thumbFileName.toLowerCase();
            if (lowercaseThumbName.endsWith(".jpg"))
                fileNameField.setBackground(new Color(255, 215, 140)); // jpeg
            else if (lowercaseThumbName.endsWith(".bmp"))
                fileNameField.setBackground(new Color(204, 204, 182)); // bitmap
            else if (lowercaseThumbName.endsWith(".gif"))
                fileNameField.setBackground(new Color(190, 239, 206)); // gif
            //else if (lowercaseThumbName.endsWith(".png"))
            //    fileNameField.setBackground(new Color(190, 239, 182)); // png
            else if (lowercaseThumbName.endsWith(".pcx"))
                fileNameField.setBackground(new Color(190, 239, 112)); // pcx
            //else if (lowercaseThumbName.endsWith(".tif"))
            //    fileNameField.setBackground(new Color(190, 204, 112)); // tiff
            else if (lowercaseThumbName.endsWith(".xpm"))
                fileNameField.setBackground(new Color(255, 92, 177)); // xpm
            //else if (lowercaseThumbName.endsWith(".pnm"))
            //    fileNameField.setBackground(new Color(158, 144, 255)); // pnm
            
            // comment out unsupported formats
        }
        fileNameField.setHorizontalAlignment(SwingConstants.CENTER);
        fileNameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                fileNameField_keyReleased(e);
            }
        });

        fileNameField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(FocusEvent e) {
                fileNameField_focusLost(e);
            }
        });
        fileNameField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                fileNameField_mouseClicked(e);
            }
        });

        this.add(thumbImage, BorderLayout.CENTER);
        this.add(fileNameField, BorderLayout.SOUTH);
    } // end init

    void fileNameField_mouseClicked(MouseEvent e) {
      if (e.getClickCount()==2) {
        if (!isLinkToUpperDir) {
          fileNameField.setEditable(true);
          oldThumbFileName = thumbFileName;
        }
      }
      else {// click once
      // set select
        thumbImage.setBorder(BorderFactory.createLoweredBevelBorder());
        thumbImage.setBackground(new Color(0, 0, 149));
      //
        if (!isDirectory) {
          parentFrame.Display_Image(imageFullFileName);
        }
        CurrentSelectedIndex = thumbID;
        // this feature is disabled because now file list is sorted but thumb list is not
        //parentFrame.FileListScrollPane.fileListTable.setRowSelectionInterval(listItemNumber, listItemNumber);
      }
    }

    void fileNameField_focusLost(FocusEvent e) {
      if (fileNameField.isEditable())
        RenameThumbNail();
      thumbImage.setBorder(BorderFactory.createEtchedBorder());
      thumbImage.setBackground(new Color(204, 204, 204));
    }

    void RenameThumbNail() {
        File tempFile = new File(imageFullFileName);
        String imagePath = imageFullFileName.substring(0, imageFullFileName.lastIndexOf(System.getProperty("file.separator")));
        String newFullFileName  = imagePath + System.getProperty("file.separator") + fileNameField.getText();
        if (tempFile.renameTo(new File(newFullFileName)))
        {
            fileNameField.setEditable(false);
            parentFrame.FileRenamed = true;
            //if (!isDirectory)
            //    imageFullFileName = newFullFileName;
            //else
                imageFullFileName = newFullFileName;
        }
        else
            JOptionPane.showMessageDialog(null, "Cannot rename to " +
                                                fileNameField.getText() +
                                                ", try a different name");
    }

    void fileNameField_keyReleased(KeyEvent e) {
      if (e.getKeyCode()== KeyEvent.VK_ESCAPE) {
        fileNameField.setText(oldThumbFileName);
        fileNameField.setEditable(false);
      }
      if (e.getKeyCode()== KeyEvent.VK_ENTER) {
        RenameThumbNail();
        fileNameField.setEditable(false);
      }
    }

    void thumbImage_mouseClicked(MouseEvent e) {
      if (e.getClickCount()==2) {// double click
        if (!isDirectory) {
          parentFrame.Display_Image_FullScreen(imageFullFileName);
          CurrentSelectedIndex = thumbID;
          //parentFrame.FileListScrollPane.fileListTable.setRowSelectionInterval(listItemNumber, listItemNumber);
        } else {
          parentFrame.ChangeThumbNailDirectory(imageFullFileName);
        }
      }
      else {// one click - I guess ?!?!
      // do this instead of focus gain
        thumbImage.setBorder(BorderFactory.createLoweredBevelBorder());
        thumbImage.setBackground(new Color(0, 0, 149));
      //
        if (!isDirectory) {
          parentFrame.Display_Image(imageFullFileName);
        }
        CurrentSelectedIndex = thumbID;
        //parentFrame.FileListScrollPane.fileListTable.setRowSelectionInterval(listItemNumber, listItemNumber);
      }
    }

    void thumbImage_popupTriggered(MouseEvent e) {
      if (e.isPopupTrigger()) {
        if (isDirectory) {
          if (isLinkToUpperDir)
            linkPopupMenu.show(thumbImage, e.getX(), e.getY());
          else
            dirPopupMenu.show(thumbImage, e.getX(), e.getY());
        }
        else
          popupMenu.show(thumbImage, e.getX(), e.getY());
        CurrentSelectedIndex = thumbID;
      }
    }

    public void SetTextField(String filename) {
        fileNameField.setText(filename);
    }

    public void Rename_Thumb() {
        oldThumbFileName = thumbFileName;
        fileNameField.setEditable(true);
        fileNameField.requestFocus();
    }

    public void setThumbID(int id) {
        thumbID = id;
    }

    public int getThumbID() {
        return thumbID;
    }

    public void doClick() {
        if (!isDirectory)
            parentFrame.Display_Image(imageFullFileName);
        CurrentSelectedIndex = thumbID;
    }

    void thumbImage_focusGained(FocusEvent e) {
        thumbImage.setBorder(BorderFactory.createLoweredBevelBorder());
        thumbImage.setBackground(new Color(0, 0, 149));
    }

    void thumbImage_focusLost(FocusEvent e) {
        thumbImage.setBorder(BorderFactory.createEtchedBorder());
        thumbImage.setBackground(new Color(204, 204, 204));
    }
} // End JThumbButton

JThumbButton[] thumb_list;
JThumbButton test;
JLabel label;
File[]  filelist,
        full_file_list;
//Added by Neoya 2002-03-06
int full_file_list_length=0;

int Thumb_width,
    Thumb_height,
    Image_thumb_height,
    Distance;

private int thumbs_on_row;

int last_number_of_row = 0;

int view_width,
    view_height;

int thumb_count;

Jsee parentFrame;
int Quality;
int CurrentSelectedIndex = 0;
boolean firstTime = false;

private boolean hold_on = false; //this to suspend and resume thread

ImageIcon folderIcon,
          unknownFileIcon,
          diskIcon, 
          mediaIcon;
FlowLayout flowLayout;
//int thumbID;

public ThumbLabel(File[] full_file_list,
                    int thumb_width,
                    int thumb_height,
                    Jsee ParentFrame,
                    int quality) {
    Thumb_width = thumb_width;
    Thumb_height = thumb_height;

    // 17 is the height of filename text field
    Image_thumb_height = thumb_width - 17;

    parentFrame = ParentFrame;

//Popup menu for thumb label
    view_item.addActionListener(
        new ActionListener() {
            public void actionPerformed( ActionEvent event ) {
                parentFrame.Display_Image_FullScreen(thumb_list[CurrentSelectedIndex].imageFullFileName);
            }
        }
    );

    convert_item.addActionListener(
        new ActionListener() {
            public void actionPerformed( ActionEvent event ) {
                //ConversionDialog cd = new ConversionDialog(parentFrame, "Image Format Conversion", true);
                //cd.show();
                // add a thumb
                //JThumbButton newThumb = new JThumbButton();
            }
        }
    );

    popupMenu.add(view_item);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -