List of usage examples for javax.swing JFileChooser getSelectedFile
public File getSelectedFile()
From source file:de.evaluationtool.gui.EvaluationFrameActionListener.java
private void saveTSV() { JFileChooser chooser = new JFileChooser("Save evaluation result as TSV"); chooser.setCurrentDirectory(frame.defaultDirectory); int returnVal = chooser.showSaveDialog(frame); if (returnVal == JFileChooser.APPROVE_OPTION) { if (chooser.getSelectedFile().exists() && (JOptionPane.showConfirmDialog(frame, "File already exists. Overwrite?") != JOptionPane.YES_OPTION)) { return; }/*from ww w . jav a2 s . c o m*/ frame.saveTSV(chooser.getSelectedFile()); } }
From source file:lu.lippmann.cdb.ext.hydviga.ui.GapFillingKnowledgeDBExplorerFrame.java
/** * Constructor./* ww w. j a va 2 s. co m*/ */ GapFillingKnowledgeDBExplorerFrame(final Instances ds, final int dateIdx, final StationsDataProvider gcp) throws Exception { LogoHelper.setLogo(this); this.setTitle("KnowledgeDB: explorer"); this.gcp = gcp; this.tablePanel = new JXPanel(); this.tablePanel.setBorder(new TitledBorder("Cases")); final JXPanel highPanel = new JXPanel(); highPanel.setLayout(new BoxLayout(highPanel, BoxLayout.X_AXIS)); highPanel.add(this.tablePanel); this.geomapPanel = new JXPanel(); this.geomapPanel.add(buildGeoMapChart(new ArrayList<String>(), new ArrayList<String>())); highPanel.add(this.geomapPanel); this.caseChartPanel = new JXPanel(); this.caseChartPanel.setBorder(new TitledBorder("Inspected fake gap")); this.mostSimilarChartPanel = new JXPanel(); this.mostSimilarChartPanel.setBorder(new TitledBorder("Most similar")); this.nearestChartPanel = new JXPanel(); this.nearestChartPanel.setBorder(new TitledBorder("Nearest")); this.downstreamChartPanel = new JXPanel(); this.downstreamChartPanel.setBorder(new TitledBorder("Downstream")); this.upstreamChartPanel = new JXPanel(); this.upstreamChartPanel.setBorder(new TitledBorder("Upstream")); getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)); //getContentPane().add(new JCheckBox("Use incomplete series")); getContentPane().add(highPanel); //getContentPane().add(new JXButton("Export")); getContentPane().add(caseChartPanel); getContentPane().add(mostSimilarChartPanel); getContentPane().add(nearestChartPanel); getContentPane().add(downstreamChartPanel); getContentPane().add(upstreamChartPanel); //final Instances kdbDS=GapFillingKnowledgeDB.getKnowledgeDBWithBestCasesOnly(); final Instances kdbDS = GapFillingKnowledgeDB.getKnowledgeDB(); final JXTable gapsTable = buidJXTable(kdbDS); final JScrollPane tableScrollPane = new JScrollPane(gapsTable); tableScrollPane.setPreferredSize( new Dimension(COMPONENT_WIDTH - 100, 40 + (int) (tableScrollPane.getPreferredSize().getHeight()))); this.tablePanel.add(tableScrollPane); gapsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); gapsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(final ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { final int modelRow = gapsTable.getSelectedRow(); final String attrname = gapsTable.getModel().getValueAt(modelRow, 1).toString(); final int gapsize = (int) Double .valueOf(gapsTable.getModel().getValueAt(modelRow, 4).toString()).doubleValue(); final int position = (int) Double .valueOf(gapsTable.getModel().getValueAt(modelRow, 5).toString()).doubleValue(); final String mostSimilarFlag = gapsTable.getModel().getValueAt(modelRow, 14).toString(); final String nearestFlag = gapsTable.getModel().getValueAt(modelRow, 15).toString(); final String downstreamFlag = gapsTable.getModel().getValueAt(modelRow, 16).toString(); final String upstreamFlag = gapsTable.getModel().getValueAt(modelRow, 17).toString(); final String algoname = gapsTable.getModel().getValueAt(modelRow, 12).toString(); final boolean useDiscretizedTime = Boolean .valueOf(gapsTable.getModel().getValueAt(modelRow, 13).toString()); try { geomapPanel.removeAll(); caseChartPanel.removeAll(); mostSimilarChartPanel.removeAll(); nearestChartPanel.removeAll(); downstreamChartPanel.removeAll(); upstreamChartPanel.removeAll(); final Set<String> selected = new HashSet<String>(); final Instances tmpds = WekaDataProcessingUtil.buildFilteredDataSet(ds, 0, ds.numAttributes() - 1, Math.max(0, position - GapsUtil.getCountOfValuesBeforeAndAfter(gapsize)), Math.min(position + gapsize + GapsUtil.getCountOfValuesBeforeAndAfter(gapsize), ds.numInstances() - 1)); final List<String> attributeNames = WekaTimeSeriesUtil .getNamesOfAttributesWithoutGap(tmpds); //final List<String> attributeNames=WekaDataStatsUtil.getAttributeNames(ds); attributeNames.remove(attrname); attributeNames.remove("timestamp"); if (Boolean.valueOf(mostSimilarFlag)) { final String mostSimilarStationName = WekaTimeSeriesSimilarityUtil .findMostSimilarTimeSerie(tmpds, tmpds.attribute(attrname), attributeNames, false); selected.add(mostSimilarStationName); final Attribute mostSimilarStationAttr = tmpds.attribute(mostSimilarStationName); final ChartPanel cp0 = GapsUIUtil.buildGapChartPanel(ds, dateIdx, mostSimilarStationAttr, gapsize, position); cp0.getChart().removeLegend(); cp0.setPreferredSize(CHART_DIMENSION); mostSimilarChartPanel.add(cp0); } if (Boolean.valueOf(nearestFlag)) { final String nearestStationName = gcp.findNearestStation(attrname, attributeNames); selected.add(nearestStationName); final Attribute nearestStationAttr = ds.attribute(nearestStationName); final ChartPanel cp0 = GapsUIUtil.buildGapChartPanel(ds, dateIdx, nearestStationAttr, gapsize, position); cp0.getChart().removeLegend(); cp0.setPreferredSize(CHART_DIMENSION); nearestChartPanel.add(cp0); } if (Boolean.valueOf(downstreamFlag)) { final String downstreamStationName = gcp.findDownstreamStation(attrname, attributeNames); selected.add(downstreamStationName); final Attribute downstreamStationAttr = ds.attribute(downstreamStationName); final ChartPanel cp0 = GapsUIUtil.buildGapChartPanel(ds, dateIdx, downstreamStationAttr, gapsize, position); cp0.getChart().removeLegend(); cp0.setPreferredSize(CHART_DIMENSION); downstreamChartPanel.add(cp0); } if (Boolean.valueOf(upstreamFlag)) { final String upstreamStationName = gcp.findUpstreamStation(attrname, attributeNames); selected.add(upstreamStationName); final Attribute upstreamStationAttr = ds.attribute(upstreamStationName); final ChartPanel cp0 = GapsUIUtil.buildGapChartPanel(ds, dateIdx, upstreamStationAttr, gapsize, position); cp0.getChart().removeLegend(); cp0.setPreferredSize(CHART_DIMENSION); upstreamChartPanel.add(cp0); } final GapFiller gapFiller = GapFillerFactory.getGapFiller(algoname, useDiscretizedTime); final ChartPanel cp = GapsUIUtil.buildGapChartPanelWithCorrection(ds, dateIdx, ds.attribute(attrname), gapsize, position, gapFiller, selected); cp.getChart().removeLegend(); cp.setPreferredSize(new Dimension((int) CHART_DIMENSION.getWidth(), (int) (CHART_DIMENSION.getHeight() * 1.5))); caseChartPanel.add(cp); geomapPanel.add(buildGeoMapChart(Arrays.asList(attrname), selected)); getContentPane().repaint(); pack(); } catch (Exception e1) { e1.printStackTrace(); } } } }); gapsTable.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(final MouseEvent e) { if (!e.isPopupTrigger()) { // nothing? } else { final JPopupMenu jPopupMenu = new JPopupMenu("feur"); final JMenuItem mExport = new JMenuItem("Export this table as CSV"); mExport.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent e) { final JFileChooser fc = new JFileChooser(); fc.setAcceptAllFileFilterUsed(false); final int returnVal = fc.showSaveDialog(gapsTable); if (returnVal == JFileChooser.APPROVE_OPTION) { try { final File file = fc.getSelectedFile(); WekaDataAccessUtil.saveInstancesIntoCSVFile(kdbDS, file); } catch (Exception ee) { ee.printStackTrace(); } } } }); jPopupMenu.add(mExport); jPopupMenu.show(gapsTable, e.getX(), e.getY()); } } }); setPreferredSize(new Dimension(FRAME_WIDTH, 1000)); pack(); setVisible(true); /* select the first row */ gapsTable.setRowSelectionInterval(0, 0); }
From source file:com.igormaznitsa.jhexed.swing.editor.ui.dialogs.hexeditors.DialogEditSVGImageValue.java
private void buttonSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveAsActionPerformed final JFileChooser dlg = new JFileChooser(); dlg.addChoosableFileFilter(new FileFilter() { @Override/*from ww w . jav a2 s. com*/ public boolean accept(File f) { return f.isDirectory() || f.getName().toLowerCase(Locale.ENGLISH).endsWith(".svg"); } @Override public String getDescription() { return "SVG files (*.svg)"; } }); if (dlg.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { File file = dlg.getSelectedFile(); if (FilenameUtils.getExtension(file.getName()).isEmpty()) { file = new File(file.getParentFile(), file.getName() + ".svg"); } if (file.exists() && JOptionPane.showConfirmDialog(this.parent, "Overwrite file '" + file.getAbsolutePath() + "\'?", "Overwriting", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) { return; } try { FileUtils.writeByteArrayToFile(file, this.value.getImage().getImageData()); } catch (IOException ex) { Log.error("Can't write image [" + file + ']', ex); JOptionPane.showMessageDialog(this, "Can't save the file for error!", "IO Error", JOptionPane.ERROR_MESSAGE); } } }
From source file:com.mirth.connect.client.ui.AttachmentExportDialog.java
private void browseSelected() { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (userPreferences != null) { File currentDir = new File(userPreferences.get("currentDirectory", "")); if (currentDir.exists()) { chooser.setCurrentDirectory(currentDir); }//from w w w . j ava 2 s . c o m } if (chooser.showOpenDialog(getParent()) == JFileChooser.APPROVE_OPTION) { if (userPreferences != null) { userPreferences.put("currentDirectory", chooser.getCurrentDirectory().getPath()); } fileField.setText(chooser.getSelectedFile().getAbsolutePath()); } }
From source file:net.aepik.alasca.gui.util.LoadFileFrame.java
/** * Perform action on event for this object. */// ww w. j av a 2 s . c om public void actionPerformed(ActionEvent e) { Object o = e.getSource(); if (o == boutonOpenFile) { JFileChooser jfcProgramme = new JFileChooser("."); jfcProgramme.setMultiSelectionEnabled(false); jfcProgramme.setDialogTitle("Selectionner un fichier"); jfcProgramme.setApproveButtonText("Selectionner"); jfcProgramme.setApproveButtonToolTipText("Cliquer apres avoir selectionn un fichier"); jfcProgramme.setAcceptAllFileFilterUsed(false); if (jfcProgramme.showDialog(this, null) == JFileChooser.APPROVE_OPTION) { try { filename.setText(jfcProgramme.getSelectedFile().getCanonicalPath()); } catch (IOException ioe) { JOptionPane.showMessageDialog(null, "Erreur de nom de fichier.", "Erreur", JOptionPane.ERROR_MESSAGE); } } } if (o == boutonOk && filename.getText().length() != 0) { if (!this.loadFile(filename.getText(), (String) this.syntaxes.getSelectedItem())) { JOptionPane.showMessageDialog(this, this.getErrorMessage(), "Erreur", JOptionPane.ERROR_MESSAGE); } else { windowClosing(null); } } if (o == boutonAnnuler) { windowClosing(null); } }
From source file:lu.lippmann.cdb.ext.hydviga.ui.GapsOverviewPanel.java
public void refresh(final Instances dataSet, final int dateIdx) { final Instances gapsDescriptionsDataset = GapsUtil.buildGapsDescription(gcp, dataSet, dateIdx); final JXTable gapsDescriptionsTable = new JXTable(); final InstanceTableModel gapsDescriptionsTableModel = new InstanceTableModel(false); gapsDescriptionsTableModel.setDataset(gapsDescriptionsDataset); gapsDescriptionsTable.setModel(gapsDescriptionsTableModel); gapsDescriptionsTable.setEditable(true); gapsDescriptionsTable.setShowHorizontalLines(false); gapsDescriptionsTable.setShowVerticalLines(false); gapsDescriptionsTable.setVisibleRowCount(5); gapsDescriptionsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); gapsDescriptionsTable.setSortable(false); gapsDescriptionsTable.packAll();/*from w w w .ja v a 2 s . c om*/ gapsDescriptionsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); gapsDescriptionsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { int modelRow = gapsDescriptionsTable.getSelectedRow(); if (modelRow < 0) modelRow = 0; final String attrname = gapsDescriptionsTableModel.getValueAt(modelRow, 1).toString(); final Attribute attr = dataSet.attribute(attrname); final int gapsize = (int) Double .valueOf(gapsDescriptionsTableModel.getValueAt(modelRow, 5).toString()).doubleValue(); final int position = (int) Double .valueOf(gapsDescriptionsTableModel.getValueAt(modelRow, 6).toString()).doubleValue(); try { final ChartPanel cp = GapsUIUtil.buildGapChartPanel(dataSet, dateIdx, attr, gapsize, position); visualOverviewPanel.removeAll(); visualOverviewPanel.add(cp, BorderLayout.CENTER); geomapPanel.removeAll(); geomapPanel.add(gcp.getMapPanel(Arrays.asList(attrname), new ArrayList<String>(), false)); jxp.updateUI(); } catch (Exception ee) { ee.printStackTrace(); } } } }); gapsDescriptionsTable.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(final MouseEvent e) { final InstanceTableModel instanceTableModel = (InstanceTableModel) gapsDescriptionsTable.getModel(); final int row = gapsDescriptionsTable.rowAtPoint(e.getPoint()); //final int row=gapsDescriptionsTable.getSelectedRow(); final int modelRow = gapsDescriptionsTable.convertRowIndexToModel(row); //final int modelRow=(int)Double.valueOf(instanceTableModel.getValueAt(row,0).toString()).doubleValue(); //System.out.println(row+" "+modelRow); final String attrname = instanceTableModel.getValueAt(modelRow, 1).toString(); final Attribute attr = dataSet.attribute(attrname); final int gapsize = (int) Double.valueOf(instanceTableModel.getValueAt(modelRow, 5).toString()) .doubleValue(); final int position = (int) Double.valueOf(instanceTableModel.getValueAt(modelRow, 6).toString()) .doubleValue(); if (!e.isPopupTrigger()) { // nothing? } else { final JPopupMenu jPopupMenu = new JPopupMenu("feur"); final JMenuItem interactiveFillMenuItem = new JMenuItem("Fill this gap (interactively)"); interactiveFillMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent e) { final GapFillingFrame jxf = new GapFillingFrame(atv, dataSet, attr, dateIdx, GapsUtil.getCountOfValuesBeforeAndAfter(gapsize), position, gapsize, gcp, false); jxf.setSize(new Dimension(900, 700)); //jxf.setExtendedState(Frame.MAXIMIZED_BOTH); jxf.setLocationRelativeTo(jPopupMenu); jxf.setVisible(true); //jxf.setResizable(false); } }); jPopupMenu.add(interactiveFillMenuItem); final JMenuItem lookupInKnowledgeDBMenuItem = new JMenuItem( "Show the most similar cases from KnowledgeDB"); lookupInKnowledgeDBMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent e) { final double x = gcp.getCoordinates(attrname)[0]; final double y = gcp.getCoordinates(attrname)[1]; final String season = instanceTableModel.getValueAt(modelRow, 3).toString() .split("/")[0]; final boolean isDuringRising = instanceTableModel.getValueAt(modelRow, 11).toString() .equals("true"); try { final Calendar cal = Calendar.getInstance(); final String dateAsString = instanceTableModel.getValueAt(modelRow, 2).toString() .replaceAll("'", ""); cal.setTime(FormatterUtil.DATE_FORMAT.parse(dateAsString)); final int year = cal.get(Calendar.YEAR); new SimilarCasesFrame(dataSet, dateIdx, gcp, attrname, gapsize, position, x, y, year, season, isDuringRising); } catch (Exception e1) { e1.printStackTrace(); } } }); jPopupMenu.add(lookupInKnowledgeDBMenuItem); final JMenuItem mExport = new JMenuItem("Export this table as CSV"); mExport.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent e) { final JFileChooser fc = new JFileChooser(); fc.setAcceptAllFileFilterUsed(false); final int returnVal = fc.showSaveDialog(gapsDescriptionsTable); if (returnVal == JFileChooser.APPROVE_OPTION) { try { final File file = fc.getSelectedFile(); WekaDataAccessUtil.saveInstancesIntoCSVFile(gapsDescriptionsDataset, file); } catch (Exception ee) { ee.printStackTrace(); } } } }); jPopupMenu.add(mExport); jPopupMenu.show(gapsDescriptionsTable, e.getX(), e.getY()); } } }); final int tableWidth = (int) gapsDescriptionsTable.getPreferredSize().getWidth() + 30; final JScrollPane scrollPane = new JScrollPane(gapsDescriptionsTable); scrollPane.setPreferredSize(new Dimension(Math.min(tableWidth, 500), 500)); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); this.tablePanel.removeAll(); this.tablePanel.add(scrollPane, BorderLayout.CENTER); this.visualOverviewPanel.removeAll(); /* automatically compute the most similar series and the 'rising' flag */ new AbstractSimpleAsync<Void>(false) { @Override public Void execute() throws Exception { final int rc = gapsDescriptionsTableModel.getRowCount(); for (int i = 0; i < rc; i++) { final int modelRow = i; try { final String attrname = gapsDescriptionsTableModel.getValueAt(modelRow, 1).toString(); final Attribute attr = dataSet.attribute(attrname); final int gapsize = (int) Double .valueOf(gapsDescriptionsTableModel.getValueAt(modelRow, 5).toString()) .doubleValue(); final int position = (int) Double .valueOf(gapsDescriptionsTableModel.getValueAt(modelRow, 6).toString()) .doubleValue(); /* most similar */ gapsDescriptionsTableModel.setValueAt("...", modelRow, 7); final int cvba = GapsUtil.getCountOfValuesBeforeAndAfter(gapsize); Instances gapds = WekaDataProcessingUtil.buildFilteredDataSet(dataSet, 0, dataSet.numAttributes() - 1, Math.max(0, position - cvba), Math.min(position + gapsize + cvba, dataSet.numInstances() - 1)); final String mostsimilar = WekaTimeSeriesSimilarityUtil.findMostSimilarTimeSerie(gapds, attr, WekaTimeSeriesUtil.getNamesOfAttributesWithoutGap(gapds), false); gapsDescriptionsTableModel.setValueAt(mostsimilar, modelRow, 7); /* 'rising' flag */ gapsDescriptionsTableModel.setValueAt("...", modelRow, 11); final List<String> attributeNames = WekaDataStatsUtil.getAttributeNames(dataSet); attributeNames.remove("timestamp"); final String nearestStationName = gcp.findNearestStation(attr.name(), attributeNames); final Attribute nearestStationAttr = dataSet.attribute(nearestStationName); //System.out.println(nearestStationName+" "+nearestStationAttr); final boolean isDuringRising = GapsUtil.isDuringRising(dataSet, position, gapsize, new int[] { dateIdx, attr.index(), nearestStationAttr.index() }); gapsDescriptionsTableModel.setValueAt(isDuringRising, modelRow, 11); gapsDescriptionsTableModel.fireTableDataChanged(); } catch (Exception e) { gapsDescriptionsTableModel.setValueAt("n/a", modelRow, 7); gapsDescriptionsTableModel.setValueAt("n/a", modelRow, 11); e.printStackTrace(); } } return null; } @Override public void onSuccess(Void result) { } @Override public void onFailure(Throwable caught) { caught.printStackTrace(); } }.start(); /* select the first row */ gapsDescriptionsTable.setRowSelectionInterval(0, 0); }
From source file:gov.nij.er.ui.EntityResolutionDemo.java
private void saveParameters() { JFileChooser fc = new JFileChooser(); fc.setFileFilter(new SerializedParameterFileFilter()); int returnVal = fc.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); try {// w w w.ja v a2s . co m saveParameters(file); } catch (Exception e) { e.printStackTrace(); } } }
From source file:gov.nij.er.ui.EntityResolutionDemo.java
private void loadParameters() { JFileChooser fc = new JFileChooser(); fc.setFileFilter(new SerializedParameterFileFilter()); int returnVal = fc.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); try {/* ww w . j a va 2 s . co m*/ loadParameters(file); } catch (Exception e) { e.printStackTrace(); } } }
From source file:net.sf.profiler4j.console.Console.java
/** * Saves the current project./* ww w . ja v a 2 s .co m*/ * * @param saveAs force the user to select a file name even * @return <code>true</code> if the user has cancelled (only in the case of save as) */ public boolean saveProject(boolean saveAs) { if (project.getFile() == null || saveAs) { JFileChooser fc = new JFileChooser(lastDir); fc.setDialogTitle("Save Project As"); fc.addChoosableFileFilter(projectFilter); if (fc.showSaveDialog(mainFrame) == JFileChooser.APPROVE_OPTION) { File f = fc.getSelectedFile(); if (!f.getName().endsWith(".p4j")) { f = new File(f.getAbsolutePath() + ".p4j"); } project.setFile(f); } else { return true; } } Element rootEl = new Element("Profiler4jProject"); Document doc = new Document(rootEl); rootEl.addContent(new Element("Host").setText(project.getHostname())); rootEl.addContent(new Element("Port").setText(String.valueOf(project.getPort()))); Element rulesEl = new Element("Rules"); rootEl.addContent(rulesEl); rulesEl.setAttribute("access", project.getAccess().name()); rulesEl.setAttribute("beanProps", String.valueOf(project.isBeanprops())); for (Rule rule : project.getRules()) { rulesEl.addContent( new Element("Rule").setText(rule.getPattern()).setAttribute("action", rule.getAction().name())); } Element exportPatternEl = new Element(PROJECT_XML_ELEMENT__EXPORT_PATTERN); String enabled = String.valueOf(project.isExportAutomaticallyEnabled()); exportPatternEl.setAttribute(PROJECT_XML_ATTRIBUTE__ENABLED, enabled); exportPatternEl.setAttribute(PROJECT_XML_ATTRIBUTE__PATTERN, project.getExportPattern()); rootEl.addContent(exportPatternEl); try { FileWriter fw = new FileWriter(project.getFile()); XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); outputter.output(doc, fw); fw.close(); project.clearChanged(); } catch (IOException e) { error("I/O Error", e); } return false; }
From source file:org.cytoscape.dyn.internal.graphMetrics.SaveChartDialog.java
@Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == cancelButton) { this.setVisible(false); this.dispose(); } else if (e.getSource() == saveChartButton) { JFileChooser saveFileDialog = new JFileChooser(); saveFileDialog/* ww w . j a v a 2 s . co m*/ .addChoosableFileFilter(new ExtensionFileFilter(".jpeg", ".jpg", "Jpeg images (.jpeg, .jpg)")); saveFileDialog.addChoosableFileFilter( new ExtensionFileFilter(".png", "Portable Network Graphic images (.png)")); saveFileDialog .addChoosableFileFilter(new ExtensionFileFilter(".svg", "Scalable Vector Graphics (.svg)")); int save = saveFileDialog.showSaveDialog(this); if (save == JFileChooser.APPROVE_OPTION) { File file = saveFileDialog.getSelectedFile(); int width = ((SpinnerNumberModel) widthSpinner.getModel()).getNumber().intValue(); int height = ((SpinnerNumberModel) heightSpinner.getModel()).getNumber().intValue(); ExtensionFileFilter filter = null; try { filter = (ExtensionFileFilter) saveFileDialog.getFileFilter(); if (!filter.hasExtension(file)) { file = filter.appendExtension(file); } } catch (ClassCastException ex) { // Try to infer the type of file by its extension FileFilter[] filters = saveFileDialog.getChoosableFileFilters(); for (int i = 0; i < filters.length; ++i) { if (filters[i] instanceof ExtensionFileFilter) { filter = (ExtensionFileFilter) filters[i]; if (filter.hasExtension(file)) { break; } filter = null; } } if (filter == null) { // Could not infer the type JOptionPane.showMessageDialog(null, "File type not specified!\nWhen giving file name, please also select one of the supported file types.", "Error", JOptionPane.ERROR_MESSAGE); return; } } // Save the chart to the specified file name try { String ext = filter.getExtension(); if (ext.equals("jpeg")) { ChartUtilities.saveChartAsJPEG(file, chart, width, height); } else if (ext.equals("png")) { ChartUtilities.saveChartAsPNG(file, chart, width, height); } else { VectorGraphics graphics = new SVGGraphics2D(file, new Dimension(width, height)); graphics.startExport(); chart.draw(graphics, new Rectangle2D.Double(0, 0, width, height)); graphics.endExport(); } } catch (IOException ex) { JOptionPane.showMessageDialog(null, "An error occurred while creating or writing to the file.", "Error", JOptionPane.ERROR_MESSAGE); return; } this.setVisible(false); this.dispose(); } else if (save == JFileChooser.ERROR_OPTION) { JOptionPane.showMessageDialog(null, "An error occurred while initializing the window.", "Error", JOptionPane.ERROR_MESSAGE); } } }