Added a bunch of functionality that will be needed when opening the editor with an...
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / description / DistributionMapDialog.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.editor.description;
12
13 import java.awt.Color;
14 import java.util.Map;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.browser.Browser;
18 import org.eclipse.swt.browser.ProgressEvent;
19 import org.eclipse.swt.browser.ProgressListener;
20 import org.eclipse.swt.events.ControlEvent;
21 import org.eclipse.swt.events.ControlListener;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Dialog;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.swt.widgets.ProgressBar;
27 import org.eclipse.swt.widgets.Shell;
28
29 import eu.etaxonomy.cdm.ext.EditGeoService;
30 import eu.etaxonomy.cdm.ext.IEditGeoService;
31 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
32 import eu.etaxonomy.taxeditor.editor.AbstractTaxonEditor;
33
34 /**
35 * @author n.hoffmann
36 * @created 26.05.2009
37 * @version 1.0
38 */
39 public class DistributionMapDialog extends Dialog {
40
41 private IEditGeoService geoService;
42
43 private AbstractTaxonEditor editor;
44
45 private Browser browser;
46
47 private Shell shell;
48
49 /**
50 * @param parent
51 * @param style
52 */
53 public DistributionMapDialog(Shell parent, int style) {
54 super(parent, style);
55 this.setText("Map Viewer");
56 geoService = new EditGeoService();
57 }
58
59 /**
60 * @param parent
61 */
62 public DistributionMapDialog(AbstractTaxonEditor editor, Shell parent) {
63 this(parent, 0);
64
65 this.editor = editor;
66 }
67
68 public Object open () {
69 Shell parent = getParent();
70 shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.MAX | SWT.RESIZE | SWT.APPLICATION_MODAL);
71 shell.setText(getText());
72
73 shell.setSize(810, 450);
74
75
76 // Composite composite = new Composite(shell, SWT.NONE);
77
78 GridLayout gridLayout = new GridLayout();
79 gridLayout.numColumns = 1;
80 shell.setLayout(gridLayout);
81
82 browser = new Browser(shell, SWT.NONE);
83
84 GridData data = new GridData();
85 data.horizontalAlignment = GridData.FILL;
86 data.verticalAlignment = GridData.FILL;
87 data.grabExcessHorizontalSpace = true;
88 data.grabExcessVerticalSpace = true;
89
90 browser.setLayoutData(data);
91
92
93 final ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
94 data = new GridData();
95 data.horizontalAlignment = GridData.END;
96 progressBar.setLayoutData(data);
97
98
99 browser.addProgressListener(new ProgressListener() {
100 public void changed(ProgressEvent event) {
101 if (event.total == 0) return;
102 int ratio = event.current * 100 / event.total;
103 progressBar.setSelection(ratio);
104 }
105 public void completed(ProgressEvent event) {
106 progressBar.setSelection(0);
107 }
108 });
109
110 shell.addControlListener(new ControlListener(){
111
112 public void controlMoved(ControlEvent e) {
113 // do nothing
114 }
115
116 public void controlResized(ControlEvent e) {
117 loadMap();
118 }
119
120 });
121
122 shell.open();
123 loadMap();
124
125 Display display = parent.getDisplay();
126 while (!shell.isDisposed()) {
127 if (!display.readAndDispatch()) display.sleep();
128 }
129 return null;
130 }
131
132 private void loadMap(){
133 browser.setUrl(getMapUrl());
134 }
135
136 private String getMapUrl(){
137 // FIXME make these hardcoded parameters configurable via preferences
138 String url = "http://edit.csic.es/v1/areas.php";
139 Map<PresenceAbsenceTermBase<?>,Color> presenceAbsenceTermColors = null;
140 int width = calculateWidth();
141 int height = 0;
142 String bbox = "-180,-90,180,90";
143 String backLayer = null;
144
145 String parameter = geoService.getEditGeoServiceUrlParameterString(
146 editor.getTaxon(),
147 presenceAbsenceTermColors,
148 width,
149 height,
150 bbox,
151 backLayer);
152
153 return url + "?" + parameter;
154 }
155
156 /**
157 * @return
158 */
159 private int calculateWidth(){
160 return shell.getSize().x - 10;
161 }
162
163 /**
164 * @return the editor
165 */
166 public AbstractTaxonEditor getEditor() {
167 return editor;
168 }
169 }