Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.spi / src / main / java / org / gvsig / tools / dynform / spi / dynformfield / JCustomSpinner.java @ 1881

History | View | Annotate | Download (4.05 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.dynform.spi.dynformfield;
25

    
26
import javax.swing.Action;
27
import javax.swing.JComponent;
28
import javax.swing.JPopupMenu;
29
import javax.swing.JSpinner;
30
import javax.swing.text.JTextComponent;
31

    
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
import org.gvsig.tools.dynform.spi.DynFormSPILocator;
36

    
37
/**
38
 * @author gvSIG team
39
 *
40
 */
41
public class JCustomSpinner extends JSpinner{
42

    
43
        /**
44
         *  First approximation to a JSpinner that allows null values.
45
         */
46
        private static final long serialVersionUID = -3949965102298015698L;
47
        private boolean allowNull = false;
48
        private JPopupMenu popupMenu = null;
49
        private JTextComponent jtext = null;
50
        private String title = null;
51

    
52
           protected static final Logger logger = LoggerFactory
53
           .getLogger(JCustomSpinner.class);
54

    
55
        /**
56
         * @param title
57
         */
58
        public JCustomSpinner(String title){
59
            this(title, true);
60
    }
61

    
62
        /**
63
         * @param title
64
         * @param allowNull
65
         */
66
        public JCustomSpinner(String title, boolean allowNull){
67
        super(new CustomSpinnerDateModel(allowNull));
68
        this.title = title;
69
        this.allowNull  = allowNull;
70
        initComponents();
71
    }
72

    
73
        /**
74
         * @param allow
75
         */
76
        public void setAllowNull(boolean allow){
77
                this.allowNull = allow;
78
        }
79

    
80
        /**
81
         * @return true if allow null value
82
         */
83
        public boolean allowNull(){
84
                return this.allowNull;
85
        }
86

    
87
        private void setActionEnabled(String name, boolean enabled) {
88
                Action action = this.getActionMap().get(name);
89
                if( action != null ) {
90
                        action.setEnabled(enabled);
91
                }
92
        }
93

    
94
        public void setEnabled(boolean enabled){
95
                setActionEnabled("Cut", enabled);
96
                setActionEnabled("Paste", enabled);
97
                super.setEnabled(enabled);
98
        }
99

    
100
        /**
101
         * @param enabled
102
         */
103
        public void setEditable(boolean enabled){
104
                setActionEnabled("Cut", enabled);
105
                setActionEnabled("Paste", enabled);
106
                if( this.jtext != null ) {
107
                        this.jtext.setEditable(enabled); // Parece que esto no funciona
108
                                // Asi que nos limitaremos a usar setEnabled.
109
                }
110
                super.setEnabled(enabled);
111
        }
112

    
113
        private void initComponents() {
114
        try {
115
                DefaultEditor x = (DefaultEditor) this.getEditor();
116
                this.jtext = x.getTextField();
117
        } catch(Exception ex) {
118
                // Do norhing
119
        }
120
        if( this.jtext != null ) { // Parece que esto no funciona, los menus no se a?aden.
121
                this.add(getJPopupMenu());
122
                this.setComponentPopupMenu(getJPopupMenu());
123
        }
124
        }
125

    
126
        /* (non-Javadoc)
127
         * @see javax.swing.JSpinner#setEditor(javax.swing.JComponent)
128
         */
129
        @Override
130
        public void setEditor(JComponent arg0) {
131
            super.setEditor(arg0);
132
            this.popupMenu = null;
133
            initComponents();
134
        }
135

    
136
        /**
137
         * @return
138
         */
139
        public JPopupMenu getJPopupMenu(){
140
                if(this.popupMenu == null){
141
                        this.popupMenu = DynFormSPILocator.getDynFormSPIManager().createTextFieldPopupMenu(title, this.jtext, false);
142
                }
143
                return this.popupMenu;
144
        }
145

    
146
        /**
147
         * @param name
148
         * @param action
149
         */
150
        public void addActionToPopupMenu(String name, Action action){
151
                if(name != null && !name.isEmpty()){
152
                        action.putValue(Action.NAME, name);
153
                }
154
                getJPopupMenu().add(action);
155
        }
156

    
157
        /**
158
         *
159
         */
160
        public void addSeparatorToPopupMenu(){
161
                getJPopupMenu().addSeparator();
162
        }
163
}