001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.examples.navigator.actions;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.jface.action.Action;
014: import org.eclipse.jface.dialogs.MessageDialog;
015: import org.eclipse.jface.text.BadLocationException;
016: import org.eclipse.jface.text.FindReplaceDocumentAdapter;
017: import org.eclipse.jface.text.IDocument;
018: import org.eclipse.jface.text.IRegion;
019: import org.eclipse.jface.viewers.ISelection;
020: import org.eclipse.jface.viewers.ISelectionProvider;
021: import org.eclipse.jface.viewers.IStructuredSelection;
022: import org.eclipse.swt.widgets.Display;
023: import org.eclipse.ui.IEditorPart;
024: import org.eclipse.ui.IWorkbenchPage;
025: import org.eclipse.ui.PartInitException;
026: import org.eclipse.ui.examples.navigator.PropertiesTreeData;
027: import org.eclipse.ui.ide.IDE;
028: import org.eclipse.ui.internal.examples.navigator.Activator;
029: import org.eclipse.ui.texteditor.IDocumentProvider;
030: import org.eclipse.ui.texteditor.ITextEditor;
031:
032: /**
033: * @since 3.2
034: *
035: */
036: public class OpenPropertyAction extends Action {
037:
038: private IWorkbenchPage page;
039: private PropertiesTreeData data;
040: private ISelectionProvider provider;
041:
042: /**
043: * Construct the OpenPropertyAction with the given page.
044: * @param p The page to use as context to open the editor.
045: * @param selectionProvider The selection provider
046: */
047: public OpenPropertyAction(IWorkbenchPage p,
048: ISelectionProvider selectionProvider) {
049: setText("Open Property"); //$NON-NLS-1$
050: page = p;
051: provider = selectionProvider;
052: }
053:
054: /* (non-Javadoc)
055: * @see org.eclipse.jface.action.Action#isEnabled()
056: */
057: public boolean isEnabled() {
058: ISelection selection = provider.getSelection();
059: if (!selection.isEmpty()) {
060: IStructuredSelection sSelection = (IStructuredSelection) selection;
061: if (sSelection.size() == 1
062: && sSelection.getFirstElement() instanceof PropertiesTreeData) {
063: data = ((PropertiesTreeData) sSelection
064: .getFirstElement());
065: return true;
066: }
067: }
068: return false;
069: }
070:
071: /* (non-Javadoc)
072: * @see org.eclipse.jface.action.Action#run()
073: */
074: public void run() {
075: /* In production code, you should always externalize strings,
076: * but this is an example. */
077: try {
078: if (isEnabled()) {
079: IFile propertiesFile = data.getFile();
080: IEditorPart editor = IDE.openEditor(page,
081: propertiesFile);
082:
083: if (editor instanceof ITextEditor) {
084: ITextEditor textEditor = (ITextEditor) editor;
085:
086: IDocumentProvider documentProvider = textEditor
087: .getDocumentProvider();
088: IDocument document = documentProvider
089: .getDocument(editor.getEditorInput());
090:
091: FindReplaceDocumentAdapter searchAdapter = new FindReplaceDocumentAdapter(
092: document);
093:
094: try {
095: String searchText = data.getName() + "="; //$NON-NLS-1$
096: IRegion region = searchAdapter
097: .find(0, searchText,
098: true /* forwardSearch */,
099: true /* caseSensitive */,
100: false /* wholeWord */, false /* regExSearch */);
101:
102: ((ITextEditor) editor).selectAndReveal(region
103: .getOffset(), region.getLength());
104:
105: } catch (BadLocationException e) {
106: Activator.logError(0,
107: "Could not open property!", e); //$NON-NLS-1$
108: MessageDialog.openError(Display.getDefault()
109: .getActiveShell(),
110: "Error Opening Property", //$NON-NLS-1$
111: "Could not open property!"); //$NON-NLS-1$
112: }
113: return;
114: }
115: }
116: } catch (PartInitException e) {
117: Activator.logError(0, "Could not open property!", e); //$NON-NLS-1$
118: MessageDialog.openError(Display.getDefault()
119: .getActiveShell(), "Error Opening Property", //$NON-NLS-1$
120: "Could not open property!"); //$NON-NLS-1$
121: }
122: }
123: }
|