JFace - How can I make only one column editable in TreeViewer based on checkbox value from another column in the same row?

穿精又带淫゛_ 提交于 2019-12-11 02:37:23

问题


I have a TreeViewer which has two columns: ProximityClustersColumn: which has names as String, selectionColumn: which has checkbox as shown in the figure TreeViewer

I have two questions:

  1. On clicking the selection column's checkbox, the corresponding name of ProximityClustersColumn should become editable. For eg: When I click on the checkbox corresponding to "Studium organisieren-Formelles", the cell "Studium organisieren-Formelles" should become editable.

  2. Also, as seen in the figure, a check must be made such that only one value in the group, whose checkbox is checked becomes editable. In other words, for each group, only one category name can be checked and that corresponding name should be editable. For eg: If you look at the second group, there are two proximity Cluster names, i.e "Infos für Studis" and "Finanzielles im Studium", along with their respective checkboxes. Now, I can choose one among the two names, by selecting the corresponding checkbox. Suppose, I click on the checkbox corresponding to "Infos für Studis", only that cell should become editable.

The main idea is that : I should be able to select only one name from each group and edit it.

I have tried EditingSupport as suggested by @keyur, but the "canEdit" method is not called at all.

My LabelProvider extends ColumnLabelProvider and implements ITableLabelProvider. My ContentProvider implements ITreeContentProvider.

Is there any reason why EditingSupport will fail?

public class ProximityClustersEditingSupport extends EditingSupport{
    private TreeViewer viewer;
    private CellEditor editor;

      public ProximityClustersEditingSupport(ColumnViewer columnViewer, TreeViewer treeViewer) {
        super(columnViewer);
        this.viewer = treeViewer;
        this.editor = new TextCellEditor(treeViewer.getTree());
      }

      @Override
      protected CellEditor getCellEditor(Object element) {
        return new TextCellEditor();
      }

      @Override
      protected boolean canEdit(Object element) {
        return true;
      }

      @Override
      protected Object getValue(Object element) {
          if(element instanceof ProbeSort)
                return ((ProximityClusters)element).proximityClusterNames;
                return element;
      }

      @Override
      protected void setValue(Object element, Object value) {
          if (element instanceof ProbeSort)
            {
                 ((ProximityClusters)element).setProximityClusterNames(String.valueOf(value));
            }
            viewer.update(element, null);
      }
}

回答1:


I think I need more information to answer it completely. First of all is that Treeviewer or Tableviewer. To me it looks like TableViewer.

  1. What is the expected behavior of the cell when you mean editable. Those cells which are not editable should have disable type foreground colored text and the one which is editable can have normal foreground color say black. Only when user changes to focus (if tab is supported) or by clicking on the cell it is better to show the editable text where user can select the text and change/edit it. And pressing Enter key or Tab Key program can accept the change. Is that what you are looking for?

  2. I guess I didnt get the question. Can you give example from the above figure. Like what is group?




回答2:


I think EditingSupport will be useful for you.

You can create your concrete EditingSupport class and assign it to your column. It has method "canEdit" through which you can control the editing dynamically.

So what you have to do is to store the boolean flag in the model from checkbox status or read the check box status directly and return false/true value , which will enable/disable editing.



来源:https://stackoverflow.com/questions/36745388/jface-how-can-i-make-only-one-column-editable-in-treeviewer-based-on-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!