KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > CaseAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Sebastian Davids <sdavids@gmx.de> - bug 145326 [typing] toUpperCase incorrect selection
11  *******************************************************************************/

12 package org.eclipse.ui.texteditor;
13
14 import java.util.ResourceBundle JavaDoc;
15
16 import org.eclipse.swt.custom.StyledText;
17 import org.eclipse.swt.graphics.Point;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.source.ISourceViewer;
22
23 /**
24  * Action that converts the current selection to lower case or upper case.
25  * @since 3.0
26  */

27 public class CaseAction extends TextEditorAction implements IUpdate {
28
29     /** <code>true</code> if this action converts to upper case, <code>false</code> otherwise. */
30     private boolean fToUpper;
31
32     /**
33      * Creates and initializes the action for the given text editor.
34      * The action configures its visual representation from the given resource
35      * bundle.
36      *
37      * @param bundle the resource bundle
38      * @param prefix a prefix to be prepended to the various resource keys
39      * (described in <code>ResourceAction</code> constructor), or <code>null</code> if none
40      * @param editor the text editor
41      * @param toUpper <code>true</code> if this is an uppercase action, <code>false</code> otherwise.
42      *
43      * @see ResourceAction#ResourceAction(ResourceBundle, String)
44      */

45     public CaseAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, AbstractTextEditor editor, boolean toUpper) {
46         super(bundle, prefix, editor);
47         fToUpper= toUpper;
48         update();
49     }
50
51     /*
52      * @see org.eclipse.jface.action.IAction#run()
53      */

54     public void run() {
55         ITextEditor editor= getTextEditor();
56         if (editor == null)
57             return;
58
59         if (!validateEditorInputState())
60             return;
61
62         ISourceViewer viewer= ((AbstractTextEditor) editor).getSourceViewer();
63         if (viewer == null)
64             return;
65
66         IDocument document= viewer.getDocument();
67         if (document == null)
68             return;
69
70         StyledText st= viewer.getTextWidget();
71         if (st == null)
72             return;
73
74         Point sel= viewer.getSelectedRange();
75         if (sel == null)
76             return;
77
78         try {
79             // if the selection is empty, we select the word / string using the viewer's
80
// double-click strategy
81
if (sel.y == 0) {
82
83                 // TODO find a better way to do this although there are multiple partitionings on a single document
84

85 // String partition= getContentType(viewer, document, sel.x);
86
// SourceViewerConfiguration svc= fEditor.getSourceViewerConfiguration(); // never null when viewer instantiated
87
// ITextDoubleClickStrategy dcs= svc.getDoubleClickStrategy(viewer, partition);
88
// if (dcs != null) {
89
// dcs.doubleClicked(viewer);
90
// sel= viewer.getSelectedRange();
91
// }
92

93                 if (sel.y == 0)
94                     return; // if the selection is still empty, we're done
95
}
96
97             String JavaDoc target= document.get(sel.x, sel.y);
98             String JavaDoc replacement= (fToUpper ? target.toUpperCase() : target.toLowerCase());
99             if (!target.equals(replacement)) {
100                 document.replace(sel.x, target.length(), replacement);
101                 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=145326: replacement might be larger than the original
102
int adjustment= replacement.length() - target.length();
103                 if (adjustment > 0)
104                     sel.y += adjustment;
105             }
106         } catch (BadLocationException x) {
107             // ignore and return
108
return;
109         }
110
111         // reinstall selection and move it into view
112
viewer.setSelectedRange(sel.x, sel.y);
113         // don't use the viewer's reveal feature in order to avoid jumping around
114
st.showSelection();
115     }
116
117 }
118
Popular Tags