/** * Restore opened settings node if still there. * * @param oldProps properties inserted in the old model * @param selPath path to the node selected in the old model */ private void restoreInsertedProperties(HashMap<String, Property[]> oldProps, TreePath selPath) { DefaultTreeModel newModel = levelTrees.get(permissionLvl); // Create properties panel for every properties panel existing in the old model updateUsedPanels(oldProps, newModel); final JPanel holderPropertiesPanel = (JPanel) splitPane.getRightComponent(); holderPropertiesPanel.removeAll(); if (selPath != null) { // returns node from previous model DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) selPath.getLastPathComponent(); if (selectedNode.getUserObject() instanceof ClassPropertiesInfo) { // find selected class by name ClassPropertiesInfo selectedClassInfo = (ClassPropertiesInfo) selectedNode.getUserObject(); String selectedClassInfoName = selectedClassInfo.getName(); // see if it's still visible DefaultMutableTreeNode classNode; ClassPropertiesInfo classInfo; DefaultMutableTreeNode root = (DefaultMutableTreeNode) newModel.getRoot(); int classCount = newModel.getChildCount(root); for (int c = 0; c < classCount; c++) { classNode = (DefaultMutableTreeNode) newModel.getChild(root, c); classInfo = (ClassPropertiesInfo) classNode.getUserObject(); if (classInfo.getName().equalsIgnoreCase(selectedClassInfoName)) { tree.setSelectionPath(new TreePath(classNode.getPath())); c = classCount; } } } } holderPropertiesPanel.repaint(); }
/** * Cycle through the nodes in the new model.<br> * For each node in the old model that also had a panel, create (for the new permission level) and * transfer all the properties in the old one that are still present. <br> * As panel is only created if is visualized, this guarantees that all previously modified * properties keep modifications. * * @param oldProps properties inserted in the old model * @param newModel set the old properties in the new model */ private void updateUsedPanels(HashMap<String, Property[]> oldProps, DefaultTreeModel newModel) { DefaultMutableTreeNode root = (DefaultMutableTreeNode) newModel.getRoot(); int childCount = newModel.getChildCount(root); DefaultMutableTreeNode node; ClassPropertiesInfo funcInfo; Property[] storedProps; for (int i = 0; i < childCount; i++) { node = (DefaultMutableTreeNode) newModel.getChild(root, i); funcInfo = (ClassPropertiesInfo) node.getUserObject(); storedProps = oldProps.get(funcInfo.getName()); // If this functionality was viewed it might contain changes if (storedProps != null) { // Create panel for it PropertySheetPanel propertyPanel = createPropertiesPanelForClass(funcInfo.getClassInstance()); Property newProps[] = propertyPanel.getProperties(); // go through properties int newPropsLength = newProps.length; int storedPropsLength = storedProps.length; // find who has more properties if (newPropsLength < storedPropsLength) { // lowering the permission int pStored = 0; int pNew = 0; while (pNew < newPropsLength && pStored < storedPropsLength) { if (storedProps[pStored].getName().equals(newProps[pNew].getName())) { newProps[pNew].setValue(storedProps[pStored].getValue()); pNew++; } pStored++; } } else { // going to higher permission int pStored = 0; int pNew = 0; while (pNew < newPropsLength && pStored < storedPropsLength) { if (storedProps[pStored].getName().equals(newProps[pNew].getName())) { newProps[pNew].setValue(storedProps[pStored].getValue()); pStored++; } pNew++; } } // set the updated properties panel funcInfo.setPropertiesPanel(propertyPanel); } } }
private HashMap<String, Property[]> extractProperties(TreeModel model) { HashMap<String, Property[]> props = new HashMap<>(); DefaultMutableTreeNode classNode; Object root = model.getRoot(); ClassPropertiesInfo classInfo; int classCount = model.getChildCount(root); Property[] classProperties; for (int c = 0; c < classCount; c++) { classNode = (DefaultMutableTreeNode) model.getChild(root, c); classInfo = (ClassPropertiesInfo) classNode.getUserObject(); PropertySheetPanel propertiesPanel = classInfo.getPropertiesPanel(); if (propertiesPanel != null) { // force out of edit mode propertiesPanel.getTable().commitEditing(); classProperties = propertiesPanel.getProperties(); props.put(classInfo.getName(), classProperties.clone()); } } return props; }