Java JTabbedPane getOffsetOfVirtualColumn(Segment seg, int tabSize, int column, int[] totalVirtualWidth)

Here you can find the source of getOffsetOfVirtualColumn(Segment seg, int tabSize, int column, int[] totalVirtualWidth)

Description

Returns the array offset of a virtual column number (taking tabs into account) in the segment.

License

Open Source License

Parameter

Parameter Description
seg The segment
tabSize The tab size
column The virtual column number
totalVirtualWidth If this array is non-null, the total virtual width will be stored in its first location if this method returns -1.

Return

-1 if the column is out of bounds

Declaration

public static int getOffsetOfVirtualColumn(Segment seg, int tabSize, int column, int[] totalVirtualWidth) 

Method Source Code


//package com.java2s;
/*/*from w  w  w.j  a va 2 s .co m*/
 * StandardUtilities.java - Various miscallaneous utility functions
 * :tabSize=4:indentSize=4:noTabs=false:
 * :folding=explicit:collapseFolds=1:
 *
 * Copyright (C) 1999, 2006 Matthieu Casanova, Slava Pestov
 * Portions copyright (C) 2000 Richard S. Hall
 * Portions copyright (C) 2001 Dirk Moebius
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import javax.swing.text.Segment;

public class Main {
    /**
     * Returns the array offset of a virtual column number (taking tabs
     * into account) in the segment.
     *
     * @param seg The segment
     * @param tabSize The tab size
     * @param column The virtual column number
     * @param totalVirtualWidth If this array is non-null, the total
     * virtual width will be stored in its first location if this method
     * returns -1.
     *
     * @return -1 if the column is out of bounds
     */
    public static int getOffsetOfVirtualColumn(Segment seg, int tabSize, int column, int[] totalVirtualWidth) {
        int virtualPosition = 0;

        for (int i = 0; i < seg.count; i++) {
            char ch = seg.array[seg.offset + i];

            if (ch == '\t') {
                int tabWidth = tabSize - virtualPosition % tabSize;
                if (virtualPosition >= column)
                    return i;
                else
                    virtualPosition += tabWidth;
            } else {
                if (virtualPosition >= column)
                    return i;
                else
                    ++virtualPosition;
            }
        }

        if (totalVirtualWidth != null)
            totalVirtualWidth[0] = virtualPosition;
        return -1;
    }
}

Related

  1. getConfigPanelFromTabbedPane(JTabbedPane mainTabbedPane)
  2. getIndexOf(JTabbedPane tabbedPane, Component component)
  3. getJTabbedPaneAncestor(Component c)
  4. getLineTabs(StyledDocument doc, int startOffset)
  5. getNextSubTabIndex(JTabbedPane tabs, int tabIndex)
  6. getParagraphStyle(SimpleAttributeSet attrSet, int align, float firstLineIndent, float leftIndent, float rightIndent, float lineSpace, float spaceAbove, float spaceBelow, TabSet tabs)
  7. getTabbedPaneComponentIndex(JTabbedPane tabbedPane, String title)
  8. getTabbedPaneFor(Component c)
  9. getTabComponentIndex(JTabbedPane tbp, Component component)