Last active
March 10, 2019 12:20
-
-
Save ezhov-da/a3cd18b7fd56f76eb81f2620d1374020 to your computer and use it in GitHub Desktop.
java руссификация выбора файлов и добавление пункта в контекстное меню
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[code:]java[:code] | |
import java.awt.Component; | |
import java.io.File; | |
import javax.swing.JFileChooser; | |
import javax.swing.JMenuItem; | |
import javax.swing.JPopupMenu; | |
import javax.swing.filechooser.FileSystemView; | |
import sun.swing.FilePane; | |
/** | |
* Это класс, который расширет стандартный выбор файлов и | |
* имеет возможность работать с контекстным меню выбора файлов | |
* <p> | |
* @author ezhov_da | |
*/ | |
public class JFileChooserExtend extends JFileChooser | |
{ | |
protected JPopupMenu popupMenuContextForFileChooser; | |
public JFileChooserExtend() | |
{ | |
initFilePane(); | |
} | |
public JFileChooserExtend(String currentDirectoryPath) | |
{ | |
super(currentDirectoryPath); | |
initFilePane(); | |
} | |
public JFileChooserExtend(File currentDirectory) | |
{ | |
super(currentDirectory); | |
initFilePane(); | |
} | |
public JFileChooserExtend(FileSystemView fsv) | |
{ | |
super(fsv); | |
initFilePane(); | |
} | |
public JFileChooserExtend(File currentDirectory, FileSystemView fsv) | |
{ | |
super(currentDirectory, fsv); | |
initFilePane(); | |
} | |
public JFileChooserExtend(String currentDirectoryPath, FileSystemView fsv) | |
{ | |
super(currentDirectoryPath, fsv); | |
initFilePane(); | |
} | |
protected void initFilePane() | |
{ | |
Component[] components = getComponents(); | |
for (Component component : components) | |
{ | |
if (component instanceof FilePane) | |
{ | |
FilePane filePane = (FilePane) component; | |
popupMenuContextForFileChooser = filePane.getComponentPopupMenu(); | |
break; | |
} | |
} | |
} | |
//удалось ли инициализировать контекстное меню | |
public boolean isInitPopupMenu() | |
{ | |
return popupMenuContextForFileChooser != null; | |
} | |
/** | |
* Устанавливает новое имя для перехода на уровень вверх | |
* <p> | |
* @param newName | |
*/ | |
public void changeGoUp(String newName) | |
{ | |
JMenuItem menuItem = (JMenuItem) popupMenuContextForFileChooser.getComponent(0); | |
if ("Go Up".equals(menuItem.getText())) | |
{ | |
if (newName != null) | |
{ | |
menuItem.setText(newName); | |
} | |
} | |
} | |
public JPopupMenu getPopupMenuContextForFileChooser() | |
{ | |
return popupMenuContextForFileChooser; | |
} | |
} | |
import java.awt.Desktop; | |
import java.awt.event.ActionEvent; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.swing.AbstractAction; | |
import static javax.swing.Action.NAME; | |
/** | |
* Класс, который расширяет меню для выбора файлов | |
* и добавляет возможность открывать выбранные файлы | |
* <p> | |
* @author ezhov_da | |
*/ | |
public class JFileChooserOpenFiles extends JFileChooserExtend | |
{ | |
private static final Logger LOG = Logger.getLogger(JFileChooserOpenFiles.class.getName()); | |
public JFileChooserOpenFiles() | |
{ | |
super(); | |
} | |
public void addOpenFiles() | |
{ | |
super.popupMenuContextForFileChooser.insert(new AbstractAction() | |
{ | |
{ | |
putValue(NAME, "Открыть выбранный файл(ы)"); | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) | |
{ | |
File[] files = getSelectedFiles(); | |
for (File file : files) | |
{ | |
if (file.exists()) | |
{ | |
try | |
{ | |
Desktop.getDesktop().open(file); | |
} catch (IOException ex) | |
{ | |
LOG.log(Level.SEVERE, "Не удалось открыть файл:" + file.getAbsolutePath() + "]", ex); | |
} | |
} | |
} | |
} | |
}, 0); | |
} | |
} | |
[/code] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment