📄 renameframe.java
字号:
/*
* ReNameFrame.java
*
* Created on 2008年12月4日, 下午2:03
*/
package com.topking.ftp.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Observable;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import sun.net.ftp.FtpClient;
import com.topking.ftp.bean.FileBean;
import com.topking.ftp.util.PathUtil;
/**
*
* @author lzkj
*/
public class ReNameFrame extends Observable{
/** Creates new form ReNameFrame */
// Variables declaration - do not modify
private JFrame frame;
private javax.swing.JLabel L_new;
private javax.swing.JLabel L_old;
private javax.swing.JTextField T_new;
private javax.swing.JTextField T_old;
private javax.swing.JButton bt_cancel;
private javax.swing.JButton bt_ok;
private FileBean fb;
private int x;
private int y;
private String type;
private String ok;
private FtpClient ftp;
public String getOk() {
return ok;
}
public void setOk(String ok) {
this.ok = ok;
this.setChanged();
this.notifyObservers(ok);
}
// End of variables declaration
public ReNameFrame(FileBean fb,int x,int y,String type,FtpClient ftp) {
this.fb = fb;
this.x = x;
this.y = y;
this.type = type;
this.ftp = ftp;
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
frame = new JFrame("文件重命名");
frame.setResizable(false);
frame.setLocation(x, y);
frame.setIconImage(frame.getToolkit().createImage(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/rename.gif")));
L_old = new javax.swing.JLabel("原名称",new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/file.gif")),SwingConstants.LEFT);
T_old = new javax.swing.JTextField(fb.getFileName());
L_new = new javax.swing.JLabel("新名称",new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/ftp/ui/images/name.gif")),SwingConstants.LEFT);
T_new = new javax.swing.JTextField();
bt_ok = new javax.swing.JButton();
bt_cancel = new javax.swing.JButton();
// frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
T_old.setEditable(false);
bt_ok.setText("确定");
bt_ok.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(reName(fb,T_new.getText().trim(),type)){
JOptionPane.showMessageDialog(frame, "更改文件名成功","提示",JOptionPane.DEFAULT_OPTION);
if(type.equals("LOCAL")){
setOk("LOCAL_RENAME_OK");
}else{
setOk("REMOTE_RENAME_OK");
}
frame.dispose();
}else{
JOptionPane.showMessageDialog(frame, "更改文件名失败","错误",JOptionPane.ERROR_MESSAGE);
}
}
});
bt_cancel.setText("取消");
bt_cancel.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.dispose();
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frame.getContentPane());
frame.getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(L_old, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(L_new))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(T_new, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
.addComponent(T_old, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(bt_ok)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 185, Short.MAX_VALUE)
.addComponent(bt_cancel)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(L_old)
.addComponent(T_old, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(L_new)
.addComponent(T_new, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bt_ok)
.addComponent(bt_cancel)))
);
frame.pack();
frame.setVisible(true);
}// </editor-fold>
public boolean reName(FileBean fb,String newName,String type){
boolean flag = false;
if(newName!=null&&!"".equals(newName)){
if("LOCAL".equals(type)){
/*System.out.println(fb.getFilePath());
System.err.println("New Name : "+PathUtil.getLocalPath(fb.getFilePath())+newName);*/
new File(fb.getFilePath()).renameTo(new File(PathUtil.getLocalPath(fb.getFilePath())+newName));
flag = true;
}else{
if(ftp.serverIsOpen()){
try {
System.out.println("remote file : "+fb.getFilePath());
String oldFile = fb.getFilePath().substring(0, fb.getFilePath().length()-1);
String newFile = PathUtil.getRemotePath(oldFile)+newName;
ftp.rename(oldFile,newFile);
// ftp.rename("/input.txt", "/hello.txt");
flag = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}else{
JOptionPane.showMessageDialog(frame, "请输入文件名","错误",JOptionPane.ERROR_MESSAGE);
}
return flag;
}
/**
* @param args the command line arguments
*/
/* public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ReNameFrame();
}
});
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -