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