View | Details | Raw Unified | Return to bug 125825 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/ui/texteditor/ITextEditorActionDefinitionIds.java (+34 lines)
Lines 135-140 Link Here
135
	 */
135
	 */
136
	String LOWER_CASE= "org.eclipse.ui.edit.text.lowerCase"; //$NON-NLS-1$
136
	String LOWER_CASE= "org.eclipse.ui.edit.text.lowerCase"; //$NON-NLS-1$
137
137
138
	/**
139
	 * Action definition id of the capitalize case action.
140
	 * Value: <code>"org.eclipse.ui.edit.text.capitalizeCase"</code>
141
	 * @since 3.3
142
	 */
143
	String CAPITALIZE_CASE= "org.eclipse.ui.edit.text.capitalizeCase"; //$NON-NLS-1$
144
145
	/**
146
	 * Action definition id of the invert case action.
147
	 * Value: <code>"org.eclipse.ui.edit.text.invertCase"</code>
148
	 * @since 3.3
149
	 */
150
	String INVERT_CASE= "org.eclipse.ui.edit.text.invertCase"; //$NON-NLS-1$
151
	
152
	/**
153
	 * Action definition id of the upper camel case action.
154
	 * Value: <code>"org.eclipse.ui.edit.text.upperCamelCase"</code>
155
	 * @since 3.3
156
	 */
157
	String UPPER_CAMEL_CASE= "org.eclipse.ui.edit.text.upperCamelCase"; //$NON-NLS-1$
158
159
	/**
160
	 * Action definition id of the lower camel case action.
161
	 * Value: <code>"org.eclipse.ui.edit.text.lowerCamelCase"</code>
162
	 * @since 3.3
163
	 */
164
	String LOWER_CAMEL_CASE= "org.eclipse.ui.edit.text.lowerCamelCase"; //$NON-NLS-1$
165
	
166
	/**
167
	 * Action definition id of the constant case action.
168
	 * Value: <code>"org.eclipse.ui.edit.text.constantCase"</code>
169
	 * @since 3.3
170
	 */
171
	String CONSTANT_CASE= "org.eclipse.ui.edit.text.constantCase"; //$NON-NLS-1$
138
172
139
	// navigation
173
	// navigation
140
174
(-)src/org/eclipse/ui/texteditor/IAbstractTextEditorHelpContextIds.java (+35 lines)
Lines 306-311 Link Here
306
306
307
	/**
307
	/**
308
	 * Help context id for the action.
308
	 * Help context id for the action.
309
	 * Value: <code>"org.eclipse.ui.capitalize_case_action_context"</code>
310
	 * @since 3.3
311
	 */
312
	String CAPITALIZE_CASE_ACTION= PREFIX + "capitalize_case" + ACTION_POSTFIX; //$NON-NLS-1$
313
314
	/**
315
	 * Help context id for the action.
316
	 * Value: <code>"org.eclipse.ui.invert_case_action_context"</code>
317
	 * @since 3.3
318
	 */
319
	String INVERT_CASE_ACTION= PREFIX + "invert_case" + ACTION_POSTFIX; //$NON-NLS-1$
320
	
321
	/**
322
	 * Help context id for the action.
323
	 * Value: <code>"org.eclipse.ui.upper_camel_case_action_context"</code>
324
	 * @since 3.3
325
	 */
326
	String UPPER_CAMEL_CASE_ACTION= PREFIX + "upper_camel_case" + ACTION_POSTFIX; //$NON-NLS-1$
327
328
	/**
329
	 * Help context id for the action.
330
	 * Value: <code>"org.eclipse.ui.lower_camel_case_action_context"</code>
331
	 * @since 3.3
332
	 */
333
	String LOWER_CAMEL_CASE_ACTION= PREFIX + "lower_camel_case" + ACTION_POSTFIX; //$NON-NLS-1$
334
	
335
	/**
336
	 * Help context id for the action.
337
	 * Value: <code>"org.eclipse.ui.constant_case_action_context"</code>
338
	 * @since 3.3
339
	 */
340
	String CONSTANT_CASE_ACTION= PREFIX + "constant_case" + ACTION_POSTFIX; //$NON-NLS-1$
341
	
342
	/**
343
	 * Help context id for the action.
309
	 * Value: <code>"org.eclipse.ui.smart_enter_action_context"</code>
344
	 * Value: <code>"org.eclipse.ui.smart_enter_action_context"</code>
310
	 * @since 3.0
345
	 * @since 3.0
311
	 */
346
	 */
(-)src/org/eclipse/ui/texteditor/AbstractTextEditor.java (+31 lines)
Lines 10-15 Link Here
10
 *     Chris.Dennis@invidi.com - http://bugs.eclipse.org/bugs/show_bug.cgi?id=29027
10
 *     Chris.Dennis@invidi.com - http://bugs.eclipse.org/bugs/show_bug.cgi?id=29027
11
 *     Michel Ishizuka (cqw10305@nifty.com) - http://bugs.eclipse.org/bugs/show_bug.cgi?id=68963
11
 *     Michel Ishizuka (cqw10305@nifty.com) - http://bugs.eclipse.org/bugs/show_bug.cgi?id=68963
12
 *     Genady Beryozkin, me@genady.org - https://bugs.eclipse.org/bugs/show_bug.cgi?id=11668
12
 *     Genady Beryozkin, me@genady.org - https://bugs.eclipse.org/bugs/show_bug.cgi?id=11668
13
 *     Sebastian Davids, sdavids@gmx.de - http://bugs.eclipse.org/bugs/show_bug.cgi?id=125825
13
 *******************************************************************************/
14
 *******************************************************************************/
