/*
* Created on 2004/08/16
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.commons.swt.ui;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.ArrayUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Text;
import org.jpn.xucker.commons.util.WildCard;
/**
*
*
*/
public class FileDirChoicer extends Canvas implements SelectionListener{
/**
* @param arg0
* @param arg1
*/
public static final int FILE=0;
public static final int DIR=1;
Layout rowLayout;
RowData labelLayoutData;
RowData textLayoutData;
RowData buttonLayoutData;
private Button button;
private Label label;
private Text text;
private int type;
private String[] filterExtensions;
private String[] filterNames;
FileStatusChanger fileListChanger;
private Button resetButton;
private RowData resetButtonLayoutData;
public void setPathWidth(int width){
textLayoutData.width=width;
}
public void setEnabled(boolean bool){
super.setEnabled(bool);
label.setEnabled(bool);
text.setEnabled(bool);
button.setEnabled(bool);
if(resetButton!=null)
resetButton.setEnabled(bool);
}
public void removeFileListChangeListener(FileStatusChangeListener listener){
fileListChanger.removeFileListChangeListener(listener);
}
public void addFileListChangeListener(FileStatusChangeListener listener){
fileListChanger.addFileListChangeListener(listener);
}
public FileDirChoicer(Composite arg0, int arg1,int type,String labelText) {
this(arg0,arg1,type,labelText,false);
}
public void addFile(String path){
if(type==FILE){
if(isAllowFile(path)){
inSetText(path);
}
}
}
public void addDirectory(String path){
if(type==DIR){
inSetText(path);
}
}
public FileDirChoicer(Composite arg0, int arg1,int type,String labelText,boolean haveReset) {
super(arg0, arg1);
fileListChanger=new FileStatusChanger(){
protected void addFilePath(String path){
addFile(path);
}
protected void addDirectoryPath(String path){
addDirectory(path);
}
};
RowData data=new RowData();
data.height=24;
setLayoutData(data);
this.type=type;
rowLayout=new RowLayout();
setLayout(rowLayout);
label = new Label(this,SWT.NULL);
if(labelText!=null){
label.setText(labelText);
labelLayoutData=new RowData();
labelLayoutData.width=100;
label.setLayoutData(labelLayoutData);
}
text = new Text(this,SWT.READ_ONLY);
textLayoutData=new RowData();
textLayoutData.width=200;
text.setLayoutData(textLayoutData);
text.setBackground(new Color(this.getShell().getDisplay(),255,255,255));
button = new Button(this,SWT.NULL);
button.setText("参照");
buttonLayoutData=new RowData();
buttonLayoutData.width=50;
button.setLayoutData(buttonLayoutData);
button.addSelectionListener(this);
if(haveReset){
resetButton = new Button(this,SWT.NULL);
resetButton.setText("リセット");
resetButtonLayoutData=new RowData();
resetButtonLayoutData.width=50;
resetButton.setLayoutData(buttonLayoutData);
resetButton.addSelectionListener(this);
}
Transfer[] types = new Transfer[] {FileTransfer.getInstance()};
int operations = DND.DROP_MOVE | DND.DROP_COPY ;
DropTarget target2 = new DropTarget (text, operations);
target2.setTransfer(types);
target2.addDropListener(fileListChanger.getMonitor());
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetSelected(SelectionEvent event) {
if(event.getSource()==button){
if(type==FILE){
openFileDialog();
}else{
openDirectoryDialog();
}
}else if(event.getSource()==resetButton){
String pre=text.getText();
text.setText("");
fileListChanger.fireRemoveFile(pre);
}
}
/**
*
*/
private void openDirectoryDialog() {
DirectoryDialog dialog=new DirectoryDialog(this.getShell());
String result=dialog.open();
if(result!=null){
inSetText(result);
}
}
public void inSetText(String path){
text.setText(path);
fileListChanger.fireAddFile(path);
}
/**
*
*/
private void openFileDialog() {
FileDialog dialog=new FileDialog(this.getShell(),SWT.OPEN);
if(filterExtensions!=null){
dialog.setFilterExtensions(filterExtensions);
}
if(filterNames!=null){
dialog.setFilterNames(filterNames);
}
String result=dialog.open();
if(result!=null){
inSetText(result);
}
}
private void setFilePathLabel(String path){
if(path!=null){
text.setText(path);
fileListChanger.fireAddFile(path);
}
}
public void removePath(){
String path=text.getText();
if(!path.equals("")){
fileListChanger.fireAddFile(path);
text.setText("");
}
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetDefaultSelected(SelectionEvent arg0) {
}
public void setFile(File file){
if(file!=null){
text.setText(file.getAbsolutePath());
}else{
text.setText("");
}
}
public File getFile(){
File file=null;
if(!text.getText().equals("")){
file=new File(text.getText());
}
return file;
}
public String getAbsoluteFilePath(){
if(getFile()==null){
return "";
}else{
return getFile().getAbsolutePath();
}
}
public void setFilePath(String path){
if(path==null || path.equals("")){
setFile(null);
}else{
setFile(new File(path));
}
}
private boolean isAllowFile(String path){
if(filterExtensions==null ){
return true;
}else{
if(filterExtensions!=null){
File f=new File(path);
for (int i = 0; i < filterExtensions.length; i++) {
WildCard wildCard=new WildCard(filterExtensions[i]);
if(wildCard.match(path)){
return true;
}
}
}
return false;
}
}
public String[] getFilterExtensions() {
return filterExtensions;
}
public void setFilterExtensions(String[] alowExtensions) {
this.filterExtensions = alowExtensions;
}
public String[] getFilterNames() {
return filterNames;
}
public void setFilterNames(String[] filterNames) {
this.filterNames = filterNames;
}
}
|