Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.impl / src / main / java / org / gvsig / tools / dynform / impl / DefaultDynFormSPIManager.java @ 1031

History | View | Annotate | Download (6.88 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.impl;
25

    
26
import java.awt.event.ActionEvent;
27

    
28
import javax.swing.AbstractAction;
29
import javax.swing.Action;
30
import javax.swing.JPopupMenu;
31
import javax.swing.text.DefaultEditorKit;
32
import javax.swing.text.JTextComponent;
33

    
34
import org.gvsig.tools.ToolsLocator;
35
import org.gvsig.tools.dataTypes.DataType;
36
import org.gvsig.tools.dataTypes.DataTypes;
37
import org.gvsig.tools.dynform.DynFormFieldDefinition;
38
import org.gvsig.tools.dynform.DynFormLocator;
39
import org.gvsig.tools.dynform.DynFormManager;
40
import org.gvsig.tools.dynform.JDynFormField;
41
import org.gvsig.tools.dynform.spi.DynFormSPILocator;
42
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
43
import org.gvsig.tools.dynform.spi.dynformfield.JProblemIndicator;
44
import org.gvsig.tools.dynform.spi.dynformfield.JZoomDialog;
45
import org.gvsig.tools.dynobject.DynObject;
46
import org.gvsig.tools.service.Service;
47
import org.gvsig.tools.service.ServiceException;
48
import org.gvsig.tools.service.spi.AbstractServiceManager;
49
import org.gvsig.tools.service.spi.NotRegisteredException;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53
public class DefaultDynFormSPIManager extends AbstractServiceManager  implements DynFormSPIManager {
54

    
55
        private static final Logger logger = LoggerFactory.getLogger(DefaultDynFormSPIManager.class);
56
        
57
        DynFormManager manager = null;
58
        
59
        public DynFormManager getDynFormManager() {
60
                if( manager == null ) {
61
                        manager = DynFormLocator.getDynFormManager();
62
                }
63
                return manager;
64
        }
65
        
66
        public Service getService(DynObject parameters) throws ServiceException {
67
                JDynFormField  formfield = (JDynFormField) this.createService(parameters);
68
            return formfield;
69
        }
70

    
71
        protected String getRegistryKey() {
72
                return "DefaultDynFormSPIManager";
73
        }
74

    
75
        protected String getRegistryDescription() {
76
                return "Dynamics forms default SPI manager";
77
        }
78

    
79
        public JZoomDialog createJZoomDialog(String title, String message, String text) {
80
                return new DefaultZoomDialog(title, text);
81
        }
82
        
83
        public JProblemIndicator createProblemIndicator(JDynFormField field) {
84
                return new DefaultJProblemIndicator(field);
85
        }
86
        
87
    public String makeServiceName(DataType dataType, String subtype) {
88
            String name = null;
89
            if( subtype==null || subtype.trim().length()==0 ) {
90
                    name =  DynFormSPIManager.SERVICE_NAME_PREFIX + dataType.getName(); 
91
            }else{
92
                    name = DynFormSPIManager.SERVICE_NAME_PREFIX + 
93
                                dataType.getName() + "." +
94
                                subtype.trim().toLowerCase();
95
            }
96
            logger.info("----------- Name :"+name);
97
            
98
                return name;
99
    }
100
    
101
    public String makeServiceName(int dataType, String subtype) {
102
            if( dataType == DataTypes.UNKNOWN ) {
103
                    return DynFormSPIManager.SERVICE_NAME_PREFIX + "unknown"; 
104
            }
105
            return makeServiceName(ToolsLocator.getDataTypesManager().get(dataType), subtype);
106
    }
107
    
108
    public JDynFormField createJDynFormField(DynFormFieldDefinition definition,Object value) throws ServiceException  {
109
            JDynFormField formfield = null;
110
            String serviceName = makeServiceName(definition.getDataType(), definition.getSubtype()); 
111
            
112
            DynObject params = null;
113
            try {
114
                        params = this.createServiceParameters(serviceName);
115
                } catch (NotRegisteredException e) {
116
                        if( definition.getSubtype()==null ) {
117
                            serviceName = makeServiceName(DataTypes.UNKNOWN, null);
118
                            params = this.createServiceParameters(serviceName);
119
                        } else {
120
                                serviceName = makeServiceName(definition.getDataType(), null);
121
                                try {
122
                                        params = this.createServiceParameters(serviceName);
123
                                } catch (NotRegisteredException ex) {
124
                                    serviceName = makeServiceName(DataTypes.UNKNOWN, null);
125
                                    params = this.createServiceParameters(serviceName);
126
                                }
127
                        }
128
                }
129
            params.setDynValue(FIELD_FIELDDEFINITION, definition);
130
            params.setDynValue(FIELD_VALUE, value);
131
            formfield = (JDynFormField) this.createService(params);
132
            return formfield;
133
    }
134

    
135

    
136
    public JPopupMenu createTextFieldPopupMenu(final String title, final JTextComponent component, boolean zoom) {
137
                JPopupMenu popupMenu = new JPopupMenu();
138
                
139
//                JMenuItem textEditorAction = new JMenuItem("Text Editor");
140
//                textEditorAction.addActionListener(new ActionListener() {
141
//                        public void actionPerformed(ActionEvent arg0) {
142
//                                JZoomDialog dialog = DynFormSPILocator.getDynFormSPIManager().createJZoomDialog("Edit the field", null, component.getText());
143
//                                dialog.setAlwaysOnTop(true);
144
//                                dialog.setVisible(true);
145
//                                component.setText(dialog.getText());
146
//                        }
147
//                });
148
                
149
                Action textEditorAction = new AbstractAction("Text Editor") {
150
                        public void actionPerformed(ActionEvent e) {
151
                                JZoomDialog dialog = DynFormSPILocator.getDynFormSPIManager().createJZoomDialog(title, null, component.getText());
152
                                dialog.setEditable(component.isEditable());
153
                                dialog.setAlwaysOnTop(true);
154
                                dialog.setVisible(true);
155
                                if( component.isEditable() && component.isEnabled() ) {
156
                                        component.setText(dialog.getText());
157
                                }
158
                        }
159
                };
160
                Action copyAction = component.getActionMap().get(DefaultEditorKit.copyAction);
161
                Action cutAction = component.getActionMap().get(DefaultEditorKit.cutAction);
162
                Action pasteAction = component.getActionMap().get(DefaultEditorKit.pasteAction);
163
                Action selectAllAction = component.getActionMap().get(DefaultEditorKit.selectAllAction);
164
                
165
                if(copyAction == null  &&
166
                                cutAction == null &&
167
                                pasteAction == null &&
168
                                selectAllAction == null ) {
169
                        copyAction = component.getActionMap().get("Copy");
170
                        cutAction = component.getActionMap().get("Cut");
171
                        pasteAction = component.getActionMap().get("Paste");
172
                        selectAllAction = component.getActionMap().get("Select All");
173
                } else {
174
                        copyAction.putValue(Action.NAME, "Copy");
175
                        cutAction.putValue(Action.NAME, "Cut");
176
                        pasteAction.putValue(Action.NAME, "Paste");
177
                        selectAllAction.putValue(Action.NAME, "Select All");
178
                }
179
                 
180
                if(zoom){
181
                        popupMenu.add(textEditorAction);
182
                        popupMenu.addSeparator();
183
                }
184
                popupMenu.add(cutAction);
185
                popupMenu.add(copyAction);
186
                popupMenu.add(pasteAction);
187
                popupMenu.add(selectAllAction);
188
                
189
                return popupMenu;
190
    }
191
}