用于多节点编辑的TreeCellEditor

时间:2018-06-04 13:57:20

标签: jtree

我为我的JTree创建了一个编辑器。我希望这个编辑器能够编辑MultipleNodes。它目前将完美地编辑一个节点。但是,当我尝试编辑多个节点时,我开始遇到问题。

我的编辑器采用节点集合。我的匹配器比较节点的userObjects的字段,并显示具有相同值的字段和具有不同值的列表。按下窗体上的更新按钮时,我希望更新所有节点。

我有一个编辑右键单击菜单项,我从中启动了DeviceEditing。

                        EDIT_MENUITEM.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent ae) {
                            //if a DefaultDevice cell was selected...
                            itemTree.startEditingAtPath(tp);
                        }
                    });

我在deviceEditor上有一个动作监听器。

                    deviceEditor.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        System.out.println("ACTION COMMAND= " + ae.getActionCommand());
                        if ("Update".equals(ae.getActionCommand())) {
                            //get the Device From the editor. 
                            //update all the nodes' userObjects to the values of the editor except the ones with <Multiple Values> 
                            itemTree.stopEditing();//update the nodes   

                            DefaultTreeModel model = (DefaultTreeModel) itemTree.getModel();
                            Iterator<DefaultMutableTreeNode> i = nodes.iterator();
                            while (i.hasNext()) {
                                DefaultMutableTreeNode next = i.next();
                                System.out.println("Updating "+(DefaultDevice)next.getUserObject());
                                model.nodeChanged(next);
                            }

                        }
                        if ("Cancel".equals(ae.getActionCommand())) {
                            itemTree.cancelEditing();
                        }
                    }
                });

public class DeviceEditor extends javax.swing.JPanel implements TreeCellEditor, FocusListener, ActionListener {

private final List<ActionListener> listeners = new ArrayList<>();
private final List<CellEditorListener> cellListeners = new ArrayList<>();
Collection<DefaultMutableTreeNode> nodes;
private DefaultDevice myDevice = new DefaultDevice();//the single device displayed and editited by this editor. You should take this at the end, and copy its fields that are not <Multiple Values> to all the UserObjects

/**
 * Creates new form DeviceEditor
 */
public DeviceEditor(Collection<DefaultMutableTreeNode> nodes) throws NoSuchFieldException {
    System.out.println("CREATING NEW EDITOR \n");
    this.nodes = nodes;
    ObjectMatcher matcher = new ObjectMatcher();
    try {
        myDevice = matcher.match(nodes, DefaultDevice.CREATE_MULTIVALUE_DEFAULTDEVICE(), new DefaultDevice());
        //System.out.println("Device= " + myDevice.getAddress() + " " + myDevice.getHostName());
    } catch (Exception e) {
        System.out.println(e);
    }
    initComponents();

}
.
.

TreeCellEditor方法如下。我遇到问题的部分是如何更新多个节点。我相信getCellEditorValue()是个问题。

    @Override
public Component getTreeCellEditorComponent(JTree jtree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
    //Return an Editor For the Device. Editor Should accept Multiple Devices
    //Use this instead of Setting the Editor in MyItemTree.
    return this;

}

@Override
public Object getCellEditorValue() {
    //this is populating myDevice if .stopEditing is called. 
    return myDevice;
}

@Override
public boolean isCellEditable(EventObject eo) {
    return true;
}

@Override
public boolean shouldSelectCell(EventObject eo) {
    //always select the cells
    return false;
}

@Override
public boolean stopCellEditing() {
    try {
        System.out.println("\n Cell Editing Stopped");
        //update myDevice
        if (!this.addressField.getText().equals(DefaultDevice.MULTIVALUE)) {
            myDevice.setAddress(this.addressField.getText());
        }
        myDevice.setDeviceType(this.deviceTypeField.getText());
        myDevice.setLocation(this.locationField.getText());
        myDevice.setSerialNumber(this.serialField.getText());
        myDevice.setUser(this.userField.getText());
        myDevice.setPassword(new String(this.passwordField.getPassword()));
        myDevice.setVendor(this.vendorField.getText());
        myDevice.setModel(this.modelField.getText());
        myDevice.setOS(this.osField.getText());
        myDevice.setDescription(this.descriptionField.getText());
        myDevice.setVersion(this.versionField.getText());
        myDevice.setDeviceType(this.deviceTypeField.getText());
        myDevice.setDisplayHostName(this.hostNameCheckBox.isSelected());
        myDevice.setDisplayIPV4Address(this.ipV4checkBox.isSelected());
        myDevice.setDisplayIPV6Address(this.ipV6CheckBox.isSelected());

        DeviceEditor.UPDATE_DEVICES(nodes, myDevice);
        return true;
    } catch (IPConverter.InvalidIPException ex) {
        Exceptions.printStackTrace(ex);
        return false;
    }

}

@Override
public void cancelCellEditing() {
    //do nothing
}

0 个答案:

没有答案