KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > PreferenceContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.preference;
12
13 import org.eclipse.jface.viewers.ITreeContentProvider;
14 import org.eclipse.jface.viewers.Viewer;
15
16 /**
17  * Provides a tree model for <code>PreferenceManager</code> content.
18  *
19  * @since 3.0
20  */

21 public class PreferenceContentProvider implements ITreeContentProvider {
22
23     private PreferenceManager manager;
24
25     /* (non-Javadoc)
26      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
27      */

28     public void dispose() {
29         manager = null;
30     }
31
32     /**
33      * Find the parent of the provided node. Will search recursivly through the
34      * preference tree.
35      *
36      * @param parent the possible parent node.
37      * @param target the target child node.
38      * @return the parent node of the child node.
39      */

40     private IPreferenceNode findParent(IPreferenceNode parent,
41             IPreferenceNode target) {
42         if (parent.getId().equals(target.getId())) {
43             return null;
44         }
45
46         IPreferenceNode found = parent.findSubNode(target.getId());
47         if (found != null) {
48             return parent;
49         }
50
51         IPreferenceNode[] children = parent.getSubNodes();
52
53         for (int i = 0; i < children.length; i++) {
54             found = findParent(children[i], target);
55             if (found != null) {
56                 return found;
57             }
58         }
59
60         return null;
61     }
62
63     /*
64      * (non-Javadoc)
65      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
66      */

67     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {//must be an instance of <code>IPreferenceNode</code>.
68
return ((IPreferenceNode) parentElement).getSubNodes();
69     }
70
71     /*
72      * (non-Javadoc)
73      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
74      */

75     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {// must be an instance of <code>PreferenceManager</code>.
76
return getChildren(((PreferenceManager) inputElement).getRoot());
77     }
78
79     /*
80      * (non-Javadoc)
81      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
82      */

83     public Object JavaDoc getParent(Object JavaDoc element) {//must be an instance of <code>IPreferenceNode</code>.
84
IPreferenceNode targetNode = (IPreferenceNode) element;
85         IPreferenceNode root = manager.getRoot();
86         return findParent(root, targetNode);
87     }
88
89     /* (non-Javadoc)
90      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
91      */

92     public boolean hasChildren(Object JavaDoc element) {
93         return getChildren(element).length > 0;
94     }
95
96     /* (non-Javadoc)
97      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
98      */

99     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
100         manager = (PreferenceManager) newInput;
101     }
102     /**
103      * Set the manager for the preferences.
104      * @param manager The manager to set.
105      *
106      * @since 3.1
107      */

108     protected void setManager(PreferenceManager manager) {
109         this.manager = manager;
110     }
111 }
112
Popular Tags