001: /*
002: * Created on Nov 28, 2005
003: *
004: */
005: package com.vividsolutions.jump.workbench.ui.plugin.imagery;
006:
007: import java.awt.*;
008: import java.awt.event.ActionEvent;
009: import java.awt.event.ActionListener;
010: import java.awt.event.MouseAdapter;
011: import java.awt.event.MouseEvent;
012: import java.util.Collection;
013: import java.util.Iterator;
014: import java.util.Vector;
015:
016: import javax.swing.*;
017: import javax.swing.event.ListSelectionEvent;
018: import javax.swing.event.ListSelectionListener;
019:
020: import com.vividsolutions.jts.geom.Envelope;
021: import com.vividsolutions.jts.geom.Geometry;
022: import com.vividsolutions.jts.geom.GeometryCollection;
023: import com.vividsolutions.jts.geom.GeometryFactory;
024: import com.vividsolutions.jump.I18N;
025: import com.vividsolutions.jump.feature.Feature;
026: import com.vividsolutions.jump.workbench.WorkbenchContext;
027: import com.vividsolutions.jump.workbench.imagery.ImageryLayerDataset;
028: import com.vividsolutions.jump.workbench.imagery.ReferencedImageStyle;
029: import com.vividsolutions.jump.workbench.model.Layer;
030: import com.vividsolutions.jump.workbench.plugin.*;
031: import com.vividsolutions.jump.workbench.ui.GUIUtil;
032: import com.vividsolutions.jump.workbench.ui.images.IconLoader;
033:
034: /**
035: * Simple Image Layer Management UI. Allows the user to add / remove images,
036: * and view some metadata.
037: *
038: * @author David Zwiers, Vivid Solutions.
039: * @created May 8, 2006
040: */
041: public class ImageLayerManagerPlugIn extends AbstractPlugIn {
042:
043: public ImageLayerManagerPlugIn() {
044: super (
045: I18N
046: .get("ui.plugin.imagery.ImageLayerManagerPlugIn.Image-Layer-Manager"));
047: }
048:
049: public static EnableCheck createEnableCheck(
050: final WorkbenchContext context) {
051: MultiEnableCheck mec = new MultiEnableCheck();
052:
053: mec.add(new EnableCheckFactory(context)
054: .createExactlyNLayersMustBeSelectedCheck(1));
055: mec.add(new EnableCheck() {
056: public String check(JComponent component) {
057: return context.getLayerNamePanel().getSelectedLayers()[0]
058: .getStyle(ReferencedImageStyle.class) == null ? I18N
059: .get("ui.plugin.imagery.ImageLayerManagerPlugIn.Layer-must-be-an-Imagery-layer")
060: : null;
061: }
062: });
063: return mec;
064: }
065:
066: public boolean execute(PlugInContext context) throws Exception {
067: JDialog dlg = new ImageLayerManagerDialog(context);
068:
069: dlg.pack();
070: dlg.setModal(true);
071: GUIUtil.centre(dlg, context.getWorkbenchFrame());
072: dlg.setVisible(true);
073:
074: return false;
075: }
076:
077: private static class FeaturePrinter {
078: private Feature instance;
079:
080: public FeaturePrinter(Feature i) {
081: instance = i;
082: }
083:
084: public String toString() {
085: String val = (instance == null) ? " " : (String) instance
086: .getAttribute(ImageryLayerDataset.ATTR_FILE);
087: return val;
088: }
089: }
090:
091: private static class ImageLayerManagerDialog extends JDialog {
092: private PlugInContext context;
093: private Layer layer;
094: private Vector images;
095: private JList imagesPaths;
096: private JTextArea metadata;
097: private JScrollPane metadataScrollPane;
098:
099: public ImageLayerManagerDialog(PlugInContext context) {
100: super (context.getWorkbenchFrame());
101: this .context = context;
102: setTitle(I18N
103: .get("ui.plugin.imagery.ImageLayerManagerDialog.Image-Layer-Manager"));
104:
105: layer = context.getSelectedLayer(0);
106:
107: // clone it
108: images = new Vector();
109:
110: for (Iterator i = layer.getFeatureCollectionWrapper()
111: .getFeatures().iterator(); i.hasNext();) {
112: images.add(new FeaturePrinter((Feature) i.next()));
113: }
114:
115: initialize();
116: loadMetadata();
117:
118: imagesPaths.setSelectedIndex(0);
119: }
120:
121: private void initialize() {
122: JPanel mainPanel = createMainPanel();
123: JPanel buttonPanel = createButtonPanel();
124: JPanel dialogPanel = new JPanel(new BorderLayout());
125:
126: dialogPanel.add(mainPanel, BorderLayout.CENTER);
127: dialogPanel.add(buttonPanel, BorderLayout.EAST);
128:
129: getContentPane().add(dialogPanel);
130: }
131:
132: private JPanel createMainPanel() {
133: JPanel mainPanel = new JPanel(new GridBagLayout());
134:
135: imagesPaths = new JList();
136: imagesPaths.setListData(images);
137: imagesPaths.setBorder(BorderFactory
138: .createLoweredBevelBorder());
139: imagesPaths.setFont(context.getActiveInternalFrame()
140: .getFont());
141:
142: JScrollPane imagePathsScrollPane = new JScrollPane(
143: imagesPaths);
144: imagePathsScrollPane.setPreferredSize(new Dimension(400,
145: 150));
146: imagePathsScrollPane
147: .setMinimumSize(new Dimension(400, 150));
148:
149: GridBagConstraints gbc = new GridBagConstraints();
150: gbc.anchor = GridBagConstraints.NORTHWEST;
151: gbc.gridx = 0;
152: gbc.gridy = 0;
153: gbc.gridwidth = 1;
154: gbc.insets = new Insets(2, 2, 2, 2);
155: gbc.fill = GridBagConstraints.HORIZONTAL;
156:
157: imagesPaths
158: .addListSelectionListener(new ListSelectionListener() {
159: public void valueChanged(ListSelectionEvent e) {
160: loadMetadata();
161: }
162: });
163:
164: imagesPaths.addMouseListener(new MouseAdapter() {
165: public void mouseClicked(MouseEvent e) {
166: flashSelectedImages();
167: }
168: });
169:
170: mainPanel.add(imagePathsScrollPane, gbc);
171:
172: JLabel label = new JLabel();
173: label.setText("Image Metadata");
174: label.setFont(context.getActiveInternalFrame().getFont());
175: label.setBackground(getBackground());
176: gbc = new GridBagConstraints();
177: gbc.anchor = GridBagConstraints.NORTHWEST;
178: gbc.gridx = 0;
179: gbc.gridy = 1;
180: gbc.gridwidth = 1;
181: gbc.insets = new Insets(2, 2, 2, 2);
182: gbc.fill = GridBagConstraints.NONE;
183: mainPanel.add(label, gbc);
184:
185: metadata = new JTextArea();
186: metadata.setBackground(getBackground());
187: // metadata.setBorder(BorderFactory.createLoweredBevelBorder());
188: metadata.setAutoscrolls(true);
189: metadata
190: .setFont(context.getActiveInternalFrame().getFont());
191: gbc = new GridBagConstraints();
192: gbc.anchor = GridBagConstraints.NORTHWEST;
193: gbc.gridx = 0;
194: gbc.gridy = 2;
195: gbc.weightx = 1;
196: gbc.weighty = 1;
197: gbc.gridwidth = 1;
198: gbc.insets = new Insets(2, 2, 2, 2);
199: gbc.ipadx = 4;
200: gbc.ipady = 4;
201: gbc.fill = GridBagConstraints.BOTH;
202: metadata.setMargin(new Insets(4, 4, 4, 4));
203: metadata.setEditable(false);
204:
205: metadataScrollPane = new JScrollPane(metadata);
206: metadataScrollPane
207: .setPreferredSize(new Dimension(400, 100));
208:
209: mainPanel.add(metadataScrollPane, gbc);
210:
211: return mainPanel;
212: }
213:
214: private JPanel createButtonPanel() {
215: int buttonNumber = 0;
216: JPanel buttonPanel = new JPanel();
217: buttonPanel.setLayout(new GridBagLayout());
218:
219: JButton button = new JButton(
220: I18N
221: .get("ui.plugin.imagery.ImageLayerManagerDialog.Add")
222: + "...", GUIUtil.toSmallIcon(IconLoader
223: .icon("Plus.gif")));
224: button.addActionListener(new AddButtonListener());
225: buttonPanel.add(button, new GridBagConstraints(0,
226: buttonNumber++, 1, 1, 0, 0,
227: GridBagConstraints.NORTHWEST,
228: GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2,
229: 2), 0, 0));
230:
231: button = new JButton(
232: I18N
233: .get("ui.plugin.imagery.ImageLayerManagerDialog.Delete"),
234: GUIUtil.toSmallIcon(IconLoader.icon("Delete.gif")));
235: button.addActionListener(new DeleteButtonListener());
236: buttonPanel.add(button, new GridBagConstraints(0,
237: buttonNumber++, 1, 1, 0, 0,
238: GridBagConstraints.NORTHWEST,
239: GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2,
240: 2), 0, 0));
241:
242: button = new JButton(
243: I18N
244: .get("ui.plugin.imagery.ImageLayerManagerDialog.Close"));
245: button.addActionListener(new CloseButtonListener(this ));
246: buttonPanel.add(button, new GridBagConstraints(0,
247: buttonNumber++, 1, 1, 0, 0,
248: GridBagConstraints.NORTHWEST,
249: GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2,
250: 2), 0, 0));
251:
252: // filler
253: buttonPanel.add(new Label(), new GridBagConstraints(0,
254: buttonNumber++, 1, 1, 1, 1,
255: GridBagConstraints.NORTHWEST,
256: GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0,
257: 0));
258:
259: return buttonPanel;
260: }
261:
262: private void flashSelectedImages() {
263: GeometryFactory factory = new GeometryFactory();
264: Object[] values = imagesPaths.getSelectedValues();
265: Geometry[] geoms = new Geometry[values.length];
266:
267: if (values != null && values.length > 0) {
268: for (int i = 0; i < values.length; i++) {
269: FeaturePrinter current = (FeaturePrinter) values[i];
270: geoms[i] = current.instance.getGeometry();
271: }
272: }
273:
274: try {
275: GeometryCollection gc = factory
276: .createGeometryCollection(geoms);
277: context.getLayerViewPanel().flash(gc);
278: } catch (Exception ex) {
279: // ignored
280: }
281: }
282:
283: // @see ImageryLayerDataset.getSchema()
284: private void loadMetadata() {
285: StringBuffer buf = new StringBuffer();
286: if (images.size() > 0) {
287: Object[] values = imagesPaths.getSelectedValues();
288:
289: if (values != null && values.length > 0) {
290: for (int i = 0; i < values.length; i++) {
291: if (values[i] != null) {
292: FeaturePrinter current = (FeaturePrinter) values[i];
293: if (current.instance != null) {
294: appendMetadata(current.instance, buf);
295: }
296: }
297: buf.append("\n");
298: }
299: }
300: }
301: // add some spaces to ensure string is non-empty
302: buf.append("\n ");
303: metadata.setText(buf.toString());
304: // scroll to top of text area
305: metadata.setCaretPosition(0);
306: }
307:
308: private void appendMetadata(Feature imageFeat, StringBuffer buf) {
309: buf
310: .append(I18N
311: .get("ui.plugin.imagery.ImageLayerManagerDialog.Filename")
312: + ": \t"
313: + imageFeat
314: .getAttribute(ImageryLayerDataset.ATTR_FILE)
315: + "\n");
316: buf
317: .append(" "
318: + I18N
319: .get("ui.plugin.imagery.ImageLayerManagerDialog.Format")
320: + ": \t"
321: + imageFeat
322: .getAttribute(ImageryLayerDataset.ATTR_FORMAT)
323: + "\n");
324: appendEnvelope(imageFeat.getGeometry()
325: .getEnvelopeInternal(), buf);
326: }
327:
328: private void appendEnvelope(Envelope env, StringBuffer buf) {
329: buf
330: .append(" "
331: + I18N
332: .get("ui.plugin.imagery.ImageLayerManagerDialog.Lower-Left")
333: + ": \t" + env.getMinX() + ", "
334: + env.getMinY() + "\n");
335: buf
336: .append(" "
337: + I18N
338: .get("ui.plugin.imagery.ImageLayerManagerDialog.Upper-Right")
339: + ": \t" + env.getMaxX() + ", "
340: + env.getMaxY() + "\n");
341: }
342:
343: private void deleteSelectedImages() {
344: Object[] values = imagesPaths.getSelectedValues();
345: if (values != null) {
346: ImageFeatureCreator ifc = new ImageFeatureCreator();
347:
348: for (int i = 0; i < values.length; i++) {
349: if (values[i] != null) {
350: FeaturePrinter current = (FeaturePrinter) values[i];
351: // order matters here: current gets updated when manipulating the UI.
352: if (current.instance != null) {
353: layer.getFeatureCollectionWrapper().remove(
354: current.instance);
355: }
356: images.remove(current);
357: imagesPaths.setListData(images);
358: }
359: }
360: ifc.setLayerSelectability(layer);
361: }
362: }
363:
364: private void addImages() {
365: ImageFeatureCreator ifc = new ImageFeatureCreator();
366: Collection features = ifc.getImages(context, layer);
367: FeaturePrinter fp = null;
368:
369: if (features != null) {
370: for (Iterator i = features.iterator(); i.hasNext();) {
371: Feature f = (Feature) i.next();
372: fp = new FeaturePrinter(f);
373: images.addElement(fp);
374: layer.getFeatureCollectionWrapper().add(f);
375: }
376: imagesPaths.setListData(images);
377: if (fp != null) {
378: imagesPaths.setSelectedValue(fp, true);
379: }
380:
381: ifc.setLayerSelectability(layer);
382: }
383: }
384:
385: private class DeleteButtonListener implements ActionListener {
386: public void actionPerformed(ActionEvent e) {
387: deleteSelectedImages();
388: }
389: }
390:
391: private class AddButtonListener implements ActionListener {
392: public void actionPerformed(ActionEvent e) {
393: addImages();
394: }
395: }
396:
397: private class CloseButtonListener implements ActionListener {
398: private JDialog dialog;
399:
400: CloseButtonListener(JDialog dlg) {
401: dialog = dlg;
402: }
403:
404: public void actionPerformed(ActionEvent e) {
405: dialog.dispose();
406: }
407: }
408: }
409: }
|