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 @ 1118

History | View | Annotate | Download (8.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.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
import org.apache.commons.lang3.StringUtils;
34

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

    
54
public class DefaultDynFormSPIManager extends AbstractServiceManager implements DynFormSPIManager {
55

    
56
    private static final Logger logger = LoggerFactory.getLogger(DefaultDynFormSPIManager.class);
57

    
58
    DynFormManager manager = null;
59

    
60
    public DynFormManager getDynFormManager() {
61
        if ( manager == null ) {
62
            manager = DynFormLocator.getDynFormManager();
63
        }
64
        return manager;
65
    }
66

    
67
    public Service getService(DynObject parameters) throws ServiceException {
68
        Service service = this.createService(parameters);
69
        return service;
70
    }
71

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

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

    
80
    public JZoomDialog createJZoomDialog(String title, String message, String text) {
81
        return new DefaultZoomDialog(title, text);
82
    }
83

    
84
    public JProblemIndicator createProblemIndicator(JDynFormField field) {
85
        return new DefaultJProblemIndicator(field);
86
    }
87

    
88
    public String makeServiceName(DataType dataType, String subtype) {
89
        return makeServiceName(dataType, null, subtype);
90
    }
91

    
92
    public String makeServiceName(DataType dataType, String className, String subtype) {
93
        String name;
94
        if( StringUtils.isBlank(className) ) {
95
            name = DynFormSPIManager.SERVICE_NAME_PREFIX
96
                    + dataType.getName()+".any";
97
        } else {
98
            name = DynFormSPIManager.SERVICE_NAME_PREFIX
99
                    + dataType.getName()+"."+ className.trim().toLowerCase();
100
        }
101
        if( !StringUtils.isBlank(subtype) ) {
102
            name += "." + subtype.trim().toLowerCase();
103
        }
104
        return name;
105
    }
106

    
107
    public String makeServiceName(int dataType, String subtype) {
108
        if ( dataType == DataTypes.UNKNOWN ) {
109
            return DynFormSPIManager.SERVICE_NAME_PREFIX + "unknown";
110
        }
111
        return makeServiceName(ToolsLocator.getDataTypesManager().get(dataType), subtype);
112
    }
113

    
114
    public String makeServiceName(int dataType, String className, String subtype) {
115
        if ( dataType == DataTypes.UNKNOWN ) {
116
            return DynFormSPIManager.SERVICE_NAME_PREFIX + "unknown";
117
        }
118
        return makeServiceName(ToolsLocator.getDataTypesManager().get(dataType), className, subtype);
119
    }
120

    
121
    public JDynFormField createJDynFormField(DynFormFieldDefinition definition, Object value) throws ServiceException {
122
        JDynFormField formfield = null;
123
        String serviceName = makeServiceName(definition.getDataType(), definition.getSubtype());
124

    
125
        DynObject params = null;
126
        try {
127
            params = this.createServiceParameters(serviceName);
128
        } catch (NotRegisteredException e) {
129
            if ( definition.getSubtype() == null ) {
130
                serviceName = makeServiceName(DataTypes.UNKNOWN, null);
131
                params = this.createServiceParameters(serviceName);
132
            } else {
133
                serviceName = makeServiceName(definition.getDataType(), null);
134
                try {
135
                    params = this.createServiceParameters(serviceName);
136
                } catch (NotRegisteredException ex) {
137
                    serviceName = makeServiceName(DataTypes.UNKNOWN, null);
138
                    params = this.createServiceParameters(serviceName);
139
                }
140
            }
141
        }
142
        params.setDynValue(FIELD_FIELDDEFINITION, definition);
143
        params.setDynValue(FIELD_VALUE, value);
144
        formfield = (JDynFormField) this.createService(params);
145
        return formfield;
146
    }
147

    
148
    public JPopupMenu createTextFieldPopupMenu(final String title, final JTextComponent component, boolean zoom) {
149
        JPopupMenu popupMenu = new JPopupMenu();
150

    
151
//                JMenuItem textEditorAction = new JMenuItem("Text Editor");
152
//                textEditorAction.addActionListener(new ActionListener() {
153
//                        public void actionPerformed(ActionEvent arg0) {
154
//                                JZoomDialog dialog = DynFormSPILocator.getDynFormSPIManager().createJZoomDialog("Edit the field", null, component.getText());
155
//                                dialog.setAlwaysOnTop(true);
156
//                                dialog.setVisible(true);
157
//                                component.setText(dialog.getText());
158
//                        }
159
//                });
160
        Action textEditorAction = new AbstractAction("Text Editor") {
161
            public void actionPerformed(ActionEvent e) {
162
                JZoomDialog dialog = DynFormSPILocator.getDynFormSPIManager().createJZoomDialog(title, null, component.getText());
163
                dialog.setEditable(component.isEditable());
164
                dialog.setAlwaysOnTop(true);
165
                dialog.setVisible(true);
166
                if ( component.isEditable() && component.isEnabled() ) {
167
                    component.setText(dialog.getText());
168
                }
169
            }
170
        };
171
        Action copyAction = component.getActionMap().get(DefaultEditorKit.copyAction);
172
        Action cutAction = component.getActionMap().get(DefaultEditorKit.cutAction);
173
        Action pasteAction = component.getActionMap().get(DefaultEditorKit.pasteAction);
174
        Action selectAllAction = component.getActionMap().get(DefaultEditorKit.selectAllAction);
175

    
176
        if ( copyAction == null
177
                && cutAction == null
178
                && pasteAction == null
179
                && selectAllAction == null ) {
180
            copyAction = component.getActionMap().get("Copy");
181
            cutAction = component.getActionMap().get("Cut");
182
            pasteAction = component.getActionMap().get("Paste");
183
            selectAllAction = component.getActionMap().get("Select All");
184
        } else {
185
            copyAction.putValue(Action.NAME, "Copy");
186
            cutAction.putValue(Action.NAME, "Cut");
187
            pasteAction.putValue(Action.NAME, "Paste");
188
            selectAllAction.putValue(Action.NAME, "Select All");
189
        }
190

    
191
        if ( zoom ) {
192
            popupMenu.add(textEditorAction);
193
            popupMenu.addSeparator();
194
        }
195
        popupMenu.add(cutAction);
196
        popupMenu.add(copyAction);
197
        popupMenu.add(pasteAction);
198
        popupMenu.add(selectAllAction);
199

    
200
        return popupMenu;
201
    }
202
}