001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/tools/shape/IndexFrame.java $
002: /*
003: Coyright 2003 IDgis BV
004:
005: This library is free software; you can redistribute it and/or
006: modify it under the terms of the GNU Lesser General Public
007: License as published by the Free Software Foundation; either
008: version 2.1 of the License, or (at your option) any later version.
009:
010: This library is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: Lesser General Public License for more details.
014:
015: You should have received a copy of the GNU Lesser General Public
016: License along with this library; if not, write to the Free Software
017: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package org.deegree.tools.shape;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Container;
024: import java.awt.GridLayout;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.io.File;
028:
029: import javax.swing.JButton;
030: import javax.swing.JCheckBox;
031: import javax.swing.JFrame;
032: import javax.swing.JLabel;
033: import javax.swing.JOptionPane;
034: import javax.swing.JPanel;
035: import javax.swing.JProgressBar;
036: import javax.swing.event.ChangeEvent;
037: import javax.swing.event.ChangeListener;
038:
039: import org.deegree.datatypes.QualifiedName;
040: import org.deegree.framework.log.ILogger;
041: import org.deegree.framework.log.LoggerFactory;
042: import org.deegree.io.dbaseapi.DBaseIndex;
043: import org.deegree.io.rtree.HyperBoundingBox;
044: import org.deegree.io.rtree.HyperPoint;
045: import org.deegree.io.rtree.RTree;
046: import org.deegree.io.shpapi.ShapeFile;
047: import org.deegree.model.feature.Feature;
048: import org.deegree.model.spatialschema.Envelope;
049: import org.deegree.model.spatialschema.Geometry;
050: import org.deegree.model.spatialschema.GeometryFactory;
051: import org.deegree.model.spatialschema.Point;
052:
053: public class IndexFrame extends JFrame implements ActionListener,
054: ChangeListener {
055:
056: private static final ILogger LOG = LoggerFactory
057: .getLogger(IndexFrame.class);
058:
059: protected JCheckBox geometryCheckBox;
060:
061: protected JCheckBox[] checkboxes;
062:
063: protected ShapeFile shapeFile;
064:
065: protected String[] properties;
066:
067: protected JButton cancel;
068:
069: protected Indexing indexing = null;
070:
071: protected String fileName;
072:
073: protected boolean hasGeometry;
074:
075: protected boolean[] hasIndex;
076:
077: protected JCheckBox[] uniqueBoxes;
078:
079: class Indexing extends Thread {
080: boolean stop = false;
081:
082: IndexFrame frame;
083:
084: Indexing(IndexFrame frame) {
085: this .frame = frame;
086: }
087:
088: public void run() {
089: Container container = getContentPane();
090: container.removeAll();
091: container.setLayout(new GridLayout(3, 1));
092:
093: int features = shapeFile.getRecordNum();
094: container.add(new JLabel("Indexing..."));
095: JProgressBar progressBar = new JProgressBar(1, features);
096: progressBar.setStringPainted(true);
097: container.add(progressBar);
098: cancel = new JButton("Cancel");
099: cancel.addActionListener(frame);
100: container.add(cancel);
101: pack();
102:
103: boolean geometry = false;
104: DBaseIndex[] index = new DBaseIndex[properties.length];
105: RTree rtree = null;
106:
107: try {
108: String[] dataTypes = shapeFile.getDataTypes();
109: int[] lengths = shapeFile.getDataLengths();
110:
111: if (geometryCheckBox.isSelected() && !hasGeometry) {
112: geometry = true;
113: rtree = new RTree(2, 11, fileName + ".rti");
114: }
115:
116: boolean indexes = false;
117: for (int i = 0; i < index.length; i++) {
118: if (checkboxes[i].isSelected() && !hasIndex[i]) {
119: index[i] = DBaseIndex
120: .createIndex(
121: fileName + "$" + properties[i],
122: properties[i],
123: lengths[i],
124: uniqueBoxes[i].isSelected(),
125: (dataTypes[i]
126: .equalsIgnoreCase("N")
127: || dataTypes[i]
128: .equalsIgnoreCase("I") || dataTypes[i]
129: .equalsIgnoreCase("F")));
130: indexes = true;
131: } else
132: index[i] = null;
133: }
134:
135: if (geometry || indexes) {
136: for (int i = 1; i < features + 1; i++) {
137: Feature feature = shapeFile
138: .getFeatureByRecNo(i);
139:
140: if (geometry) {
141: Geometry[] geometries = feature
142: .getGeometryPropertyValues();
143: if (geometries.length == 0) {
144: LOG.logInfo("no geometries at recno"
145: + i);
146: continue;
147: }
148: Envelope envelope = null;
149: // TODO: deal with more than one geometry; handle geometry=null (allowed
150: // in shapefile)
151: envelope = (feature
152: .getDefaultGeometryPropertyValue())
153: .getEnvelope();
154: if (envelope == null) { // assume a Point-geometry
155: Point pnt = (Point) geometries[0];
156: envelope = GeometryFactory
157: .createEnvelope(pnt.getX(), pnt
158: .getY(), pnt.getX(),
159: pnt.getY(), null);
160: }
161: HyperBoundingBox box = new HyperBoundingBox(
162: new HyperPoint(envelope.getMin()
163: .getAsArray()),
164: new HyperPoint(envelope.getMax()
165: .getAsArray()));
166: rtree.insert(new Integer(i), box);
167: }
168:
169: for (int j = 0; j < index.length; j++) {
170: if (index[j] != null) {
171: QualifiedName qn = new QualifiedName(
172: properties[j]);
173: index[j].addKey((Comparable) feature
174: .getDefaultProperty(qn), i);
175: }
176: }
177:
178: progressBar.setValue(i);
179:
180: synchronized (this ) {
181: if (stop) {
182: shapeFile.close();
183: if (geometry) {
184: rtree.close();
185: new File(fileName + ".rti")
186: .delete();
187: }
188: for (int j = 0; j < index.length; j++) {
189: if (index[j] != null) {
190: index[j].close();
191: new File(fileName + "$"
192: + properties[j]
193: + ".ndx").delete();
194: }
195: }
196: System.exit(3);
197: }
198: }
199: }
200: }
201:
202: try {
203: if (geometry) {
204: rtree.close();
205: }
206: shapeFile.close();
207:
208: for (int i = 0; i < index.length; i++)
209: if (index[i] != null)
210: index[i].close();
211: } catch (Exception e) {
212: e.printStackTrace();
213: JOptionPane.showMessageDialog(frame, e);
214: System.exit(1);
215: }
216:
217: if (!geometryCheckBox.isSelected() && hasGeometry) {
218: new File(fileName + ".rti").delete();
219: }
220:
221: for (int i = 0; i < index.length; i++) {
222: if (!checkboxes[i].isSelected() && hasIndex[i]) {
223: new File(fileName + "$" + properties[i]
224: + ".ndx").delete();
225: }
226: }
227:
228: System.exit(0);
229: } catch (Exception ex) {
230: ex.printStackTrace();
231: JOptionPane.showMessageDialog(frame, ex);
232: System.exit(1);
233: }
234: }
235:
236: public void stopIndexing() {
237: synchronized (this ) {
238: stop = true;
239: }
240: }
241: }
242:
243: public IndexFrame(File file) throws Exception {
244: super ("Deegree");
245: fileName = file.getPath();
246: fileName = fileName.substring(0, fileName.length() - 4);
247: shapeFile = new ShapeFile(fileName);
248: properties = shapeFile.getProperties();
249:
250: Container container = getContentPane();
251: container.setLayout(new BorderLayout());
252: int size = properties.length;
253: JPanel panel = new JPanel(new GridLayout(10 + 2, 2));
254:
255: panel.add(new JLabel("Attributes"));
256: panel.add(new JLabel("Unique"));
257:
258: hasGeometry = shapeFile.hasRTreeIndex();
259: hasIndex = new boolean[size];
260: for (int i = 0; i < size; i++)
261: hasIndex[i] = shapeFile.hasDBaseIndex(properties[i]);
262:
263: checkboxes = new JCheckBox[size];
264: uniqueBoxes = new JCheckBox[size];
265:
266: geometryCheckBox = new JCheckBox("Geometry");
267: geometryCheckBox.setSelected(hasGeometry);
268:
269: panel.add(geometryCheckBox);
270: panel.add(new JCheckBox("")).setEnabled(false);
271:
272: for (int i = 0; i < checkboxes.length; i++) {
273: checkboxes[i] = new JCheckBox(properties[i]);
274: checkboxes[i].setSelected(hasIndex[i]);
275: if (i < 10) {
276: panel.add(checkboxes[i]);
277:
278: uniqueBoxes[i] = (JCheckBox) panel
279: .add(new JCheckBox(""));
280: if (hasIndex[i]) {
281: uniqueBoxes[i].setSelected(shapeFile
282: .isUnique(properties[i]));
283: } else {
284: checkboxes[i].addChangeListener(this );
285: }
286: uniqueBoxes[i].setEnabled(false);
287: }
288: }
289:
290: JButton start = new JButton("Start indexing");
291: start.addActionListener(this );
292: container.add("South", start);
293: container.add("Center", panel);
294:
295: setBounds(100, 100, 100, 100);
296: pack();
297:
298: setResizable(true);
299: }
300:
301: /**
302: * @param e
303: */
304: public void actionPerformed(ActionEvent e) {
305: if (e.getSource() == cancel) {
306: if (indexing != null) {
307: if (JOptionPane.showConfirmDialog(this ,
308: "Cancel indexing?", "Question",
309: JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
310: indexing.stopIndexing();
311: }
312: } else {
313: if (hasGeometry != geometryCheckBox.isSelected()) {
314: indexing = new Indexing(this );
315: indexing.start();
316: return;
317: }
318:
319: for (int i = 0; i < hasIndex.length; i++) {
320: if (hasIndex[i] != checkboxes[i].isSelected()) {
321: indexing = new Indexing(this );
322: indexing.start();
323: return;
324: }
325: }
326:
327: JOptionPane.showMessageDialog(this , "Nothing changed");
328: }
329: }
330:
331: /**
332: * @param e
333: */
334: public void stateChanged(ChangeEvent e) {
335: JCheckBox checkbox = (JCheckBox) e.getSource();
336:
337: for (int i = 0; i < checkboxes.length; i++) {
338: if (checkbox == checkboxes[i]) {
339: uniqueBoxes[i].setEnabled(checkbox.isSelected());
340: if (!checkbox.isSelected()) {
341: uniqueBoxes[i].setSelected(false);
342: }
343: break;
344: }
345: }
346: }
347: }
|