001: package com.vividsolutions.jump.workbench.ui.toolbox;
002:
003: import com.vividsolutions.jump.feature.FeatureCollection;
004: import com.vividsolutions.jump.workbench.model.Layer;
005: import com.vividsolutions.jump.workbench.model.LayerManagerProxy;
006: import com.vividsolutions.jump.workbench.model.LayerStyleUtil;
007: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
008: import com.vividsolutions.jump.workbench.plugin.ThreadedBasePlugIn;
009: import com.vividsolutions.jump.workbench.ui.plugin.AddNewLayerPlugIn;
010:
011: import java.awt.Color;
012: import java.awt.Component;
013:
014: import javax.swing.JOptionPane;
015: import javax.swing.SwingUtilities;
016:
017: /**
018: * Convenience superclass used in toolboxes that have one primary button.
019: */
020: public abstract class MainButtonPlugIn extends ThreadedBasePlugIn {
021: public static final String GENERATED_KEY = MainButtonPlugIn.class
022: .getName()
023: + " - GENERATED";
024: private String taskMonitorTitle;
025: private Component toolboxPanel;
026:
027: public MainButtonPlugIn(String taskMonitorTitle,
028: Component toolboxPanel) {
029: this .taskMonitorTitle = taskMonitorTitle;
030: this .toolboxPanel = toolboxPanel;
031: }
032:
033: public String getName() {
034: return taskMonitorTitle;
035: }
036:
037: protected Layer generateLayer(String name, String category,
038: Color color, LayerManagerProxy proxy,
039: FeatureCollection featureCollection, String description) {
040: return generateLayer(false, name, category, color, proxy,
041: featureCollection, description);
042: }
043:
044: protected Layer generateLineLayer(String name, String category,
045: Color color, LayerManagerProxy proxy,
046: FeatureCollection featureCollection, String description) {
047: return generateLayer(true, name, category, color, proxy,
048: featureCollection, description);
049: }
050:
051: private Layer generateLayer(boolean line, String name,
052: String category, Color color, LayerManagerProxy proxy,
053: FeatureCollection featureCollection, String description) {
054: Layer layer = proxy.getLayerManager().getLayer(name);
055:
056: if (layer == null) {
057: layer = new Layer(name, color, featureCollection, proxy
058: .getLayerManager());
059: proxy.getLayerManager().addLayer(category, layer);
060: layer.setVisible(false);
061:
062: if (line) {
063: LayerStyleUtil.setLinearStyle(layer, color, 2, 4);
064: }
065:
066: } else {
067: layer.setFeatureCollection(featureCollection);
068: }
069:
070: layer.setDescription(description);
071:
072: //May have been loaded from a file [Jon Aquino]
073: layer.getBlackboard().put(GENERATED_KEY, new Object());
074:
075: return layer;
076: }
077:
078: public boolean execute(PlugInContext context) throws Exception {
079: if (validateInput() != null) {
080: reportValidationError(validateInput());
081: }
082:
083: return validateInput() == null;
084: }
085:
086: private void reportValidationError(String errorMessage) {
087: JOptionPane.showMessageDialog(SwingUtilities
088: .windowForComponent(toolboxPanel), errorMessage,
089: "JUMP", JOptionPane.ERROR_MESSAGE);
090: }
091:
092: protected void removeAndDisposeLayer(String name,
093: PlugInContext context) {
094: Layer layer = context.getLayerManager().getLayer(name);
095:
096: if (layer == null) {
097: return;
098: }
099:
100: context.getLayerManager().remove(layer);
101: layer.dispose();
102: }
103:
104: public abstract String validateInput();
105: }
|