| /******************************************************************************* |
| * Copyright (c) 2007 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| * Bundesinstitut für Risikobewertung |
| *******************************************************************************/ |
| |
| package org.eclipse.stem.ui.views.graphmap; |
| |
| import java.text.DecimalFormat; |
| import java.text.NumberFormat; |
| import java.text.ParseException; |
| import java.util.List; |
| import java.util.concurrent.CopyOnWriteArrayList; |
| |
| import javax.swing.text.NumberFormatter; |
| |
| import org.eclipse.jface.action.Action; |
| import org.eclipse.jface.action.IAction; |
| import org.eclipse.jface.action.MenuManager; |
| import org.eclipse.jface.action.Separator; |
| import org.eclipse.jface.viewers.ISelection; |
| import org.eclipse.jface.viewers.ISelectionChangedListener; |
| import org.eclipse.jface.viewers.ISelectionProvider; |
| import org.eclipse.jface.viewers.SelectionChangedEvent; |
| import org.eclipse.stem.jobs.simulation.ISimulation; |
| import org.eclipse.stem.ui.Activator; |
| import org.eclipse.stem.ui.adapters.color.ColorProviderAdapter; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.events.DisposeEvent; |
| import org.eclipse.swt.events.DisposeListener; |
| import org.eclipse.swt.layout.FillLayout; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Label; |
| import org.eclipse.ui.IWorkbenchActionConstants; |
| |
| /** |
| * This class is extended by classes that render geographic (lat/long) data into |
| * a visual representation. |
| */ |
| public class GraphMapRenderer extends Composite implements ISelectionProvider, |
| ISelectionChangedListener, DisposeListener { |
| |
| private final double INITIAL_GAIN_FACTOR = 1.0; |
| private final boolean INITIAL_DRAW_POLYGON_BORDERS = true; |
| private final boolean INITIAL_USE_LOG_SCALING = true; |
| |
| private boolean drawPolygonBorders; |
| private boolean useLogScaling; |
| private double gainFactor; |
| |
| private GainFactorAction defaultGainFactorAction; |
| private GainFactorAction lastGainFactorAction; |
| private LogarithmicAction logarithmicAction; |
| private DrawPolygonBordersAction drawPolygonBordersAction; |
| private ColorProviderAdapter colorProviderAdapter; |
| |
| private List<ISelectionChangedListener> listeners; |
| |
| private ISelection selection; |
| private GraphMapCanvas mapCanvas; |
| private MenuManager menuManager; |
| |
| private Label gainScaleLabel; |
| private NumberFormat gainFormatter = new DecimalFormat("#0.000"); |
| |
| public GraphMapRenderer(Composite parent, int style) { |
| super(parent, style); |
| drawPolygonBorders = INITIAL_DRAW_POLYGON_BORDERS; |
| useLogScaling = INITIAL_USE_LOG_SCALING; |
| gainFactor = INITIAL_GAIN_FACTOR; |
| listeners = new CopyOnWriteArrayList<ISelectionChangedListener>(); |
| |
| setSize(SWT.DEFAULT, 100); |
| setLayout(new FillLayout()); |
| |
| mapCanvas = new GraphMapCanvas(this, SWT.NONE); |
| mapCanvas.setGainFactor(gainFactor); |
| mapCanvas.setDrawPolygonBorders(drawPolygonBorders); |
| mapCanvas.setUseLogScaling(useLogScaling); |
| mapCanvas.setColorProvider(colorProviderAdapter); |
| mapCanvas.addSelectionChangedListener(this); |
| menuManager = createContextMenuManager(); |
| mapCanvas.setMenu(menuManager.createContextMenu(this)); |
| |
| pack(); |
| } |
| |
| public GraphMapCanvas getCanvas() { |
| return mapCanvas; |
| } |
| |
| public MenuManager getMenuManager() { |
| return menuManager; |
| } |
| |
| public void render(StemPolygonList polygonsToRender, ISimulation simulation) { |
| mapCanvas.setGainFactor(gainFactor); |
| mapCanvas.setUseLogScaling(useLogScaling); |
| mapCanvas.render(polygonsToRender, simulation); |
| } |
| |
| /** |
| * Pass a {@link SelectionChangedEvent} along to listeners. |
| * |
| * @param selection |
| * the {@link ISelection} to be giving to the listeners. |
| */ |
| public void fireSelection(ISelection selection) { |
| |
| SelectionChangedEvent event = new SelectionChangedEvent(this, selection); |
| for (ISelectionChangedListener listener : listeners) { |
| listener.selectionChanged(event); |
| } // for each ISelectionChangedListener |
| } // fireSelection |
| |
| /** |
| * Reset the renderer. |
| */ |
| public void reset() { |
| // Reset the gain factor and update the checks in the context menu |
| lastGainFactorAction.setChecked(false); |
| defaultGainFactorAction.run(); |
| defaultGainFactorAction.setChecked(true); |
| |
| useLogScaling = INITIAL_USE_LOG_SCALING; |
| logarithmicAction.setChecked(useLogScaling); |
| |
| drawPolygonBorders = INITIAL_DRAW_POLYGON_BORDERS; |
| drawPolygonBordersAction.setChecked(drawPolygonBorders); |
| |
| mapCanvas.reset(); |
| mapCanvas.setDrawPolygonBorders(drawPolygonBorders); |
| mapCanvas.setGainFactor(gainFactor); |
| mapCanvas.setUseLogScaling(useLogScaling); |
| } // reset |
| |
| /** |
| * labels the map with current gain and log/lin mode |
| */ |
| public void updateScaleLogLinLabel() { |
| StringBuilder sb = new StringBuilder(); // creates empty builder, |
| // capacity 16 |
| if (useLogScaling) { |
| sb.append(Messages.getString("MapMenu.LogScale")); |
| } else { |
| sb.append(Messages.getString("MapMenu.LinScale")); |
| } |
| sb.append(" "); |
| sb.append(Messages.getString("MapMenu.Gain")); |
| sb.append(" x"); |
| sb.append(gainFormatter.format(gainFactor)); |
| gainScaleLabel.setText(sb.toString()); |
| } |
| |
| public void setGainScaleLabel(Label gainScaleLabel) { |
| this.gainScaleLabel = gainScaleLabel; |
| } |
| |
| /** |
| * @param colorProvider |
| * the colorProvider to set |
| */ |
| public void setColorProviderAdapter(ColorProviderAdapter colorProvider) { |
| this.colorProviderAdapter = colorProvider; |
| mapCanvas.setColorProvider(colorProvider); |
| } |
| |
| @Override |
| public void selectionChanged(SelectionChangedEvent event) { |
| fireSelection(event.getSelection()); |
| } |
| |
| @Override |
| public void dispose() { |
| mapCanvas.removeSelectionChangedListener(this); |
| } |
| |
| @Override |
| public void addSelectionChangedListener(ISelectionChangedListener listener) { |
| listeners.add(listener); |
| } |
| |
| @Override |
| public void removeSelectionChangedListener( |
| ISelectionChangedListener listener) { |
| listeners.remove(listener); |
| } |
| |
| @Override |
| public ISelection getSelection() { |
| return selection; |
| } |
| |
| @Override |
| public void setSelection(ISelection selection) { |
| this.selection = selection; |
| fireSelection(selection); |
| } |
| |
| @Override |
| public void widgetDisposed(DisposeEvent e) { |
| if (!isDisposed()) { |
| dispose(); |
| } |
| } |
| |
| /** |
| * |
| * @param gainFactor |
| */ |
| private void setGainFactor(double gainFactor) { |
| this.gainFactor = gainFactor; |
| updateScaleLogLinLabel(); |
| } // setGainFactor |
| |
| /** |
| * Switch the option of drawing the borders of polygons on the map. |
| */ |
| private void toggleDrawPolygonBordersChoice() { |
| drawPolygonBorders = !drawPolygonBorders; |
| redraw(); |
| mapCanvas.setDrawPolygonBorders(drawPolygonBorders); |
| redraw(); |
| } // toggleDrawPolygonBordersChoice |
| |
| /** |
| * Switch the option of scaling the display values logrithmically. |
| */ |
| private void toggleUseLogScaling() { |
| useLogScaling = !useLogScaling; |
| updateScaleLogLinLabel(); |
| redraw(); |
| mapCanvas.setUseLogScaling(useLogScaling); |
| } // toggleUseLogScaling |
| |
| /** |
| * Create the view's context menu and add the action handlers to it. |
| * |
| * @return the MenuManager |
| */ |
| private MenuManager createContextMenuManager() { |
| |
| // Context Menu |
| MenuManager contextMenuManager = new org.eclipse.jface.action.MenuManager(); |
| |
| contextMenuManager.add(createGainFactorMenu()); |
| |
| logarithmicAction = new LogarithmicAction(); |
| contextMenuManager.add(logarithmicAction); |
| |
| contextMenuManager.add(new Separator()); |
| drawPolygonBordersAction = new DrawPolygonBordersAction(); |
| contextMenuManager.add(drawPolygonBordersAction); |
| |
| // connectPolygonCentersAction = new ConnectPolygonCentersAction(); |
| // contextMenuManager.add(connectPolygonCentersAction); |
| |
| contextMenuManager.add(new Separator()); |
| |
| // Defect 302663 |
| // contextMenuManager.add(createReportsSelectionMenu()); |
| |
| contextMenuManager.add(new Separator()); |
| |
| // --------------------------------------------------------------------- |
| |
| contextMenuManager.add(new ResetMapCanvasAction()); |
| |
| // Place Holder for Menu Additions |
| contextMenuManager.add(new Separator( |
| IWorkbenchActionConstants.MB_ADDITIONS)); |
| |
| // --------------------------------------------------------------------- |
| |
| return contextMenuManager; |
| |
| } // createContextMenu |
| |
| /** |
| * @return |
| */ |
| private MenuManager createGainFactorMenu() { |
| MenuManager gainFactorMenu = new MenuManager( |
| Messages.getString("MapMenu.GainFactor")); |
| gainFactorMenu.add(new GainFactorAction(0.001)); |
| gainFactorMenu.add(new GainFactorAction(0.01)); |
| gainFactorMenu.add(new GainFactorAction(0.1)); |
| |
| defaultGainFactorAction = new GainFactorAction(1.0); // Default |
| lastGainFactorAction = defaultGainFactorAction; |
| gainFactorMenu.add(defaultGainFactorAction); |
| |
| gainFactorMenu.add(new GainFactorAction(10.0)); |
| gainFactorMenu.add(new GainFactorAction(100.0)); |
| gainFactorMenu.add(new GainFactorAction(1000.0)); |
| gainFactorMenu.add(new GainFactorAction(10000.0)); |
| gainFactorMenu.add(new GainFactorAction(100000.0)); |
| gainFactorMenu.add(new GainFactorAction(1000000.0)); |
| gainFactorMenu.add(new GainFactorAction(10000000.0)); |
| return gainFactorMenu; |
| } |
| |
| private String getText(double factor) { |
| NumberFormatter formatter = new NumberFormatter(); |
| String retValue = ""; |
| try { |
| retValue = formatter.valueToString(new Double(factor)); |
| } catch (ParseException e) { |
| Activator.logError("Problem parsing gain factor value \"" + factor |
| + "\"", e); |
| } |
| return retValue; |
| } // getText |
| |
| private class GainFactorAction extends Action { |
| |
| private double factor; |
| |
| GainFactorAction(double factor) { |
| super(GraphMapRenderer.this.getText(factor), |
| IAction.AS_RADIO_BUTTON); |
| this.factor = factor; |
| setChecked(factor == INITIAL_GAIN_FACTOR); |
| } |
| |
| @Override |
| public String getText() { |
| return GraphMapRenderer.this.getText(factor); |
| } |
| |
| @Override |
| public void run() { |
| setGainFactor(factor); |
| lastGainFactorAction = this; |
| Composite parent = GraphMapRenderer.this.getParent(); |
| ((GraphMapControl) parent).refresh(); |
| } |
| } |
| |
| private class LogarithmicAction extends Action { |
| |
| public LogarithmicAction() { |
| super(Messages.getString("MapMenu.Logrithmic_Scaling"), |
| IAction.AS_CHECK_BOX); |
| setChecked(useLogScaling); |
| } |
| |
| @Override |
| public String getText() { |
| return Messages.getString("MapMenu.Logrithmic_Scaling"); |
| } |
| |
| @Override |
| public void run() { |
| toggleUseLogScaling(); |
| Composite parent = GraphMapRenderer.this.getParent(); |
| ((GraphMapControl) parent).refresh(); |
| } |
| } |
| |
| private class DrawPolygonBordersAction extends Action { |
| |
| public DrawPolygonBordersAction() { |
| super(Messages.getString("MapMenu.Polygon_Borders"), |
| IAction.AS_CHECK_BOX); |
| |
| if (drawPolygonBorders) { |
| setChecked(true); |
| } |
| } |
| |
| @Override |
| public String getText() { |
| return Messages.getString("MapMenu.Polygon_Borders"); |
| } |
| |
| @Override |
| public void run() { |
| toggleDrawPolygonBordersChoice(); |
| Composite parent = GraphMapRenderer.this.getParent(); |
| ((GraphMapControl) parent).refresh(); |
| } |
| } |
| |
| private class ResetMapCanvasAction extends Action { |
| |
| @Override |
| public String getText() { |
| return Messages.getString("MapMenu.Reset"); |
| } |
| |
| @Override |
| public void run() { |
| reset(); |
| } |
| } |
| |
| } |