001: package com.vividsolutions.jump.workbench.ui.plugin.imagery;
002:
003: import java.io.File;
004: import java.util.*;
005:
006: import javax.swing.JFileChooser;
007: import javax.swing.JOptionPane;
008: import javax.swing.filechooser.FileFilter;
009:
010: import com.vividsolutions.jts.geom.Coordinate;
011: import com.vividsolutions.jts.geom.GeometryFactory;
012: import com.vividsolutions.jts.util.Assert;
013: import com.vividsolutions.jump.I18N;
014: import com.vividsolutions.jump.feature.BasicFeature;
015: import com.vividsolutions.jump.feature.Feature;
016: import com.vividsolutions.jump.util.Block;
017: import com.vividsolutions.jump.util.CollectionUtil;
018: import com.vividsolutions.jump.util.FileUtil;
019: import com.vividsolutions.jump.util.OrderedMap;
020: import com.vividsolutions.jump.workbench.imagery.ImageryLayerDataset;
021: import com.vividsolutions.jump.workbench.imagery.ReferencedImage;
022: import com.vividsolutions.jump.workbench.imagery.ReferencedImageFactory;
023: import com.vividsolutions.jump.workbench.imagery.ReferencedImageStyle;
024: import com.vividsolutions.jump.workbench.model.Layer;
025: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
026: import com.vividsolutions.jump.workbench.ui.GUIUtil;
027: import com.vividsolutions.jump.workbench.ui.GenericNames;
028: import com.vividsolutions.jump.workbench.ui.plugin.PersistentBlackboardPlugIn;
029: import com.vividsolutions.jump.util.Blackboard;
030:
031: public class ImageFeatureCreator {
032:
033: public Collection getImages(PlugInContext context, Layer layer) {
034: final String LAST_DIRECTORY_KEY = getClass().getName()
035: + " - LAST DIRECTORY";
036: final String LAST_FILE_FILTER_DESCRIPTION_KEY = getClass()
037: .getName()
038: + " - LAST FILE FILTER DESCRIPTION";
039:
040: Blackboard blackboard = PersistentBlackboardPlugIn.get(context
041: .getWorkbenchContext());
042: JFileChooser fileChooser = GUIUtil
043: .createJFileChooserWithExistenceChecking();
044:
045: fileChooser.setCurrentDirectory(new File((String) blackboard
046: .get(LAST_DIRECTORY_KEY, fileChooser
047: .getCurrentDirectory().getPath())));
048: GUIUtil.removeChoosableFileFilters(fileChooser);
049:
050: OrderedMap fileFilterToReferencedImageFactoryMap = getFileFilterToReferencedImageFactoryMap(insertCompositeReferencedImageFactory(context
051: .getWorkbenchContext().getRegistry().getEntries(
052: ReferencedImageFactory.REGISTRY_CLASSIFICATION)));
053:
054: for (Iterator i = fileFilterToReferencedImageFactoryMap
055: .keyList().iterator(); i.hasNext();) {
056: FileFilter fileFilter = (FileFilter) i.next();
057: fileChooser.addChoosableFileFilter(fileFilter);
058: }
059: fileChooser
060: .setFileFilter((FileFilter) fileFilterToReferencedImageFactoryMap
061: .keyList().get(0));
062:
063: for (Iterator i = Arrays.asList(
064: fileChooser.getChoosableFileFilters()).iterator(); i
065: .hasNext();) {
066: FileFilter fileFilter = (FileFilter) i.next();
067: if (fileFilter.getDescription().equals(
068: blackboard
069: .get(LAST_FILE_FILTER_DESCRIPTION_KEY, ""))) {
070: fileChooser.setFileFilter(fileFilter);
071: break;
072: }
073: }
074: fileChooser.setMultiSelectionEnabled(true);
075:
076: Collection selectedFeatures = null;
077:
078: // Show modal here
079: if (JFileChooser.APPROVE_OPTION == fileChooser
080: .showOpenDialog(context.getWorkbenchFrame())) {
081: blackboard.put(LAST_DIRECTORY_KEY, fileChooser
082: .getCurrentDirectory().getPath());
083: blackboard.put(LAST_FILE_FILTER_DESCRIPTION_KEY,
084: fileChooser.getFileFilter().getDescription());
085:
086: File[] selectedFiles = fileChooser.getSelectedFiles();
087:
088: if (selectedFiles != null && selectedFiles.length > 0) {
089: ReferencedImageFactory rif = (ReferencedImageFactory) fileFilterToReferencedImageFactoryMap
090: .get(fileChooser.getFileFilter());
091:
092: if (rif.isAvailable()) {
093: selectedFeatures = createFeatures(rif,
094: selectedFiles,
095: getImageryLayerDataset(layer));
096: } else {
097: Object[] options = { "OK" };
098: JOptionPane
099: .showOptionDialog(
100: null,
101: I18N
102: .get("ui.plugin.imagery.ImageFeatureCreator.The-driver-for")
103: + " "
104: + rif.getTypeName()
105: + " "
106: + I18N
107: .get("ui.plugin.imagery.ImageFeatureCreator.is-not-available-Please-check-your-configuration"),
108: I18N
109: .get("ui.plugin.imagery.ImageFeatureCreator.Warning"),
110: JOptionPane.DEFAULT_OPTION,
111: JOptionPane.WARNING_MESSAGE, null,
112: options, options[0]);
113: }
114: }
115: }
116:
117: return selectedFeatures;
118: }
119:
120: /**
121: * Sets whether an imagery layer is selectable, editable, and read-only.
122: * The policy used is that a imagery layer is selectable/editable iff:
123: * <ul>
124: * <li>There is a single image in the layer
125: * <li>The image type is editable
126: * </ul>
127: *
128: * @param layer the layer to update
129: */
130: public void setLayerSelectability(Layer layer) {
131: boolean selectable = false;
132: Collection features = layer.getFeatureCollectionWrapper()
133: .getFeatures();
134:
135: if (features.size() == 1) {
136: Feature f = (Feature) features.iterator().next();
137: String imageFilePath = (String) f
138: .getAttribute(ImageryLayerDataset.ATTR_FILE);
139: String factoryClassPath = (String) f
140: .getAttribute(ImageryLayerDataset.ATTR_FACTORY);
141:
142: if (imageFilePath != null && factoryClassPath != null) {
143: try {
144: ReferencedImageFactory imageFactory = (ReferencedImageFactory) Class
145: .forName(factoryClassPath).newInstance();
146: selectable = imageFactory
147: .isEditableImage(imageFilePath);
148: } catch (Exception ex) {
149: // do nothing
150: }
151: }
152: }
153:
154: layer.setSelectable(selectable);
155: layer.setEditable(selectable);
156: layer.setReadonly(!selectable);
157: }
158:
159: private ReferencedImageFactory getReferencedImageFactoryProper(
160: ReferencedImageFactory factory, File file) {
161: return factory instanceof CompositeReferencedImageFactory ? ((CompositeReferencedImageFactory) factory)
162: .referencedImageFactory(FileUtil.getExtension(file))
163: : factory;
164: }
165:
166: private ImageryLayerDataset getImageryLayerDataset(Layer layer) {
167: ReferencedImageStyle irs = (ReferencedImageStyle) layer
168: .getStyle(ReferencedImageStyle.class);
169: return irs.getImageryLayerDataset();
170: }
171:
172: private Collection createFeatures(
173: final ReferencedImageFactory referencedImageFactory,
174: File[] files, final ImageryLayerDataset imageryLayerDataset) {
175: return CollectionUtil.collect(Arrays.asList(files),
176: new Block() {
177: public Object yield(Object file) {
178: return createFeature(referencedImageFactory,
179: (File) file, imageryLayerDataset);
180: }
181: });
182: }
183:
184: private Feature createFeature(
185: ReferencedImageFactory referencedImageFactory, File file,
186: ImageryLayerDataset imageryLayerDataset) {
187:
188: Feature feature = new BasicFeature(ImageryLayerDataset
189: .getSchema());
190: feature.setAttribute(ImageryLayerDataset.ATTR_FILE, file
191: .getPath());
192: feature.setAttribute(ImageryLayerDataset.ATTR_FORMAT,
193: getReferencedImageFactoryProper(referencedImageFactory,
194: file).getTypeName());
195: feature.setAttribute(ImageryLayerDataset.ATTR_FACTORY,
196: getReferencedImageFactoryProper(referencedImageFactory,
197: file).getClass().getName());
198: // Set Geometry to an empty Geometry, in case an exception occurs before
199: // the Geometry is actually set. [Jon Aquino 2005-04-12]
200: feature.setGeometry(new GeometryFactory()
201: .createPoint((Coordinate) null));
202: // Set the Geometry. [Jon Aquino 2005-04-12]
203: imageryLayerDataset.createImage(feature);
204: return feature;
205: }
206:
207: private OrderedMap getFileFilterToReferencedImageFactoryMap(
208: Collection referencedImageFactories) {
209: OrderedMap fileFilterToReferencedImageFactoryMap = new OrderedMap();
210: for (Iterator i = referencedImageFactories.iterator(); i
211: .hasNext();) {
212: ReferencedImageFactory referencedImageFactory = (ReferencedImageFactory) i
213: .next();
214: // if(referencedImageFactory.isAvailable()){
215: fileFilterToReferencedImageFactoryMap.put(
216: createFileFilter(referencedImageFactory),
217: referencedImageFactory);
218: // }
219: }
220: return fileFilterToReferencedImageFactoryMap;
221: }
222:
223: private FileFilter createFileFilter(
224: ReferencedImageFactory referencedImageFactory) {
225: return GUIUtil.createFileFilter((referencedImageFactory
226: .getTypeName().equals(
227: referencedImageFactory.getDescription()) ? ""
228: : referencedImageFactory.getTypeName() + " - ")
229: + referencedImageFactory.getDescription(),
230: referencedImageFactory.getExtensions());
231: }
232:
233: private Collection insertCompositeReferencedImageFactory(
234: List referencedImageFactories) {
235: return CollectionUtil
236: .concatenate(Arrays
237: .asList(new Collection[] {
238: Collections
239: .singleton(new CompositeReferencedImageFactory(
240: referencedImageFactories)),
241: referencedImageFactories }));
242: }
243:
244: private static class CompositeReferencedImageFactory implements
245: ReferencedImageFactory {
246:
247: private List referencedImageFactories;
248:
249: public CompositeReferencedImageFactory(
250: List referencedImageFactories) {
251: this .referencedImageFactories = new ArrayList();
252: for (Iterator i = referencedImageFactories.iterator(); i
253: .hasNext();) {
254: ReferencedImageFactory referencedImageFactory = (ReferencedImageFactory) i
255: .next();
256: if (referencedImageFactory.isAvailable()) {
257: this .referencedImageFactories
258: .add(referencedImageFactory);
259: }
260: }
261: }
262:
263: public String getTypeName() {
264: return I18N
265: .get("ui.plugin.imagery.ImageFeatureCreator.All-Formats");
266: }
267:
268: public boolean isEditableImage(String location) {
269: return false;
270: }
271:
272: public String getDescription() {
273: return getTypeName();
274: }
275:
276: public String[] getExtensions() {
277: return (String[]) CollectionUtil.concatenate(
278: CollectionUtil.collect(referencedImageFactories,
279: new Block() {
280: public Object yield(
281: Object referencedImageFactory) {
282: return Arrays
283: .asList(((ReferencedImageFactory) referencedImageFactory)
284: .getExtensions());
285: }
286: })).toArray(new String[] {});
287: }
288:
289: public ReferencedImageFactory referencedImageFactory(
290: String extension) {
291: for (Iterator i = referencedImageFactories.iterator(); i
292: .hasNext();) {
293: ReferencedImageFactory referencedImageFactory = (ReferencedImageFactory) i
294: .next();
295: if (Arrays.asList(
296: referencedImageFactory.getExtensions())
297: .contains(extension.toLowerCase())) {
298: return referencedImageFactory;
299: }
300: }
301: Assert.shouldNeverReachHere();
302: return null;
303: }
304:
305: public ReferencedImage createImage(String location) {
306: throw new UnsupportedOperationException();
307: }
308:
309: public boolean isAvailable() {
310: return true;
311: }
312: }
313: }
|