48 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
49 |
import java.awt.Window; |
import java.awt.Window; |
50 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
51 |
|
import java.awt.event.ActionListener; |
52 |
import java.awt.event.MouseAdapter; |
import java.awt.event.MouseAdapter; |
53 |
import java.awt.event.MouseEvent; |
import java.awt.event.MouseEvent; |
54 |
|
|
687 |
editFile(index); |
editFile(index); |
688 |
lastSelected = tmp; |
lastSelected = tmp; |
689 |
} |
} |
690 |
else if (editFile != null) |
else |
|
{ |
|
691 |
completeEditing(); |
completeEditing(); |
|
editFile = null; |
|
|
lastSelected = null; |
|
|
} |
|
692 |
} |
} |
693 |
|
else |
694 |
|
completeEditing(); |
695 |
} |
} |
696 |
|
|
697 |
/** |
/** |
700 |
* @param index - |
* @param index - |
701 |
* the current index of the item in the list to be edited. |
* the current index of the item in the list to be edited. |
702 |
*/ |
*/ |
703 |
private void editFile(int index) |
void editFile(int index) |
704 |
{ |
{ |
705 |
list.ensureIndexIsVisible(index); |
list.ensureIndexIsVisible(index); |
706 |
editFile = (File) list.getModel().getElementAt(index); |
editFile = (File) list.getModel().getElementAt(index); |
707 |
if (editFile.canWrite()) |
if (editFile.canWrite()) |
708 |
{ |
{ |
|
Rectangle bounds = list.getCellBounds(index, index); |
|
|
Icon icon = getFileView(fc).getIcon(editFile); |
|
709 |
editField = new JTextField(editFile.getName()); |
editField = new JTextField(editFile.getName()); |
710 |
// FIXME: add action listener for typing |
editField.addActionListener(new EditingActionListener()); |
|
// FIXME: painting for textfield is messed up when typing |
|
|
list.add(editField); |
|
|
editField.requestFocus(); |
|
|
editField.selectAll(); |
|
711 |
|
|
712 |
|
Rectangle bounds = list.getCellBounds(index, index); |
713 |
|
Icon icon = getFileView(fc).getIcon(editFile); |
714 |
if (icon != null) |
if (icon != null) |
715 |
bounds.x += icon.getIconWidth() + 4; |
bounds.x += icon.getIconWidth() + 4; |
716 |
editField.setBounds(bounds); |
editField.setBounds(bounds); |
717 |
|
|
718 |
|
list.add(editField); |
719 |
|
|
720 |
|
editField.requestFocus(); |
721 |
|
editField.selectAll(); |
722 |
} |
} |
723 |
else |
else |
724 |
{ |
completeEditing(); |
725 |
editField = null; |
list.repaint(); |
|
editFile = null; |
|
|
lastSelected = null; |
|
|
} |
|
726 |
} |
} |
727 |
|
|
728 |
/** |
/** |
729 |
* Completes the editing. |
* Completes the editing. |
730 |
*/ |
*/ |
731 |
private void completeEditing() |
void completeEditing() |
732 |
{ |
{ |
733 |
if (editField != null) |
if (editField != null && editFile != null) |
734 |
{ |
{ |
735 |
String text = editField.getText(); |
String text = editField.getText(); |
736 |
if (text != null && !text.equals("")) |
if (text != null && text != "" && !text.equals(fc.getName(editFile))) |
737 |
editFile.renameTo(new File(text)); |
if (editFile.renameTo |
738 |
|
(fc.getFileSystemView().createFileObject |
739 |
|
(fc.getCurrentDirectory(), text))) |
740 |
|
rescanCurrentDirectory(fc); |
741 |
list.remove(editField); |
list.remove(editField); |
|
list.revalidate(); |
|
|
list.repaint(); |
|
742 |
} |
} |
743 |
|
editFile = null; |
744 |
|
lastSelected = null; |
745 |
|
editField = null; |
746 |
|
list.repaint(); |
747 |
|
} |
748 |
|
|
749 |
|
/** |
750 |
|
* ActionListener for the editing text field. |
751 |
|
*/ |
752 |
|
class EditingActionListener implements ActionListener |
753 |
|
{ |
754 |
|
|
755 |
|
/** |
756 |
|
* This method is invoked when an action occurs. |
757 |
|
* |
758 |
|
* @param e - |
759 |
|
* the <code>ActionEvent</code> that occurred |
760 |
|
*/ |
761 |
|
public void actionPerformed(ActionEvent e) |
762 |
|
{ |
763 |
|
if (e.getActionCommand().equals("notify-field-accept")) |
764 |
|
completeEditing(); |
765 |
|
else if (editField != null) |
766 |
|
{ |
767 |
|
list.remove(editField); |
768 |
|
editFile = null; |
769 |
|
lastSelected = null; |
770 |
|
editField = null; |
771 |
|
list.repaint(); |
772 |
|
} |
773 |
|
} |
774 |
} |
} |
775 |
} |
} |
776 |
|
|