001: /*******************************************************************************
002: * Copyright (c) 2000, 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: * Sebastian Davids <sdavids@gmx.de> - bug 145326 [typing] toUpperCase incorrect selection
011: *******************************************************************************/package org.eclipse.ui.texteditor;
012:
013: import java.util.ResourceBundle;
014:
015: import org.eclipse.swt.custom.StyledText;
016: import org.eclipse.swt.graphics.Point;
017:
018: import org.eclipse.jface.text.BadLocationException;
019: import org.eclipse.jface.text.IDocument;
020: import org.eclipse.jface.text.source.ISourceViewer;
021:
022: /**
023: * Action that converts the current selection to lower case or upper case.
024: * @since 3.0
025: */
026: public class CaseAction extends TextEditorAction implements IUpdate {
027:
028: /** <code>true</code> if this action converts to upper case, <code>false</code> otherwise. */
029: private boolean fToUpper;
030:
031: /**
032: * Creates and initializes the action for the given text editor.
033: * The action configures its visual representation from the given resource
034: * bundle.
035: *
036: * @param bundle the resource bundle
037: * @param prefix a prefix to be prepended to the various resource keys
038: * (described in <code>ResourceAction</code> constructor), or <code>null</code> if none
039: * @param editor the text editor
040: * @param toUpper <code>true</code> if this is an uppercase action, <code>false</code> otherwise.
041: *
042: * @see ResourceAction#ResourceAction(ResourceBundle, String)
043: */
044: public CaseAction(ResourceBundle bundle, String prefix,
045: AbstractTextEditor editor, boolean toUpper) {
046: super (bundle, prefix, editor);
047: fToUpper = toUpper;
048: update();
049: }
050:
051: /*
052: * @see org.eclipse.jface.action.IAction#run()
053: */
054: public void run() {
055: ITextEditor editor = getTextEditor();
056: if (editor == null)
057: return;
058:
059: if (!validateEditorInputState())
060: return;
061:
062: ISourceViewer viewer = ((AbstractTextEditor) editor)
063: .getSourceViewer();
064: if (viewer == null)
065: return;
066:
067: IDocument document = viewer.getDocument();
068: if (document == null)
069: return;
070:
071: StyledText st = viewer.getTextWidget();
072: if (st == null)
073: return;
074:
075: Point sel = viewer.getSelectedRange();
076: if (sel == null)
077: return;
078:
079: try {
080: // if the selection is empty, we select the word / string using the viewer's
081: // double-click strategy
082: if (sel.y == 0) {
083:
084: // TODO find a better way to do this although there are multiple partitionings on a single document
085:
086: // String partition= getContentType(viewer, document, sel.x);
087: // SourceViewerConfiguration svc= fEditor.getSourceViewerConfiguration(); // never null when viewer instantiated
088: // ITextDoubleClickStrategy dcs= svc.getDoubleClickStrategy(viewer, partition);
089: // if (dcs != null) {
090: // dcs.doubleClicked(viewer);
091: // sel= viewer.getSelectedRange();
092: // }
093:
094: if (sel.y == 0)
095: return; // if the selection is still empty, we're done
096: }
097:
098: String target = document.get(sel.x, sel.y);
099: String replacement = (fToUpper ? target.toUpperCase()
100: : target.toLowerCase());
101: if (!target.equals(replacement)) {
102: document.replace(sel.x, target.length(), replacement);
103: // https://bugs.eclipse.org/bugs/show_bug.cgi?id=145326: replacement might be larger than the original
104: int adjustment = replacement.length() - target.length();
105: if (adjustment > 0)
106: sel.y += adjustment;
107: }
108: } catch (BadLocationException x) {
109: // ignore and return
110: return;
111: }
112:
113: // reinstall selection and move it into view
114: viewer.setSelectedRange(sel.x, sel.y);
115: // don't use the viewer's reveal feature in order to avoid jumping around
116: st.showSelection();
117: }
118:
119: }
|