14
package org.eclipse.ui.texteditor;
15
package org.eclipse.ui.texteditor;
15
16
Lines 4546-4551 Link Here
4546
		action.setActionDefinitionId(ITextEditorActionDefinitionIds.LOWER_CASE);
4547
		action.setActionDefinitionId(ITextEditorActionDefinitionIds.LOWER_CASE);
4547
		setAction(ITextEditorActionConstants.LOWER_CASE, action);
4548
		setAction(ITextEditorActionConstants.LOWER_CASE, action);
4548
4549
4550
		action= new InvertCaseAction(EditorMessages.getBundleForConstructedKeys(), "Editor.InvertCase.", this); //$NON-NLS-1$
4551
		action.setHelpContextId(IAbstractTextEditorHelpContextIds.INVERT_CASE_ACTION);
4552
		action.setActionDefinitionId(ITextEditorActionDefinitionIds.INVERT_CASE);
4553
		setAction(ITextEditorActionConstants.INVERT_CASE, action);
4554
4555
		action= new CapitalizeCaseAction(EditorMessages.getBundleForConstructedKeys(), "Editor.CapitalizeCase.", this); //$NON-NLS-1$
4556
		action.setHelpContextId(IAbstractTextEditorHelpContextIds.CAPITALIZE_CASE_ACTION);
4557
		action.setActionDefinitionId(ITextEditorActionDefinitionIds.CAPITALIZE_CASE);
4558
		setAction(ITextEditorActionConstants.CAPITALIZE_CASE, action);
4559
4560
		action= new CamelCaseAction(EditorMessages.getBundleForConstructedKeys(), "Editor.LowerCamelCase.", this, false); //$NON-NLS-1$
4561
		action.setHelpContextId(IAbstractTextEditorHelpContextIds.LOWER_CAMEL_CASE_ACTION);
4562
		action.setActionDefinitionId(ITextEditorActionDefinitionIds.LOWER_CAMEL_CASE);
4563
		setAction(ITextEditorActionConstants.LOWER_CAMEL_CASE, action);
4564
4565
		action= new CamelCaseAction(EditorMessages.getBundleForConstructedKeys(), "Editor.UpperCamelCase.", this, true); //$NON-NLS-1$
4566
		action.setHelpContextId(IAbstractTextEditorHelpContextIds.UPPER_CAMEL_CASE_ACTION);
4567
		action.setActionDefinitionId(ITextEditorActionDefinitionIds.UPPER_CAMEL_CASE);
4568
		setAction(ITextEditorActionConstants.UPPER_CAMEL_CASE, action);
4569
4570
		action= new ConstantCaseAction(EditorMessages.getBundleForConstructedKeys(), "Editor.ConstantCase.", this); //$NON-NLS-1$
4571
		action.setHelpContextId(IAbstractTextEditorHelpContextIds.CONSTANT_CASE_ACTION);
4572
		action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONSTANT_CASE);
4573
		setAction(ITextEditorActionConstants.CONSTANT_CASE, action);
4574
4549
		action= new InsertLineAction(EditorMessages.getBundleForConstructedKeys(), "Editor.SmartEnter.", this, false); //$NON-NLS-1$
4575
		action= new InsertLineAction(EditorMessages.getBundleForConstructedKeys(), "Editor.SmartEnter.", this, false); //$NON-NLS-1$
4550
		action.setHelpContextId(IAbstractTextEditorHelpContextIds.SMART_ENTER_ACTION);
4576
		action.setHelpContextId(IAbstractTextEditorHelpContextIds.SMART_ENTER_ACTION);
4551
		action.setActionDefinitionId(ITextEditorActionDefinitionIds.SMART_ENTER);
4577
		action.setActionDefinitionId(ITextEditorActionDefinitionIds.SMART_ENTER);
Lines 4613-4618 Link Here
4613
		markAsSelectionDependentAction(ITextEditorActionConstants.SHIFT_RIGHT_TAB, true);
4639
		markAsSelectionDependentAction(ITextEditorActionConstants.SHIFT_RIGHT_TAB, true);
4614
		markAsSelectionDependentAction(ITextEditorActionConstants.UPPER_CASE, true);
4640
		markAsSelectionDependentAction(ITextEditorActionConstants.UPPER_CASE, true);
4615
		markAsSelectionDependentAction(ITextEditorActionConstants.LOWER_CASE, true);
4641
		markAsSelectionDependentAction(ITextEditorActionConstants.LOWER_CASE, true);
4642
		markAsSelectionDependentAction(ITextEditorActionConstants.INVERT_CASE, true);
4643
		markAsSelectionDependentAction(ITextEditorActionConstants.CAPITALIZE_CASE, true);
4644
		markAsSelectionDependentAction(ITextEditorActionConstants.LOWER_CAMEL_CASE, true);
4645
		markAsSelectionDependentAction(ITextEditorActionConstants.UPPER_CAMEL_CASE, true);
4646
		markAsSelectionDependentAction(ITextEditorActionConstants.CONSTANT_CASE, true);
4616
4647
4617
		markAsPropertyDependentAction(ITextEditorActionConstants.UNDO, true);
4648
		markAsPropertyDependentAction(ITextEditorActionConstants.UNDO, true);
4618
		markAsPropertyDependentAction(ITextEditorActionConstants.REDO, true);
4649
		markAsPropertyDependentAction(ITextEditorActionConstants.REDO, true);
