/* * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI * for visualizing and manipulating spatial features with geometry and attributes. * * JUMP is Copyright (C) 2003 Vivid Solutions * * This class implements extensions to JUMP and is * Copyright (C) Stefan Steiniger. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * For more information, contact: * Stefan Steiniger * perriger@gmx.de */ /*********************************************** * created on 19.10.2007 * last modified: * * author: sstein * * description: * * ***********************************************/ package org.openjump.core.ui.plugin.tools.statistics; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Iterator; import java.util.List; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JInternalFrame; import org.openjump.core.apitools.FeatureSchemaTools; import org.openjump.core.ui.plot.Plot2DPanelOJ; import com.vividsolutions.jump.I18N; import com.vividsolutions.jump.feature.AttributeType; import com.vividsolutions.jump.feature.Feature; import com.vividsolutions.jump.feature.FeatureCollection; import com.vividsolutions.jump.feature.FeatureSchema; import com.vividsolutions.jump.task.TaskMonitor; import com.vividsolutions.jump.workbench.WorkbenchContext; import com.vividsolutions.jump.workbench.model.Layer; import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn; import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory; import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck; import com.vividsolutions.jump.workbench.plugin.PlugInContext; import com.vividsolutions.jump.workbench.plugin.ThreadedPlugIn; import com.vividsolutions.jump.workbench.ui.GUIUtil; import com.vividsolutions.jump.workbench.ui.GenericNames; import com.vividsolutions.jump.workbench.ui.MenuNames; import com.vividsolutions.jump.workbench.ui.MultiInputDialog; import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller; public class CreateHistogramPlugIn extends AbstractPlugIn implements ThreadedPlugIn{ private String sHistogram = "Histogram"; private String sCount = "count"; private MultiInputDialog dialog; private String T2 ="number of ranges"; private String CLAYER = "select layer"; private String ATTRIBUTE = "select attribute"; private Layer selLayer = null; private int ranges = 7; private FeatureCollection fc = null; private String selAttribute = null; private String sName = "Create Histogram Plot"; private String sWrongDataType = "Wrong datatype of chosen attribute"; //File selFile = null; /** * this method is called on the startup by JUMP/OpenJUMP. * We set here the menu entry for calling the function. */ public void initialize(PlugInContext context) throws Exception { ATTRIBUTE = GenericNames.SELECT_ATTRIBUTE; T2 = I18N.get("org.openjump.core.ui.plugin.tools.statistics.CreateHistogramPlugIn.Number-of-ranges"); CLAYER = GenericNames.SELECT_LAYER; sHistogram = I18N.get("org.openjump.core.ui.plugin.tools.statistics.CreateHistogramPlugIn.Histogram-Plot"); sCount = I18N.get("org.openjump.core.ui.plugin.tools.statistics.CreateHistogramPlugIn.count"); sName = I18N.get("org.openjump.core.ui.plugin.tools.statistics.CreateHistogramPlugIn.Create-Histogram-Plot"); sWrongDataType = I18N.get("org.openjump.core.ui.plugin.tools.statistics.CreateBarPlotPlugIn.Wrong-datatype-of-chosen-attribute"); FeatureInstaller featureInstaller = new FeatureInstaller(context.getWorkbenchContext()); featureInstaller.addMainMenuItem( this, //exe new String[] {MenuNames.TOOLS, MenuNames.STATISTICS, MenuNames.PLOT }, this.sName + "...", //name methode .getName recieved by AbstractPlugIn false, //checkbox null, //icon createEnableCheck(context.getWorkbenchContext())); //enable check } /** * This method is used to define when the menu entry is activated or * disabled. In this example we allow the menu entry to be usable only * if one layer exists. */ public static MultiEnableCheck createEnableCheck(WorkbenchContext workbenchContext) { EnableCheckFactory checkFactory = new EnableCheckFactory(workbenchContext); return new MultiEnableCheck() .add(checkFactory.createAtLeastNLayersMustExistCheck(1)) .add(checkFactory.createTaskWindowMustBeActiveCheck()); } /** * this function is called by JUMP/OpenJUMP if one clicks on the menu entry. * It is called before the "run" method and useful to do all the GUI /user-input things * In this example we call two additional methods {@link #setDialogValues(MultiInputDialog, PlugInContext)} * and {@link #getDialogValues(MultiInputDialog)} to obtain the Layer and the buffer radius by the user. */ public boolean execute(PlugInContext context) throws Exception{ this.reportNothingToUndoYet(context); dialog = new MultiInputDialog( context.getWorkbenchFrame(),sName, true); this.setDialogValues(dialog, context); GUIUtil.centreOnWindow(dialog); dialog.setVisible(true); if (! dialog.wasOKPressed()) { return false; } this.getDialogValues(dialog); return true; } public void run(TaskMonitor monitor, PlugInContext context) throws Exception { createHistogram(context, this.selLayer); } private void setDialogValues(MultiInputDialog dialog, PlugInContext context) { dialog.addLayerComboBox(CLAYER, context.getCandidateLayer(0), context.getLayerManager()); List list = FeatureSchemaTools.getFieldsFromLayerWithoutGeometryAndString(context.getCandidateLayer(0)); Object val = list.size()>0?list.iterator().next():null; final JComboBox jcb_attribute = dialog.addComboBox(ATTRIBUTE, val, list,ATTRIBUTE); if (list.size() == 0) jcb_attribute.setEnabled(false); dialog.addIntegerField(T2, this.ranges, 6, T2); dialog.getComboBox(CLAYER).addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { List list = getFieldsFromLayerWithoutGeometryAndString(); if (list.size() == 0) { jcb_attribute.setModel(new DefaultComboBoxModel(new String[0])); jcb_attribute.setEnabled(false); } jcb_attribute.setModel(new DefaultComboBoxModel(list.toArray(new String[0]))); } }); } private void getDialogValues(MultiInputDialog dialog) { //this.itemlayer = dialog.getLayer(this.CLAYER); this.ranges = dialog.getInteger(T2); this.selLayer = dialog.getLayer(CLAYER); this.fc = this.selLayer.getFeatureCollectionWrapper(); this.selAttribute = dialog.getText(ATTRIBUTE); } private boolean createHistogram(final PlugInContext context, Layer selLayer) throws Exception { FeatureSchema fs = this.fc.getFeatureSchema(); AttributeType type = null; if ((fs.getAttributeType(this.selAttribute) == AttributeType.DOUBLE) || (fs.getAttributeType(this.selAttribute) == AttributeType.INTEGER)){ //-- move on type = fs.getAttributeType(this.selAttribute); } else{ //System.out.println("CreateHistogramPlugIn: wrong datatype of chosen attribute"); context.getWorkbenchFrame().warnUser(sWrongDataType); return false; } double[] data = new double[this.fc.size()]; int i=0; for (Iterator iter = fc.iterator(); iter.hasNext();) { Feature f = (Feature) iter.next(); Object val = f.getAttribute(this.selAttribute); if (type == AttributeType.DOUBLE){ data[i] = ((Double)val).doubleValue(); } else if (type == AttributeType.INTEGER){ data[i] = ((Integer)val).doubleValue(); } i++; } //double[] data2 = { 45, 89, 6, 32, 63, 12 }; final Plot2DPanelOJ plot = new Plot2DPanelOJ(); plot.addHistogramPlotOJ(this.selAttribute, data, this.ranges, context, selLayer, this.selAttribute); plot.plotToolBar.setVisible(true); plot.setAxisLabel(0, this.selAttribute); plot.setAxisLabel(1, this.sCount); // FrameView fv = new FrameView(plot); // -- replace the upper line by: JInternalFrame frame = new JInternalFrame(sHistogram); frame.setLayout(new BorderLayout()); frame.add(plot, BorderLayout.CENTER); frame.setClosable(true); frame.setResizable(true); frame.setMaximizable(true); frame.setSize(450, 450); frame.setVisible(true); /**********************************************/ /* JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar); JMenu mTools = new JMenu("Tools"); menuBar.add(mTools); mTools.add(new AbstractAction("Save Image as PNG File", IconLoader.icon("disk.png")) { public void actionPerformed(ActionEvent e){ java.io.File file = CreateHistogramPlugIn.selectFile(context); if (file != null){ try { System.out.println("CreateHistogramPlugIn: write grahics file"); plot.toGraphicFile(file); } catch (IOException ex) { JOptionPane.showConfirmDialog(null, "Save failed : " + ex.getMessage(), "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); } } else{ JOptionPane.showConfirmDialog(null, "Save failed : " + "file not found", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); } } }); */ /**********************************************/ context.getWorkbenchFrame().addInternalFrame(frame); return true; } private List getFieldsFromLayerWithoutGeometryAndString() { return FeatureSchemaTools.getFieldsFromLayerWithoutGeometryAndString(dialog.getLayer(CLAYER)); } /* public static File selectFile(PlugInContext context){ JFileChooser fc = GUIUtil.createJFileChooserWithOverwritePrompting("png"); // Show save dialog; this method does not return until the dialog is closed fc.showSaveDialog(context.getWorkbenchFrame()); File file = fc.getSelectedFile(); try{ String name = file.getPath(); name = CreateHistogramPlugIn.addExtension(name,"png"); File newFile = new File(name); return newFile; } catch(Exception e){ return null; } } private static String addExtension(String path, String extension) { if (path.toUpperCase().endsWith(extension.toUpperCase())) { return path; } if (path.endsWith(".")) { return path + extension; } return path + "." + extension; }*/ }