~ubuntu-branches/ubuntu/utopic/sikuli/utopic

« back to all changes in this revision

Viewing changes to sikuli-ide/src/main/java/org/sikuli/ide/SikuliDocument.java

  • Committer: Bazaar Package Importer
  • Author(s): Gilles Filippini
  • Date: 2011-10-04 23:32:13 UTC
  • mfrom: (4.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20111004233213-36fm78hx0z53tkuw
Tags: 1.0~x~rc3-dfsg1-2
* New patch fix-cmake-sikuli-ide.patch:
  + Fix random FTBFS due to missing inter target dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010-2011, Sikuli.org
 
3
 * Released under the MIT License.
 
4
 *
 
5
 */
1
6
package org.sikuli.ide;
2
7
 
3
8
import javax.swing.text.*;
354
359
      currentPos = offs;
355
360
   }
356
361
 
 
362
   // line starting from 0
 
363
   public int getLineStartOffset(int linenum) throws BadLocationException { 
 
364
      Element map = this.getDefaultRootElement(); 
 
365
      if (linenum < 0) {
 
366
         throw new BadLocationException("Negative line", -1); 
 
367
      } else if (linenum >= map.getElementCount()) {
 
368
         throw new BadLocationException("No such line", this.getLength()+1); 
 
369
      } else {
 
370
         Element lineElem = map.getElement(linenum);
 
371
         return lineElem.getStartOffset(); 
 
372
      }  
 
373
   }   
 
374
 
 
375
   public int getLineLength(int linenum) throws BadLocationException {
 
376
      Element map = this.getDefaultRootElement();
 
377
      if (linenum < 0) {
 
378
         throw new BadLocationException("Negative line", -1); 
 
379
      } else if (linenum >= map.getElementCount()) {
 
380
         throw new BadLocationException("No such line", this.getLength()+1); 
 
381
      } else {
 
382
         Element lineElem = map.getElement(linenum);
 
383
         return lineElem.getEndOffset() - lineElem.getStartOffset();
 
384
      }  
 
385
   }   
 
386
 
 
387
   /**
 
388
    * Change the indentation of a line. Any existing leading whitespace is replaced by
 
389
    * the appropriate number of tab characters and padded with blank characters if
 
390
    * necessary.
 
391
    * @param linenum the line number (0-based)
 
392
    * @param columns the number of columns by which to increase the indentation (if
 
393
    *        columns is greater than 0) or decrease the indentation (if columns is
 
394
    *        less than 0)
 
395
    * @throws BadLocationException if the specified line does not exist
 
396
    */
 
397
   public void changeIndentation(int linenum, int columns) throws BadLocationException {
 
398
      if (columns == 0) return;
 
399
      int lineStart = getLineStartOffset(linenum);
 
400
      int lineLength = getLineLength(linenum);
 
401
      String line = this.getText(lineStart, lineLength);
 
402
 
 
403
      // determine current indentation and number of whitespace characters
 
404
      int wsChars;
 
405
      int indentation = 0;
 
406
      int tabWidth = UserPreferences.getInstance().getTabWidth();
 
407
      for (wsChars = 0; wsChars < line.length(); wsChars++) {
 
408
         char c = line.charAt(wsChars);
 
409
         if (c == ' ') {
 
410
            indentation++;
 
411
         } else if (c == '\t') {
 
412
            indentation += tabWidth;
 
413
         } else {
 
414
            break;
 
415
         }
 
416
      }
 
417
 
 
418
      int newIndentation = indentation + columns;
 
419
      if (newIndentation <= 0) {
 
420
         this.remove(lineStart, wsChars);
 
421
         return;
 
422
      }
 
423
 
 
424
      // build whitespace string for new indentation
 
425
      StringBuilder newWs = new StringBuilder(newIndentation / tabWidth + tabWidth - 1);
 
426
      int ind = 0;
 
427
      for (; ind + tabWidth <= newIndentation; ind += tabWidth) {
 
428
         newWs.append('\t');
 
429
      }
 
430
      for (; ind < newIndentation; ind++) {
 
431
         newWs.append(' ');
 
432
      }
 
433
      this.replace(lineStart, wsChars, newWs.toString(), null);
 
434
   }
357
435
 
358
436
   public Vector getKeywords(){
359
437
      return this.keywords;