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
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;
14 import java.util.HashMap;
17 import org.eclipse.swt.SWT;
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;
25 import org.eclipse.core.resources.IContainer;
26 import org.eclipse.core.resources.IFile;
27 import org.eclipse.core.resources.IStorage;
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;
35 import org.eclipse.jface.text.source.ISourceViewer;
36 import org.eclipse.jface.text.source.SourceViewerConfiguration;
38 import org.eclipse.ui.IEditorInput;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.part.IShowInTargetList;
42 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
43 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
45 import org.eclipse.ui.editors.text.EditorsUI;
46 import org.eclipse.ui.editors.text.ITextEditorHelpContextIds;
47 import org.eclipse.ui.editors.text.TextEditor;
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;
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;
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;
68 * Properties file editor.
72 public class PropertiesFileEditor extends TextEditor {
76 protected OpenAction fOpenAction;
79 * Property change listener on Editors UI store.
82 private IPropertyChangeListener fPropertyChangeListener;
84 private Map<IEditorInput, IType> fAccessorTypes= new HashMap<IEditorInput, IType>();
91 * @see org.eclipse.ui.editors.text.TextEditor#initializeEditor()
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);
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);
114 EditorsUI.getPreferenceStore().addPropertyChangeListener(fPropertyChangeListener);
118 protected void doSetInput(IEditorInput input) throws CoreException {
119 super.doSetInput(input);
123 fFile= (IFile) getEditorInput().getAdapter(IFile.class);
128 fJob= new Job(PropertiesFileEditorMessages.PropertiesFileEditor_find_accessor_type) {
130 protected IStatus run(IProgressMonitor monitor) {
132 fAccessorTypes.put(getEditorInput(), findAccessorType(monitor));
133 } catch (JavaModelException e) {
136 return Status.OK_STATUS;
139 fJob.setSystem(true);
145 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#initializeKeyBindingScopes()
149 protected void initializeKeyBindingScopes() {
150 setKeyBindingScopes(new String[] { "org.eclipse.jdt.ui.propertiesEditorScope" }); //$NON-NLS-1$
154 * @see org.eclipse.ui.editors.text.TextEditor#createActions()
157 protected void createActions() {
158 super.createActions();
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();
167 fOpenAction= new OpenAction(this);
168 fOpenAction.setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EDITOR);
169 setAction(JdtActionConstants.OPEN, fOpenAction);
173 * Configures the toggle comment action.
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);
187 * @see AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
190 protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
194 ISourceViewer sourceViewer= getSourceViewer();
195 if (sourceViewer == null)
198 ((PropertiesFileSourceViewerConfiguration) getSourceViewerConfiguration()).handlePropertyChangeEvent(event);
201 super.handlePreferenceStoreChanged(event);
206 * @see AbstractTextEditor#affectsTextPresentation(PropertyChangeEvent)
209 protected boolean affectsTextPresentation(PropertyChangeEvent event) {
210 return ((PropertiesFileSourceViewerConfiguration)getSourceViewerConfiguration()).affectsTextPresentation(event) || super.affectsTextPresentation(event);
215 * @see org.eclipse.ui.editors.text.TextEditor#getAdapter(java.lang.Class)
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 };
227 return super.getAdapter(adapter);
231 * @see org.eclipse.ui.part.WorkbenchPart#getOrientation()
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)
240 * @see org.eclipse.ui.texteditor.StatusTextEditor#updateStatusField(java.lang.String)
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);
252 * @see org.eclipse.ui.texteditor.AbstractTextEditor#getSourceViewer()
254 ISourceViewer internalGetSourceViewer() {
255 return getSourceViewer();
259 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#collectContextMenuPreferencePages()
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);
272 * @see org.eclipse.ui.editors.text.TextEditor#editorContextMenuAboutToShow(org.eclipse.jface.action.IMenuManager)
276 protected void editorContextMenuAboutToShow(IMenuManager menu) {
277 super.editorContextMenuAboutToShow(menu);
279 addAction(menu, ITextEditorActionConstants.GROUP_EDIT, IJavaEditorActionDefinitionIds.TOGGLE_COMMENT);
283 * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#isTabsToSpacesConversionEnabled()
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);
293 * @see org.eclipse.ui.editors.text.TextEditor#dispose()
297 public void dispose() {
298 EditorsUI.getPreferenceStore().removePropertyChangeListener(fPropertyChangeListener);
304 public IType getAccessorType() {
305 return fAccessorTypes.get(getEditorInput());
308 private IType findAccessorType(IProgressMonitor pm) throws JavaModelException {
309 IType accessorType= FindBrokenNLSKeysAction.getAccessorType(fFile);
310 if (accessorType != null)
312 if (pm != null && pm.isCanceled()) {
316 IContainer parent= fFile.getParent();
317 IJavaElement javaElement= JavaCore.create(parent);
319 if (!(javaElement instanceof IPackageFragment))
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];
327 if (pm != null && pm.isCanceled()) {
334 private boolean evaluateCU(ICompilationUnit compilationUnit, IFile file) throws JavaModelException {
335 IStorage bundle= FindBrokenNLSKeysAction.getResourceBundle(compilationUnit);
336 if (!(bundle instanceof IFile))
339 return file.equals(bundle);