oyoahafilechooserui.java
来自「java 开源,Swing外观:JGoodies look & feel. ..」· Java 代码 · 共 2,128 行 · 第 1/4 页
JAVA
2,128 行
} else if(name.equals("by_date")) { sortedMode = OyoahaDirectoryModel.BY_DATE; } else if(name.equals("by_size")) { sortedMode = OyoahaDirectoryModel.BY_SIZE; } OyoahaDirectoryModel m = getOyoahaModel(); if(m!=null) { list.clearSelection(); m.setMode(sortedMode); } } else { if(name.equals("inverse")) { OyoahaDirectoryModel m = getOyoahaModel(); if(m!=null) { list.clearSelection(); m.inverse(); } } } } protected JPopupMenu getSortPopupMenu() { if(popup==null) { popup = new JPopupMenu(); JMenu menu1 = new JMenu("organize by..."); JMenuItem extention = new JRadioButtonMenuItem("Extention"); JMenuItem csextention = new JRadioButtonMenuItem("Extention (Case Sensitive)"); JMenuItem type = new JRadioButtonMenuItem("Type"); JMenuItem none = new JRadioButtonMenuItem("None"); extention.setName("use_extention"); csextention.setName("use_csextention"); type.setName("use_type"); none.setName("use_none"); ButtonGroup group = new ButtonGroup(); group.add(extention); group.add(csextention); group.add(type); group.add(none); switch(organizedMode) { case OyoahaDirectoryModel.USE_CASESENSITIVE_EXTENTION: csextention.setSelected(true); break; case OyoahaDirectoryModel.USE_TYPE: type.setSelected(true); break; case OyoahaDirectoryModel.USE_NONE: none.setSelected(true); break; default: extention.setSelected(true); break; } extention.addActionListener(this); csextention.addActionListener(this); type.addActionListener(this); none.addActionListener(this); menu1.add(extention); menu1.add(csextention); menu1.add(type); menu1.add(none); popup.add(menu1); JMenu menu2 = new JMenu("sort by..."); JMenuItem name = new JRadioButtonMenuItem("Name"); JMenuItem csname = new JRadioButtonMenuItem("Name (Case Sensitive)"); JMenuItem size = new JRadioButtonMenuItem("Size"); JMenuItem date = new JRadioButtonMenuItem("Date"); name.setName("by_name"); csname.setName("by_csname"); size.setName("by_size"); date.setName("by_date"); group = new ButtonGroup(); group.add(name); group.add(csname); group.add(size); group.add(date); switch(sortedMode) { case OyoahaDirectoryModel.BY_CASESENSITIVE_NAME: csname.setSelected(true); break; case OyoahaDirectoryModel.BY_DATE: date.setSelected(true); break; case OyoahaDirectoryModel.BY_SIZE: size.setSelected(true); break; default: name.setSelected(true); break; } name.addActionListener(this); csname.addActionListener(this); size.addActionListener(this); date.addActionListener(this); menu2.add(name); menu2.add(csname); menu2.add(size); menu2.add(date); JMenuItem inverse = new JCheckBoxMenuItem("Inverse sort order"); inverse.setName("inverse"); inverse.addActionListener(this); menu2.add(inverse); popup.add(menu2); } return popup; } protected JPanel getBottomPanel() { if(bottomPanel == null) { bottomPanel = new JPanel(); } return bottomPanel; } protected void createModel() { model = new OyoahaDirectoryModel(getFileChooser(), getOyoahaFileView(getFileChooser())); } public OyoahaDirectoryModel getOyoahaModel() { return model; } protected void installListeners(JFileChooser fc) { propertyChangeListener = createPropertyChangeListener(fc); if(propertyChangeListener != null) { fc.addPropertyChangeListener(propertyChangeListener); } fc.addPropertyChangeListener(model); ancestorListener = new AncestorListener() { public void ancestorAdded(AncestorEvent e) { JButton approveButton = getApproveButton(getFileChooser()); if(approveButton != null) { approveButton.requestFocus(); } } public void ancestorRemoved(AncestorEvent e) { } public void ancestorMoved(AncestorEvent e) { } }; fc.addAncestorListener(ancestorListener);//InputMap inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);//SwingUtilities.replaceUIInputMap(fc, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);//ActionMap actionMap = getActionMap();//SwingUtilities.replaceUIActionMap(fc, actionMap); } /*protected InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { return (InputMap)UIManager.get("FileChooser.ancestorInputMap"); } return null; } protected ActionMap getActionMap() { return createActionMap(); } protected ActionMap createActionMap() { AbstractAction escAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { if(editing) { cancelEdit(); list.repaint(); } else { getFileChooser().cancelSelection(); } } public boolean isEnabled() { return getFileChooser().isEnabled(); } }; ActionMap map = new ActionMapUIResource(); map.put("cancelSelection", escAction); return map; }*/ protected JComponent createList(JFileChooser fc) { list = new JList(); list.setCellRenderer(new FileRenderer()); if(fc.isMultiSelectionEnabled()) { list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); } else { list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } list.setModel(getOyoahaModel()); list.addListSelectionListener(createListSelectionListener(fc)); list.addMouseListener(createDoubleClickListener(fc, list)); JScrollPane scrollpane = new JScrollPane(list); scrollpane.setPreferredSize(new Dimension(400,200)); return scrollpane; }//------------------------------------------------------------------------------ protected JTextField editCell = null; protected int lastIndex = -1; protected int currentEditingIndex = -1; protected boolean editing = false; protected int editX = 20; protected void setEditIndex(int i) { lastIndex = i; } protected void resetEditIndex() { lastIndex = -1; } protected void cancelEdit() { editing = false; if(editCell!=null && editCell.isVisible()) { //if value has changed try to rename the item if(currentEditingIndex>=0) { File f = (File)list.getModel().getElementAt(currentEditingIndex); String newFileName = editCell.getText(); newFileName = newFileName.trim(); if(!newFileName.equals(getFileChooser().getName(f))) { File f2 = getFileChooser().getFileSystemView().createFileObject(getFileChooser().getCurrentDirectory(), newFileName); if(f.renameTo(f2)) { getOyoahaModel().replace(f, f2); } else { //TODO - show a dialog indicating failure } } } currentEditingIndex = -1; editCell.setVisible(false); editCell.setText(""); editCell.setBounds(0,0,1,1); list.remove(editCell); } } protected JTextField getEditCell() { if(editCell==null) { editCell = new JTextField(); editCell.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(OyoahaUtilities.getColorFromScheme(OyoahaUtilities.BLACK), 1), BorderFactory.createEmptyBorder(0,2,0,2))); editCell.addActionListener(new EditActionListener()); } return editCell; } protected class EditActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { cancelEdit(); list.repaint(); } }//------------------------------------------------------------------------------ public void clearIconCache() { getOyoahaFileView(getFileChooser()).clearIconCache(); } public OyoahaFileView getOyoahaFileView(JFileChooser fc) { FileView tmp = getFileView(fc); if(!(tmp instanceof OyoahaFileView)) return new OyoahaFileView(fc); return (OyoahaFileView)tmp; } public FileView getFileView(JFileChooser fc) { if(fileView==null) { fileView = new OyoahaFileView(fc); } return fileView; } protected class FileRenderer extends OyoahaListCellRenderer { protected FileRendererIcon fileRendererIcon; public FileRenderer() { this.drawsFocusBorderAroundIcon = false; fileRendererIcon = new FileRendererIcon(); setIcon(fileRendererIcon); } public Component getListCellRendererComponent(JList list, Object value, int index, boolean selected, boolean hasFocus) { this.list = list; this.selected = selected; this.hasFocus = selected; this.isLeftToRight = OyoahaUtilities.isLeftToRight(list); File file = (File)value; String fileName = getFileChooser().getName(file); if(sortedMode==OyoahaDirectoryModel.BY_DATE) { DateFormat df = DateFormat.getDateInstance(); setText(df.format(new Date(file.lastModified())) + " - " + fileName.toLowerCase()); } else if(sortedMode==OyoahaDirectoryModel.BY_SIZE) { if(file.isDirectory()) { setText(fileName.toLowerCase()); } else { long l = file.length(); NumberFormat nf = NumberFormat.getNumberInstance(); setText(nf.format(l) + " - " + fileName.toLowerCase()); } } else if(sortedMode==OyoahaDirectoryModel.BY_CASESENSITIVE_NAME) { setText(fileName); } else { setText(fileName.toLowerCase()); } Icon icon = getOyoahaFileView(getFileChooser()).getIcon(file); if(icon==null) { icon = getFileChooser().getIcon(file); } fileRendererIcon.icon = icon; //- - - - - - - - - - - - - if(selected || hasFocus) { editX = this.getBorderStart(); setForeground(list.getSelectionForeground()); fileRendererIcon.color = null; } Color c = getOyoahaFileView(getFileChooser()).getColor(file); if(c!=null) { fileRendererIcon.color = c; } else { fileRendererIcon.color = null; } setForeground(list.getForeground()); setOpaque(false); setEnabled(list.isEnabled()); setFont(list.getFont()); setBorder(UIManager.getBorder("List.selectionBorder")); return this; } public void paint(Graphics g) { int imageOffset; if(drawsFocusBorderAroundIcon) { imageOffset = 0; } else { imageOffset = getLabelStart()-1; if(imageOffset<0) imageOffset = 0; } Color bColor; if(selected) { bColor = this.list.getSelectionBackground(); } else { bColor = this.list.getBackground(); } if(bColor==null) { bColor = getBackground(); } g.setColor(bColor); if(selected) { OyoahaBackgroundObject o = OyoahaUtilities.getBackgroundObject("Tree.Renderer"); if(isLeftToRight) { Shape s = OyoahaUtilities.normalizeClip(g, imageOffset, 0, getWidth() - 1 - imageOffset, getHeight()); if(o!=null && bColor instanceof UIResource) { o.paintBackground(g, this, imageOffset, 0, getWidth() - 1 - imageOffset, getHeight(), OyoahaUtilities.SELECTED_ENABLED); } else { OyoahaUtilities.paintColorBackground(g, this, imageOffset, 0, getWidth() - 1 - imageOffset, getHeight(), bColor, OyoahaUtilities.SELECTED_ENABLED); } g.setClip(s); } else { Shape s = OyoahaUtilities.normalizeClip(g, 0, 0, getWidth() - 1 - imageOffset, getHeight()); if(o!=null && bColor instanceof UIResource) { o.paintBackground(g, this, 0, 0, getWidth() - 1 - imageOffset, getHeight(), OyoahaUtilities.SELECTED_ENABLED); } else { OyoahaUtilities.paintColorBackground(g, this, 0, 0, getWidth() - 1 - imageOffset, getHeight(), bColor, OyoahaUtilities.SELECTED_ENABLED); } g.setClip(s); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?