Package net.helipilot50.stocktrade.displayproject.actions

Source Code of net.helipilot50.stocktrade.displayproject.actions.SelectionRange

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package net.helipilot50.stocktrade.displayproject.actions;

import java.awt.Component;

import javax.swing.ComboBoxEditor;
import javax.swing.JComponent;
import javax.swing.text.JTextComponent;

import net.helipilot50.stocktrade.displayproject.controls.FillInField;
import net.helipilot50.stocktrade.framework.ParameterHolder;


/**
* The GetSelectionRange method returns the starting and ending position values for a text selection range in a character field.
* GetSelectionRange returns two output parameters to indicate the starting and ending positions of a text selection range: StartPosition and EndPosition.
* @author Peter
*
*/
public class SelectionRange extends PendingAction {

    private static final int DEFAULT_VALUE = -1;
    protected int start = 0;
    protected int end = 0;
    private SelectionRange(Component pComponent, int start, int end) {
        super(pComponent);
        this.start = start;
        this.end = end;
    }
    public void performAction() {
//        GridBagConstraints gbc = null;
        if (this._component != null) {
            Component comp = this._component;
            if (comp instanceof FillInField) {
                // If we're setting this value on a combobox which is editable, we set this on the editor
                ComboBoxEditor editor = ((FillInField)comp).getEditor();
                if (editor instanceof Component) {
                    comp = (Component)editor;
                }
            }

            if (comp instanceof JTextComponent){
                JTextComponent jtc = (JTextComponent)comp;
                // Forte indexes the first character as character 1 (which is different to many of the other
                // methods it uses, such as CopyRange) so we're off by one. The same applies to the end too.
                jtc.setSelectionStart(this.start-1);
                if (this.end == DEFAULT_VALUE) {
                    jtc.setSelectionEnd(jtc.getText().length());
                }
                else {
                    jtc.setSelectionEnd(this.end-1);
                }
            }
        }
    }

    public static void set(JComponent comp, int Start, int End){
        ActionMgr.addAction(new SelectionRange(comp, Start, End));
    }

    public static void set(JComponent comp, int Start){
        SelectionRange.set(comp, Start, DEFAULT_VALUE);
    }

    public static void get(JComponent comp, ParameterHolder Start, ParameterHolder End){
        SelectionRange action = ActionMgr.getAction(comp, SelectionRange.class);
        if (action != null) {
            Start.setInt(action.start);
            End.setInt(action.end);
            return;
        }
        if ((comp != null) &&
                (comp instanceof JTextComponent)){
            JTextComponent jtc = (JTextComponent)comp;
            Start.setInt(jtc.getSelectionStart());
            End.setInt(jtc.getSelectionEnd());
        }
    }

}
TOP

Related Classes of net.helipilot50.stocktrade.displayproject.actions.SelectionRange

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.