gvsig-raster / org.gvsig.raster.tools / trunk / templates / rasterTaskProjectTemplate / alg_with_preview_template / sources / app / ProjectTemplateExtension.java @ 2354
History | View | Annotate | Download (7.93 KB)
1 |
package org.gvsig.raster.projecttemplate.app; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.util.ArrayList; |
7 |
import java.util.List; |
8 |
|
9 |
import javax.swing.JComponent; |
10 |
import javax.swing.JOptionPane; |
11 |
import javax.swing.JPanel; |
12 |
|
13 |
import org.gvsig.andami.IconThemeHelper; |
14 |
import org.gvsig.andami.PluginServices; |
15 |
import org.gvsig.andami.plugins.Extension; |
16 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
17 |
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel; |
18 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
19 |
import org.gvsig.fmap.dal.coverage.util.FileUtils; |
20 |
import org.gvsig.fmap.dal.exception.ReadException; |
21 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
22 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
23 |
import org.gvsig.fmap.mapcontrol.MapControl; |
24 |
import org.gvsig.i18n.Messages; |
25 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
26 |
import org.gvsig.raster.projecttemplate.swing.ProjectTemplatePanel; |
27 |
import org.gvsig.raster.projecttemplate.swing.ProjectTemplateSwingLocator; |
28 |
import org.gvsig.raster.swing.RasterSwingLibrary; |
29 |
import org.gvsig.raster.swing.RasterSwingLocator; |
30 |
import org.gvsig.raster.swing.newlayer.CreateNewLayerPanel; |
31 |
import org.gvsig.raster.swing.newlayer.FileNameManagement; |
32 |
import org.gvsig.raster.swing.preview.PreviewPanel; |
33 |
import org.gvsig.tools.ToolsLocator; |
34 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
35 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
|
40 |
public class ProjectTemplateExtension extends Extension implements ActionListener { |
41 |
private int windowWidth = 600; |
42 |
private int windowHeight = 350; |
43 |
private IWindow window = null; |
44 |
private PreviewPanel previewPanel = null; |
45 |
private ProjectTemplatePreview processPreview = null; |
46 |
private ProjectTemplatePanel panel = null; |
47 |
private CreateNewLayerPanel newLayerPanel = null; |
48 |
private static final Logger logger = LoggerFactory.getLogger(ProjectTemplateExtension.class); |
49 |
|
50 |
public void initialize() { |
51 |
|
52 |
// Adds an entry to the TOC's floating menu to those layers defined in this extension
|
53 |
|
54 |
//ExtensionPoint exPoint = ToolsLocator.getExtensionPointManager().add("My_TocActions");
|
55 |
//exPoint.append("MyMenuEntry", "", new ProjectTemplateTocMenuEntry());
|
56 |
|
57 |
// Adds a new tab to the "add layer" dialog
|
58 |
|
59 |
//AddLayer.addWizard(ProjectTemplateWizard.class);
|
60 |
|
61 |
// Adds a new button in the raster bar
|
62 |
|
63 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
64 |
ExtensionPoint point = extensionPoints.add("GenericToolBarMenu");
|
65 |
point.append("ProjectTemplate", "", ProjectTemplateTocMenuEntry.getSingleton()); |
66 |
ProjectTemplateTocMenuEntry.setExtension(this);
|
67 |
|
68 |
Messages.addResourceFamily("org.gvsig.raster.projecttemplate.app.i18n.text",
|
69 |
ProjectTemplateExtension.class.getClassLoader(), |
70 |
ProjectTemplateExtension.class.getClass().getName()); |
71 |
|
72 |
initilizeIcons(); |
73 |
} |
74 |
|
75 |
public void postInitialize() { |
76 |
} |
77 |
|
78 |
public void execute(String actionCommand) { |
79 |
if (actionCommand.compareTo("ProjectTemplate") == 0) { |
80 |
if(getSelectedLayer() == null) { |
81 |
RasterSwingLibrary.messageBoxError("layer_not_selected", null); |
82 |
return;
|
83 |
} |
84 |
try {
|
85 |
initializeMainPanel(); |
86 |
} catch (ReadException e) {
|
87 |
logger.error("Error initializing the main panel", e);
|
88 |
} |
89 |
} |
90 |
} |
91 |
|
92 |
/**
|
93 |
* Creates and initializes the main panel
|
94 |
* @throws ReadException
|
95 |
*/
|
96 |
private void initializeMainPanel() throws ReadException { |
97 |
loadProjectTemplatePanel(); |
98 |
|
99 |
MainPanel mainPanel = new MainPanel(
|
100 |
getProjectTemplatePanel().getComponent(), |
101 |
(JComponent)getNewLayerPanel());
|
102 |
|
103 |
window = new ProjectTemplateWindow(
|
104 |
mainPanel, |
105 |
(JPanel)getPreviewPanel(),
|
106 |
Messages.getText("projecttemplate"),
|
107 |
windowWidth, |
108 |
windowHeight, |
109 |
false,
|
110 |
this);
|
111 |
|
112 |
PluginServices.getMDIManager().addWindow(window); |
113 |
getPreviewPanel().previewInitializeSizes(); |
114 |
getPreviewPanel().refreshPreview(); |
115 |
} |
116 |
|
117 |
/**
|
118 |
* Loads the main panel with the data
|
119 |
*/
|
120 |
private void loadProjectTemplatePanel() { |
121 |
//TODO: Load panel with the initial data
|
122 |
LayerElement element = new LayerElement(getSelectedLayer());
|
123 |
getProjectTemplatePanel().getDataModel().setInputLayer(element); |
124 |
} |
125 |
|
126 |
|
127 |
/**
|
128 |
* Gets the list of layers from the gvSIG view
|
129 |
* @return
|
130 |
*/
|
131 |
private FLayers getLayers() {
|
132 |
MapControl mapControl = null;
|
133 |
IWindow[] windowList = PluginServices.getMDIManager().getAllWindows();
|
134 |
for (int i = 0; i < windowList.length; i++) { |
135 |
if (windowList[i] instanceof AbstractViewPanel) |
136 |
mapControl = ((AbstractViewPanel) windowList[i]).getMapControl(); |
137 |
} |
138 |
if (mapControl != null) |
139 |
return mapControl.getMapContext().getLayers();
|
140 |
return null; |
141 |
} |
142 |
|
143 |
/**
|
144 |
* Gets the selected layer
|
145 |
* @return
|
146 |
*/
|
147 |
private FLyrRaster getSelectedLayer() {
|
148 |
FLayers lyrs = getLayers(); |
149 |
for (int i = 0; i < lyrs.getLayersCount(); i++) { |
150 |
FLayer lyr = lyrs.getLayer(i); |
151 |
if(lyr.isActive() && lyr instanceof FLyrRaster) |
152 |
return (FLyrRaster)lyr;
|
153 |
} |
154 |
return null; |
155 |
} |
156 |
|
157 |
public boolean isEnabled() { |
158 |
return false; |
159 |
} |
160 |
|
161 |
public boolean isVisible() { |
162 |
return false; |
163 |
} |
164 |
|
165 |
void initilizeIcons() {
|
166 |
IconThemeHelper.registerIcon(null, "gvsig-icon", this); |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Gets the {@link ProjectTemplatePanel}
|
171 |
* @return {@link ProjectTemplatePanel}
|
172 |
*/
|
173 |
private CreateNewLayerPanel getNewLayerPanel() {
|
174 |
if(newLayerPanel == null) |
175 |
newLayerPanel = RasterSwingLocator.getSwingManager().createNewLayerPanel( |
176 |
new FileNameManagementImpl(),
|
177 |
Messages.getText("create_output_layer"));
|
178 |
return newLayerPanel;
|
179 |
} |
180 |
|
181 |
/**
|
182 |
* Gets the {@link PreviewPanel}
|
183 |
* @return {@link PreviewPanel}
|
184 |
*/
|
185 |
private PreviewPanel getPreviewPanel() {
|
186 |
if(previewPanel == null) |
187 |
previewPanel = RasterSwingLocator.getSwingManager().createSinglePreviewPanel(null, getDataSourcePreview());
|
188 |
return previewPanel;
|
189 |
} |
190 |
|
191 |
/**
|
192 |
* Gets the {@link ProjectTemplatePreview}
|
193 |
* @return {@link ProjectTemplatePreview}
|
194 |
*/
|
195 |
private ProjectTemplatePreview getDataSourcePreview() {
|
196 |
if(processPreview == null) { |
197 |
processPreview = new ProjectTemplatePreview();
|
198 |
} |
199 |
processPreview.setData(getProjectTemplatePanel().getDataModel()); |
200 |
return processPreview;
|
201 |
} |
202 |
|
203 |
/**
|
204 |
* Gets the {@link ProjectTemplatePanel}
|
205 |
* @return {@link ProjectTemplatePanel}
|
206 |
*/
|
207 |
private ProjectTemplatePanel getProjectTemplatePanel() {
|
208 |
if(panel == null) |
209 |
panel = ProjectTemplateSwingLocator.getSwingManager().createProjectTemplatePanel(); |
210 |
return panel;
|
211 |
} |
212 |
|
213 |
public void actionPerformed(ActionEvent e) { |
214 |
|
215 |
} |
216 |
|
217 |
public class FileNameManagementImpl implements FileNameManagement { |
218 |
private FileUtils fileUtils = RasterLocator.getManager().getFileUtils();
|
219 |
|
220 |
public String getDefaultFilePath() { |
221 |
return System.getProperty("user.home"); |
222 |
} |
223 |
|
224 |
public List<String> getPrefixSupported() { |
225 |
List<String> list = new ArrayList<String>(); |
226 |
list.add("tif");
|
227 |
return list;
|
228 |
} |
229 |
|
230 |
public String getUniqueLayerName() { |
231 |
return fileUtils.getOnlyLayerName();
|
232 |
} |
233 |
|
234 |
public void usesUniqueLayerName() { |
235 |
fileUtils.usesOnlyLayerName(); |
236 |
} |
237 |
|
238 |
} |
239 |
|
240 |
public class LayerElement { |
241 |
private FLayer lyr = null; |
242 |
public LayerElement(FLayer lyr) {
|
243 |
this.lyr = lyr;
|
244 |
} |
245 |
|
246 |
public FLayer getLayer() {
|
247 |
return lyr;
|
248 |
} |
249 |
|
250 |
public String toString() { |
251 |
return lyr.getName();
|
252 |
} |
253 |
|
254 |
public int getNumOfElements() { |
255 |
if(lyr instanceof FLyrRaster) |
256 |
return ((FLyrRaster)lyr).getDataStore().getBandCount();
|
257 |
return 0; |
258 |
} |
259 |
} |
260 |
} |