KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2007 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  * Benjamin Muskalla - initial API and implementation - https://bugs.eclipse.org/bugs/show_bug.cgi?id=41573
10  *******************************************************************************/

11 package org.eclipse.ui.texteditor;
12
13 import java.util.ResourceBundle JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.ITextSelection;
23
24
25 /**
26  * Action for joining two or more lines together by deleting the
27  * line delimiters and trimming the whitespace between them.
28  *
29  * @since 3.3
30  */

31 public class JoinLinesAction extends TextEditorAction {
32
33     private String JavaDoc fJoint= null;
34     
35     
36     /**
37      * Creates a line joining action.
38      *
39      * @param bundle the resource bundle for UI strings
40      * @param prefix the prefix for the property keys into <code>bundle</code>
41      * @param editor the editor
42      * @param joint the string to put between the lines
43      */

44     public JoinLinesAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor, String JavaDoc joint) {
45         super(bundle, prefix, editor);
46         Assert.isLegal(joint != null);
47         fJoint= joint;
48         update();
49     }
50
51     /*
52      * @see org.eclipse.jface.action.Action#run()
53      */

54     public void run() {
55         
56         ITextEditor editor= getTextEditor();
57         if (editor == null)
58             return;
59
60         if (!validateEditorInputState())
61             return;
62
63         IDocument document= getDocument(editor);
64         if (document == null)
65             return;
66
67         ITextSelection selection= getSelection(editor);
68         if (selection == null)
69             return;
70
71         int startLine= selection.getStartLine();
72         int endLine= selection.getEndLine();
73         try {
74             int caretOffset= joinLines(document, startLine, endLine);
75             if (caretOffset > -1)
76                 editor.selectAndReveal(caretOffset, 0);
77         } catch (BadLocationException e) {
78             // should not happen
79
}
80         
81     }
82
83     /**
84      * Returns the editor's document.
85      *
86      * @param editor the editor
87      * @return the editor's document
88      */

89     private static IDocument getDocument(ITextEditor editor) {
90
91         IDocumentProvider documentProvider= editor.getDocumentProvider();
92         if (documentProvider == null)
93             return null;
94
95         IDocument document= documentProvider.getDocument(editor.getEditorInput());
96         if (document == null)
97             return null;
98
99         return document;
100     }
101
102     /**
103      * Returns the editor's selection.
104      *
105      * @param editor the editor
106      * @return the editor's selection
107      */

108     private static ITextSelection getSelection(ITextEditor editor) {
109
110         ISelectionProvider selectionProvider= editor.getSelectionProvider();
111         if (selectionProvider == null)
112             return null;
113
114         ISelection selection= selectionProvider.getSelection();
115         if (!(selection instanceof ITextSelection))
116             return null;
117
118         return (ITextSelection) selection;
119     }
120     
121     /*
122      * @see org.eclipse.ui.texteditor.TextEditorAction#update()
123      */

124     public void update() {
125         super.update();
126         if (!isEnabled())
127             return;
128
129         if (!canModifyEditor()) {
130             setEnabled(false);
131             return;
132         }
133
134         ITextEditor editor= getTextEditor();
135         setEnabled(editor.isEditable());
136     }
137     
138     /**
139      * Joins several text lines to one line.
140      *
141      * @param document the document
142      * @param startLine the start line
143      * @param endLine the end line
144      * @return the new caret offset
145      * @throws BadLocationException
146      */

147     private int joinLines(IDocument document, int startLine, int endLine) throws BadLocationException {
148         if (startLine == document.getNumberOfLines() - 1) {
149             // do nothing because we are in the last line
150
return -1;
151         }
152
153         if (startLine == endLine)
154             endLine++; // append join with the next line
155

156         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
157         for (int line= startLine; line <= endLine; line++) {
158             buffer.append(trim(document, line, line == startLine));
159             if (line != endLine)
160                 buffer.append(fJoint);
161         }
162
163         int startLineOffset= document.getLineOffset(startLine);
164         int endLineOffset= document.getLineOffset(endLine) + document.getLineLength(endLine) - getLineDelimiterLength(document, endLine);
165         String JavaDoc replaceString= buffer.toString();
166         document.replace(startLineOffset, endLineOffset - startLineOffset, replaceString);
167         return startLineOffset + replaceString.length();
168     }
169
170     private String JavaDoc trim(IDocument document, int line, boolean ignoreLeadingWhitespace) throws BadLocationException {
171         int lineOffset= document.getLineOffset(line);
172         int lineLength= document.getLineLength(line);
173         lineLength= lineLength - getLineDelimiterLength(document, line);
174         if (!ignoreLeadingWhitespace)
175             return document.get(lineOffset, lineLength).trim();
176         
177         while (lineLength > 0 && Character.isWhitespace(document.getChar(lineOffset + lineLength - 1)))
178             lineLength--;
179         
180         return document.get(lineOffset, lineLength);
181     }
182     
183     private int getLineDelimiterLength(IDocument document, int line) throws BadLocationException {
184         String JavaDoc lineDelimiter= document.getLineDelimiter(line);
185         return lineDelimiter != null ? lineDelimiter.length() : 0;
186         
187     }
188
189 }
190
Popular Tags