KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > mapper > views > contentoutline > JFaceNodeAdapterForXML


1 package org.hibernate.eclipse.mapper.views.contentoutline;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import org.eclipse.jface.viewers.StructuredViewer;
7 import org.eclipse.swt.widgets.Display;
8 import org.eclipse.ui.PlatformUI;
9 import org.eclipse.ui.views.properties.PropertySheetPage;
10 import org.eclipse.wst.sse.core.internal.provisional.INodeAdapterFactory;
11 import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
12 import org.eclipse.wst.sse.ui.internal.contentoutline.IJFaceNodeAdapter;
13 import org.eclipse.wst.xml.ui.internal.XMLUIMessages;
14 import org.eclipse.wst.xml.ui.internal.contentoutline.BufferedOutlineUpdater;
15 import org.eclipse.wst.xml.ui.internal.contentoutline.JFaceNodeAdapter;
16 import org.eclipse.wst.xml.ui.internal.contentoutline.JFaceNodeAdapterFactory;
17 import org.eclipse.wst.xml.ui.internal.contentoutline.RefreshPropertySheetJob;
18 import org.w3c.dom.NamedNodeMap JavaDoc;
19 import org.w3c.dom.Node JavaDoc;
20
21
22 /**
23  * For xml files in general.
24  *
25  * Adapts a DOM node to a JFace viewer.
26  */

27 public class JFaceNodeAdapterForXML extends JFaceNodeAdapter {
28     final static Class JavaDoc ADAPTER_KEY = IJFaceNodeAdapter.class;
29     protected BufferedOutlineUpdater fUpdater;
30     protected INodeAdapterFactory adapterFactory;
31
32     public JFaceNodeAdapterForXML(INodeAdapterFactory adapterFactory) {
33         super(adapterFactory);
34         this.adapterFactory = adapterFactory;
35     }
36
37     
38
39     /**
40      * Fetches the label text specific to this object instance.
41      */

42     public String JavaDoc getLabelText(Object JavaDoc object) {
43         String JavaDoc result = getNodeName(object);
44         Node JavaDoc node = (Node JavaDoc) object;
45         NamedNodeMap JavaDoc attributes = node.getAttributes();
46         if(attributes!=null) {
47             Node JavaDoc firstAttribute = attributes.item(0);
48             if(firstAttribute!=null) {
49                 return result + " " + firstAttribute.getNodeName() + "=\"" + firstAttribute.getNodeValue() + "\"";
50             }
51         }
52         
53         return result;
54     }
55
56     private String JavaDoc getNodeName(Object JavaDoc object) {
57         Node JavaDoc node = (Node JavaDoc) object;
58         String JavaDoc nodeName = node.getNodeName();
59         return nodeName;
60     }
61
62     public Object JavaDoc getParent(Object JavaDoc object) {
63
64         Node JavaDoc node = (Node JavaDoc) object;
65         return node.getParentNode();
66     }
67
68     public boolean hasChildren(Object JavaDoc object) {
69         return super.hasChildren(object);
70     }
71
72     /**
73      * Allowing the INodeAdapter to compare itself against the type allows it
74      * to return true in more than one case.
75      */

76     public boolean isAdapterForType(Object JavaDoc type) {
77         return type.equals(ADAPTER_KEY);
78     }
79
80     /** only needed to provide better notifychanged operation */
81     private BufferedOutlineUpdater getOutlineUpdater() {
82         if (fUpdater == null)
83             fUpdater = new BufferedOutlineUpdater();
84         return fUpdater;
85     }
86     
87     /** only needed to provide better notifychanged operation */
88     Display getDisplay() {
89
90         // Note: the workbench should always have a display
91
// (unless running headless), whereas Display.getCurrent()
92
// only returns the display if the currently executing thread
93
// has one.
94
if (PlatformUI.isWorkbenchRunning())
95             return PlatformUI.getWorkbench().getDisplay();
96         else
97             return null;
98     }
99
100     /**
101      * Called by the object being adapter (the notifier) when something has
102      * changed.
103      */

104     public void notifyChanged(INodeNotifier notifier, int eventType, Object JavaDoc changedFeature, Object JavaDoc oldValue, Object JavaDoc newValue, int pos) {
105
106         // future_TODO: the 'uijobs' used in this method were added to solve
107
// threading problems when the dom
108
// is updated in the background while the editor is open. They may be
109
// a bit overkill and not that useful.
110
// (That is, may be be worthy of job manager management). If they are
111
// found to be important enough to leave in,
112
// there's probably some optimization that can be done.
113
Collection JavaDoc listeners = ((JFaceNodeAdapterFactory) adapterFactory).getListeners();
114         Iterator JavaDoc iterator = listeners.iterator();
115
116         while (iterator.hasNext()) {
117             Object JavaDoc listener = iterator.next();
118             if (notifier instanceof Node JavaDoc && (listener instanceof StructuredViewer) && (eventType == INodeNotifier.STRUCTURE_CHANGED || (eventType == INodeNotifier.CHANGE /*&& changedFeature == null*/))) {
119
120                 // System.out.println("JFaceNodeAdapter notified on event type > " + eventType + " at " + changedFeature);
121

122                 // refresh on structural and "unknown" changes
123
StructuredViewer structuredViewer = (StructuredViewer) listener;
124                 // https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=5230
125
if (structuredViewer.getControl() != null /*
126                                                            * &&
127                                                            * structuredViewer.getControl().isVisible()
128                                                            */
)
129                     getOutlineUpdater().processNode(structuredViewer, (Node JavaDoc) notifier);
130             } else if ((listener instanceof PropertySheetPage) && ((eventType == INodeNotifier.CHANGE) || (eventType == INodeNotifier.STRUCTURE_CHANGED))) {
131                 PropertySheetPage propertySheetPage = (PropertySheetPage) listener;
132                 if (propertySheetPage.getControl() != null /*
133                                                             * &&
134                                                             * !propertySheetPage.getControl().isDisposed()
135                                                             */
) {
136                     RefreshPropertySheetJob refreshPropertySheetJob = new RefreshPropertySheetJob(getDisplay(), XMLUIMessages.JFaceNodeAdapter_1, propertySheetPage); //$NON-NLS-1$
137
refreshPropertySheetJob.schedule();
138                 }
139             }
140         }
141     }
142 }
143
Popular Tags