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 / CssOutlineDoubleClickListener.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: CssOutlineDoubleClickListener.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 net.sourceforge.phpeclipse.css.core.model.IStyleRule;
17 import net.sourceforge.phpeclipse.css.ui.CssUI;
18
19 import org.eclipse.jface.viewers.DoubleClickEvent;
20 import org.eclipse.jface.viewers.IDoubleClickListener;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.ui.IPageLayout;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.PartInitException;
26 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
27
28 /**
29  * A double click listener that is intended to be attached to an outline page
30  * displaying CSS elements. If a style rule is double clicked, it will attempt
31  * to show the standard properties view.
32  */
33 public class CssOutlineDoubleClickListener implements IDoubleClickListener {
34
35         // Instance Variables ------------------------------------------------------
36
37         /**
38          * The associated content outline page.
39          */
40         private ContentOutlinePage outlinePage;
41
42         // Constructors ------------------------------------------------------------
43
44         /**
45          * Constructor.
46          * 
47          * @param outlinePage the associated outline page
48          */
49         public CssOutlineDoubleClickListener(ContentOutlinePage outlinePage) {
50                 this.outlinePage = outlinePage;
51         }
52
53         // IDoubleClickListener Implementation -------------------------------------
54
55         /*
56          * @see IDoubleClickListener#doubleClick(DoubleClickEvent)
57          */
58         public void doubleClick(DoubleClickEvent event) {
59                 Object selectedElement = getSelectedElement(event.getSelection());
60                 if (selectedElement instanceof IStyleRule) {
61                         showPropertiesView();
62                 }
63         }
64
65         // Private Methods ---------------------------------------------------------
66
67         /**
68          * Shows the properties view in the current workbench window.
69          */
70         private void showPropertiesView() {
71                 IWorkbenchPage workbenchPage = outlinePage.getSite().getPage();
72                 try {
73                         workbenchPage.showView(IPageLayout.ID_PROP_SHEET);
74                 } catch (PartInitException e) {
75                         CssUI.log(
76                                 "Could not show properties view", e); //$NON-NLS-1$
77                 }
78         }
79
80         /**
81          * Finds the element that is selected. As this is a double click listener,
82          * only a single element can be selected at once.
83          * 
84          * @param selection the selection
85          * @return the selected element
86          */
87         private Object getSelectedElement(ISelection selection) {
88                 Object element = null;
89                 if (selection instanceof IStructuredSelection) {
90                         element = ((IStructuredSelection) selection).getFirstElement();
91                 }
92                 return element;
93         }
94
95 }