intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / outline / CssOutlinePage.java
1 /*
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
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: CssOutlinePage.java,v 1.1 2004-09-02 18:11:49 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.outline;
15
16 import java.util.List;
17
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;
28
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;
47
48 /**
49  * Implements the outline page associated with the CSS editor.
50  */
51 public class CssOutlinePage extends ContentOutlinePage {
52
53         // Inner Classes -----------------------------------------------------------
54
55         /**
56          * Action that activates or deactivates the lexical sorting of elements in
57          * the outline tree viewer.
58          */
59         private class ToggleLexicalSortingAction extends ResourceAction {
60
61                 // Constants -----------------------------------------------------------
62
63                 private static final int CATEGORY_AT_RULE = 0;
64                 private static final int CATEGORY_STYLE_RULE = 1;
65
66                 // Instance Variables --------------------------------------------------
67
68                 /**
69                  * The viewer sorter. At-rules are displayed before style rules,
70                  * otherwise the default lexical sorting applies.
71                  */
72                 private final ViewerSorter sorter = new ViewerSorter() {
73                         public int category(Object element) {
74                                 if (element instanceof IAtRule) {
75                                         return CATEGORY_AT_RULE;
76                                 } else {
77                                         return CATEGORY_STYLE_RULE;
78                                 }
79                         }
80                 };
81
82                 // Constructors --------------------------------------------------------
83
84                 /**
85                  * Constructor.
86                  */
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);
94                 }
95
96                 // Action Implementation -----------------------------------------------
97
98                 /*
99                  * @see org.eclipse.jface.action.Action#run()
100                  */
101                 public void run() {
102                         valueChanged(isChecked(), true);
103                 }
104
105                 // Private Methods -----------------------------------------------------
106
107                 /**
108                  * Updates the sorting of the outline.
109                  * 
110                  * @param checked Whether lexical sorting is enabled
111                  * @param store Whether the new state should be written back as a 
112                  *        preference
113                  */
114                 private void valueChanged(final boolean checked, boolean store) {
115                         setChecked(checked);
116                         BusyIndicator.showWhile(getTreeViewer().getControl().getDisplay(),
117                                 new Runnable() {
118                                         public void run() {
119                                                 getTreeViewer().setSorter(checked ? sorter : null);
120                                         }
121                                 });
122                         if (store) {
123                                 CssUI plugin = CssUI.getDefault();
124                                 plugin.getPreferenceStore().setValue(
125                                         CssUIPreferences.OUTLINE_SORT_LEXICALLY, checked);
126                         }
127                 }
128
129         }
130
131         /**
132          * This action toggles whether this Java Outline page links its selection
133          * to the active editor.
134          */
135         private class ToggleLinkingAction extends ResourceAction {
136                 
137                 /**
138                  * Constructs a new action.
139                  */
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);
147                 }
148
149                 /*
150                  * @see org.eclipse.jface.action.Action#run()
151                  */
152                 public void run() {
153                         valueChanged(isChecked(), true);
154                 }
155         
156                 // Private Methods -----------------------------------------------------
157
158                 /**
159                  * Updates the sorting of the outline.
160                  * 
161                  * @param checked Whether lexical sorting is enabled
162                  * @param store Whether the new state should be written back as a 
163                  *        preference
164                  */
165                 private void valueChanged(final boolean checked, boolean store) {
166                         setChecked(checked);
167                         BusyIndicator.showWhile(getTreeViewer().getControl().getDisplay(),
168                                 new Runnable() {
169                                         public void run() {
170                                                 editor.synchronizeOutlinePage();
171                                         }
172                                 });
173                         if (store) {
174                                 CssUI plugin = CssUI.getDefault();
175                                 plugin.getPreferenceStore().setValue(
176                                         CssUIPreferences.OUTLINE_LINK_WITH_EDITOR, checked);
177                         }
178                 }
179
180         }
181
182         // Instance Variables ------------------------------------------------------
183
184         /**
185          * The associated editor.
186          */
187         private CssEditor editor;
188
189         /**
190          * Toolbar action for showing only the selected element in the editor.
191          */
192         private ShowSelectedElementOnlyAction showSelectedElementOnlyAction = 
193                 new ShowSelectedElementOnlyAction(); //$NON-NLS-1$
194                 
195         // Constructors ------------------------------------------------------------
196
197         /**
198          * Constructor.
199          * 
200          * @param editor The associated text editor
201          */
202         public CssOutlinePage(CssEditor editor) {
203                 this.editor = editor;
204         }
205
206         // ContentOutlinePage Implementation ---------------------------------------
207
208         /*
209          * @see org.eclipse.ui.part.IPage#createControl(Composite)
210          */
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());
220         }
221
222         /*
223          * @see org.eclipse.ui.part.IPage#dispose()
224          */
225         public void dispose() {
226                 if (editor != null) {
227                         editor.outlinePageClosed();
228                         editor = null;
229                 }
230                 super.dispose();
231         }
232
233         /*
234          * @see org.eclipse.ui.part.IPageBookViewPage#init(org.eclipse.ui.part.IPageSite)
235          */
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();
244         }
245
246         /*
247          * @see org.eclipse.ui.part.Page#makeContributions(IMenuManager, IToolBarManager, IStatusLineManager)
248          */
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());
254                 }
255                 super.makeContributions(menuManager, toolBarManager, statusLineManager);
256         }
257
258         // Public Methods ----------------------------------------------------------
259
260         /**
261          * Selects a specific element in the outline page.
262          * 
263          * @param element the element to select
264          */
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;
276                                         } else {
277                                                 selection = new StructuredSelection(element);
278                                         }
279                                         viewer.setSelection(selection, true);
280                                 }
281                         }
282                 }
283         }
284
285         /**
286          * Updates the outline page.
287          */
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);
297                                         viewer.expandAll();
298                                         control.setRedraw(true);
299                                 }
300                         }
301                 }
302         }
303
304         // Private Methods ---------------------------------------------------------
305
306         /**
307          * Returns the parsed model of the style sheet that is loaded into the 
308          * associated editor.
309          * 
310          * @return the parsed style sheet
311          */
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());
317                 }
318                 return null;
319         }
320
321 }