@Override public T getChild(String name) { T child = treeElements.get(name); if ((child != null) && (child.getParent() != null) && child.getParent().equals(this)) { return child; } else { return null; } }
@Override public Vector<T> getChildren() { Vector<T> children = new Vector<T>(); for (T sc : treeElements.values()) { if (sc.getParent() != null && sc.getParent() == this) { children.add(sc); } } return children; }
@Transactional(readOnly = false) public void save(T entity) { @SuppressWarnings("unchecked") Class<T> entityClass = Reflections.getClassGenricType(getClass(), 1); // 如果没有设置父节点,则代表为跟节点,有则获取父节点实体 if (entity.getParent() == null || StringUtils.isBlank(entity.getParentId()) || "0".equals(entity.getParentId())) { entity.setParent(null); } else { entity.setParent(super.get(entity.getParentId())); } if (entity.getParent() == null) { T parentEntity = null; try { parentEntity = entityClass.getConstructor(String.class).newInstance("0"); } catch (Exception e) { throw new ServiceException(e); } entity.setParent(parentEntity); entity.getParent().setParentIds(StringUtils.EMPTY); } // 获取修改前的parentIds,用于更新子节点的parentIds String oldParentIds = entity.getParentIds(); // 设置新的父节点串 entity.setParentIds(entity.getParent().getParentIds() + entity.getParent().getId() + ","); // 保存或更新实体 super.save(entity); // 更新子节点 parentIds T o = null; try { o = entityClass.newInstance(); } catch (Exception e) { throw new ServiceException(e); } o.setParentIds("%," + entity.getId() + ",%"); List<T> list = dao.findByParentIdsLike(o); for (T e : list) { if (e.getParentIds() != null && oldParentIds != null) { e.setParentIds(e.getParentIds().replace(oldParentIds, entity.getParentIds())); preUpdateChild(entity, e); dao.updateParentIds(e); } } }
private void compactNodes(T node) { for (int i = 0; i < node.getChildCount(); i++) { MPSTreeNode child = (MPSTreeNode) node.getChildAt(i); if (myBuilder.isNamespaceNode(child)) { compactNodes((T) child); } } if (node.getParent() != null && // skip root node.getChildCount() == 1 && myBuilder.isNamespaceNode((MPSTreeNode) node.getChildAt(0))) { T child = (T) node.getChildAt(0); myBuilder.setName(node, myBuilder.getName(node) + "." + myBuilder.getName(child)); Enumeration children = child.children(); List<MPSTreeNode> oldChildren = new ArrayList<MPSTreeNode>(); while (children.hasMoreElements()) { oldChildren.add((MPSTreeNode) children.nextElement()); } for (MPSTreeNode c : oldChildren) { child.remove(c); node.add(c); } node.remove(child); } }
@Override public void scan(final CtElement element) { if (element != null && clazz.isAssignableFrom(element.getClass())) { final T potentialVariable = (T) element; if (name.equals(potentialVariable.getSimpleName())) { // Since the AST is not completely available yet, we can not check if element's // parent (ep) contains the innermost element of `stack` (ie). Therefore, we // have to check if one of the following condition holds: // // 1) Does `stack` contain `ep`? // 2) Is `ep` the body of one of `stack`'s CtExecutable elements? // // The first condition is easy to see. If `stack` contains `ep` then `ep` and // all it's declared variables are in scope of `ie`. Unfortunately, there is a // special case in which a variable (a CtLocalVariable) has been declared in a // block (CtBlock) of, for instance, a method. Such a block is not contained in // `stack`. This peculiarity calls for the second condition. final CtElement parentOfPotentialVariable = potentialVariable.getParent(); for (final ASTPair astPair : stack) { if (astPair.element == parentOfPotentialVariable) { finish(potentialVariable); return; } else if (astPair.element instanceof CtExecutable) { final CtExecutable executable = (CtExecutable) astPair.element; if (executable.getBody() == parentOfPotentialVariable) { finish(potentialVariable); return; } } } } } super.scan(element); }
/** * Sorts the map of nodes, so that a node's parent is guaranteed to come before that node (unless * the parent is missing). * * <p>Relies on ordering guarantees of returned map (i.e. LinkedHashMap, which guarantees * insertion order even if a key is re-inserted into the map). * * <p>TODO Inefficient implementation! */ @VisibleForTesting static <T extends TreeNode> Map<String, T> sortParentFirst(Map<String, T> nodes) { Map<String, T> result = Maps.newLinkedHashMap(); for (T node : nodes.values()) { List<T> tempchain = Lists.newLinkedList(); T nodeinchain = node; while (nodeinchain != null) { tempchain.add(0, nodeinchain); nodeinchain = (nodeinchain.getParent() == null) ? null : nodes.get(nodeinchain.getParent()); } for (T n : tempchain) { result.put(n.getId(), n); } } return result; }
/** * Returns an enriched list of preferences. Contains the user preferences, implied user parent * preferences (quality between 0.005 and 0.006), default preference (quality of 0.003), default * parent preference (quality of 0.002), all preference (quality of 0.001).<br> * <br> * This necessary to compensate the misconfiguration of many browsers which don't expose all the * metadata actually understood by end users. * * @param <T> * @param userPreferences The user preferences to enrich. * @param defaultValue The default value. * @param allValue The ALL value. * @return The enriched user preferences. */ @SuppressWarnings("unchecked") protected <T extends Metadata> List<Preference<T>> getEnrichedPreferences( List<Preference<T>> userPreferences, T defaultValue, T allValue) { List<Preference<T>> result = new ArrayList<Preference<T>>(); // 0) List all undesired metadata List<T> undesired = null; for (Preference<T> pref : userPreferences) { if (pref.getQuality() == 0) { if (undesired == null) { undesired = new ArrayList<T>(); } undesired.add(pref.getMetadata()); } } // 1) Add the user preferences result.addAll(userPreferences); // 2) Add the user parent preferences T parent; for (int i = 0; i < result.size(); i++) { Preference<T> userPref = result.get(i); parent = (T) userPref.getMetadata().getParent(); // Add the parent, if it is not proscribed. if ((parent != null)) { if (canAdd(parent, undesired)) { result.add(new Preference<T>(parent, 0.005f + (0.001f * userPref.getQuality()))); } } } // 3) Add the default preference if (defaultValue != null && canAdd(defaultValue, undesired)) { Preference<T> defaultPref = new Preference<T>(defaultValue, 0.003f); result.add(defaultPref); T defaultParent = (T) defaultValue.getParent(); if (defaultParent != null && canAdd(defaultParent, undesired)) { result.add(new Preference<T>(defaultParent, 0.002f)); } } // 5) Add "all" preference for (int i = result.size() - 1; i >= 0; i--) { // Remove any existing preference if (result.get(i).getMetadata().equals(allValue)) { result.remove(i); } } result.add(new Preference<T>(allValue, 0.001f)); // 6) Return the enriched preferences return result; }
public static <T extends DomElement> T addElementAfter(@NotNull final T anchor) { final DomElement parent = anchor.getParent(); final DomCollectionChildDescription childDescription = (DomCollectionChildDescription) anchor.getChildDescription(); assert parent != null; final List<? extends DomElement> list = childDescription.getValues(parent); final int i = list.indexOf(anchor); assert i >= 0; return (T) childDescription.addValue(parent, i + 1); }
/** * Remove a node from the tree and from its parent (if exists) * * @param id * @return */ public <T extends TreeNode> T removeTreeNode(Long id) { T node = getTreeNode(id); if (node != null) { if (node instanceof Project) { delete(node); } else { TreeNode oldParent = node.getParent(); LinkedList<TreeNode> oldParentChildren = oldParent.getChildren(); if (oldParentChildren != null) { oldParentChildren.remove(node); oldParent.setChildren(oldParentChildren); } } } return node; }
@Override public <T extends MUIElement> void move( T element, MElementContainer<? super T> newParent, int index, boolean leavePlaceholder) { // Cache where we were MElementContainer<MUIElement> curParent = element.getParent(); int curIndex = curParent.getChildren().indexOf(element); // Move the model element if (index == -1) { newParent.getChildren().add(element); } else { newParent.getChildren().add(index, element); } if (leavePlaceholder) { MPlaceholder ph = MAdvancedFactory.INSTANCE.createPlaceholder(); ph.setRef(element); curParent.getChildren().add(curIndex, ph); } }
protected Object getDescriptor(T element) { PsiElement parent = element.getParent(); return parent instanceof XmlAttribute ? ((XmlAttribute) parent).getDescriptor() : null; }
public boolean isChildOf(Composite parent) { return scrollable.getParent() == parent; }