001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package org.openjump.sigle.plugin.replace;
034:
035: import java.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.util.*;
038:
039: import javax.swing.ImageIcon;
040: import javax.swing.JComboBox;
041: import javax.swing.JOptionPane;
042: import javax.swing.JTextField;
043:
044: import org.openjump.sigle.utilities.gui.DialogUtil;
045:
046: import com.vividsolutions.jump.I18N;
047: import com.vividsolutions.jump.feature.*;
048: import com.vividsolutions.jump.task.*;
049: import com.vividsolutions.jump.workbench.WorkbenchContext;
050: import com.vividsolutions.jump.workbench.model.*;
051: import com.vividsolutions.jump.workbench.plugin.*;
052: import com.vividsolutions.jump.workbench.ui.*;
053: import com.vividsolutions.jump.workbench.ui.images.IconLoader;
054:
055: /**
056: *
057: * @author Erwan Bocher Laboratoire RESO UMR CNRS 6590
058: * @url www.projet-sigle.org
059: * @curentdate 25 juil. 06
060: * @package name org.openjump.sigle.plugin.replace
061: * @license Licence CeCILL http://www.cecill.info/
062: * @todo TODO
063: *
064: */
065:
066: public class ReplaceValuePlugIn extends AbstractPlugIn implements
067: ThreadedPlugIn {
068:
069: private MultiInputDialog dialog;
070: //-- replace later with correct language
071: private static String ATTRIBUTE = GenericNames.ATTRIBUTE;
072: private static String VALUE = I18N
073: .get("org.openjump.sigle.plugin.ReplaceValuePlugIn.New-value");
074: private static String TYPE = "";
075: private static String SELECTED_ONLY = GenericNames.USE_SELECTED_FEATURES_ONLY;
076:
077: private Layer layer;
078: private String attrName;
079: private String value = "";
080: private boolean useSelected = false;
081: public static final ImageIcon ICON = IconLoader.icon("Wrench.gif");
082:
083: public ReplaceValuePlugIn() {
084:
085: }
086:
087: public void initialize(PlugInContext context) {
088:
089: context.getFeatureInstaller().addMainMenuItem(
090: this ,
091: new String[] { MenuNames.TOOLS,
092: MenuNames.TOOLS_EDIT_ATTRIBUTES },
093: this .getName(), false, null,
094: createEnableCheck(context.getWorkbenchContext()));
095:
096: }
097:
098: public String getName() {
099: return I18N
100: .get("org.openjump.sigle.plugin.ReplaceValuePlugIn.Replace-Attribute-Value");
101: }
102:
103: public boolean execute(PlugInContext context) throws Exception {
104:
105: ATTRIBUTE = GenericNames.ATTRIBUTE;
106: VALUE = I18N
107: .get("org.openjump.sigle.plugin.ReplaceValuePlugIn.New-value");
108: SELECTED_ONLY = GenericNames.USE_SELECTED_FEATURES_ONLY;
109:
110: dialog = new MultiInputDialog(context.getWorkbenchFrame(),
111: getName(), true);
112: setDialogValues(dialog, context);
113: if (layer.isEditable()) {
114: GUIUtil.centreOnWindow(dialog);
115: dialog.setVisible(true);
116: if (!dialog.wasOKPressed()) {
117: return false;
118: }
119: getDialogValues(dialog);
120: return true;
121: } else {
122:
123: JOptionPane.showMessageDialog(dialog, I18N
124: .get("ui.SchemaPanel.layer-must-be-editable"));
125: }
126: return false;
127:
128: }
129:
130: public void run(TaskMonitor monitor, PlugInContext context)
131: throws Exception {
132:
133: // input-proofing
134: if (layer == null)
135: return;
136: if (attrName == null)
137: return;
138: if (value == null)
139: return;
140:
141: List srcFeatures = layer.getFeatureCollectionWrapper()
142: .getFeatures();
143:
144: if (useSelected) {
145:
146: Collection featureSelected = context.getLayerViewPanel()
147: .getSelectionManager()
148: .getFeaturesWithSelectedItems(layer);
149: ;
150:
151: //System.out.println("Feature selected");
152: monitor
153: .report(I18N
154: .get("org.openjump.sigle.plugin.ReplaceValuePlugIn.Replacing-values"));
155: replaceValue(featureSelected, attrName, value);
156:
157: }
158:
159: else {
160:
161: //System.out.println("All features");
162: replaceValue(srcFeatures, attrName, value);
163: }
164:
165: }
166:
167: private void setDialogValues(final MultiInputDialog dialog,
168: PlugInContext context) {
169:
170: //Initial layer value is null
171:
172: layer = context.getSelectedLayer(0);
173:
174: // combos sélection d'un champ
175: List columns = DialogUtil
176: .getFieldsFromLayerWithoutGeometry(layer);
177: String column1 = null;
178:
179: if (columns.size() > 0)
180: column1 = (String) columns.get(0); // récupération du premier attribut s'il existe
181:
182: dialog.addComboBox(ATTRIBUTE, column1, columns, "Toto");
183:
184: final JTextField textfield = dialog
185: .addTextField(
186: I18N
187: .get("org.openjump.sigle.plugin.ReplaceValuePlugIn.Attribute-type"),
188: (layer.getFeatureCollectionWrapper()
189: .getFeatureSchema()
190: .getAttributeType(column1)).toString(),
191: 10, null, null);
192: textfield.setEnabled(false);
193:
194: dialog.getComboBox(ATTRIBUTE).addActionListener(
195: new ActionListener() {
196: public void actionPerformed(ActionEvent e) {
197: // recuperation de la combo couche
198: JComboBox cb = (JComboBox) e.getSource();
199: // recuperation de l'attribut selectionné
200: String attributeName = (String) cb
201: .getSelectedItem();
202:
203: textfield.setText((layer
204: .getFeatureCollectionWrapper()
205: .getFeatureSchema()
206: .getAttributeType(attributeName))
207: .toString());
208:
209: }
210: });
211:
212: dialog.addTextField(VALUE, value, 20, null, null);
213:
214: dialog.addCheckBox(SELECTED_ONLY, useSelected);
215:
216: }
217:
218: private void getDialogValues(MultiInputDialog dialog) {
219:
220: attrName = dialog.getText(ATTRIBUTE);
221: value = dialog.getText(VALUE);
222: useSelected = dialog.getBoolean(SELECTED_ONLY);
223:
224: }
225:
226: private void replaceValue(Collection selectedFC, String attrName,
227: String value) {
228:
229: AttributeType type;
230:
231: for (Iterator i = selectedFC.iterator(); i.hasNext();) {
232: Feature f = (Feature) i.next();
233:
234: type = f.getSchema().getAttributeType(attrName);
235:
236: if (type == AttributeType.DOUBLE) {
237:
238: f.setAttribute(attrName, new Double(value));
239:
240: }
241:
242: else if (type == AttributeType.INTEGER)
243:
244: {
245: f.setAttribute(attrName, new Integer(value));
246:
247: } else if (type == AttributeType.STRING) {
248:
249: f.setAttribute(attrName, new String(value));
250:
251: }
252:
253: else {
254:
255: }
256:
257: }
258:
259: }
260:
261: public static MultiEnableCheck createEnableCheck(
262: WorkbenchContext workbenchContext) {
263: EnableCheckFactory checkFactory = new EnableCheckFactory(
264: workbenchContext);
265:
266: return new MultiEnableCheck()
267: .add(checkFactory.createAtLeastNLayersMustExistCheck(1))
268: .add(
269: checkFactory
270: .createSelectedLayersMustBeEditableCheck());
271: }
272:
273: }
|