001/**
002 * InstanceEditor.java
003 * jCOLIBRI2 framework. 
004 * @author Juan A. Recio-Garc�a.
005 * GAIA - Group for Artificial Intelligence Applications
006 * http://gaia.fdi.ucm.es
007 * 02/11/2007
008 */
009package es.ucm.fdi.gaia.jcolibri.method.gui.editors;
010
011import java.awt.BorderLayout;
012import java.awt.Container;
013import java.awt.Dimension;
014import java.awt.Frame;
015import java.awt.event.ActionEvent;
016import java.awt.event.ActionListener;
017import java.util.Collection;
018
019import javax.swing.Icon;
020import javax.swing.JButton;
021import javax.swing.JComponent;
022import javax.swing.JDialog;
023
024import es.ucm.fdi.gaia.jcolibri.datatypes.Instance;
025import es.ucm.fdi.gaia.jcolibri.exception.OntologyAccessException;
026import es.ucm.fdi.gaia.jcolibri.util.FileIO;
027import es.ucm.fdi.gaia.jcolibri.util.OntoBridgeSingleton;
028import es.ucm.fdi.gaia.ontobridge.OntoBridge;
029import es.ucm.fdi.gaia.ontobridge.test.gui.PnlSelectInstance;
030import org.apache.logging.log4j.LogManager;
031
032/**
033 * Parameter Editor for Instance values.
034 * 
035 * @author Juan A. Recio-Garcia
036 * @version 1.0
037 * @see ParameterEditor
038 */
039public class InstanceEditor extends JButton implements ParameterEditor {
040        private static final long serialVersionUID = 1L;
041
042        
043        private static Icon INSTANCE = new javax.swing.ImageIcon(FileIO.findFile("/es/ucm/fdi/gaia/ontobridge/test/gui/instance.gif"));
044        
045        JDialog ontoDialog;
046        PnlSelectInstance ontoPanel;
047        String selected;
048        
049        /**
050         *  Creates a new instance
051         */
052        public InstanceEditor()
053        {
054                this.setText("...");
055                
056                ontoDialog = new JDialog((Frame)null, true);
057                OntoBridge ob = OntoBridgeSingleton.getOntoBridge();
058                ontoPanel = new PnlSelectInstance(ob);
059                Container main = ontoDialog.getContentPane();
060                main.setLayout(new BorderLayout());
061                main.add(ontoPanel, BorderLayout.CENTER);
062                
063                JButton select = new JButton("Select Instance");
064                select.addActionListener(new ActionListener(){
065                        public void actionPerformed(ActionEvent e) {
066                                ontoDialog.setVisible(false);   
067                        }
068                });
069                main.add(select,BorderLayout.SOUTH);
070                
071                ontoDialog.pack();
072                
073                Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
074                ontoDialog.setBounds((screenSize.width - ontoDialog.getWidth()) / 2,
075                        (screenSize.height - ontoDialog.getHeight()) / 2, 
076                        ontoDialog.getWidth(),
077                        ontoDialog.getHeight());
078                
079                this.addActionListener(new ActionListener(){
080                        public void actionPerformed(ActionEvent e) {
081                                selectInstance();       
082                        }
083                });
084        }
085        
086        
087        void selectInstance()
088        {
089                ontoDialog.setVisible(true);
090                selected = ontoPanel.getSelectedInstance();
091                if(selected==null)
092                {
093                        this.setText("...");
094                        this.setIcon(null);
095                }
096                else
097                {
098                        this.setText(selected);
099                        this.setIcon(INSTANCE);
100                }
101        }
102        
103
104        /**
105         * Returns an Instance object
106         */
107        public Object getEditorValue() {
108            try {
109                if(selected==null)
110                        return null;
111                return new Instance(selected);
112            } catch (OntologyAccessException e) {
113                LogManager.getLogger(this.getClass()).error(e);
114            }
115            return null;
116        }
117
118        /**
119         * Returns the JComponent
120         */
121        public JComponent getJComponent() {
122                return (JComponent) this;
123        }
124
125        /**
126         * Receives an Instance (or String) value
127         */
128        public void setEditorValue(Object value) {
129                if(value==null)
130                {
131                    this.setText("");
132                    this.setIcon(null);
133                    return;
134                } 
135                selected = value.toString();
136                this.setText(selected);
137                this.setIcon(INSTANCE);
138        }
139
140        /**
141         * Does nothing
142         */
143        public void setAllowedValues(Collection<Object> allowedValues)
144        {  
145        }
146}