001: package net.refractions.udig.style.sld.editor.internal;
002:
003: /*******************************************************************************
004: * Copyright (c) 2003, 2005 IBM Corporation and others.
005: * All rights reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * IBM Corporation - initial API and implementation
012: *******************************************************************************/
013:
014: import net.refractions.udig.style.sld.editor.EditorPageManager;
015:
016: import org.eclipse.jface.viewers.ITreeContentProvider;
017: import org.eclipse.jface.viewers.Viewer;
018:
019: /**
020: * Provides a tree model for <code>PreferenceManager</code> content.
021: *
022: * @since 3.0
023: */
024: public class EditorPageContentProvider implements ITreeContentProvider {
025:
026: private EditorPageManager manager;
027:
028: /* (non-Javadoc)
029: * @see org.eclipse.jface.viewers.IContentProvider#dispose()
030: */
031: public void dispose() {
032: manager = null;
033: }
034:
035: /**
036: * Find the parent of the provided node. Will search recursivly through the
037: * preference tree.
038: *
039: * @param parent the possible parent node.
040: * @param target the target child node.
041: * @return the parent node of the child node.
042: */
043: private IEditorNode findParent(IEditorNode parent,
044: IEditorNode target) {
045: if (parent.getId().equals(target.getId()))
046: return null;
047:
048: IEditorNode found = parent.findSubNode(target.getId());
049: if (found != null)
050: return parent;
051:
052: IEditorNode[] children = parent.getSubNodes();
053:
054: for (int i = 0; i < children.length; i++) {
055: found = findParent(children[i], target);
056: if (found != null)
057: return found;
058: }
059:
060: return null;
061: }
062:
063: /*
064: * (non-Javadoc)
065: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
066: */
067: public Object[] getChildren(Object parentElement) {//must be an instance of <code>IPreferenceNode</code>.
068: return ((IEditorNode) parentElement).getSubNodes();
069: }
070:
071: /*
072: * (non-Javadoc)
073: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
074: */
075: public Object[] getElements(Object inputElement) {// must be an instance of <code>PreferenceManager</code>.
076: return getChildren(((EditorPageManager) inputElement).getRoot());
077: }
078:
079: /*
080: * (non-Javadoc)
081: * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
082: */
083: public Object getParent(Object element) {//must be an instance of <code>IEditorNode</code>.
084: IEditorNode targetNode = (IEditorNode) element;
085: IEditorNode root = manager.getRoot();
086: return findParent(root, targetNode);
087: }
088:
089: /* (non-Javadoc)
090: * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
091: */
092: public boolean hasChildren(Object element) {
093: return getChildren(element).length > 0;
094: }
095:
096: /* (non-Javadoc)
097: * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
098: */
099: public void inputChanged(Viewer viewer, Object oldInput,
100: Object newInput) {
101: manager = (EditorPageManager) newInput;
102: }
103:
104: /**
105: * Set the manager for the preferences.
106: * @param manager The manager to set.
107: *
108: * @since 3.1
109: */
110: protected void setManager(EditorPageManager manager) {
111: this.manager = manager;
112: }
113: }
|