root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / histogram / ui / HistogramDialog.java @ 10799
History | View | Annotate | Download (7.07 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.rastertools.histogram.ui; |
20 |
|
21 |
import java.awt.BorderLayout; |
22 |
import java.io.IOException; |
23 |
|
24 |
import javax.swing.JPanel; |
25 |
|
26 |
import com.iver.andami.PluginServices; |
27 |
import com.iver.andami.ui.mdiManager.IWindow; |
28 |
import com.iver.andami.ui.mdiManager.WindowInfo; |
29 |
/**
|
30 |
* <code>HistogramDialog</code>. Creaci?n de la ventana para gvSIG.
|
31 |
*
|
32 |
* @version 20/03/2007
|
33 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
34 |
* @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
|
35 |
*/
|
36 |
public class HistogramDialog extends JPanel implements IWindow { |
37 |
private static final long serialVersionUID = 7362459094802955247L; |
38 |
private HistogramPanel histogramPanel = null; |
39 |
|
40 |
public HistogramDialog(int width, int height){ |
41 |
this.setSize(width, height);
|
42 |
this.setLayout(new BorderLayout(5, 5)); |
43 |
|
44 |
this.add(getHistogramPanel(), java.awt.BorderLayout.CENTER);
|
45 |
} |
46 |
|
47 |
/**
|
48 |
* Obtiene el panel con el histograma
|
49 |
* @return HistogramPanel
|
50 |
*/
|
51 |
public HistogramPanel getHistogramPanel(){
|
52 |
if(histogramPanel == null){ |
53 |
histogramPanel = new HistogramPanel();
|
54 |
// histogramPanel.getBCreateTable().addActionListener(histogramPanel);
|
55 |
} |
56 |
return histogramPanel;
|
57 |
} |
58 |
/*
|
59 |
public void actionPerformed(ActionEvent e) {
|
60 |
|
61 |
//Bot?n de cerrar
|
62 |
if(e.getActionCommand().compareTo(ButtonsPanel.BUTTON_CLOSE + "") == 0){
|
63 |
try{
|
64 |
PluginServices.getMDIManager().closeWindow(this);
|
65 |
}catch(ArrayIndexOutOfBoundsException ex){
|
66 |
//Si la ventana no se puede eliminar no hacemos nada
|
67 |
}
|
68 |
}
|
69 |
// --------------------------------------
|
70 |
//Bot?n Crear tabla.
|
71 |
JButton table = histogramPanel.getBCreateTable();
|
72 |
if(e.getSource() == table){
|
73 |
|
74 |
try {
|
75 |
// -------Mostrar un fileChooser------------------
|
76 |
String fName;
|
77 |
JFileChooser chooser = new JFileChooser();
|
78 |
chooser.setDialogTitle(PluginServices.getText(this, "guardar_tabla"));
|
79 |
|
80 |
int returnVal = chooser.showOpenDialog(this);
|
81 |
if(returnVal == JFileChooser.APPROVE_OPTION){
|
82 |
fName = chooser.getSelectedFile().toString();
|
83 |
if(!fName.endsWith(".dbf"))
|
84 |
fName += ".dbf";
|
85 |
|
86 |
//-------------Crear el dbf----------------------
|
87 |
|
88 |
DbaseFileWriterNIO dbfWrite = null;
|
89 |
DbaseFileHeaderNIO myHeader;
|
90 |
Value[] record;
|
91 |
|
92 |
int histogram[][]=histogramPanel.getLastHistogram();
|
93 |
int numBands = histogram.length;
|
94 |
int numRecors = histogram[0].length;
|
95 |
|
96 |
File file = new File(fName);
|
97 |
|
98 |
String names[] = new String[numBands+1];
|
99 |
int types[] = new int [numBands+1];
|
100 |
int lengths[] = new int [numBands+1];
|
101 |
|
102 |
names[0]="Value";
|
103 |
types[0]=4;
|
104 |
lengths[0]=15;
|
105 |
for (int band = 0; band < numBands; band++){
|
106 |
names[band+1]="Band"+band;
|
107 |
types[band+1]=4;
|
108 |
lengths[band+1]=15;
|
109 |
}
|
110 |
|
111 |
myHeader = DbaseFileHeaderNIO.createDbaseHeader(names,types,lengths);
|
112 |
|
113 |
myHeader.setNumRecords(numRecors);
|
114 |
dbfWrite = new DbaseFileWriterNIO(myHeader,
|
115 |
(FileChannel) getWriteChannel(file.getPath()));
|
116 |
record = new Value[numBands+1];
|
117 |
|
118 |
for (int j = 0; j < numRecors; j++) {
|
119 |
record[0] = ValueFactory.createValue(j);
|
120 |
for (int r = 0; r < numBands; r++) {
|
121 |
record[r+1] = ValueFactory.createValue(histogram[r][j]);
|
122 |
}
|
123 |
|
124 |
dbfWrite.write(record);
|
125 |
}
|
126 |
|
127 |
dbfWrite.close();
|
128 |
|
129 |
//------------A?adir el dbf al proyecto--------------
|
130 |
ProjectExtension ext = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
|
131 |
String name = file.getName();
|
132 |
LayerFactory.getDataSourceFactory().addFileDataSource("gdbms dbf driver", name, fName);
|
133 |
DataSource dataSource;
|
134 |
dataSource = LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING);
|
135 |
|
136 |
SelectableDataSource sds = new SelectableDataSource(dataSource);
|
137 |
EditableAdapter auxea=new EditableAdapter();
|
138 |
auxea.setOriginalDataSource(sds);
|
139 |
ProjectTable projectTable = ProjectFactory.createTable(name, auxea);
|
140 |
//ext.getProject().addTable(projectTable);
|
141 |
ext.getProject().addDocument(projectTable);
|
142 |
|
143 |
Table t = new Table();
|
144 |
t.setModel(projectTable);
|
145 |
//projectTable.setAndamiWindow(t);
|
146 |
PluginServices.getMDIManager().addWindow(t);
|
147 |
}
|
148 |
} catch (DriverLoadException e1) {
|
149 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),this.getName() + " " +
|
150 |
PluginServices.getText(this,"table_not_create"));
|
151 |
NotificationManager.addError(e1);
|
152 |
} catch (NoSuchTableException e1) {
|
153 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),this.getName() + " " +
|
154 |
PluginServices.getText(this,"table_not_create"));
|
155 |
NotificationManager.addError(e1);
|
156 |
} catch (ReadDriverException e1) {
|
157 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),this.getName() + " " +
|
158 |
PluginServices.getText(this,"table_not_create"));
|
159 |
NotificationManager.addError(e1);
|
160 |
}catch (IOException ex) {
|
161 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),this.getName() + " " +
|
162 |
PluginServices.getText(this,"table_not_create"));
|
163 |
NotificationManager.addError("Error al crear un DbaseFileHeaderNIO", ex);
|
164 |
}
|
165 |
}
|
166 |
|
167 |
//--------------------------------------
|
168 |
}
|
169 |
*/
|
170 |
/**
|
171 |
*
|
172 |
* @param path
|
173 |
* @return
|
174 |
* @throws IOException
|
175 |
*/
|
176 |
/*
|
177 |
private WritableByteChannel getWriteChannel(String path)throws IOException {
|
178 |
WritableByteChannel channel;
|
179 |
|
180 |
File f = new File(path);
|
181 |
|
182 |
if (!f.exists()) {
|
183 |
System.out.println("Creando fichero " + f.getAbsolutePath());
|
184 |
|
185 |
if (!f.createNewFile()) {
|
186 |
throw new IOException("Cannot create file " + f);
|
187 |
}
|
188 |
}
|
189 |
|
190 |
RandomAccessFile raf = new RandomAccessFile(f, "rw");
|
191 |
channel = raf.getChannel();
|
192 |
|
193 |
return channel;
|
194 |
}
|
195 |
*/
|
196 |
|
197 |
public WindowInfo getWindowInfo() {
|
198 |
WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
|
199 |
m_viewinfo.setTitle(PluginServices.getText(this, "histograma")); |
200 |
m_viewinfo.setHeight(this.getHeight());
|
201 |
m_viewinfo.setWidth(this.getWidth());
|
202 |
return m_viewinfo;
|
203 |
} |
204 |
} |