我正在制作一个包含2个视图的应用程序。第一个包含一个树查看器,它显示我系统中的文件夹,第二个包含一个表查看器,它显示在第一个视图中选择的目录的内容。长话短说:文件浏览器。
//initialization of the table viewer
tableViewer = new TableViewer(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
tableViewer.setContentProvider(new FileTableContentProvider());
tableViewer.setLabelProvider(new FileTableLabelProvider());
//from within the implementation of any view or editor
/*
* getSite() - Returns the site for this workbench part (a workbench part can be a view (IViewPart) or an editor (IEditorPart))
* this view is a selection provider; the view sends the event to all the views registered to the selection service
*/
getSite().setSelectionProvider(tableViewer);
//the table column "Name" is added to the table viewer
TableColumn columnName = new TableColumn(tableViewer.getTable(), SWT.LEFT);
columnName.setText("Name");
columnName.setResizable(true);
columnName.setWidth(200);
//the table column "Date modified" is added to the table viewer
TableColumn columnDateModified = new TableColumn(tableViewer.getTable(), SWT.LEFT);
columnDateModified.setText("Date modified");
columnDateModified.setResizable(true);
columnDateModified.setWidth(200);
//the table column "Type" is added to the table viewer
TableColumn columnType = new TableColumn(tableViewer.getTable(), SWT.LEFT);
columnType.setText("Type");
columnType.setResizable(true);
columnType.setWidth(200);
//make the header of the table visible
tableViewer.getTable().setHeaderVisible(true);
/*
* getSite().getPage() - gets the active workbench page.
*/
getSite().getPage().addSelectionListener("com.awebofcode.fileexplorer.view.filetree",(ISelectionListener)this);
/*
* add a doubleClickListener for:
* 1) if the object selected is a file, then the file will be opened with the associated program
* 2) if the object selected is a directory, then enter the folder and update the tree viewer
*/
tableViewer.addDoubleClickListener(new IDoubleClickListener(){
@Override
public void doubleClick(DoubleClickEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
File itemSelected = (File) selection.getFirstElement();
//if the selected item is a file a double click will launch the associated program
if (itemSelected.isFile() && itemSelected.exists()){
Program.launch(itemSelected.getAbsolutePath());
}else if (itemSelected.isDirectory()){
/*
* Update the tree viewer;
* PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(FileTreeView.ID) --> returns the view with the specified ID
* setSelection() will send an event and the setSelectionChanged will run
*/
((FileTreeView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(FileTreeView.ID)).getTreeViewer().setSelection(selection);
}
}
});
//create the cell editor
CellEditor[] editors = new CellEditor[1];
editors[0] = new TextCellEditor(tableViewer.getTable());
tableViewer.setColumnProperties(new String[]{"Name", "Date modified", "Type"});
//assign the cell editors to the table viewer
tableViewer.setCellEditors(editors);
//set the cell modifier to the table viewer
tableViewer.setCellModifier(new NameCellModifier(tableViewer));
我创建了一个操作,将从第二个视图重命名所选文件或文件夹。
当我点击文件名时我不想进入编辑模式。我想保持选择模式并且只有在我点击文件 - >之后重命名(操作重命名)必须启用编辑模式。
问题是我找不到如何启用/禁用TextCellEditor
。
答案 0 :(得分:1)
我找不到干净的解决方案。我会向你解释到目前为止我做了什么。我修改了我发布的代码。我使用TableViewerColumn
代替TableColumn
。
类FileTableView:
public class FileTableView extends ViewPart implements ISelectionListener{
private TableViewer tableViewer;
private boolean cellEditable = false;
private FirstColumnEditingSupport obj;
public FileTableView() {
super();
}
@Override
public void createPartControl(Composite parent) {
//initialization of the table viewer
tableViewer = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.MULTI); //SWT.MULTI - for multiple selection; SWT.FULL_SELECTION - for selection of an entire row
tableViewer.setContentProvider(new FileTableContentProvider());
//it is not necessary to add a label provider for the table viewer because we will set a label provider for every column of the table
//tableViewer.setLabelProvider(new FileTableLabelProvider());
//extract the table widget of the table viewer
final Table table = tableViewer.getTable();
//make the header of the table visible
table.setHeaderVisible(true);
//hide the lines of the table
table.setLinesVisible(false);
//create the columns of the table
createColumns(parent, tableViewer);
//set the sorter to the table viewer
tableComparator = new TableViewerComparator();
tableViewer.setComparator(tableComparator);
//from within the implementation of any view or editor
/*
* getSite() - Returns the site for this workbench part (a workbench part can be a view (IViewPart) or an editor (IEditorPart))
* this view is a selection provider; the view sends the event to all the views registered to the selection service
*/
getSite().setSelectionProvider(tableViewer);
tableViewer.addSelectionChangedListener(new ISelectionChangedListener(){
public void selectionChanged(SelectionChangedEvent event){
setCellEditable(false);
}
});
}
/*
* method used to update the viewer from outside
*/
public void refresh(){
tableViewer.refresh();
}
@Override
public void setFocus() {
tableViewer.getControl().setFocus();
}
//method that returns the table viewer
public TableViewer getTableViewer(){
return tableViewer;
}
/*
* get the value of the cellEditable
*/
public boolean getCellEditable(){
return cellEditable;
}
/*
* set the value of the cellEditable
*/
public void setCellEditable(boolean cellEditable){
this.cellEditable = cellEditable;
}
public FirstColumnEditingSupport getEditingSupport(){
return obj;
}
/*
* method that creates columns of the table
*/
private void createColumns(final Composite parent, final TableViewer viewer){
String[] titles = {"Name", "Date Modified", "Size"};
int[] width = {200, 200, 200};
//first column is for the name of the file
TableViewerColumn col = createTableViewerColumn(titles[0], width[0], 0);
col.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element){
return ((File) element).getName();
}
public Image getImage(Object element){
return getFirsColumnImage((File) element);
}
});
obj = new FirstColumnEditingSupport(tableViewer,this);
col.setEditingSupport(obj);
//second column is for the last date modified
col = createTableViewerColumn(titles[1], width[1], 1);
col.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element){
return formatLastModifiedDate((File) element);
}
});
//third column is for size
col = createTableViewerColumn(titles[2], width[2], 2);
col.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element){
return formatLength((File) element);
}
});
}
private TableViewerColumn createTableViewerColumn(String title, int width, final int columnNumber){
final TableViewerColumn viewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
column.setText(title);
column.setWidth(width);
column.setResizable(true);
column.setMoveable(false);
column.addSelectionListener(getSelectionAdapter(column, columnNumber));
return viewerColumn;
}
private SelectionAdapter getSelectionAdapter(final TableColumn column, final int index){
SelectionAdapter selectionAdapter = new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
tableComparator.setColumn(index);
int direction = tableComparator.getDirection();
tableViewer.getTable().setSortDirection(direction);
tableViewer.getTable().setSortColumn(column);
tableViewer.refresh();
}
};
return selectionAdapter;
}
/*
* method used to return the last modified date in "dd-MM-yyyy hh:mm:ss" format
*/
private String formatLastModifiedDate(File file){
Date d = new Date(file.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
return sdf.format(d);
}
/*
* method used to return the length of the file in KB
*/
private String formatLength(File file){
long size = file.length()/1024;
NumberFormat f = new DecimalFormat("#,###,### KB");
return f.format(size);
}
}
FirstColumnEditingSupport.java文件:
/*
* The EditingSupport implementation defines how the content can be changed.
*/
public class FirstColumnEditingSupport extends EditingSupport {
private TableViewer tableViewer;
private FileTableView view;
private TextCellEditor textEditor;
public FirstColumnEditingSupport(TableViewer viewer, FileTableView view) {
super(viewer);
this.tableViewer = viewer;
this.view = view;
textEditor = new TextCellEditor(tableViewer.getTable());
}
@Override
/*
* (non-Javadoc)
* @see org.eclipse.jface.viewers.EditingSupport#getCellEditor(java.lang.Object)
* EditingSupport returns in his getCellEditor() method an object of type CellEditor. This object creates the controls to change the data.
*/
protected CellEditor getCellEditor(Object element) {
return textEditor;
}
@Override
/*
* (non-Javadoc)
* @see org.eclipse.jface.viewers.EditingSupport#canEdit(java.lang.Object)
* The canEdit() method defines, if the cell can be edited.
*/
protected boolean canEdit(Object element) {
return view.getCellEditable();
}
@Override
/*
* (non-Javadoc)
* @see org.eclipse.jface.viewers.EditingSupport#getValue(java.lang.Object)
* The getValue() method receives the current object and returns the value which should be displayed.
*/
protected Object getValue(Object element) {
return ((File) element).getName();
}
@Override
/*
* (non-Javadoc)
* @see org.eclipse.jface.viewers.EditingSupport#setValue(java.lang.Object, java.lang.Object)
* The method setValue() in EditingSupport receives the changed value based on the user input. In this method you assign the value to your data object.
*/
protected void setValue(Object element, Object value) {
//value is the user input
File oldFile = (File) element;
//String path = oldFile.getAbsolutePath().substring(oldFile.getAbsolutePath().lastIndexOf(File.pathSeparatorChar));
//System.out.println(oldFile.getParent());
oldFile.renameTo(new File(oldFile.getParent() + "\\" + (String) value));
tableViewer.refresh();
}
public TextCellEditor getTextCellEditor(){
return textEditor;
}
}
我的RenameAction文件:
public class RenameAction extends Action implements ISelectionListener, ActionFactory.IWorkbenchAction {
private final IWorkbenchWindow window;
private IStructuredSelection itemSelected;
public final static String ID = PlatformUI.PLUGIN_ID + ".RenameAction";
public RenameAction(IWorkbenchWindow window){
this.window = window;
setId(ID);
setText("&Rename");
setToolTipText("Rename the file or directory selected");
window.getSelectionService().addSelectionListener(this);
}
@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
if(selection.isEmpty() || !(selection instanceof IStructuredSelection)){
return;
}else {
itemSelected = (IStructuredSelection)selection;
}
}
public void dispose(){
window.getSelectionService().removeSelectionListener(this);
}
public void run(){
Object firstElement = itemSelected.getFirstElement();
File item = (File) firstElement;
if(item != null){
FileTableView myTreeView= (FileTableView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(FileTableView.ID);
myTreeView.setCellEditable(true);
}
}
}
我的想法是:
myTreeView.setCellEditable(true)
)键入文件的新名称并更改选择(单击其他表格行)后,编辑模式将被禁用:
tableViewer.addSelectionChangedListener(new ISelectionChangedListener(){
public void selectionChanged(SelectionChangedEvent event){
setCellEditable(false);
}
});
我希望我的解决方案可以帮到你。