(-)src/org/eclipse/ui/texteditor/CaseAction.java (-70 / +7 lines)
Lines 7-29 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Sebastian Davids <sdavids@gmx.de> - bug 125825
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.ui.texteditor;
12
package org.eclipse.ui.texteditor;
12
13
13
import java.util.ResourceBundle;
14
import java.util.ResourceBundle;
14
15
15
import org.eclipse.swt.custom.StyledText;
16
import org.eclipse.swt.graphics.Point;
17
18
import org.eclipse.jface.text.BadLocationException;
19
import org.eclipse.jface.text.IDocument;
20
import org.eclipse.jface.text.source.ISourceViewer;
21
22
/**
16
/**
23
 * Action that converts the current selection to lower case or upper case.
17
 * Action that converts the current selection to lower case or upper case.
24
 * @since 3.0
18
 * @since 3.0
25
 */
19
 */
26
public class CaseAction extends TextEditorAction implements IUpdate {
20
public class CaseAction extends AbstractCaseAction {
27
21
28
	/** <code>true</code> if this action converts to upper case, <code>false</code> otherwise. */
22
	/** <code>true</code> if this action converts to upper case, <code>false</code> otherwise. */
29
	private boolean fToUpper;
23
	private boolean fToUpper;
Lines 37-112 Link Here
37
	 * @param prefix a prefix to be prepended to the various resource keys
31
	 * @param prefix a prefix to be prepended to the various resource keys
38
	 *   (described in <code>ResourceAction</code> constructor), or  <code>null</code> if none
32
	 *   (described in <code>ResourceAction</code> constructor), or  <code>null</code> if none
39
	 * @param editor the text editor
33
	 * @param editor the text editor
40
	 * @param toUpper <code>true</code> if this is an uppercase action, <code>false</code> otherwise.
34
	 * @param toUpper <code>true</code> if this is an upper case action, <code>false</code> otherwise
41
	 *
35
	 *
42
	 * @see ResourceAction#ResourceAction(ResourceBundle, String)
36
	 * @see ResourceAction#ResourceAction(ResourceBundle, String)
43
	 */
37
	 */
44
	public CaseAction(ResourceBundle bundle, String prefix, AbstractTextEditor editor, boolean toUpper) {
38
	public CaseAction(ResourceBundle bundle, String prefix, AbstractTextEditor editor, boolean toUpper) {
45
		super(bundle, prefix, editor);
39
		super(bundle, prefix, editor);
46
		fToUpper= toUpper;
40
		fToUpper= toUpper;
47
		update();
48
	}
41
	}
49
42
	
50
	/*
43
	/*
51
	 * @see org.eclipse.jface.action.IAction#run()
44
	 * @see org.eclipse.ui.texteditor.AbstractCaseAction#changeCase(String)
52
	 */
45
	 */
53
	public void run() {
46
	protected String changeCase(String input) {
54
		ITextEditor editor= getTextEditor();
47
		return fToUpper ? input.toUpperCase() : input.toLowerCase();
55
		if (editor == null)
56
			return;
57
58
		if (!validateEditorInputState())
59
			return;
60
61
		ISourceViewer viewer= ((AbstractTextEditor) editor).getSourceViewer();
62
		if (viewer == null)
63
			return;
64
65
		IDocument document= viewer.getDocument();
66
		if (document == null)
67
			return;
68
69
		StyledText st= viewer.getTextWidget();
70
		if (st == null)
71
			return;
72
73
		Point sel= viewer.getSelectedRange();
74
		if (sel == null)
75
			return;
76
77
		try {
78
			// if the selection is empty, we select the word / string using the viewer's
79
			// double-click strategy
80
			if (sel.y == 0)  {
81
82
				// TODO find a better way to do this although there are multiple partitionings on a single document
83
84
//				String partition= getContentType(viewer, document, sel.x);
85
//				SourceViewerConfiguration svc= fEditor.getSourceViewerConfiguration(); // never null when viewer instantiated
86
//				ITextDoubleClickStrategy dcs= svc.getDoubleClickStrategy(viewer, partition);
87
//				if (dcs != null) {
88
//					dcs.doubleClicked(viewer);
89
//					sel= viewer.getSelectedRange();
90
//				}
91
92
				if (sel.y == 0)
93
					return;	// if the selection is still empty, we're done
94
			}
95
96
			String target= document.get(sel.x, sel.y);
97
			String replacement= (fToUpper ? target.toUpperCase() : target.toLowerCase());
98
			if (!target.equals(replacement)) {
99
				document.replace(sel.x, target.length(), replacement);
100
			}
101
		} catch (BadLocationException x) {
102
			// ignore and return
103
			return;
104
		}
105
106
		// reinstall selection and move it into view
107
		viewer.setSelectedRange(sel.x, sel.y);
108
		// don't use the viewer's reveal feature in order to avoid jumping around
109
		st.showSelection();
110
	}
48
	}
111
112
}
49
}
(-)src/org/eclipse/ui/texteditor/ITextEditorActionConstants.java (+35 lines)
Lines 228-233 Link Here
228
	String LOWER_CASE= "LowerCase"; //$NON-NLS-1$
228
	String LOWER_CASE= "LowerCase"; //$NON-NLS-1$
229
229
230
	/**
230
	/**
231
	 * Name of the action to invert the case of a selection
232
	 * Value: <code>"InvertCase"</code>
233
	 * @since 3.3
234
	 */
235
	String INVERT_CASE= "InvertCase"; //$NON-NLS-1$
236
237
	/**
238
	 * Name of the action to turn a selection to capitalized case
239
	 * Value: <code>"CapitalizeCase"</code>
240
	 * @since 3.3
241
	 */
242
	String CAPITALIZE_CASE= "CapitalizeCase"; //$NON-NLS-1$
243
	
244
	/**
245
	 * Name of the action to turn a selection to upper camel case
246
	 * Value: <code>"UpperCamelCase"</code>
247
	 * @since 3.3
248
	 */
