diff options
author | amywu | 2008-04-03 06:45:06 +0000 |
---|---|---|
committer | amywu | 2008-04-03 06:45:06 +0000 |
commit | 817060942beb6bfe4bb97b0fb3361457b23f5dee (patch) | |
tree | 0ed22384b96937005757cb9216443113833345b9 /bundles/org.eclipse.wst.css.ui/src | |
parent | b73bdcb4557a9db8959063baab106b1e2b63dab1 (diff) | |
download | webtools.sourceediting-817060942beb6bfe4bb97b0fb3361457b23f5dee.tar.gz webtools.sourceediting-817060942beb6bfe4bb97b0fb3361457b23f5dee.tar.xz webtools.sourceediting-817060942beb6bfe4bb97b0fb3361457b23f5dee.zip |
Diffstat (limited to 'bundles/org.eclipse.wst.css.ui/src')
4 files changed, 295 insertions, 0 deletions
diff --git a/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/handlers/CleanupDocumentHandler.java b/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/handlers/CleanupDocumentHandler.java new file mode 100644 index 0000000000..a623b3ee18 --- /dev/null +++ b/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/handlers/CleanupDocumentHandler.java @@ -0,0 +1,120 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.wst.css.ui.internal.handlers; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.IHandler; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.window.Window; +import org.eclipse.swt.custom.BusyIndicator; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.ui.texteditor.ITextEditor; +import org.eclipse.wst.css.core.internal.cleanup.CleanupProcessorCSS; +import org.eclipse.wst.css.ui.internal.edit.ui.CleanupDialogCSS; +import org.eclipse.wst.sse.core.StructuredModelManager; +import org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupProcessor; +import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; +import org.eclipse.wst.sse.ui.internal.SSEUIMessages; + +public class CleanupDocumentHandler extends AbstractHandler implements IHandler { + private IStructuredCleanupProcessor fCleanupProcessor; + + public void dispose() { + // nulling out just in case + fCleanupProcessor = null; + } + + public Object execute(ExecutionEvent event) throws ExecutionException { + IEditorPart editorPart = HandlerUtil.getActiveEditor(event); + ITextEditor textEditor = null; + if (editorPart instanceof ITextEditor) + textEditor = (ITextEditor) editorPart; + else { + Object o = editorPart.getAdapter(ITextEditor.class); + if (o != null) + textEditor = (ITextEditor) o; + } + + if (textEditor != null) { + final ITextEditor editor = textEditor; + Dialog cleanupDialog = new CleanupDialogCSS(editor.getSite().getShell()); + if (cleanupDialog.open() == Window.OK) { + // setup runnable + Runnable runnable = new Runnable() { + public void run() { + IStructuredCleanupProcessor cleanupProcessor = getCleanupProcessor(); + if (cleanupProcessor != null) { + IStructuredModel model = null; + try { + model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput())); + if (model != null) + cleanupProcessor.cleanupModel(model); + } + finally { + if (model != null) + model.releaseFromEdit(); + } + } + } + }; + + // TODO: make independent of 'model'. + IStructuredModel model = null; + try { + model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput())); + if (model != null) { + // begin recording + ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); + model.beginRecording(this, SSEUIMessages.Cleanup_Document_UI_, SSEUIMessages.Cleanup_Document_UI_, selection.getOffset(), selection.getLength()); //$NON-NLS-1$ //$NON-NLS-2$ + + // tell the model that we are about to make a big + // model change + model.aboutToChangeModel(); + + // run + BusyIndicator.showWhile(editor.getEditorSite().getWorkbenchWindow().getShell().getDisplay(), runnable); + } + } + finally { + if (model != null) { + // tell the model that we are done with the big + // model + // change + model.changedModel(); + + // end recording + ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); + model.endRecording(this, selection.getOffset(), selection.getLength()); + model.releaseFromEdit(); + } + } + } + } + return null; + } + + public void selectionChanged(IAction action, ISelection selection) { + // do nothing + } + + IStructuredCleanupProcessor getCleanupProcessor() { + if (fCleanupProcessor == null) + fCleanupProcessor = new CleanupProcessorCSS(); + + return fCleanupProcessor; + } +} diff --git a/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/handlers/StructuredSelectEnclosingHandler.java b/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/handlers/StructuredSelectEnclosingHandler.java new file mode 100644 index 0000000000..ac19982754 --- /dev/null +++ b/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/handlers/StructuredSelectEnclosingHandler.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.wst.css.ui.internal.handlers; + +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.text.Region; +import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode; +import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; +import org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredSelectHandler; + +public class StructuredSelectEnclosingHandler extends AbstractStructuredSelectHandler { + + protected IndexedRegion getCursorIndexedRegion(IDocument document, ITextSelection textSelection) { + IndexedRegion indexedRegion = null; + + indexedRegion = getIndexedRegion(document, textSelection.getOffset()); + + return indexedRegion; + } + + protected Region getNewSelectionRegion(IndexedRegion indexedRegion, ITextSelection textSelection) { + Region newRegion = null; + if (indexedRegion instanceof ICSSNode) { + ICSSNode cursorNode = (ICSSNode) indexedRegion; + Region cursorNodeRegion = new Region(indexedRegion.getStartOffset(), indexedRegion.getEndOffset() - indexedRegion.getStartOffset()); + int currentOffset = textSelection.getOffset(); + int currentEndOffset = currentOffset + textSelection.getLength(); + if (cursorNodeRegion.getOffset() >= currentOffset && cursorNodeRegion.getOffset() <= currentEndOffset && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() >= currentOffset && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() <= currentEndOffset) { + ICSSNode newNode = cursorNode.getParentNode(); + + if (newNode instanceof IndexedRegion) { + IndexedRegion newIndexedRegion = (IndexedRegion) newNode; + newRegion = new Region(newIndexedRegion.getStartOffset(), newIndexedRegion.getEndOffset() - newIndexedRegion.getStartOffset()); + } + } + else + newRegion = cursorNodeRegion; + } + return newRegion; + } +} diff --git a/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/handlers/StructuredSelectNextHandler.java b/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/handlers/StructuredSelectNextHandler.java new file mode 100644 index 0000000000..bfccb26a81 --- /dev/null +++ b/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/handlers/StructuredSelectNextHandler.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.wst.css.ui.internal.handlers; + +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.text.Region; +import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode; +import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; +import org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredSelectHandler; + +public class StructuredSelectNextHandler extends AbstractStructuredSelectHandler { + + protected IndexedRegion getCursorIndexedRegion(IDocument document, ITextSelection textSelection) { + int offset = textSelection.getOffset() + textSelection.getLength() - 1; + if (offset < 0) + offset = 0; + + IndexedRegion indexedRegion = null; + + indexedRegion = getIndexedRegion(document, offset); + + return indexedRegion; + } + + protected Region getNewSelectionRegion(IndexedRegion indexedRegion, ITextSelection textSelection) { + Region newRegion = null; + + if (indexedRegion instanceof ICSSNode) { + ICSSNode cursorNode = (ICSSNode) indexedRegion; + + Region cursorNodeRegion = new Region(indexedRegion.getStartOffset(), indexedRegion.getEndOffset() - indexedRegion.getStartOffset()); + int currentOffset = textSelection.getOffset(); + int currentEndOffset = currentOffset + textSelection.getLength(); + if (cursorNodeRegion.getOffset() >= currentOffset && cursorNodeRegion.getOffset() <= currentEndOffset && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() >= currentOffset && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() <= currentEndOffset) { + ICSSNode newNode = cursorNode.getNextSibling(); + if (newNode == null) { + newNode = cursorNode.getParentNode(); + + if (newNode instanceof IndexedRegion) { + IndexedRegion newIndexedRegion = (IndexedRegion) newNode; + newRegion = new Region(newIndexedRegion.getStartOffset(), newIndexedRegion.getEndOffset() - newIndexedRegion.getStartOffset()); + } + } + else { + if (newNode instanceof IndexedRegion) { + IndexedRegion newIndexedRegion = (IndexedRegion) newNode; + newRegion = new Region(currentOffset, newIndexedRegion.getEndOffset() - currentOffset); + } + } + } + else + newRegion = cursorNodeRegion; + } + return newRegion; + } +} diff --git a/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/handlers/StructuredSelectPreviousHandler.java b/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/handlers/StructuredSelectPreviousHandler.java new file mode 100644 index 0000000000..5827a4f664 --- /dev/null +++ b/bundles/org.eclipse.wst.css.ui/src/org/eclipse/wst/css/ui/internal/handlers/StructuredSelectPreviousHandler.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.wst.css.ui.internal.handlers; + +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.text.Region; +import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode; +import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; +import org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredSelectHandler; + +public class StructuredSelectPreviousHandler extends AbstractStructuredSelectHandler { + + protected IndexedRegion getCursorIndexedRegion(IDocument document, ITextSelection textSelection) { + IndexedRegion indexedRegion = null; + + indexedRegion = getIndexedRegion(document, textSelection.getOffset()); + + return indexedRegion; + } + + protected Region getNewSelectionRegion(IndexedRegion indexedRegion, ITextSelection textSelection) { + Region newRegion = null; + if (indexedRegion instanceof ICSSNode) { + ICSSNode cursorNode = (ICSSNode) indexedRegion; + + Region cursorNodeRegion = new Region(indexedRegion.getStartOffset(), indexedRegion.getEndOffset() - indexedRegion.getStartOffset()); + int currentOffset = textSelection.getOffset(); + int currentEndOffset = currentOffset + textSelection.getLength(); + if (cursorNodeRegion.getOffset() >= currentOffset && cursorNodeRegion.getOffset() <= currentEndOffset && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() >= currentOffset && cursorNodeRegion.getOffset() + cursorNodeRegion.getLength() <= currentEndOffset) { + ICSSNode newNode = cursorNode.getPreviousSibling(); + if (newNode == null) { + newNode = cursorNode.getParentNode(); + + if (newNode instanceof IndexedRegion) { + IndexedRegion newIndexedRegion = (IndexedRegion) newNode; + newRegion = new Region(newIndexedRegion.getStartOffset(), newIndexedRegion.getEndOffset() - newIndexedRegion.getStartOffset()); + } + } + else { + if (newNode instanceof IndexedRegion) { + IndexedRegion newIndexedRegion = (IndexedRegion) newNode; + newRegion = new Region(newIndexedRegion.getStartOffset(), currentEndOffset - newIndexedRegion.getStartOffset()); + } + } + } + else + newRegion = cursorNodeRegion; + } + return newRegion; + } +} |