]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/propertiesfileeditor/PropertiesFileEditor.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / propertiesfileeditor / PropertiesFileEditor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2012 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  *     Brock Janiczak <brockj@tpg.com.au> - [nls tooling] Properties file editor should have "toggle comment" action - https://bugs.eclipse.org/bugs/show_bug.cgi?id=192045
11  *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.propertiesfileeditor;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import org.eclipse.swt.SWT;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.Job;
24
25 import org.eclipse.core.resources.IContainer;
26 import org.eclipse.core.resources.IFile;
27 import org.eclipse.core.resources.IStorage;
28
29 import org.eclipse.jface.action.IAction;
30 import org.eclipse.jface.action.IMenuManager;
31 import org.eclipse.jface.preference.IPreferenceStore;
32 import org.eclipse.jface.util.IPropertyChangeListener;
33 import org.eclipse.jface.util.PropertyChangeEvent;
34
35 import org.eclipse.jface.text.source.ISourceViewer;
36 import org.eclipse.jface.text.source.SourceViewerConfiguration;
37
38 import org.eclipse.ui.IEditorInput;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.part.IShowInTargetList;
41
42 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
43 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
44
45 import org.eclipse.ui.editors.text.EditorsUI;
46 import org.eclipse.ui.editors.text.ITextEditorHelpContextIds;
47 import org.eclipse.ui.editors.text.TextEditor;
48
49 import org.eclipse.jdt.core.ICompilationUnit;
50 import org.eclipse.jdt.core.IJavaElement;
51 import org.eclipse.jdt.core.IPackageFragment;
52 import org.eclipse.jdt.core.IType;
53 import org.eclipse.jdt.core.JavaCore;
54 import org.eclipse.jdt.core.JavaModelException;
55
56 import org.eclipse.jdt.ui.JavaUI;
57 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
58 import org.eclipse.jdt.ui.actions.JdtActionConstants;
59 import org.eclipse.jdt.ui.text.JavaTextTools;
60
61 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
62 import org.eclipse.jdt.internal.ui.JavaPlugin;
63 import org.eclipse.jdt.internal.ui.actions.FindBrokenNLSKeysAction;
64 import org.eclipse.jdt.internal.ui.javaeditor.ToggleCommentAction;
65
66
67 /**
68  * Properties file editor.
69  *
70  * @since 3.1
71  */
72 public class PropertiesFileEditor extends TextEditor {
73
74
75         /** Open action. */
76         protected OpenAction fOpenAction;
77
78         /**
79          * Property change listener on Editors UI store.
80          * @since 3.7
81          */
82         private IPropertyChangeListener fPropertyChangeListener;
83
84         private Map<IEditorInput, IType> fAccessorTypes= new HashMap<IEditorInput, IType>();
85
86         private Job fJob;
87
88         private IFile fFile;
89
90         /*
91          * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
92          * @since 3.4
93          */
94         @Override
95         protected void initializeEditor() {
96                 setDocumentProvider(JavaPlugin.getDefault().getPropertiesFileDocumentProvider());
97                 IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
98                 setPreferenceStore(store);
99                 JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools();
100                 setSourceViewerConfiguration(new PropertiesFileSourceViewerConfiguration(textTools.getColorManager(), store, this, IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING));
101                 setEditorContextMenuId("#TextEditorContext"); //$NON-NLS-1$
102                 setRulerContextMenuId("#TextRulerContext"); //$NON-NLS-1$
103                 setHelpContextId(ITextEditorHelpContextIds.TEXT_EDITOR);
104                 configureInsertMode(SMART_INSERT, false);
105                 setInsertMode(INSERT);
106
107                 // Need to listen on Editors UI preference store because JDT disables this functionality in its preferences.
108                 fPropertyChangeListener= new IPropertyChangeListener() {
109                         public void propertyChange(PropertyChangeEvent event) {
110                                 if (AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS.equals(event.getProperty()))
111                                         handlePreferenceStoreChanged(event);
112                         }
113                 };
114                 EditorsUI.getPreferenceStore().addPropertyChangeListener(fPropertyChangeListener);
115         }
116
117         @Override
118         protected void doSetInput(IEditorInput input) throws CoreException {
119                 super.doSetInput(input);
120                 if (fJob != null)
121                         fJob.cancel();
122
123                 fFile= (IFile) getEditorInput().getAdapter(IFile.class);
124                 if (fFile == null)
125                         return;
126
127                 if (fJob == null) {
128                         fJob= new Job(PropertiesFileEditorMessages.PropertiesFileEditor_find_accessor_type) {
129                                 @Override
130                                 protected IStatus run(IProgressMonitor monitor) {
131                                         try {
132                                                 fAccessorTypes.put(getEditorInput(), findAccessorType(monitor));
133                                         } catch (JavaModelException e) {
134                                                 JavaPlugin.log(e);
135                                         }
136                                         return Status.OK_STATUS;
137                                 }
138                         };
139                         fJob.setSystem(true);
140                 }
141                 fJob.schedule();
142         }
143
144         /*
145          * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#initializeKeyBindingScopes()
146          * @since 3.4
147          */
148         @Override
149         protected void initializeKeyBindingScopes() {
150                 setKeyBindingScopes(new String[] { "org.eclipse.jdt.ui.propertiesEditorScope" });  //$NON-NLS-1$
151         }
152
153         /*
154          * @see org.eclipse.ui.editors.text.TextEditor#createActions()
155          */
156         @Override
157         protected void createActions() {
158                 super.createActions();
159
160                 IAction action= new ToggleCommentAction(PropertiesFileEditorMessages.getBundleForConstructedKeys(), "ToggleComment.", this); //$NON-NLS-1$
161                 action.setActionDefinitionId(IJavaEditorActionDefinitionIds.TOGGLE_COMMENT);
162                 setAction(IJavaEditorActionDefinitionIds.TOGGLE_COMMENT, action);
163                 markAsStateDependentAction(IJavaEditorActionDefinitionIds.TOGGLE_COMMENT, true);
164                 PlatformUI.getWorkbench().getHelpSystem().setHelp(action, IJavaHelpContextIds.TOGGLE_COMMENT_ACTION);
165                 configureToggleCommentAction();
166
167                 fOpenAction= new OpenAction(this);
168                 fOpenAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EDITOR);
169                 setAction(JdtActionConstants.OPEN, fOpenAction);
170         }
171
172         /**
173          * Configures the toggle comment action.
174          *
175          * @since 3.4
176          */
177         private void configureToggleCommentAction() {
178                 IAction action= getAction(IJavaEditorActionDefinitionIds.TOGGLE_COMMENT);
179                 if (action instanceof ToggleCommentAction) {
180                         ISourceViewer sourceViewer= getSourceViewer();
181                         SourceViewerConfiguration configuration= getSourceViewerConfiguration();
182                         ((ToggleCommentAction)action).configure(sourceViewer, configuration);
183                 }
184         }
185
186         /*
187          * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
188          */
189         @Override
190         protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
191
192                 try {
193
194                         ISourceViewer sourceViewer= getSourceViewer();
195                         if (sourceViewer == null)
196                                 return;
197
198                         ((PropertiesFileSourceViewerConfiguration) getSourceViewerConfiguration()).handlePropertyChangeEvent(event);
199
200                 } finally {
201                         super.handlePreferenceStoreChanged(event);
202                 }
203         }
204
205         /*
206          * @see AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
207          */
208         @Override
209         protected boolean affectsTextPresentation(PropertyChangeEvent event) {
210                 return ((PropertiesFileSourceViewerConfiguration)getSourceViewerConfiguration()).affectsTextPresentation(event) || super.affectsTextPresentation(event);
211         }
212
213
214         /*
215          * @see org.eclipse.ui.editors.text.TextEditor#getAdapter(java.lang.Class)
216          */
217         @Override
218         public Object getAdapter(Class adapter) {
219                 if (adapter == IShowInTargetList.class) {
220                         return new IShowInTargetList() {
221                                 public String[] getShowInTargetIds() {
222                                         return new String[] { JavaUI.ID_PACKAGES, JavaPlugin.ID_RES_NAV };
223                                 }
224
225                         };
226                 }
227                 return super.getAdapter(adapter);
228         }
229
230         /*
231          * @see org.eclipse.ui.part.WorkbenchPart#getOrientation()
232          * @since 3.2
233          */
234         @Override
235         public int getOrientation() {
236                 return SWT.LEFT_TO_RIGHT;       // properties editors are always left to right by default (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=110986)
237         }
238
239         /*
240          * @see org.eclipse.ui.texteditor.StatusTextEditor#updateStatusField(java.lang.String)
241          */
242         @Override
243         protected void updateStatusField(String category) {
244                 super.updateStatusField(category);
245                 if (getEditorSite() != null) {
246                         getEditorSite().getActionBars().getStatusLineManager().setMessage(null);
247                         getEditorSite().getActionBars().getStatusLineManager().setErrorMessage(null);
248                 }
249         }
250
251         /*
252          * @see org.eclipse.ui.texteditor.AbstractTextEditor#getSourceViewer()
253          */
254         ISourceViewer internalGetSourceViewer() {
255                 return getSourceViewer();
256         }
257
258         /*
259          * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#collectContextMenuPreferencePages()
260          * @since 3.1
261          */
262         @Override
263         protected String[] collectContextMenuPreferencePages() {
264                 String[] ids= super.collectContextMenuPreferencePages();
265                 String[] more= new String[ids.length + 1];
266                 more[0]= "org.eclipse.jdt.ui.preferences.PropertiesFileEditorPreferencePage"; //$NON-NLS-1$
267                 System.arraycopy(ids, 0, more, 1, ids.length);
268                 return more;
269         }
270
271         /*
272          * @see org.eclipse.ui.editors.text.TextEditor#editorContextMenuAboutToShow(org.eclipse.jface.action.IMenuManager)
273          * @since 3.4
274          */
275         @Override
276         protected void editorContextMenuAboutToShow(IMenuManager menu) {
277                 super.editorContextMenuAboutToShow(menu);
278
279                 addAction(menu, ITextEditorActionConstants.GROUP_EDIT, IJavaEditorActionDefinitionIds.TOGGLE_COMMENT);
280         }
281
282         /*
283          * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isTabsToSpacesConversionEnabled()
284          * @since 3.7
285          */
286         @Override
287         protected boolean isTabsToSpacesConversionEnabled() {
288                 // Can't use our own preference store because JDT disables this functionality in its preferences.
289                 return EditorsUI.getPreferenceStore().getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS);
290         }
291
292         /*
293          * @see org.eclipse.ui.editors.text.TextEditor#dispose()
294          * @since 3.7
295          */
296         @Override
297         public void dispose() {
298                 EditorsUI.getPreferenceStore().removePropertyChangeListener(fPropertyChangeListener);
299                 if (fJob != null)
300                         fJob.cancel();
301                 super.dispose();
302         }
303
304         public IType getAccessorType() {
305                 return fAccessorTypes.get(getEditorInput());
306         }
307
308         private IType findAccessorType(IProgressMonitor pm) throws JavaModelException {
309                 IType accessorType= FindBrokenNLSKeysAction.getAccessorType(fFile);
310                 if (accessorType != null)
311                         return accessorType;
312                 if (pm != null && pm.isCanceled()) {
313                         return null;
314                 }
315
316                 IContainer parent= fFile.getParent();
317                 IJavaElement javaElement= JavaCore.create(parent);
318
319                 if (!(javaElement instanceof IPackageFragment))
320                         return null;
321
322                 ICompilationUnit[] compilationUnits= ((IPackageFragment) javaElement).getCompilationUnits();
323                 for (int i= 0; i < compilationUnits.length; i++) {
324                         if (evaluateCU(compilationUnits[i], fFile)) {
325                                 return compilationUnits[i].getTypes()[0];
326                         }
327                         if (pm != null && pm.isCanceled()) {
328                                 return null;
329                         }
330                 }
331                 return null;
332         }
333
334         private boolean evaluateCU(ICompilationUnit compilationUnit, IFile file) throws JavaModelException {
335                 IStorage bundle= FindBrokenNLSKeysAction.getResourceBundle(compilationUnit);
336                 if (!(bundle instanceof IFile))
337                         return false;
338
339                 return file.equals(bundle);
340         }
341 }