2 * Copyright (c) 2003-2004 Christopher Lenz and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * Christopher Lenz - initial API and implementation
11 * $Id: CssOutlinePage.java,v 1.1 2004-09-02 18:11:49 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.outline;
16 import java.util.List;
18 import net.sourceforge.phpeclipse.core.model.ISourceReference;
19 import net.sourceforge.phpeclipse.css.core.model.IAtRule;
20 import net.sourceforge.phpeclipse.css.core.model.IStyleSheet;
21 import net.sourceforge.phpeclipse.css.ui.CssUI;
22 import net.sourceforge.phpeclipse.css.ui.internal.CssDocumentProvider;
23 import net.sourceforge.phpeclipse.css.ui.internal.CssUIMessages;
24 import net.sourceforge.phpeclipse.css.ui.internal.CssUIPreferences;
25 import net.sourceforge.phpeclipse.css.ui.internal.editor.CssEditor;
26 import net.sourceforge.phpeclipse.css.ui.internal.editor.ShowSelectedElementOnlyAction;
27 import net.sourceforge.phpeclipse.ui.views.outline.ProblemsLabelDecorator;
29 import org.eclipse.jface.action.IMenuManager;
30 import org.eclipse.jface.action.IStatusLineManager;
31 import org.eclipse.jface.action.IToolBarManager;
32 import org.eclipse.jface.viewers.DecoratingLabelProvider;
33 import org.eclipse.jface.viewers.ISelection;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.jface.viewers.StructuredSelection;
36 import org.eclipse.jface.viewers.TreeViewer;
37 import org.eclipse.jface.viewers.ViewerSorter;
38 import org.eclipse.swt.custom.BusyIndicator;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.ui.IActionBars;
42 import org.eclipse.ui.part.IPageSite;
43 import org.eclipse.ui.texteditor.IDocumentProvider;
44 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
45 import org.eclipse.ui.texteditor.ResourceAction;
46 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
49 * Implements the outline page associated with the CSS editor.
51 public class CssOutlinePage extends ContentOutlinePage {
53 // Inner Classes -----------------------------------------------------------
56 * Action that activates or deactivates the lexical sorting of elements in
57 * the outline tree viewer.
59 private class ToggleLexicalSortingAction extends ResourceAction {
61 // Constants -----------------------------------------------------------
63 private static final int CATEGORY_AT_RULE = 0;
64 private static final int CATEGORY_STYLE_RULE = 1;
66 // Instance Variables --------------------------------------------------
69 * The viewer sorter. At-rules are displayed before style rules,
70 * otherwise the default lexical sorting applies.
72 private final ViewerSorter sorter = new ViewerSorter() {
73 public int category(Object element) {
74 if (element instanceof IAtRule) {
75 return CATEGORY_AT_RULE;
77 return CATEGORY_STYLE_RULE;
82 // Constructors --------------------------------------------------------
87 public ToggleLexicalSortingAction() {
88 super(CssUIMessages.getResourceBundle(),
89 "CssOutlinePage.sort."); //$NON-NLS-1$
90 CssUI plugin = CssUI.getDefault();
91 boolean checked = plugin.getPreferenceStore().getBoolean(
92 CssUIPreferences.OUTLINE_SORT_LEXICALLY); //$NON-NLS-1$
93 valueChanged(checked, false);
96 // Action Implementation -----------------------------------------------
99 * @see org.eclipse.jface.action.Action#run()
102 valueChanged(isChecked(), true);
105 // Private Methods -----------------------------------------------------
108 * Updates the sorting of the outline.
110 * @param checked Whether lexical sorting is enabled
111 * @param store Whether the new state should be written back as a
114 private void valueChanged(final boolean checked, boolean store) {
116 BusyIndicator.showWhile(getTreeViewer().getControl().getDisplay(),
119 getTreeViewer().setSorter(checked ? sorter : null);
123 CssUI plugin = CssUI.getDefault();
124 plugin.getPreferenceStore().setValue(
125 CssUIPreferences.OUTLINE_SORT_LEXICALLY, checked);
132 * This action toggles whether this Java Outline page links its selection
133 * to the active editor.
135 private class ToggleLinkingAction extends ResourceAction {
138 * Constructs a new action.
140 public ToggleLinkingAction() {
141 super(CssUIMessages.getResourceBundle(),
142 "CssOutlinePage.linkWithEditor."); //$NON-NLS-1$
143 CssUI plugin = CssUI.getDefault();
144 boolean checked = plugin.getPreferenceStore().getBoolean(
145 CssUIPreferences.OUTLINE_LINK_WITH_EDITOR); //$NON-NLS-1$
146 valueChanged(checked, false);
150 * @see org.eclipse.jface.action.Action#run()
153 valueChanged(isChecked(), true);
156 // Private Methods -----------------------------------------------------
159 * Updates the sorting of the outline.
161 * @param checked Whether lexical sorting is enabled
162 * @param store Whether the new state should be written back as a
165 private void valueChanged(final boolean checked, boolean store) {
167 BusyIndicator.showWhile(getTreeViewer().getControl().getDisplay(),
170 editor.synchronizeOutlinePage();
174 CssUI plugin = CssUI.getDefault();
175 plugin.getPreferenceStore().setValue(
176 CssUIPreferences.OUTLINE_LINK_WITH_EDITOR, checked);
182 // Instance Variables ------------------------------------------------------
185 * The associated editor.
187 private CssEditor editor;
190 * Toolbar action for showing only the selected element in the editor.
192 private ShowSelectedElementOnlyAction showSelectedElementOnlyAction =
193 new ShowSelectedElementOnlyAction(); //$NON-NLS-1$
195 // Constructors ------------------------------------------------------------
200 * @param editor The associated text editor
202 public CssOutlinePage(CssEditor editor) {
203 this.editor = editor;
206 // ContentOutlinePage Implementation ---------------------------------------
209 * @see org.eclipse.ui.part.IPage#createControl(Composite)
211 public void createControl(Composite parent) {
212 super.createControl(parent);
213 TreeViewer viewer = getTreeViewer();
214 viewer.addDoubleClickListener(new CssOutlineDoubleClickListener(this));
215 viewer.setContentProvider(new CssOutlineContentProvider());
216 viewer.setLabelProvider(new DecoratingLabelProvider(
217 new CssOutlineLabelProvider(),
218 new ProblemsLabelDecorator(editor)));
219 viewer.setInput(getStyleSheet());
223 * @see org.eclipse.ui.part.IPage#dispose()
225 public void dispose() {
226 if (editor != null) {
227 editor.outlinePageClosed();
234 * @see org.eclipse.ui.part.IPageBookViewPage#init(org.eclipse.ui.part.IPageSite)
236 public void init(IPageSite pageSite) {
237 super.init(pageSite);
238 IActionBars bars = pageSite.getActionBars();
239 bars.setGlobalActionHandler(
240 ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY,
241 showSelectedElementOnlyAction);
242 showSelectedElementOnlyAction.setEditor(editor);
243 showSelectedElementOnlyAction.update();
247 * @see org.eclipse.ui.part.Page#makeContributions(IMenuManager, IToolBarManager, IStatusLineManager)
249 public void makeContributions(IMenuManager menuManager,
250 IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
251 if (toolBarManager != null) {
252 toolBarManager.add(new ToggleLexicalSortingAction());
253 toolBarManager.add(new ToggleLinkingAction());
255 super.makeContributions(menuManager, toolBarManager, statusLineManager);
258 // Public Methods ----------------------------------------------------------
261 * Selects a specific element in the outline page.
263 * @param element the element to select
265 public void select(ISourceReference element) {
266 TreeViewer viewer = getTreeViewer();
267 if (viewer != null) {
268 ISelection selection = viewer.getSelection();
269 if (selection instanceof IStructuredSelection) {
270 IStructuredSelection structuredSelection =
271 (IStructuredSelection) selection;
272 List elements = structuredSelection.toList();
273 if (!elements.contains(element)) {
274 if (element == null) {
275 selection = StructuredSelection.EMPTY;
277 selection = new StructuredSelection(element);
279 viewer.setSelection(selection, true);
286 * Updates the outline page.
288 public void update() {
289 IStyleSheet styleSheet = getStyleSheet();
290 if (styleSheet != null) {
291 TreeViewer viewer = getTreeViewer();
292 if (viewer != null) {
293 Control control = viewer.getControl();
294 if ((control != null) && !control.isDisposed()) {
295 control.setRedraw(false);
296 viewer.setInput(styleSheet);
298 control.setRedraw(true);
304 // Private Methods ---------------------------------------------------------
307 * Returns the parsed model of the style sheet that is loaded into the
310 * @return the parsed style sheet
312 private IStyleSheet getStyleSheet() {
313 IDocumentProvider provider = editor.getDocumentProvider();
314 if (provider instanceof CssDocumentProvider) {
315 CssDocumentProvider cssProvider = (CssDocumentProvider) provider;
316 return cssProvider.getStyleSheet(editor.getEditorInput());