2020
2121import org .eclipse .core .resources .IResource ;
2222import org .eclipse .core .runtime .FileLocator ;
23+ import org .eclipse .jface .text .IBlockTextSelection ;
2324import org .eclipse .jface .text .ITextSelection ;
25+ import org .eclipse .jface .viewers .ISelection ;
2426
2527import de .anbos .eclipse .easyshell .plugin .Activator ;
2628import de .anbos .eclipse .easyshell .plugin .misc .ResourceUtils ;
@@ -31,7 +33,7 @@ public class Resource {
3133 // internal
3234 private File file = null ;
3335 private IResource resource = null ;
34- private ITextSelection textSelection = null ;
36+ private ISelection textSelection = null ;
3537
3638 // resolved
3739 private String projectName = Activator .getResourceString ("easyshell.plugin.name" );
@@ -42,7 +44,7 @@ public Resource(Resource myRes) {
4244 this .textSelection = myRes .getTextSelection ();
4345 }
4446
45- public Resource (File file , IResource resource , ITextSelection textSelection ) {
47+ public Resource (File file , IResource resource , ISelection textSelection ) {
4648 this .file = file ;
4749 this .resource = resource ;
4850 this .textSelection = textSelection ;
@@ -56,6 +58,10 @@ public Resource(IResource resource) {
5658 this (resource .getLocation ().toFile (), resource , null );
5759 }
5860
61+ public void setTextSelection (ISelection textSelection ) {
62+ this .textSelection = textSelection ;
63+ }
64+
5965 public File getFile () {
6066 return file ;
6167 }
@@ -64,7 +70,7 @@ public IResource getResource() {
6470 return resource ;
6571 }
6672
67- public ITextSelection getTextSelection () {
73+ public ISelection getTextSelection () {
6874 return textSelection ;
6975 }
7076
@@ -160,38 +166,46 @@ public String getResourcePath() {
160166 }
161167
162168 public String getSelectedTextStartLine () {
163- if (textSelection != null ) {
164- return String .valueOf (textSelection .getStartLine ()+1 );
165- }
166- return "" ;
169+ if (textSelection != null && textSelection instanceof ITextSelection ) {
170+ return String .valueOf ((( ITextSelection ) textSelection ) .getStartLine ()+1 );
171+ }
172+ return "" ;
167173 }
168174
169175 public String getSelectedTextEndLine () {
170- if (textSelection != null ) {
171- return String .valueOf (textSelection .getEndLine ()+1 );
172- }
173- return "" ;
176+ if (textSelection != null && textSelection instanceof ITextSelection ) {
177+ return String .valueOf ((( ITextSelection ) textSelection ) .getEndLine ()+1 );
178+ }
179+ return "" ;
174180 }
175181
176182 public String getSelectedTextLength () {
177- if (textSelection != null ) {
178- return String .valueOf (textSelection .getLength ());
179- }
180- return "" ;
183+ if (textSelection != null ) {
184+ if (textSelection instanceof IBlockTextSelection ) {
185+ return String .valueOf (((IBlockTextSelection )textSelection ).getText ().length ());
186+ } else {
187+ return String .valueOf (((ITextSelection )textSelection ).getLength ());
188+ }
189+ }
190+ return "" ;
181191 }
182192
183193 public String getSelectedTextOffset () {
184- if (textSelection != null ) {
185- return String .valueOf (textSelection .getOffset ());
186- }
187- return "" ;
194+ if (textSelection != null && textSelection instanceof ITextSelection ) {
195+ return String .valueOf ((( ITextSelection ) textSelection ) .getOffset ());
196+ }
197+ return "" ;
188198 }
189199
190200 public String getSelectedText () {
191- if (textSelection != null ) {
192- return textSelection .getText ();
193- }
194- return "" ;
201+ if (textSelection != null ) {
202+ if (textSelection instanceof IBlockTextSelection ) {
203+ return ((IBlockTextSelection )textSelection ).getText ();
204+ } else {
205+ return ((ITextSelection )textSelection ).getText ();
206+ }
207+ }
208+ return "" ;
195209 }
196210
197211 public String getProjectLocation () {
@@ -325,8 +339,4 @@ public String getScriptBash(String parameter) {
325339 return file != null && file .exists () ? file .getAbsolutePath () : "file does not exists" ;
326340 }
327341
328- public void setTextSelection (ITextSelection sel ) {
329- textSelection = sel ;
330- }
331-
332342}
0 commit comments