249
	String UPPER_CAMEL_CASE= "UpperCamelCase"; //$NON-NLS-1$
250
251
	/**
252
	 * Name of the action to turn a selection to lower camel case
253
	 * Value: <code>"LowerCamelCase"</code>
254
	 * @since 3.3
255
	 */
256
	String LOWER_CAMEL_CASE= "LowerCamelCase"; //$NON-NLS-1$
257
	
258
	/**
259
	 * Name of the action to turn a selection to constant case
260
	 * Value: <code>"ConstantCase"</code>
261
	 * @since 3.3
262
	 */
263
	String CONSTANT_CASE= "ConstantCase"; //$NON-NLS-1$
264
265
	/**
231
	 * Name of the action to find next.
266
	 * Name of the action to find next.
232
	 * Value: <code>"FindNext"</code>
267
	 * Value: <code>"FindNext"</code>
233
	 * @since 2.0
268
	 * @since 2.0
(-)plugin.xml (+30 lines)
Lines 385-390 Link Here
385
	        id="org.eclipse.ui.edit.text.lowerCase">
385
	        id="org.eclipse.ui.edit.text.lowerCase">
386
	  </command>
386
	  </command>
387
	  <command
387
	  <command
388
	        name="%invertCase.label"
389
	        description="%invertCase.description"
390
	        categoryId="org.eclipse.ui.category.textEditor"
391
	        id="org.eclipse.ui.edit.text.invertCase">
392
	  </command>
393
	  <command
394
	        name="%capitalizeCase.label"
395
	        description="%capitalizeCase.description"
396
	        categoryId="org.eclipse.ui.category.textEditor"
397
	        id="org.eclipse.ui.edit.text.capitalizeCase">
398
	  </command>
399
	  <command
400
	        name="%lowerCamelCase.label"
401
	        description="%lowerCamelCase.description"
402
	        categoryId="org.eclipse.ui.category.textEditor"
403
	        id="org.eclipse.ui.edit.text.lowerCamelCase">
404
	  </command>
405
	  <command
406
	        name="%upperCamelCase.label"
407
	        description="%upperCamelCase.description"
408
	        categoryId="org.eclipse.ui.category.textEditor"
409
	        id="org.eclipse.ui.edit.text.upperCamelCase">
410
	  </command>
411
	  <command
412
	        name="%constantCase.label"
413
	        description="%constantCase.description"
414
	        categoryId="org.eclipse.ui.category.textEditor"
415
	        id="org.eclipse.ui.edit.text.constantCase">
416
	  </command>	  	  	  
417
	  <command
388
	        name="%onlyShowSelectedElement.label"
418
	        name="%onlyShowSelectedElement.label"
389
	        description="%onlyShowSelectedElement.description"
419
	        description="%onlyShowSelectedElement.description"
390
	        categoryId="org.eclipse.ui.category.window"
420
	        categoryId="org.eclipse.ui.category.window"
(-)plugin.properties (+10 lines)
Lines 38-43 Link Here
38
upperCase.label= To Upper Case
38
upperCase.label= To Upper Case
39
lowerCase.description= Changes the selection to lower case
39
lowerCase.description= Changes the selection to lower case
40
lowerCase.label= To Lower Case
40
lowerCase.label= To Lower Case
41
invertCase.description= Inverts the case of the selection
42
invertCase.label= Invert Case
43
capitalizeCase.description= Capitalizes the selection
44
capitalizeCase.label= Capitalize
45
lowerCamelCase.description= Converts the selection to lower camel case
46
lowerCamelCase.label= To Lower Camel Case
47
upperCamelCase.description= Converts the selection to upper camel case
48
upperCamelCase.label= To Upper Camel Case
49
constantCase.description= Converts the selection to constant case
50
constantCase.label= To Constant Case
41
51
42
goToLastEditPosition.label= Last Edit Location
52
goToLastEditPosition.label= Last Edit Location
43
goToLastEditPosition.description= Last edit location
53
goToLastEditPosition.description= Last edit location
(-)src/org/eclipse/ui/texteditor/ConstantCaseAction.java (+50 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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
 *     Sebastian Davids <sdavids@gmx.de> - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.ui.texteditor;
12
13
import java.util.ResourceBundle;
14
15
import org.eclipse.ui.internal.texteditor.Strings;
16
17
/**
18
 * Action that converts the current selection to constant case.
19
 * 
20
 * @since 3.3
21
 */
