📄 sarannotationsupport.java
字号:
package gov.nasa.worldwind.applications.sar;
import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.event.SelectEvent;
import gov.nasa.worldwind.event.SelectListener;
import gov.nasa.worldwind.examples.BasicDragger;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.geom.Vec4;
import gov.nasa.worldwind.globes.Globe;
import gov.nasa.worldwind.layers.AnnotationLayer;
import gov.nasa.worldwind.pick.PickedObject;
import gov.nasa.worldwind.pick.PickedObjectList;
import gov.nasa.worldwind.render.*;
import gov.nasa.worldwind.view.OrbitView;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
/**
* Handles SAR annotations
* @author Patrick Murris
* @version $Id: SARAnnotationSupport.java 10895 2009-05-06 00:24:04Z patrickmurris $
*/
public class SARAnnotationSupport
{
private AnnotationLayer annotationLayer;
private AnnotationAttributes defaults;
private WorldWindow wwd;
private SARAnnotation lastPickedObject;
private SARAnnotation currentAnnotation;
private ScreenAnnotation helpMessageAnnotation;
private Color savedBorderColor;
private String angleFormat;
public SARAnnotationSupport()
{
this.annotationLayer = new AnnotationLayer();
this.helpMessageAnnotation = new ScreenAnnotation("", new Point(0, 0));
this.helpMessageAnnotation.getAttributes().setFrameShape(FrameFactory.SHAPE_NONE);
this.helpMessageAnnotation.getAttributes().setInsets(new Insets(0, 0, 0, 0));
this.helpMessageAnnotation.getAttributes().setDrawOffset(new Point(0, -20));
this.helpMessageAnnotation.getAttributes().setTextAlign(MultiLineTextRenderer.ALIGN_CENTER);
this.helpMessageAnnotation.getAttributes().setEffect(MultiLineTextRenderer.EFFECT_OUTLINE);
this.helpMessageAnnotation.getAttributes().setFont(Font.decode("Arial-Bold-14"));
this.helpMessageAnnotation.getAttributes().setTextColor(Color.YELLOW);
this.helpMessageAnnotation.getAttributes().setBackgroundColor(Color.BLACK);
this.helpMessageAnnotation.getAttributes().setSize(new Dimension(220, 0));
this.annotationLayer.addAnnotation(this.helpMessageAnnotation);
this.defaults = new AnnotationAttributes();
this.defaults.setHighlightScale(1.1);
}
/**
* Set the WorldWindow reference. Adds an annotation layer and a SelectListener to WW.
* @param wwd the WorldWindow reference.
*/
public void setWwd(WorldWindow wwd)
{
this.wwd = wwd;
// Add annotation layer
this.wwd.getModel().getLayers().add(this.annotationLayer);
// Add a select listener to select or highlight annotations on rollover
this.wwd.addSelectListener(new SelectListener()
{
private BasicDragger dragger = new BasicDragger(SARAnnotationSupport.this.wwd);
public void selected(SelectEvent event)
{
// Select/unselect on left click on annotations
if (event.getEventAction().equals(SelectEvent.LEFT_CLICK))
{
if (event.hasObjects())
{
if (event.getTopObject() instanceof Annotation)
{
// Check for text or url
PickedObject po = event.getTopPickedObject();
if(po.getValue(AVKey.TEXT) != null)
{
//System.out.println("Text: \"" + po.getValue(AVKey.TEXT) + "\" Hyperlink: " + po.getValue(AVKey.URL));
//if(SARAnnotationSupport.this.currentAnnotation == event.getTopObject())
// return;
}
// Left click on an annotation - select
select(event.getTopObject());
}
}
}
// Edit annotation on double click
else if (event.getEventAction().equals(SelectEvent.LEFT_DOUBLE_CLICK))
{
if (event.hasObjects())
{
if (event.getTopObject() instanceof Annotation)
{
edit((SARAnnotation)event.getTopObject());
}
}
}
// Highlight on rollover
else if (event.getEventAction().equals(SelectEvent.ROLLOVER) && !this.dragger.isDragging())
{
highlight(event.getTopObject());
}
// Have drag events drag the selected object.
else if (event.getEventAction().equals(SelectEvent.DRAG_END)
|| event.getEventAction().equals(SelectEvent.DRAG))
{
if (event.hasObjects())
{
// If selected annotation delegate dragging computations to a dragger.
if(event.getTopObject() == SARAnnotationSupport.this.currentAnnotation)
{
this.dragger.selected(event);
// Update help text when dragging
updateHelpMessage(SARAnnotationSupport.this.currentAnnotation);
// Mark the owner track as dirty.
if (SARAnnotationSupport.this.currentAnnotation.getOwner() != null)
SARAnnotationSupport.this.currentAnnotation.getOwner().markDirty();
}
}
// We missed any roll-over events while dragging, so highlight any under the cursor now,
// or de-highlight the dragged shape if it's no longer under the cursor.
if (event.getEventAction().equals(SelectEvent.DRAG_END))
{
PickedObjectList pol = SARAnnotationSupport.this.wwd.getObjectsAtCurrentPosition();
if (pol != null)
{
highlight(pol.getTopObject());
SARAnnotationSupport.this.wwd.redraw();
}
}
}
}
});
// Add a mouse listener to deselect annotation on click anywhere - including terrain
this.wwd.getInputHandler().addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent mouseEvent)
{
if (currentAnnotation != null && mouseEvent.getButton() == MouseEvent.BUTTON1)
{
select(null);
mouseEvent.consume();
getWwd().redraw();
}
}
});
// Listen for angle format change
this.wwd.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent propertyChangeEvent)
{
if (propertyChangeEvent.getPropertyName() == SAR2.ANGLE_FORMAT)
setAngleFormat((String)propertyChangeEvent.getNewValue());
}
});
}
public WorldWindow getWwd()
{
return this.wwd;
}
public String getAngleFormat()
{
return this.angleFormat;
}
public void setAngleFormat(String format)
{
this.angleFormat = format;
updateHelpMessage(this.lastPickedObject);
}
private void select(Object o)
{
if(this.currentAnnotation != null)
{
// Unselect current
//this.currentAnnotation.getAttributes().setHighlighted(false);
this.currentAnnotation.getAttributes().setBorderColor(SARAnnotationSupport.this.savedBorderColor);
}
if(o != null && o instanceof SARAnnotation && this.currentAnnotation != o)
{
// Select new one if not current one already
this.currentAnnotation = (SARAnnotation)o;
//this.currentAnnotation.getAttributes().setHighlighted(true);
this.savedBorderColor = this.currentAnnotation.getAttributes().getBorderColor();
this.currentAnnotation.getAttributes().setBorderColor(Color.YELLOW);
}
else
{
// Clear current annotation
this.currentAnnotation = null; // switch off
}
}
private void highlight(Object o)
{
// Manage highlighting of Annotations.
if (this.lastPickedObject == o)
return; // same thing selected
// Turn off highlight if on.
if (this.lastPickedObject != null) // && this.lastPickedObject != this.currentAnnotation)
{
this.lastPickedObject.getAttributes().setHighlighted(false);
this.lastPickedObject = null;
updateHelpMessage(null); // Clear help text
}
// Turn on highlight if object selected.
if (o != null && o instanceof SARAnnotation)
{
this.lastPickedObject = (SARAnnotation) o;
this.lastPickedObject.getAttributes().setHighlighted(true);
updateHelpMessage(this.lastPickedObject); // Update help text
}
}
private void updateHelpMessage(SARAnnotation annotation)
{
if (annotation != null)
{
Position pos = annotation.getPosition();
this.helpMessageAnnotation.getAttributes().setVisible(true);
this.helpMessageAnnotation.setText(String.format("Lat %s Lon %s",
SAR2.formatAngle(angleFormat, pos.getLatitude()),
SAR2.formatAngle(angleFormat, pos.getLongitude())));
// set help message screen position - follow annotation
Vec4 surfacePoint = this.getWwd().getSceneController().getTerrain().getSurfacePoint(
pos.getLatitude(), pos.getLongitude());
if (surfacePoint == null)
{
Globe globe = this.getWwd().getModel().getGlobe();
surfacePoint = globe.computePointFromPosition(pos.getLatitude(), pos.getLongitude(),
globe.getElevation(pos.getLatitude(), pos.getLongitude()));
}
Vec4 screenPoint = this.getWwd().getView().project(surfacePoint);
if (screenPoint != null)
this.helpMessageAnnotation.setScreenPoint(new Point((int)screenPoint.x, (int)screenPoint.y));
}
else
{
this.helpMessageAnnotation.getAttributes().setVisible(false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -