All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jclarion.clarion.control.OptionControl Maven / Gradle / Ivy

/**
 * Copyright 2010, by Andrew Barnham
 *
 * The contents of this file are subject to
 * GNU Lesser General Public License (LGPL), v.3
 * http://www.gnu.org/licenses/lgpl.txt
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 */
package org.jclarion.clarion.control;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import org.jclarion.clarion.ClarionNumber;
import org.jclarion.clarion.ClarionObject;
import org.jclarion.clarion.ClarionString;
import org.jclarion.clarion.constants.Create;
import org.jclarion.clarion.constants.Prop;
import org.jclarion.clarion.runtime.CWinImpl;
import org.jclarion.clarion.swing.ClarionBorder;
import org.jclarion.clarion.swing.ClarionLayoutManager;

public class OptionControl extends AbstractControl implements SimpleMnemonicAllowed  {

    private List controls=new ArrayList();
    
    @Override
    public void addChild(AbstractControl control)
    {
        add((RadioControl)control);
    }
    
    public void add(RadioControl control)
    {
        controls.add(control);
        control.setParent(this);
        if (control.getValue()==null) control.setValue(String.valueOf(controls.size()));
    }
    
    public Collection getChildren()
    {
        return controls;
    }

    public Collection getRadioButtons() {
        return controls;
    }
    
    @Override
    public boolean isAcceptAllControl() {
        return true;
    }

    @Override
    public boolean validateInput() {
        return true;
    }
    
    @Override
    public int getCreateType() {
        return Create.OPTION;
    }
    

    private JPanel panel;
    

    @Override
    public void clearMetaData() {
        this.panel=null;
        super.clearMetaData();
    }

    @Override
    protected void debugMetaData(StringBuilder sb) {
        super.debugMetaData(sb);
        debugMetaData(sb,"panel",panel);
    }
    
    @Override
    public void constructSwingComponent(Container parent) {
        panel = new JPanel();
        panel.setOpaque(false);
        panel.setLayout(new ClarionLayoutManager());
        parent.add(panel);
        panel.setBorder(new ClarionBorder(this, -1, 1, true));

        if (getUseObject() == null) {
            use(new ClarionString());
        }

        KeyListener radioKey = new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {

                int dir = 0;

                if (e.getKeyCode() == KeyEvent.VK_DOWN
                        || e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    dir = 1;
                }

                if (e.getKeyCode() == KeyEvent.VK_UP
                        || e.getKeyCode() == KeyEvent.VK_LEFT) {
                    dir = -1;
                }

                if (dir == 0)
                    return;

                JComponent _r = (JComponent) e.getComponent();
                RadioControl r = (RadioControl) _r.getClientProperty("clarion");

                OptionControl g = (OptionControl) r.getParent();
                Collection _c = g.getChildren();
                RadioControl c[] = new RadioControl[_c.size()];
                _c.toArray(c);

                // Container g = r.getParent();
                // Component c[] = g.getComponents();

                int scan = 0;
                while (scan < c.length) {
                    if (c[scan] == r) {
                        break;
                    }
                    scan++;
                }
                if (scan == c.length) return;

                for (int nxt = 1; nxt < c.length; nxt++) {
                    int test = (c.length + nxt * dir + scan) % c.length;
                    if (!c[test].getComponent().isEnabled()) continue;
                    if (!c[test].getComponent().isVisible()) continue;

                    RadioControl rc = c[test];
                    rc.doClickOnFocus();
                    c[test].getComponent().requestFocusInWindow();
                    return;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyTyped(KeyEvent e) {
            }
        };

        int count = 0;
        for (final RadioControl rc : getRadioButtons()) {
            count++;
            if (rc.getValue() == null) rc.setValue(String.valueOf(count));
            rc.constructSwingComponent(parent);
            rc.getButton().addKeyListener(radioKey);
        }

        configureFont(panel);
        configureColor(panel);
        setPositionAndState();

    }

    protected JToggleButton getToggleButton() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ClarionObject getLocalProperty(int index) {
        
        if (index == Prop.SELSTART) 
        {
            int count = 0;
            for (RadioControl rc : getRadioButtons()) {
                count++;
                if (getUseObject().compareTo(rc.getValue()) == 0) {
                    return new ClarionNumber(count);
                }
            }
        }
        return super.getLocalProperty(index);
    }

    @Override
    protected void notifyLocalChange(int indx, ClarionObject value) {
        
        if (indx==Prop.SELSTART) {
            
            int ofs = value.intValue();
            if (ofs<1 || ofs > getChildren().size()) return;
            final RadioControl rc = controls.get(ofs-1);
            
            CWinImpl.run(new Runnable() {
               public void run()
               {
                   rc.setForcedRadioSelect();
                   rc.getButton().doClick();
               }
            });
        }
        
        super.notifyLocalChange(indx, value);
    }

    @Override
    public Component getComponent() {
        return panel;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy