001: /*
002: * Created on 20.06.2005
003: *
004: * CVS information:
005: * $Author: javamap $
006: * $Date: 2007-06-18 21:15:27 -0700 (Mon, 18 Jun 2007) $
007: * $ID$
008: * $Rev: 856 $
009: * $Id: EditAttributeByFormulaPlugIn.java 856 2007-06-19 04:15:27Z javamap $
010: * $Log$
011: * Revision 1.4 2007/03/24 19:33:19 mentaer
012: * changed ui, so that pirol formula propeties file is not assumed (used formulas are not stored)
013: *
014: * Revision 1.3 2007/03/24 18:13:11 mentaer
015: * changed to inherit AbstractPlugIn instead of StandardPirolPlugIn, subsequently changed also to normal logger
016: *
017: * Revision 1.2 2007/02/03 14:19:47 mentaer
018: * modified debug output for pirol stuff
019: *
020: * Revision 1.1 2006/11/23 18:53:15 mentaer
021: * added EditAttributeByFormula Plugin by Pirol including some parts of the baseclasses - note: plugin needs java 1.5
022: *
023: * Revision 1.6 2006/11/05 14:28:05 mentaer
024: * translated pirol attribute calculator plugin
025: *
026: * Revision 1.5 2006/11/05 13:47:35 mentaer
027: * refactoring of menu positions and set OJ version to 1.1 B
028: *
029: * Revision 1.4 2006/11/04 19:22:42 mentaer
030: * changed menu position
031: *
032: * Revision 1.3 2006/11/04 19:16:09 mentaer
033: * changed enable check
034: *
035: * Revision 1.2 2006/11/04 19:11:58 mentaer
036: * *** empty log message ***
037: *
038: * Revision 1.1 2006/11/04 19:09:34 mentaer
039: * added Pirol Plugin for Attribute Calculations for testing, which needs the baseclasses.jar
040: *
041: * Revision 1.12 2006/05/09 14:31:39 orahn
042: * kleiner GUI verschönerung
043: *
044: * Revision 1.11 2006/02/01 17:35:56 orahn
045: * + Unterstützung f. Attributnamen mit Leerzeichen
046: * + kleines, generelles Update für das PlugIn
047: *
048: * Revision 1.10 2005/08/03 13:50:44 orahn
049: * +i18n
050: * -warnings
051: *
052: * Revision 1.9 2005/07/13 10:12:55 orahn
053: * Einsatz: MetaInformationHandler
054: *
055: * Revision 1.8 2005/07/12 16:33:56 orahn
056: * +Nutzung des PropertiesHandler
057: *
058: * Revision 1.7 2005/06/30 10:42:12 orahn
059: * besseres Fehler-Feedback bei der Formeleingabe
060: *
061: * Revision 1.6 2005/06/30 08:37:40 orahn
062: * misslungene Formel beschädigt nicht mehr das Layer
063: *
064: * Revision 1.5 2005/06/29 16:03:57 orahn
065: * aufgemotzt
066: *
067: * Revision 1.4 2005/06/28 15:35:18 orahn
068: * fast soweit: es fehlt noch eine "taste" wie backspace für ganze operatoren bzw. operanden
069: * und das Formel-Text-Feld muß gegen direkte User-Eingaben geschützt werden
070: *
071: * Revision 1.3 2005/06/23 13:57:20 orahn
072: * nutzt jetzt uniqueAttributeName
073: *
074: * Revision 1.2 2005/06/23 13:41:17 orahn
075: * erste BETA des Formel-Parser-PlugIns
076: *
077: * Revision 1.1 2005/06/20 18:17:34 orahn
078: * erster Ansatz
079: *
080: */
081: package de.fho.jump.pirol.plugins.EditAttributeByFormula;
082:
083: import java.io.FileNotFoundException;
084: import java.io.IOException;
085:
086: import org.apache.log4j.Logger;
087:
088: import com.vividsolutions.jump.I18N;
089: import com.vividsolutions.jump.feature.FeatureCollection;
090: import com.vividsolutions.jump.workbench.WorkbenchContext;
091: import com.vividsolutions.jump.workbench.model.Layer;
092: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
093: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
094: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
095: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
096: import com.vividsolutions.jump.workbench.ui.MenuNames;
097:
098: import de.fho.jump.pirol.utilities.FormulaParsing.FormulaValue;
099: import de.fho.jump.pirol.utilities.Properties.PropertiesHandler;
100: import de.fho.jump.pirol.utilities.apiTools.FeatureCollectionTools;
101: import de.fho.jump.pirol.utilities.attributes.AttributeInfo;
102: import de.fho.jump.pirol.utilities.metaData.MetaInformationHandler;
103: import de.fho.jump.pirol.utilities.plugIns.StandardPirolPlugIn;
104: import de.fho.jump.pirol.utilities.settings.PirolPlugInSettings;
105:
106: /**
107: *
108: * PlugIn that creates a new attribute and assigns values to it, that are
109: * calculated by processing a formula that was created by the user.
110: *
111: * @author Ole Rahn
112: * <br>
113: * <br>FH Osnabrück - University of Applied Sciences Osnabrück,
114: * <br>Project: PIROL (2005),
115: * <br>Subproject: Daten- und Wissensmanagement
116: *
117: * @version $Rev: 856 $
118: */
119: //--[sstein 24.March.2007] changed to normal plugin - thus we do not need
120: // to load and create the properties file
121: //public class EditAttributeByFormulaPlugIn extends StandardPirolPlugIn {
122: public class EditAttributeByFormulaPlugIn extends AbstractPlugIn {
123:
124: protected static PropertiesHandler storedFormulas = null;
125: protected static final String storedFormulasFileName = "Formula.properties"; //$NON-NLS-1$
126: //[sstein 24.March.2007] added this logger instead using Personal logger
127: private static final Logger LOG = Logger
128: .getLogger(EditAttributeByFormulaPlugIn.class);
129:
130: public void initialize(PlugInContext context) throws Exception {
131: context.getFeatureInstaller().addMainMenuItemWithJava14Fix(
132: this ,
133: new String[] { MenuNames.TOOLS,
134: MenuNames.TOOLS_EDIT_ATTRIBUTES },
135: this .getName(), false, null,
136: createEnableCheck(context.getWorkbenchContext()));
137: }
138:
139: public String getName() {
140: return I18N
141: .get("pirol.plugIns.EditAttributeByFormulaPlugIn.Attribute-Calculator");
142: }
143:
144: public static MultiEnableCheck createEnableCheck(
145: WorkbenchContext workbenchContext) {
146: EnableCheckFactory checkFactory = new EnableCheckFactory(
147: workbenchContext);
148:
149: return new MultiEnableCheck()
150: .add(
151: checkFactory
152: .createAtLeastNLayersMustBeSelectedCheck(1))
153: .add(
154: checkFactory
155: .createSelectedLayersMustBeEditableCheck());
156: }
157:
158: /* //-- [sstein 24.March 2007] disabled since we make it to a
159: // normal plugin, not a StandardPirolPlugin
160: public EditAttributeByFormulaPlugIn(){
161: super(new PersonalLogger(DebugUserIds.ALL));
162: }
163: */
164:
165: /**
166: * @inheritDoc
167: */
168: public String getIconString() {
169: return null;
170: }
171:
172: /**
173: *@inheritDoc
174: */
175: public String getCategoryName() {
176: return PirolPlugInSettings.getName_AttributeMenu();
177: }
178:
179: /**
180: * @inheritDoc
181: */
182: public boolean execute(PlugInContext context) throws Exception {
183: Layer layer = StandardPirolPlugIn.getSelectedLayer(context);
184:
185: if (layer == null) {
186: StandardPirolPlugIn
187: .warnUser(
188: context,
189: I18N
190: .get("pirol.plugIns.EditAttributeByFormulaPlugIn.no-layer-selected")); //$NON-NLS-1$
191: //--[sstein 24.March 2007]: disabled since I changed from StandardPirolPlugIn to AbstractPlugIn
192: return false;
193: //return this.finishExecution(context, false);
194: } else if (!layer.isEditable()) {
195: StandardPirolPlugIn
196: .warnUser(
197: context,
198: I18N
199: .get("pirol.plugIns.EditAttributeByFormulaPlugIn.layer-not-editable")); //$NON-NLS-1$
200: //--[sstein 24.March 2007] disabled since I changed from StandardPirolPlugIn to AbstractPlugIn
201: //return this.finishExecution(context, false);
202: return false;
203: }
204:
205: try {
206: //--[sstein 24.March 2007] currently the following line is useless
207: EditAttributeByFormulaPlugIn.storedFormulas = new PropertiesHandler(
208: EditAttributeByFormulaPlugIn.storedFormulasFileName);
209: //--[sstein 24.March 2007] disabled - we dont assume an existing file
210: //EditAttributeByFormulaPlugIn.storedFormulas.load();
211: }
212: /* catch (FileNotFoundException e1) {
213: //this.logger.printWarning(e1.getMessage());
214: this.LOG.warn(e1.getMessage());
215: }
216: catch (IOException e1) {
217: //this.logger.printWarning(e1.getMessage());
218: this.LOG.warn(e1.getMessage());
219: }
220: **/
221: catch (Exception e1) {
222: //this.logger.printWarning(e1.getMessage());
223: this .LOG.warn(e1.getMessage());
224: }
225:
226: /* [sstein 24.March 2007] replaced - since we dont have stored formulas
227: EditAttributeByFormulaDialog dialog = new EditAttributeByFormulaDialog(context.getWorkbenchFrame(),
228: I18N.get("pirol.plugIns.EditAttributeByFormulaPlugIn.specify-attribute-and-formula"),
229: true,
230: I18N.get("pirol.plugIns.EditAttributeByFormulaPlugIn.editByFormula-explaining-text"),
231: layer.getFeatureCollectionWrapper().getFeatureSchema(),
232: EditAttributeByFormulaPlugIn.storedFormulas); //$NON-NLS-1$ //$NON-NLS-2$
233: */
234: EditAttributeByFormulaDialog dialog = new EditAttributeByFormulaDialog(
235: context.getWorkbenchFrame(),
236: I18N
237: .get("pirol.plugIns.EditAttributeByFormulaPlugIn.specify-attribute-and-formula"),
238: true,
239: I18N
240: .get("pirol.plugIns.EditAttributeByFormulaPlugIn.editByFormula-explaining-text"),
241: layer.getFeatureCollectionWrapper().getFeatureSchema());
242:
243: dialog.setVisible(true);
244:
245: String formula = dialog.getFormula();
246:
247: if (!dialog.wasOkClicked() || formula == null
248: || formula.length() == 0) {
249: //--[sstein 24.March 2007]: disabled since I changed from StandardPirolPlugIn to AbstractPlugIn
250: //return this.finishExecution(context, false);
251: return false;
252: }
253:
254: AttributeInfo attrInfo = dialog.getAttributeInfo();
255:
256: FeatureCollection oldFc = layer.getFeatureCollectionWrapper()
257: .getUltimateWrappee();
258:
259: attrInfo.setUniqueAttributeName(FeatureCollectionTools
260: .getUniqueAttributeName(oldFc, attrInfo
261: .getAttributeName()));
262:
263: try {
264: FormulaValue parsedFormula = dialog.getParsedFormula();
265:
266: FeatureCollection newFc = FeatureCollectionTools
267: .applyFormulaToFeatureCollection(oldFc, attrInfo,
268: parsedFormula, true);
269: layer.setFeatureCollection(newFc);
270:
271: MetaInformationHandler metaInfHandler = new MetaInformationHandler(
272: layer);
273: metaInfHandler
274: .addMetaInformation(
275: I18N
276: .get("pirol.plugIns.EditAttributeByFormulaPlugIn.formula-for")
277: + attrInfo.getUniqueAttributeName(),
278: formula); //$NON-NLS-1$
279:
280: if (storedFormulas != null) {
281: storedFormulas
282: .setProperty(attrInfo.toString(), formula);
283: storedFormulas
284: .store(I18N
285: .get("pirol.plugIns.EditAttributeByFormulaPlugIn.editByFormula-properties-comment")); //$NON-NLS-1$
286: }
287:
288: } catch (Exception e) {
289: //--[sstein 24.March 2007]: disabled since I changed from StandardPirolPlugIn to AbstractPlugIn
290: //this.handleThrowable(e);
291: LOG.debug(e.getMessage());
292: e.printStackTrace();
293: //return this.finishExecution(context, false);
294: return false;
295: }
296: //--[sstein 24.March 2007]: disabled since I changed from StandardPirolPlugIn to AbstractPlugIn
297: //return this.finishExecution(context, true);
298: return false;
299: }
300:
301: }
|