Refactory
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / actions / PHPOpenDeclarationAction.java
1 /***********************************************************************************************************************************
2  * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This program and the accompanying materials are made
3  * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4  * http://www.eclipse.org/legal/cpl-v10.html
5  * 
6  * Contributors: www.phpeclipse.de
7  **********************************************************************************************************************************/
8 package net.sourceforge.phpeclipse.actions;
9
10 import net.sourceforge.phpdt.internal.ui.actions.ActionUtil;
11 import net.sourceforge.phpdt.internal.ui.actions.SelectionConverter;
12 import net.sourceforge.phpdt.ui.actions.SelectionDispatchAction;
13 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
14
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.ITextSelection;
18 import org.eclipse.jface.text.TextSelection;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.ui.IWorkbenchSite;
22 import org.eclipse.ui.IWorkbenchWindow;
23
24 public class PHPOpenDeclarationAction extends SelectionDispatchAction {
25         private IWorkbenchWindow fWindow;
26
27         private PHPEditor fEditor;
28
29         public void dispose() {
30         }
31
32         public PHPOpenDeclarationAction(IWorkbenchSite site) {
33                 super(site);
34                 // setText(ActionMessages.getString("OpenAction.label")); //$NON-NLS-1$
35                 // setToolTipText(ActionMessages.getString("OpenAction.tooltip"));
36                 // //$NON-NLS-1$
37                 // setDescription(ActionMessages.getString("OpenAction.description"));
38                 // //$NON-NLS-1$
39                 // WorkbenchHelp.setHelp(this, IJavaHelpContextIds.OPEN_ACTION);
40         }
41
42         /**
43          * Note: This constructor is for internal use only. Clients should not call
44          * this constructor.
45          */
46         public PHPOpenDeclarationAction(PHPEditor editor) {
47                 this(editor.getEditorSite());
48                 fEditor = editor;
49                 // setText(ActionMessages.getString("OpenAction.declaration.label"));
50                 // //$NON-NLS-1$
51                 setEnabled(SelectionConverter.canOperateOn(fEditor));
52         }
53
54         public void init(IWorkbenchWindow window) {
55                 this.fWindow = window;
56         }
57
58         public void selectionChanged(IAction action, ISelection selection) {
59                 if (!selection.isEmpty()) {
60                         if (selection instanceof TextSelection) {
61                                 action.setEnabled(true);
62                         } else if (fWindow.getActivePage() != null
63                                         && fWindow.getActivePage().getActivePart() != null) {
64                                 //
65                         }
66                 }
67         }
68
69         private boolean checkEnabled(IStructuredSelection selection) {
70                 if (selection.isEmpty())
71                         return false;
72                 return true;
73         }
74
75         /*
76          * (non-Javadoc) Method declared on SelectionDispatchAction.
77          */
78         public void run(ITextSelection selection) {
79                 if (!ActionUtil.isProcessable(getShell(), fEditor))
80                         return;
81                 OpenDeclarationEditorAction openAction = new OpenDeclarationEditorAction(
82                                 fEditor);
83                 openAction.openSelectedElement(selection);
84         }
85
86         /*
87          * (non-Javadoc) Method declared on SelectionDispatchAction.
88          */
89         public void run(IStructuredSelection selection) {
90                 if (!checkEnabled(selection))
91                         return;
92                 run(selection.toArray());
93         }
94
95         /**
96          * Note: this method is for internal use only. Clients should not call this
97          * method.
98          */
99         public void run(Object[] elements) {
100                 if (elements != null && elements.length > 0) {
101                         ITextSelection selection = (ITextSelection) fEditor
102                                         .getSelectionProvider().getSelection();
103                         IDocument doc = fEditor.getDocumentProvider().getDocument(
104                                         fEditor.getEditorInput());
105                         int pos = selection.getOffset();
106
107                         OpenDeclarationEditorAction openAction = new OpenDeclarationEditorAction(
108                                         fEditor);
109                         openAction.openSelectedPosition(doc, pos);
110                 }
111         }
112
113 }