/*********************************************************************************************************************************** * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This program and the accompanying materials are made * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: www.phpeclipse.de **********************************************************************************************************************************/ package net.sourceforge.phpeclipse.actions; import net.sourceforge.phpdt.core.ICodeFormatter; import net.sourceforge.phpdt.core.ToolFactory; import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility; import net.sourceforge.phpdt.internal.corext.util.Strings; import net.sourceforge.phpdt.internal.ui.preferences.CodeFormatterPreferencePage; import net.sourceforge.phpeclipse.phpeditor.PHPEditor; import org.eclipse.jface.action.IAction; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.TextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.ui.IEditorActionDelegate; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkbenchWindowActionDelegate; import org.eclipse.ui.actions.ActionDelegate; public class PHPFormatAction extends ActionDelegate implements IWorkbenchWindowActionDelegate, IEditorActionDelegate { private IWorkbenchWindow fWindow; private PHPEditor fEditor; public void init(IWorkbenchWindow window) { this.fWindow = window; } public void selectionChanged(IAction action, ISelection selection) { if (!selection.isEmpty()) { if (selection instanceof TextSelection) { action.setEnabled(true); } else if (fWindow.getActivePage() != null && fWindow.getActivePage().getActivePart() != null) { // } } } public void run(IAction action) { if (fEditor == null) { IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor(); if (targetEditor != null && (targetEditor instanceof PHPEditor)) { fEditor = (PHPEditor) targetEditor; } } if (fEditor != null) { ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection(); format(selection); } } public void setActiveEditor(IAction action, IEditorPart targetEditor) { if (targetEditor != null && (targetEditor instanceof PHPEditor)) { fEditor = (PHPEditor) targetEditor; } } public void format(ITextSelection selection) { try { IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); if (doc != null) { // int pos = selection.getOffset(); String content; if (selection != null && selection.getLength() > 0) { content = doc.get(selection.getOffset(), selection.getLength()); } else { content = doc.get(0, doc.getLength()); } if (content != null && content.length() > 0) { ICodeFormatter formatter = ToolFactory.createCodeFormatter(); String lineDelimiter = StubUtility.getLineDelimiterFor(doc); int indent = Strings.computeIndent(content, CodeFormatterPreferencePage.getTabSize()); String formatted = formatter.format(content, indent, null, lineDelimiter); if (!content.equals(formatted)) { if (selection != null && selection.getLength() > 0) { doc.replace(selection.getOffset(), selection.getLength(), formatted); } else { doc.replace(0, doc.getLength(), formatted); } } } } } catch (BadLocationException e) { e.printStackTrace(); } } }