Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I didn't find a clean solution. I will explain you what I have done so far. I have modified the code that I have posted. I use <code>TableViewerColumn</code> instead of <code>TableColumn</code>.</p> <p>Class FileTableView:</p> <pre class="lang-java prettyprint-override"><code>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); } } </code></pre> <p>FirstColumnEditingSupport.java file:</p> <pre class="lang-java prettyprint-override"><code>/* * 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; } } </code></pre> <p>My RenameAction file:</p> <pre class="lang-java prettyprint-override"><code>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("&amp;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); } } } </code></pre> <p>My idea was:</p> <ul> <li>by default: the cell editor of the first column is disabled</li> <li>when the rename action is triggered (press the menu button), the cell editor of the column is enabled (<code>myTreeView.setCellEditable(true)</code>)</li> <li>the table line selected is in editing mode and you can change the name of the file. This part is not working quite well, because after you press the rename menu button, you can't see that the editing mode is enabled. After you click on the table line, the editing mode is activated. Also, the editing mode is enabled for all the table lines and I wanted to be enabled only for the selected one. For this problem I haven't got a solution.</li> <li><p>after you finish typing the new name of the file, and change the selection (click on a different table line), the editing mode is disabled:</p> <pre class="lang-java prettyprint-override"><code>tableViewer.addSelectionChangedListener(new ISelectionChangedListener(){ public void selectionChanged(SelectionChangedEvent event){ setCellEditable(false); } }); </code></pre></li> </ul> <p>I hope my solution can help you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload