FolderCompareFrame with good layout
點選(此處)摺疊或開啟
-
import java.awt.BorderLayout;
-
import java.awt.Dimension;
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
import java.awt.event.MouseEvent;
-
import java.awt.event.MouseListener;
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.security.MessageDigest;
-
import java.security.NoSuchAlgorithmException;
-
import java.util.ArrayList;
-
import java.util.Arrays;
-
import java.util.Collections;
-
import java.util.LinkedHashSet;
-
import java.util.List;
-
import java.util.Properties;
-
import java.util.Set;
-
-
import javax.swing.DefaultListModel;
-
import javax.swing.JButton;
-
import javax.swing.JComponent;
-
import javax.swing.JFileChooser;
-
import javax.swing.JFrame;
-
import javax.swing.JLabel;
-
import javax.swing.JList;
-
import javax.swing.JPanel;
-
import javax.swing.JScrollPane;
-
import javax.swing.JTextField;
-
import javax.swing.plaf.basic.BasicBorders;
-
-
-
public class FolderCompareFrame2 extends JFrame implements ActionListener, MouseListener {
-
-
private static final String[] SUFFIX_FILTER = new String[] {".java", ".properties", ".xml", ".jsp", ".sql", ".jar", ".php" };
-
private static final String[] EXCLUDE_FILTER = new String[] {"/test/", "/integration/", "/target/"};
-
-
private static final long serialVersionUID = 3278358999317656288L;
-
private static final String notepadPlusCmd = "C:/Program Files (x86)/Notepad++/notepad++.exe";
-
-
private static final String CMD_SELECT_SOURCE = "source";
-
private static final String CMD_SELECT_TARGET = "target";
-
private static final String CMD_COMPARE = "compare";
-
-
private JTextField sourceInput;
-
private JTextField targetInput;
-
-
private JList<String> resultList;
-
private DefaultListModel<String> resultListModel = new DefaultListModel<String>();
-
-
private String sourceFolder;
-
private String targetFolder;
-
-
private JFileChooser fileChooser = new JFileChooser();
-
-
public FolderCompareFrame2() {
-
setSize(800, 600);
-
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
-
-
setDefaultCloseOperation(EXIT_ON_CLOSE);
-
setTitle("Folder Compare");
-
setLayout(new BorderLayout());
-
addTopPanel();
-
addResultList();
-
loadSettings();
-
setVisible(true);
-
-
}
-
-
private void addTopPanel() {
-
JPanel topPanel = new JPanel(new BorderLayout(10, 0));
-
-
JPanel inputPanel = new JPanel(new BorderLayout());
-
addSourceFolderInput(inputPanel);
-
addTargetFolderInput(inputPanel);
-
JButton compareButton = new JButton("Compare");
-
compareButton.setActionCommand(CMD_COMPARE);
-
compareButton.addActionListener(this);
-
-
topPanel.add(inputPanel, BorderLayout.CENTER);
-
topPanel.add(compareButton, BorderLayout.EAST);
-
-
add(topPanel, BorderLayout.NORTH);
-
-
}
-
-
private void loadSettings() {
-
Properties props = new Properties();
-
try {
-
props.load(new FileInputStream(this.getClass().getSimpleName() + ".config"));
-
sourceInput.setText(props.getProperty("folder1"));
-
targetInput.setText(props.getProperty("folder2"));
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
private void saveSettings() {
-
Properties props = new Properties();
-
try {
-
props.put("folder1", sourceInput.getText());
-
props.put("folder2", targetInput.getText());
-
props.store(new FileOutputStream(this.getClass().getSimpleName() + ".config"), null);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
private void addResultList() {
-
resultList = new JList<String>();
-
JScrollPane pane = new JScrollPane(resultList);
-
pane.setBorder(BasicBorders.getTextFieldBorder());
-
add(pane, BorderLayout.CENTER);
-
resultList.addMouseListener(this);
-
resultList.setModel(resultListModel);
-
}
-
-
private void addTargetFolderInput(JComponent p) {
-
JPanel panel = new JPanel();
-
p.add(panel, BorderLayout.SOUTH);
-
panel.setLayout(new BorderLayout());
-
-
JLabel label = new JLabel("Target:");
-
label.setPreferredSize(new Dimension(60, 0));
-
panel.add(label, BorderLayout.WEST);
-
-
targetInput = new JTextField();
-
panel.add(targetInput, BorderLayout.CENTER);
-
-
JButton button = new JButton("...");
-
button.setActionCommand(CMD_SELECT_TARGET);
-
button.addActionListener(this);
-
panel.add(button, BorderLayout.EAST);
-
}
-
-
private void addSourceFolderInput(JComponent p) {
-
JPanel panel = new JPanel();
-
p.add(panel, BorderLayout.NORTH);
-
panel.setLayout(new BorderLayout());
-
-
JLabel label = new JLabel("Source:");
-
label.setPreferredSize(new Dimension(60, 0));
-
panel.add(label, BorderLayout.WEST);
-
-
sourceInput = new JTextField();
-
panel.add(sourceInput, BorderLayout.CENTER);
-
-
JButton button = new JButton("...");
-
button.setActionCommand(CMD_SELECT_SOURCE);
-
button.addActionListener(this);
-
panel.add(button, BorderLayout.EAST);
-
}
-
-
public static void main(String[] args) {
-
new FolderCompareFrame2();
-
}
-
-
@Override
-
public void actionPerformed(ActionEvent e) {
-
if (e.getActionCommand() == CMD_SELECT_SOURCE) {
-
fileChooser.setCurrentDirectory(new File(sourceInput.getText()));
-
int result = fileChooser.showOpenDialog(FolderCompareFrame2.this);
-
if(result == JFileChooser.APPROVE_OPTION) {
-
sourceInput.setText(fileChooser.getSelectedFile().getAbsolutePath());
-
}
-
} else if (e.getActionCommand() == CMD_SELECT_TARGET) {
-
fileChooser.setCurrentDirectory(new File(targetInput.getText()));
-
int result = fileChooser.showOpenDialog(FolderCompareFrame2.this);
-
if(result == JFileChooser.APPROVE_OPTION) {
-
targetInput.setText(fileChooser.getSelectedFile().getAbsolutePath());
-
}
-
} else if (e.getActionCommand() == CMD_COMPARE) {
-
sourceFolder = sourceInput.getText().replaceAll("\\\\", "/");
-
if (!sourceFolder.endsWith("/")) {
-
sourceFolder = sourceFolder + "/";
-
}
-
targetFolder = targetInput.getText().replaceAll("\\\\", "/");
-
if (!targetFolder.endsWith("/")) {
-
targetFolder = targetFolder + "/";
-
}
-
saveSettings();
-
resultListModel.clear();;
-
compare(sourceFolder, targetFolder);
-
}
-
}
-
-
/**
-
* Start NotePad++
-
* @param sourceFile
-
* @param targetFile
-
*/
-
private void compareFile(String sourceFile, String targetFile) {
-
ProcessBuilder pb = new ProcessBuilder(notepadPlusCmd, sourceFile, targetFile);
-
try {
-
pb.start();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
private boolean endWith(String file, String[] suffix) {
-
for (String s : suffix) {
-
if (file.endsWith(s)) {
-
return true;
-
}
-
}
-
return false;
-
}
-
-
private boolean containString(String file, String[] containString) {
-
for (String s : containString) {
-
if (file.contains(s)) {
-
return true;
-
}
-
}
-
return false;
-
}
-
-
private List<String> list(File folder) {
-
if (folder.isFile()) {
-
return Collections.singletonList(folder.getAbsolutePath().replace("\\", "/"));
-
}
-
List<String> list = new ArrayList<String>();
-
for (File f : folder.listFiles()) {
-
list.addAll(list(f));
-
}
-
return list;
-
}
-
-
private void compare(String source, String target) {
-
System.out.println("Compare File: " + source + " with " + target);
-
File sourceFile = new File(source);
-
File targetFile = new File(target);
-
-
if (!sourceFile.exists() || !targetFile.exists()) {
-
//Source file or target file not exist.
-
if ((sourceFile.isFile() || targetFile.isFile())
-
&& endWith(source, SUFFIX_FILTER) && !containString(source, EXCLUDE_FILTER)) {
-
resultListModel.addElement(source.substring(sourceFolder.length()));
-
}
-
-
//Source file is a folder, target folder not exist.
-
if (sourceFile.isDirectory()) {
-
for (String f : list(sourceFile)) {
-
if (endWith(f, SUFFIX_FILTER) && !containString(f, EXCLUDE_FILTER)) {
-
resultListModel.addElement(f.substring(sourceFolder.length()));
-
}
-
}
-
}
-
-
if (targetFile.isDirectory()) {
-
for (String f : list(targetFile)) {
-
if (endWith(f, SUFFIX_FILTER) && !containString(f, EXCLUDE_FILTER)) {
-
resultListModel.addElement(f.substring(targetFolder.length()));
-
}
-
}
-
}
-
-
} else if (sourceFile.isFile() && targetFile.isFile() && endWith(source, SUFFIX_FILTER)
-
&& !containString(source, EXCLUDE_FILTER) && !fileMatch(sourceFile, targetFile)) {
-
resultListModel.addElement(source.substring(sourceFolder.length()));
-
} else if (sourceFile.isDirectory() && targetFile.isDirectory()) {
-
if (!source.endsWith("/")) {
-
source = source + "/";
-
}
-
if (!target.endsWith("/")) {
-
target = target + "/";
-
}
-
-
Set<String> subFolders = new LinkedHashSet<String>();
-
subFolders.addAll(Arrays.asList(sourceFile.list()));
-
subFolders.addAll(Arrays.asList(targetFile.list()));
-
for (String subFolder : subFolders) {
-
compare(source + subFolder, target + subFolder);
-
}
-
}
-
}
-
-
private boolean fileMatch(File sourceFile, File targetFile) {
-
if (sourceFile.length() != targetFile.length()) {
-
return false;
-
}
-
-
try {
-
if (!Arrays.equals(md5(readFileToByteArray(sourceFile)), md5(readFileToByteArray(targetFile)))) {
-
return false;
-
}
-
} catch (IOException e) {
-
e.printStackTrace();
-
return false;
-
}
-
-
return true;
-
}
-
-
private byte[] md5(byte[] buf) {
-
try {
-
MessageDigest md5 = MessageDigest.getInstance("MD5");
-
return md5.digest(buf);
-
} catch (NoSuchAlgorithmException e) {
-
throw new IllegalArgumentException();
-
}
-
}
-
-
private byte[] readFileToByteArray(File f) throws IOException {
-
FileInputStream input = new FileInputStream(f);
-
try {
-
if (f.isFile() && f.canRead()) {
-
int size = (int)f.length();
-
byte[] data = new byte[size];
-
-
int offset = 0;
-
int readed;
-
-
while (offset < size && (readed = input.read(data, offset, size - offset)) != -1) {
-
offset += readed;
-
}
-
return data;
-
} else {
-
return null;
-
}
-
} finally {
-
input.close();
-
}
-
}
-
-
@Override
-
public void mouseClicked(MouseEvent e) {
-
if (e.getClickCount() == 2 && resultList.getSelectedIndex() >= 0) {
-
String file = (String) resultList.getSelectedValue();
-
compareFile(sourceFolder + file, targetFolder + file);
-
}
-
}
-
public void mousePressed(MouseEvent e) {}
-
public void mouseReleased(MouseEvent e) {}
-
public void mouseEntered(MouseEvent e) {}
-
public void mouseExited(MouseEvent e) {}
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10742815/viewspace-2130753/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- GOODGo
- A good PlayerGo
- good booksGo
- css layoutCSS
- layout佈局
- jquery ui layoutjQueryUI
- Layout Inflation :Unconditional layout, inflation from view adapterViewAPT
- Some good websites for C++GoWebC++
- Good Bye 2013Go
- How Good Are Your Opinion 2Go
- Cheap ghd straighteners a goodAIGo
- How To Kill Good IdeasGoIdea
- Good documentation or books related to OracleGoOracle
- Good Links related OracleGoOracle
- Good story for dear friends.Go
- Number of k-good subarraysGo
- Flutter layout 作弊稿Flutter
- flex 3 rows layoutFlex
- Scala介面Pannel、Layout
- macOS Development - Auto LayoutMacdev
- fixed layout androidAndroid
- fixed fluid layoutUI
- 2010//11/24 11.24 好友共享美食 Good Food, Good FriendsGo
- Good site on Oracle tech blogGoOracle
- 對maven的通俗理解,goodMavenGo
- Your Prediction Gets As Good As Your DataGo
- Ghd cheap good earthenware oneGo
- Good Coding Skills Are Not Enough (轉)Go
- learn english, a good website to learn englishGoWeb
- good wife真的很好看Go
- rember me all time good luckREMGo
- CF1762F Good PairsGoAI
- 六年級數學期中考試只考了88分, 但試卷被老師寫下:Good! very good!! very very good!!!Go
- This is a good question,初學者都犯暈!Go
- 1512. Number of Good PairsGoAI
- 一個RPC的demo(good)RPCGo
- Some good articles about SQL*loaderGoSQL
- All good things come to an endGo