22
public class ConstantCaseAction extends AbstractCaseAction {
23
24
	/**
25
	 * Creates and initializes the action for the given text editor. The action
26
	 * configures its visual representation from the given resource bundle.
27
	 * 
28
	 * @param bundle
29
	 *            the resource bundle
30
	 * @param prefix
31
	 *            a prefix to be prepended to the various resource keys
32
	 *            (described in <code>ResourceAction</code> constructor), or
33
	 *            <code>null</code> if none
34
	 * @param editor
35
	 *            the text editor
36
	 * 
37
	 * @see ResourceAction#ResourceAction(ResourceBundle, String)
38
	 */
39
	public ConstantCaseAction(ResourceBundle bundle, String prefix,
40
			AbstractTextEditor editor) {
41
		super(bundle, prefix, editor);
42
	}
43
44
	/*
45
	 * @see org.eclipse.ui.texteditor.AbstractCaseAction#changeCase(String)
46
	 */
47
	protected String changeCase(String input) {
48
		return Strings.constantCase(input);
49
	}
50
}
(-)src/org/eclipse/ui/texteditor/AbstractCaseAction.java (+139 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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 125825
11
 *******************************************************************************/
12
package org.eclipse.ui.texteditor;
13
14
import java.util.ResourceBundle;
15
16
import org.eclipse.jface.text.BadLocationException;
17
import org.eclipse.jface.text.IDocument;
18
import org.eclipse.jface.text.source.ISourceViewer;
19
import org.eclipse.swt.custom.StyledText;
20
import org.eclipse.swt.graphics.Point;
21
22
/**
23
 * Abstract action that converts the current text selection to a different case.
24
 * <p>
25
 * Subclasses should override {@link #changeCase(String)} to convert the
26
 * selection to a specific case.
27
 * </p>
28
 * 
29
 * @since 3.3
30
 */
31
/* package */abstract class AbstractCaseAction extends TextEditorAction {
32
33
	/**
34
	 * Creates and initializes the action for the given text editor. The action
35
	 * configures its visual representation from the given resource bundle.
36
	 * 
37
	 * @param bundle
38
	 *            the resource bundle
39
	 * @param prefix
40
	 *            a prefix to be prepended to the various resource keys
41
	 *            (described in <code>ResourceAction</code> constructor), or
42
	 *            <code>null</code> if none
43
	 * @param editor
44
	 *            the text editor
45
	 * 
46
	 * @see ResourceAction#ResourceAction(ResourceBundle, String)
47
	 */
48
	protected AbstractCaseAction(ResourceBundle bundle, String prefix,
49
			ITextEditor editor) {
50
		super(bundle, prefix, editor);
51
		update();
52
	}
53
54
	/*
55
	 * @see org.eclipse.jface.action.IAction#run()
56
	 */
57
	public void run() {
58
		ITextEditor editor = getTextEditor();
59
		if (editor == null)
60
			return;
61
62
		if (!validateEditorInputState())
63
			return;
64
65
		ISourceViewer viewer = ((AbstractTextEditor) editor).getSourceViewer();
66
		if (viewer == null)
67
			return;
68
69
		IDocument document = viewer.getDocument();
70
		if (document == null)
71
			return;
72
73
		StyledText st = viewer.getTextWidget();
74
		if (st == null)
75
			return;
76
77
		Point sel = viewer.getSelectedRange();
78
		if (sel == null)
79
			return;
80
81
		try {
82
			// if the selection is empty, we select the word / string using the
83
			// viewer's
84
			// double-click strategy
85
			if (sel.y == 0) {
86
87
				// TODO find a better way to do this although there are multiple
88
				// partitionings on a single document
89
90
				// String partition= getContentType(viewer, document, sel.x);
91
				// SourceViewerConfiguration svc=
92
				// fEditor.getSourceViewerConfiguration(); // never null when
93
				// viewer instantiated
94
				// ITextDoubleClickStrategy dcs=
95
				// svc.getDoubleClickStrategy(viewer, partition);
96
				// if (dcs != null) {
97
				// dcs.doubleClicked(viewer);
98
				// sel= viewer.getSelectedRange();
99
				// }
100
101
				if (sel.y == 0)
102
					return; // if the selection is still empty, we're done
103
			}
104
105
			String target = document.get(sel.x, sel.y);
106
			String replacement = changeCase(target);
107
			if (!target.equals(replacement)) {
108
				document.replace(sel.x, target.length(), replacement);
109
				// the replacement might be larger than the original
110
				int adjustment = replacement.length() - target.length();
111
				if (adjustment > 0)
112
					sel.y += adjustment;
113
			}
114
		} catch (BadLocationException x) {
115
			// ignore and return
116
			return;
117
		}
118
119
		// reinstall selection and move it into view
120
		viewer.setSelectedRange(sel.x, sel.y);
121
		// don't use the viewer's reveal feature in order to avoid jumping
122
		// around
123
		st.showSelection();
124
	}
125
126
	/**
127
	 * Template method subclasses should override to convert the given String to
128
	 * a specific case.
129
	 * <p>
130
	 * Since case mappings are not always 1:1 char mappings, the resulting
131
	 * String may be a different length than the String input.
132
	 * </p>
133
	 * 
134
	 * @param input
135
	 *            the String to be converted; must not be <code>null</code>
136
	 * @return the replacement String in different case
137
	 */
138
	protected abstract String changeCase(String input);
139
}
(-)src/org/eclipse/ui/texteditor/InvertCaseAction.java (+50 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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
 *     Sebastian Davids <sdavids@gmx.de> - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.ui.texteditor;
12
13
import java.util.ResourceBundle;
14
15
import org.eclipse.ui.internal.texteditor.Strings;
16
17
/**
18
 * Action that inverts the case of the current selection.
19
 * 
20
 * @since 3.3
21
 */
22
public class InvertCaseAction extends AbstractCaseAction {
23
24
	/**
25
	 * Creates and initializes the action for the given text editor. The action
26
	 * configures its visual representation from the given resource bundle.
27
	 * 
28
	 * @param bundle
29
	 *            the resource bundle
30
	 * @param prefix
31
	 *            a prefix to be prepended to the various resource keys
32
	 *            (described in <code>ResourceAction</code> constructor), or
33
	 *            <code>null</code> if none
34
	 * @param editor
35
	 *            the text editor
36
	 * 
37
	 * @see ResourceAction#ResourceAction(ResourceBundle, String)
38
	 */
39
	public InvertCaseAction(ResourceBundle bundle, String prefix,
40
			AbstractTextEditor editor) {
41
		super(bundle, prefix, editor);
42
	}
43
44
	/*
45
	 * @see org.eclipse.ui.texteditor.AbstractCaseAction#changeCase(String)
46
	 */
47
	protected String changeCase(String input) {
48
		return Strings.invertCase(input);
49
	}
50
}
(-)src/org/eclipse/ui/texteditor/CamelCaseAction.java (+50 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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
 *     Sebastian Davids <sdavids@gmx.de> - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.ui.texteditor;
12
13
import java.util.ResourceBundle;
14
15
import org.eclipse.ui.internal.texteditor.Strings;
16
17
/**
18
 * Action that converts the current selection to lower camel case or upper camel case.
19
 * @since 3.3
20
 */
21
public class CamelCaseAction extends AbstractCaseAction {
22
23
	/** <code>true</code> if this action converts to upper camel case, <code>false</code> otherwise. */
24
	private boolean fToUpper;
25
26
	/**
27
	 * Creates and initializes the action for the given text editor.
28
	 * The action configures its visual representation from the given resource
29
	 * bundle.
30
	 *
31
	 * @param bundle the resource bundle
32
	 * @param prefix a prefix to be prepended to the various resource keys
33
	 *   (described in <code>ResourceAction</code> constructor), or  <code>null</code> if none
34
	 * @param editor the text editor
35
	 * @param toUpper <code>true</code> if this is an upper camel case action, <code>false</code> otherwise
36
	 *
37
	 * @see ResourceAction#ResourceAction(ResourceBundle, String)
38
	 */
39
	public CamelCaseAction(ResourceBundle bundle, String prefix, AbstractTextEditor editor, boolean toUpper) {
40
		super(bundle, prefix, editor);
41
		fToUpper= toUpper;
42
	}
43
44
	/*
45
	 * @see org.eclipse.ui.texteditor.AbstractCaseAction#changeCase(String)
46
	 */
47
	protected String changeCase(String input) {
48
		return fToUpper ? Strings.upperCamelCase(input) : Strings.lowerCamelCase(input);
49
	}
50
}
(-)src/org/eclipse/ui/internal/texteditor/Strings.java (+395 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     Sebastian Davids <sdavids@gmx.de> - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.ui.internal.texteditor;
12
13
import java.util.StringTokenizer;
14
15
/**
16
 * Helper class to provide String manipulation functions not available in
17
 * standard JDK.
18
 * 
19
 * @since 3.3
20
 */
21
public class Strings {
22
23
	private Strings() {
24
		// Do not instantiate
25
	}
26
27
	/**
28
	 * Capitalizes the given String.
29
	 * <p>
30
	 * Since case mappings are not always 1:1 char mappings, the resulting
31
	 * String may be a different length than the String input.
32
	 * </p>
33
	 * <p>
34
	 * Rules:
35
	 * </p>
36
	 * <ul>
37
	 * <li>the first character will be converted to upper case</li>
38
	 * <li>each letter directly after a whitespace will be converted to upper
39
	 * case</li>
40
	 * </ul>
41
	 * <p>
42
	 * Examples:
43
	 * </p>
44
	 * 
45
	 * <pre>
46
	 * Strings.capitalize(null)                = null
47
	 * Strings.capitalize(&quot;to be capitalized&quot;) = &quot;To Be Capitalized&quot;
48
	 * Strings.capitalize(&quot;§&quot;)                 = &quot;SS&quot;
49
	 * Strings.capitalize(&quot;a\tba&quot;)             = &quot;A\tBa&quot;
50
	 * </pre>
51
	 * 
52
	 * @param input
53
	 *            the String to be capitalized, may be null
54
	 * @return the capitalized String or <code>null</code> if null String
55
	 *         input
56
	 * @see java.lang.String#toUpperCase()
57
	 */
58
	public static String capitalize(String input) {
59
		if (input == null)
60
			return null;
61
62
		int len = input.length();
63
64
		if (len == 0)
65
			return input;
66
67
		if (len == 1)
68
			return input.toUpperCase();
69
70
		StringTokenizer st = new StringTokenizer(input, " \t\n\r\f", true); //$NON-NLS-1$
71
72
		// common case has no 1:M mappings so initialize buffer w/
73
		// length of original String to avoid resizing
74
		StringBuffer result = new StringBuffer(input.length());
75
76
		while (st.hasMoreTokens())
77
			appendCapitalized(result, st.nextToken());
78
79
		return result.toString();
80
	}
81
82
	/**
83
	 * Converts the given String to constant case.
84
	 * <p>
85
	 * Since case mappings are not always 1:1 char mappings, the resulting
86
	 * String may be a different length than the String input.
87
	 * </p>
88
	 * <p>
89
	 * Rules:
90
	 * </p>
91
	 * <ul>
92
	 * <li>all letters will be converted to upper case</li>
93
	 * <li>each upper case letter in the input String will be prefixed by an _
94
	 * in the result</li>
95
	 * <li>if the first character of the input String is an upper case letter
96
	 * then it wil not be prefixed by _ in the result</li>
97
	 * </ul>
98
	 * <p>
99
	 * Examples:
100
	 * </p>
101
	 * 
102
	 * <pre>
103
	 * Strings.constantCase(null)                        = null
104
	 * Strings.constantCase(&quot;toConstantCase&quot;)            = &quot;TO_CONSTANT_CASE&quot;
105
	 * Strings.constantCase(&quot;NoLeadingUnderscore&quot;)       = &quot;NO_LEADING_UNDERSCORE&quot;
106
	 * Strings.constantCase(&quot;__someExtra__Underscores_&quot;) = &quot;__SOME_EXTRA___UNDERSCORES_&quot;
107
	 * </pre>
108
	 * 
109
	 * @param input
110
	 *            the String to be converted to constant case, may be null
111
	 * @return the constant case String or <code>null</code> if null String
112
	 *         input
113
	 */
114
	public static String constantCase(String input) {
115
		if (input == null)
116
			return null;
117
118
		int len = input.length();
119
120
		if (len == 0)
121
			return input;
122
123
		if (len == 1)
124
			return input.toUpperCase();
125
126
		// it is likely that the result is longer than the input;
127
		// nethertheless do not set a larger initial size and depend on
128
		// StringBuffer's resize strategy
129
		StringBuffer buffer = new StringBuffer(input.length());
130
131
		// toUpperCase on an entire String is more performant than
132
		// several String.valueOf(c).toUpperCase() calls in a loop;
133
		// so we first just copy and insert _'s
134
135
		// first letter should not be prefixed by _
136
		buffer.append(input.charAt(0));
137
138
		// now prefix the remaining
139
		for (int i = 1; i < len; i++) {
140
			char c = input.charAt(i);
141
			if (Character.isUpperCase(c))
142
				buffer.append('_');
143
			buffer.append(c);
144
		}
145
146
		// now convert to uppercase
147
		return buffer.toString().toUpperCase();
148
	}
149
150
	/**
151
	 * Inverts the case of the given String's letters.
152
	 * <p>
153
	 * Since case mappings are not always 1:1 char mappings, the resulting
154
	 * String may be a different length than the String input.
155
	 * </p>
156
	 * <p>
157
	 * This operation is not symmetric;
158
	 * <code>Strings.invertCase(Strings.invertCase(input))</code> might not be
159
	 * equal to <code>input</code>.
160
	 * </p>
161
	 * <p>
162
	 * Rules:
163
	 * </p>
164
	 * <ul>
165
	 * <li>each lower case letter will be converted to upper case</li>
166
	 * <li>each upper case letter will be converted to lower case</li>
167
	 * </ul>
168
	 * <p>
169
	 * Examples:
170
	 * </p>
171
	 * 
172
	 * <pre>
173
	 * Strings.invertCase(null)             = null
174
	 * Strings.invertCase(&quot;To Be Inverted&quot;) = &quot;tO bE iNVERTED&quot;
175
	 * Strings.invertCase(&quot;tO bE iNVERTED&quot;) = &quot;To Be Inverted&quot;
176
	 * Strings.invertCase(&quot;Ma&szlig;e&quot;)           = &quot;mASSE&quot;
177
	 * Strings.invertCase(&quot;mASSE&quot;)          = &quot;Masse&quot;
178
	 * Strings.invertCase(&quot;StringsTest&quot;)    = &quot;sTRINGStEST&quot;
179
	 * </pre>
180
	 * 
181
	 * @param input
182
	 *            the String to be inverted, may be null
183
	 * @return the inverted String or <code>null</code> if null String input
184
	 * @see java.lang.String#toLowerCase()
185
	 * @see java.lang.String#toUpperCase()
186
	 * @see java.lang.Character#isLetter(char)
187
	 */
188
	public static String invertCase(String input) {
189
		if (input == null)
190
			return null;
191
192
		int len = input.length();
193
194
		if (len == 0)
195
			return input;
196
197
		int i;
198
		char c;
199
200
		// skip to first lower case letter
201
		for (i = 0; i < len; i++) {
202
			c = input.charAt(i);
203
			if (Character.isLetter(c) && Character.isLowerCase(c))
204
				break;
205
		}
206
207
		// only upper case letters in String?
208
		if (i == len)
209
			return input.toLowerCase();
210
211
		// common case has no 1:M mappings so initialize buffer w/
212
		// length of original String to avoid resizing
213
		StringBuffer result = new StringBuffer(len);
214
215
		// append upper case letters before first lower case letter
216
		result.append(input.substring(0, i).toLowerCase());
217
218
		// convert the remaining characters
219
		for (; i < len; i++) {
220
			c = input.charAt(i);
221
			if (Character.isLetter(c)) {
222
				// String.toUpper/Lower insure 1:M mappings
223
				String letter = String.valueOf(c);
224
				if (Character.isLowerCase(c))
225
					result.append(letter.toUpperCase());
226
				else
227
					result.append(letter.toLowerCase());
228
			} else
229
				result.append(c);
230
		}
231
232
		return result.toString();
233
	}
234
235
	/**
236
	 * Converts the given String to lower camel case.
237
	 * <p>
238
	 * Since case mappings are not always 1:1 char mappings, the resulting
239
	 * String may be a different length than the String input.
240
	 * </p>
241
	 * <p>
242
	 * Rules:
243
	 * </p>
244
	 * <ul>
245
	 * <li>the first letter will be converted to lower case</li>
246
	 * <li>the character _ will be removed unless it is followed by another _
247
	 * or if it is a trailing _</li>
248
	 * <li>a letter following an _ will be converted to upper case</li>
249
	 * <li>all other letters will be in converted to lower case</li>
250
	 * </ul>
251
	 * <p>
252
	 * Examples:
253
	 * </p>
254
	 * 
255
	 * <pre>
256
	 * Strings.lowerCamelCase(null)                        = null
257
	 * Strings.lowerCamelCase(&quot;TO_LOWER_CAMEL_CASE&quot;)       = &quot;toLowerCamelCase&quot;
258
	 * Strings.lowerCamelCase(&quot;_Leading&quot;)                  = &quot;leading&quot;
259
	 * Strings.lowerCamelCase(&quot;__someExtra__underscores_&quot;) = &quot;_Someextra_Underscores_&quot;
260
	 * </pre>
261
	 * 
262
	 * @param input
263
	 *            the String to be converted to lower camel case, may be null
264
	 * @return the lower camel case String or <code>null</code> if null String
265
	 *         input
266
	 */
267
	public static String lowerCamelCase(String input) {
268
		return internalCamelCase(input, false);
269
	}
270
271
	/**
272
	 * Converts the given String to upper camel case.
273
	 * <p>
274
	 * Since case mappings are not always 1:1 char mappings, the resulting
275
	 * String may be a different length than the String input.
276
	 * </p>
277
	 * <p>
278
	 * Rules:
279
	 * </p>
280
	 * <ul>
281
	 * <li>the character _ will be removed unless it is followed by another _
282
	 * or if it is a trailing _</li>
283
	 * <li>a letter following an _ will be converted to upper case</li>
284
	 * <li>all other letters will be in converted to lower case</li>
285
	 * </ul>
286
	 * <p>
287
	 * Examples:
288
	 * </p>
289
	 * 
290
	 * <pre>
291
	 * Strings.upperCamelCase(null)                        = null
292
	 * Strings.upperCamelCase(&quot;TO_UPPER_CAMEL_CASE&quot;)       = &quot;ToUpperCamelCase&quot;
293
	 * Strings.upperCamelCase(&quot;_leading&quot;)                  = &quot;Leading&quot;
294
	 * Strings.upperCamelCase(&quot;__someExtra__underscores_&quot;) = &quot;_Someextra_Underscores_&quot;
295
	 * </pre>
296
	 * 
297
	 * @param input
298
	 *            the String to be converted to upper camel case, may be null
299
	 * @return the upper camel case String or <code>null</code> if null String
300
	 *         input
301
	 */
302
	public static String upperCamelCase(String input) {
303
		return internalCamelCase(input, true);
304
	}
305
306
	/**
307
	 * Converts the given String to upper or lower camel case.
308
	 * 
309
	 * @param input
310
	 *            the String to be converted, may be null
311
	 * @param toUpper
312
	 *            <code>true</code> if the input should be converted to upper
313
	 *            camel case, <code>false</code> otherwise
314
	 * @return the converted String or <code>null</code> if null String input
315
	 */
316
	private static String internalCamelCase(String input, boolean toUpper) {
317
		if (input == null)
318
			return null;
319
320
		if (input.length() == 0)
321
			return input;
322
323
		// the result mainly consists of lower case letters;
324
		// toLowerCase on the entire String is more performant than
325
		// several String.valueOf(c).toLowerCase() calls in the loop below
326
		String lower = input.toLowerCase();
327
		int len = lower.length();
328
		StringBuffer buffer = new StringBuffer(len);
329
		for (int i = 0; i < len; i++) {
330
			char c = lower.charAt(i);
331
			if (c == '_') {
332
				int j = i + 1;
333
				for (; j < len; j++) {
334
					c = lower.charAt(j);
335
					if (c == '_') {
336
						// keep additional _'s
337
						buffer.append('_');
338
					} else {
339
						if (toUpper || j > 1) {
340
							// String.toUpper insures 1:M mappings
341
							buffer.append(String.valueOf(c).toUpperCase());
342
						} else {
343
							// keep result's first character in lower case
344
							buffer.append(c);
345
						}
346
						break;
347
					}
348
				}
349
				if (j == len) {
350
					// keep trailing _
351
					buffer.append('_');
352
				}
353
				i = j;
354
			} else {
355
				// letters are lower case already so just append
356
				buffer.append(lower.charAt(i));
357
			}
358
		}
359
		if (toUpper) {
360
			buffer
361
					.replace(0, 1, String.valueOf(buffer.charAt(0))
362
							.toUpperCase());
363
		}
364
		return buffer.toString();
365
	}
366
367
	/**
368
	 * Appends the given String to the buffer; the first character of the String
369
	 * will be appended in upper case if it is a letter.
370
	 * 
371
	 * @param buffer
372
	 *            the <code>StringBuffer</code> the capitalized input String
373
	 *            will be appended to
374
	 * @param input
375
	 *            the String to be capitalized
376
	 */
377
	private static void appendCapitalized(StringBuffer buffer, String input) {
378
		char firstChar = input.charAt(0);
379
380
		if (!Character.isLetter(firstChar)) {
381
			buffer.append(input);
382
			return;
383
		}
384
385
		// capitalize first char of token
386
		// String.toUpper insures 1:M mappings
387
		buffer.append(String.valueOf(firstChar).toUpperCase());
388
389
		int len = input.length();
390
391
		// append remaining chars of token
392
		if (len > 1)
393
			buffer.append(input.substring(1, len));
394
	}
395
}
(-)src/org/eclipse/ui/texteditor/CapitalizeCaseAction.java (+50 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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
 *     Sebastian Davids <sdavids@gmx.de> - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.ui.texteditor;
12
13
import java.util.ResourceBundle;
14
15
import org.eclipse.ui.internal.texteditor.Strings;
16
17
/**
18
 * Action that capitalizes the current selection.
19
 * 
20
 * @since 3.3
21
 */
22
public class CapitalizeCaseAction extends AbstractCaseAction {
23
24
	/**
25
	 * Creates and initializes the action for the given text editor. The action
26
	 * configures its visual representation from the given resource bundle.
27
	 * 
28
	 * @param bundle
29
	 *            the resource bundle
30
	 * @param prefix
31
	 *            a prefix to be prepended to the various resource keys
32
	 *            (described in <code>ResourceAction</code> constructor), or
33
	 *            <code>null</code> if none
34
	 * @param editor
35
	 *            the text editor
36
	 * 
37
	 * @see ResourceAction#ResourceAction(ResourceBundle, String)
38
	 */
39
	public CapitalizeCaseAction(ResourceBundle bundle, String prefix,
40
			AbstractTextEditor editor) {
41
		super(bundle, prefix, editor);
42
	}
43
44
	/*
45
	 * @see org.eclipse.ui.texteditor.AbstractCaseAction#changeCase(String)
46
	 */
47
	protected String changeCase(String input) {
48
		return Strings.capitalize(input);
49
	}
50
}

Return to bug 125825