Skip to main content
summaryrefslogtreecommitdiffstats
blob: 05c442e9ed080e7cb27c35d17b60ec0e26559be1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*******************************************************************************
 * Copyright (c) 2006 Oracle Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Cameron Bateman/Oracle - initial API and implementation
 *    
 ********************************************************************************/

package org.eclipse.jst.jsf.designtime.internal.jsp;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jst.jsf.common.internal.JSPUtil;
import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;
import org.eclipse.jst.jsf.core.jsfappconfig.JSFAppConfigUtils;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

/**
 * On workbench startup, registers a part listener that triggers when
 * a JSP editor opens.
 * 
 * @author cbateman
 *
 */
public class StartupHandler implements IStartup 
{
    private final JSPEditorListener    _partListener = new JSPEditorListener();
    
	public void earlyStartup() 
    {
        PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable()
        {
            public void run()
            {
                IWorkbenchWindow  windows[] = 
                    PlatformUI.getWorkbench().getWorkbenchWindows();
                
                for (int i = 0; i < windows.length; i++)
                {
                    IWorkbenchPage pages[] = windows[i].getPages();
                    for (int j = 0; j < pages.length; j++)
                    {
                        IEditorReference[]  editorReferences = 
                            pages[j].getEditorReferences();
                        
                        for (int k = 0; k < editorReferences.length; k++)
                        {
                            if (_partListener.isValidJSPEditor(editorReferences[k]))
                            {
                                _partListener.setJSPModelListener(editorReferences[k]);
                            }
                        }
                    }
                    windows[i].getPartService().addPartListener(_partListener);
                }
                
                // TODO: register with all windows?
                PlatformUI.getWorkbench().addWindowListener(new IWindowListener()
                {
        
                    public void windowActivated(IWorkbenchWindow window) {
                        // do nothing
                    }
        
                    public void windowDeactivated(IWorkbenchWindow window) {
                        // do nothing
                    }
        
                    public void windowClosed(IWorkbenchWindow window) {
                        window.getPartService().removePartListener(_partListener);
                    }
        
                    public void windowOpened(IWorkbenchWindow window) {
                        window.getPartService().addPartListener(_partListener);
                    }
                });    
            }
        });
	}

	private static class JSPEditorListener implements IPartListener2
	{
		private JSPModelProcessor 		_processor;
		
		public void partActivated(IWorkbenchPartReference partRef) {
			// do nothing
		}

		public void partBroughtToTop(IWorkbenchPartReference partRef) {
			// do nothing
		}

        public void partClosed(IWorkbenchPartReference partRef) {
            if (partRef instanceof IEditorReference)
            {
                releaseJSPModelListener((IEditorReference) partRef);
            }
		}

		public void partDeactivated(IWorkbenchPartReference partRef) {
			// do nothing
		}

		public void partOpened(IWorkbenchPartReference partRef) {
            if (isValidJSPEditor(partRef))
            {
                setJSPModelListener((IEditorReference)partRef);
            }
		}

		public void partHidden(IWorkbenchPartReference partRef) {
			// do nothing
		}

		public void partVisible(IWorkbenchPartReference partRef) {
			// do nothing
		}

		public void partInputChanged(IWorkbenchPartReference partRef) {
			// do nothing
		}
       
        private boolean isJSPEditor(IEditorReference editorRef)
        {
            IFile file = getIFile(editorRef);

            if (file != null)
            {
                return JSPUtil.isJSPContentType(file);
            }

            return false;
        }
        
        /**
         * @param editorRef
         * @return true if the editor is editing the JSP content type and
         * the owning project is a JSF project
         */
        boolean isValidJSPEditor(IEditorReference editorRef)
        {
            final IFile file = getIFile(editorRef);
            
            return file != null && 
                    JSFAppConfigUtils.isValidJSFProject(file.getProject()) &&
                        isJSPEditor(editorRef);
        }
        
        
        boolean isValidJSPEditor(IWorkbenchPartReference partRef)
        {
            if (partRef instanceof IEditorReference)
            {
                return isValidJSPEditor((IEditorReference)partRef);
            }
            
            return false;
        }
        
        void setJSPModelListener(IEditorReference editorRef)
        {
            IFile file = getIFile(editorRef);
            
            if (file != null)
            {
                try
                {
                    // implicitly creates if not present
                    _processor = JSPModelProcessor.get(file);
                    _processor.refresh(false);
                }
                catch (Exception e)
                {
                    JSFCorePlugin.getDefault().getLog().log(new Status(IStatus.ERROR, JSFCorePlugin.PLUGIN_ID, 0, "Error acquiring model processor",e)); //$NON-NLS-1$
                }
            }
        }
        
        void releaseJSPModelListener(IEditorReference editorRef)
        {
        	IFile file = getIFile(editorRef);
        	
            if (file != null)
            {
                JSPModelProcessor.dispose(file);
            }
        }
        
        IFile getIFile(IEditorReference editorRef)
        {
            try
            {
                IEditorInput editorInput = editorRef.getEditorInput();
                Object adapt = editorInput.getAdapter(IFile.class);
                
                if (adapt instanceof IFile)
                {
                    return (IFile) adapt;
                }
            }
            catch (PartInitException excp)
            {
                JSFCorePlugin.getDefault().getLog().log(new Status(IStatus.ERROR, JSFCorePlugin.PLUGIN_ID, 0, "Error acquiring editor input",excp)); //$NON-NLS-1$
            }
            
            return null;
        }
	}
}

Back to the top