JSD-8582 预设浏览器支持linux
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

388 lines
15 KiB

package com.fr.plugin.customexplorer.ui;
import com.fr.base.BaseUtils;
import com.fr.design.dialog.BasicPane;
import com.fr.design.gui.ibutton.UIButton;
import com.fr.design.gui.icontainer.UIScrollPane;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.gui.itextfield.UITextField;
import com.fr.design.i18n.Toolkit;
import com.fr.design.layout.FRGUIPaneFactory;
import com.fr.design.style.background.image.ExpandFileChooser;
import com.fr.general.NameObject;
import com.fr.log.FineLoggerFactory;
import com.fr.plugin.customexplorer.preview.ExplorerInfo;
import com.fr.plugin.customexplorer.utils.ExplorerConfigUtils;
import com.fr.stable.StringUtils;
import com.fr.stable.os.OperatingSystem;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.ColorUIResource;
import javax.swing.text.Document;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
/**
* @author fr.open
* @create 2019/9/29
* 浏览器设置界面
* 自动搜索会自动搜索已安装的浏览器为了性能仅能搜索常见程序安装目录
* 添加对应搜索不到的可以自己手工添加浏览器
* 删除可删除已添加的浏览器
*/
public class ExplorerSettingPane extends BasicPane {
private static final ColorUIResource BORDER_COLOR = new ColorUIResource(168, 172, 180);
private static final Border TIP_BORDER = BorderFactory.createEmptyBorder(10, 4, 4, 4);
private static final Border DIALOG_BORDER = BorderFactory.createEmptyBorder(0, 6, 4, 6);
private ExplorerSettingListPane leftBottomPane = null;
private ExpandFileChooser fileChooser = null;
private UIButton autoSearchBtn = new UIButton(Toolkit.i18nText("Explorer_Setting_AutoSearchExplorerBtn"));
private UIButton manualAddBtn = new UIButton(BaseUtils.readIcon("com/fr/design/images/buttonicon/add.png"));
private UIButton manualDelBtn = new UIButton(BaseUtils.readIcon("com/fr/design/images/data/source/delete.png"));
private UILabel progressAddrLabel = new UILabel(Toolkit.i18nText("Explorer_Setting_ExplorerProgramAddrLabel"));
private UITextField progressAddrText = new UITextField();
private UIButton progressAddrSelBtn = new UIButton("...");
private UILabel progressStartParamLabel = new UILabel(Toolkit.i18nText("Explorer_Setting_ExplorerProgramParamLabel"));
private UITextField progressStartParamText = new UITextField();
public ExplorerSettingPane(){
setLayout(FRGUIPaneFactory.createBorderLayout());
UILabel titleLabel = new UILabel(Toolkit.i18nText("Explorer_Setting_ExplorerTitleLabel"));
titleLabel.setBorder(TIP_BORDER);
add(titleLabel, BorderLayout.NORTH);
UIScrollPane detailScrollPane = new UIScrollPane(initDetailPane());
detailScrollPane.setBorder(new MatteBorder(1, 1, 1, 1, BORDER_COLOR));
add(detailScrollPane, "Center");
setBorder(DIALOG_BORDER);
this.fileChooser = new ExpandFileChooser("", Toolkit.i18nText("Explorer_Setting_FileSelectBtnLabel"));
this.fileChooser.setMultiSelectionEnabled(false);
initListener();
initConfigExplorer();
}
@Override
protected String title4PopupWindow() {
return Toolkit.i18nText("Explorer_Setting_Name");
}
private JPanel initDetailPane() {
JPanel mainPane = FRGUIPaneFactory.createBorderLayout_S_Pane();
JPanel leftPane = FRGUIPaneFactory.createBorderLayout_M_Pane();
JPanel rightPane = FRGUIPaneFactory.createNormalFlowInnerContainer_M_Pane();
//JPanel leftTopPane = FRGUIPaneFactory.createBorderLayout_S_Pane();
JPanel leftTopPane = FRGUIPaneFactory.createNColumnGridInnerContainer_S_Pane(3);
leftTopPane.add(autoSearchBtn);
leftTopPane.add(manualAddBtn);
leftTopPane.add(manualDelBtn);
leftBottomPane = new ExplorerSettingListPane(this);
leftPane.add(leftTopPane, BorderLayout.NORTH);
leftPane.add(leftBottomPane, BorderLayout.CENTER);
JPanel progressAddrPane = FRGUIPaneFactory.createBorderLayout_S_Pane();
progressAddrText.setPreferredSize(new Dimension(300, 20));
progressAddrPane.add(progressAddrText, BorderLayout.CENTER);
progressAddrPane.add(progressAddrSelBtn, BorderLayout.EAST);
JPanel rightCompGridPane = new JPanel(new GridLayout(4, 1, 5, 15));
rightCompGridPane.add(progressAddrLabel);
rightCompGridPane.add(progressAddrPane);
rightCompGridPane.add(progressStartParamLabel);
progressStartParamText.setPreferredSize(new Dimension(300, 20));
rightCompGridPane.add(progressStartParamText);
rightPane.setBorder(BorderFactory.createEmptyBorder(40, 5, 0, 0));
rightPane.add(rightCompGridPane);
mainPane.add(leftPane, BorderLayout.WEST);
mainPane.add(rightPane, BorderLayout.CENTER);
return mainPane;
}
/**
* 确定事件
* */
public void doWithProcess() {
ExplorerConfigUtils.clearExplorerInfo();
ExplorerInfo[] expList = leftBottomPane.getAllExplorers();
for (int i = 0; i < expList.length; i++) {
ExplorerConfigUtils.addExplorerInfo(expList[i]);
}
ExplorerConfigUtils.saveConfig();
}
public void refreshExplorerInfo(ExplorerInfo exp) {
if (null != exp) {
this.progressAddrText.setText(exp.getPath());
this.progressStartParamText.setText(exp.getPara());
}
}
public void checkExplorerInfoTextUsed(boolean used) {
this.progressAddrSelBtn.setEnabled(used);
this.progressAddrText.setEnabled(used);
this.progressStartParamText.setEnabled(used);
}
private void initConfigExplorer() {
ExplorerInfo[] expList = ExplorerConfigUtils.getAllExplorerInfo();
for (int i = 0; i < expList.length; i++) {
addListItem(expList[i]);
}
}
private void initListener(){
autoSearchBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ExplorerSettingPane.this.checkButtonUsed(false);
ExplorerSettingPane.this.searchExplorerProgress();
ExplorerSettingPane.this.checkButtonUsed(true);
}
});
manualAddBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ExplorerSettingPane.this.checkButtonUsed(false);
ExplorerSettingPane.this.manulAddExplorerProgress();
ExplorerSettingPane.this.checkButtonUsed(true);
}
});
manualDelBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ExplorerSettingPane.this.manulDelExplorerProgress();
}
});
progressAddrSelBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int ret = ExplorerSettingPane.this.fileChooser.showOpenDialog(ExplorerSettingPane.this);
if (ret != 1) {
File programFile = ExplorerSettingPane.this.fileChooser.getSelectedFile();
ExplorerSettingPane.this.updateExplorerPath(programFile.getAbsolutePath());
progressAddrText.setText(programFile.getAbsolutePath());
}
}
});
progressStartParamText.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
try {
Document doc = e.getDocument();
String para = doc.getText(0, doc.getLength());
ExplorerSettingPane.this.updateExplorerPara(para);
} catch (Exception ignore) {}
}
@Override
public void removeUpdate(DocumentEvent e) {
try {
Document doc = e.getDocument();
String para = doc.getText(0, doc.getLength());
ExplorerSettingPane.this.updateExplorerPara(para);
} catch (Exception ignore) {}
}
@Override
public void changedUpdate(DocumentEvent e) {
try {
Document doc = e.getDocument();
String para = doc.getText(0, doc.getLength());
ExplorerSettingPane.this.updateExplorerPara(para);
} catch (Exception ignore) {}
}
});
progressAddrText.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
try {
Document doc = e.getDocument();
String para = doc.getText(0, doc.getLength());
ExplorerSettingPane.this.updateExplorerPath(para);
} catch (Exception ignore) {}
}
@Override
public void removeUpdate(DocumentEvent e) {
try {
Document doc = e.getDocument();
String para = doc.getText(0, doc.getLength());
ExplorerSettingPane.this.updateExplorerPath(para);
} catch (Exception ignore) {}
}
@Override
public void changedUpdate(DocumentEvent e) {
try {
Document doc = e.getDocument();
String para = doc.getText(0, doc.getLength());
ExplorerSettingPane.this.updateExplorerPath(para);
} catch (Exception ignore) {}
}
});
}
public void updateExplorerName(String name) {
if (StringUtils.isNotEmpty(name)) {
ExplorerInfo exp = leftBottomPane.getSelectedExplorer();
if (null != exp) {
exp.setName(name);
String icon = ExplorerConfigUtils.getExplorerIcon(name);
exp.setIcon(icon);
}
}
}
private void updateExplorerPath(String path) {
if (StringUtils.isNotEmpty(path)) {
ExplorerInfo exp = leftBottomPane.getSelectedExplorer();
if (null != exp) {
path = path.replaceAll("\\\\", "/");
exp.setPath(path);
}
}
}
private void updateExplorerPara(String para) {
ExplorerInfo exp = leftBottomPane.getSelectedExplorer();
if (null != exp) {
exp.setPara(para);
}
}
private void manulAddExplorerProgress() {
ExplorerInfo exp = new ExplorerInfo();
exp.setName(Toolkit.i18nText("Explorer_Setting_ManualAddExplorerDefaultName"));
addListItem(exp);
}
private void manulDelExplorerProgress() {
ExplorerConfigUtils.removeExplorerInfo(leftBottomPane.getSelectedName());
leftBottomPane.delSelectedExplorer();
}
private void searchExplorerProgress() {
if (OperatingSystem.isMacos()) {
ExplorerInfo[] expList = ExplorerConfigUtils.MAC_EXPLORER_STATIC;
for (int i = 0; i < expList.length; i++) {
String progressPath = expList[i].getPath();
if ((new File(progressPath)).exists()) {
ExplorerInfo exp = expList[i].clone();
exp.setPath(progressPath);
if (ExplorerConfigUtils.addExplorerInfo(exp)) {
addListItem(exp);
}
}
}
} else if (OperatingSystem.isLinux() || OperatingSystem.isUnix()) {
ExplorerInfo[] expList = ExplorerConfigUtils.LINUX_EXPLORER_STATIC;
String[] dirList = getSearchLinuxDirList();
for (int i = 0; i < expList.length; i++) {
for (int j = 0; j < dirList.length; j++) {
String progressPath = dirList[j] + expList[i].getPath();
if ((new File(progressPath)).exists()) {
ExplorerInfo exp = expList[i].clone();
exp.setPath(progressPath);
if (ExplorerConfigUtils.addExplorerInfo(exp)) {
addListItem(exp);
}
}
}
}
} else if (OperatingSystem.isWindows()) {
ExplorerInfo[] expList = ExplorerConfigUtils.WIN_EXPLORER_STATIC;
String[] dirList = getSearchDirList();
for (int i = 0; i < expList.length; i++) {
for (int j = 0; j < dirList.length; j++) {
String progressPath = dirList[j] + expList[i].getPath();
if ((new File(progressPath)).exists()) {
ExplorerInfo exp = expList[i].clone();
exp.setPath(progressPath);
if (ExplorerConfigUtils.addExplorerInfo(exp)) {
addListItem(exp);
}
}
}
}
} else {
FineLoggerFactory.getLogger().error(Toolkit.i18nText("Explorer_Setting_LogAutoSearchNotSupported"));
}
this.updateUI();
}
private void addListItem(ExplorerInfo item) {
NameObject itemObject = new NameObject();
itemObject.setName(item.getName());
itemObject.setObject(item);
leftBottomPane.addNameable(itemObject, leftBottomPane.getNameListSize());
}
private void checkButtonUsed(boolean isUsed) {
autoSearchBtn.setEnabled(isUsed);
manualAddBtn.setEnabled(isUsed);
}
private String[] getSearchDirList() {
String d1 = System.getenv("ProgramFiles");
String d2 = System.getenv("ProgramFiles(x86)");
String d3 = System.getenv("APPDATA");
String d4 = System.getenv("LOCALAPPDATA");
d1 = d1.replaceAll("\\\\", "/");
d2 = d2.replaceAll("\\\\", "/");
d3 = d3.replaceAll("\\\\", "/");
d4 = d4.replaceAll("\\\\", "/");
String[] autoSearchDirList = new String[]{
"C:/Program Files",
"C:/Program Files (x86)",
"D:/Program Files",
"D:/Program Files (x86)",
d1, d2, d3, d4
};
return autoSearchDirList;
}
private String[] getSearchLinuxDirList() {
String[] autoSearchDirList = new String[]{
"/usr/bin",
"/usr/sbin",
"/usr/local/bin",
"/usr/local/sbin"
};
return autoSearchDirList;
}
public static void main(String[] args) {
/*DesignerFrame localDesignerFrame = DesignerContext.getDesignerFrame();
final ExplorerSettingPane settingPane = new ExplorerSettingPane();
BasicDialog basicDialog = settingPane.showWindow(localDesignerFrame);
basicDialog.setTitle(Toolkit.i18nText("Explorer_Setting_Name"));
basicDialog.addDialogActionListener(new DialogActionAdapter() {
@Override
public void doOk() {
settingPane.doWithProcess();
}
});
basicDialog.setVisible(true);*/
}
}