Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package eremeykin.pete.plotter; import eremeykin.pete.api.core.centrallookupapi.CentralLookup; import eremeykin.pete.api.core.logger.Logger; import eremeykin.pete.api.core.logger.LoggerManager; import eremeykin.pete.api.core.workspace.WorkspaceManager; import eremeykin.pete.api.model.Model; import eremeykin.pete.api.model.ModelChangedEvent; import eremeykin.pete.api.model.ModelChangedListener; import eremeykin.pete.api.model.ModelParameter; import java.awt.BorderLayout; import java.awt.Color; import java.io.File; import java.io.FileNotFoundException; import java.io.FilenameFilter; import java.io.IOException; import java.nio.file.Path; import java.nio.file.StandardWatchEventKinds; import java.nio.file.WatchEvent; import java.nio.file.WatchKey; import java.nio.file.WatchService; import java.util.AbstractMap; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Scanner; import java.util.concurrent.atomic.AtomicBoolean; import javax.swing.JOptionPane; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.Plot; import org.jfree.chart.plot.PolarPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import org.netbeans.api.settings.ConvertAsProperties; import org.openide.awt.ActionID; import org.openide.awt.ActionReference; import org.openide.util.Exceptions; import org.openide.util.Lookup; import org.openide.util.LookupEvent; import org.openide.util.LookupListener; import org.openide.windows.TopComponent; import org.openide.util.NbBundle.Messages; import org.openide.util.Task; /** * Top component which displays something. */ //@ConvertAsProperties( // dtd = "-//eremeykin.pete.plotter//Plotter//EN", // autostore = false //) //@TopComponent.Description( // preferredID = "PlotterTopComponent", // //iconBase="SET/PATH/TO/ICON/HERE", // persistenceType = TopComponent.PERSISTENCE_ALWAYS //) //@TopComponent.Registration(mode = "editor", openAtStartup = false) //@ActionID(category = "Window", id = "eremeykin.pete.plotter.PlotterTopComponent") //@ActionReference(path = "Menu/Window" /*, position = 333 */) //@TopComponent.OpenActionRegistration( // displayName = "#CTL_PlotterAction", // preferredID = "PlotterTopComponent" //) //@Messages({ // "CTL_PlotterAction=Plotter", // "CTL_PlotterTopComponent=Plotter Window", // "HINT_PlotterTopComponent=This is a Plotter window" //}) public abstract class PlotterTopComponent extends TopComponent implements LookupListener, ModelChangedListener, FileWatcher.Updateable { private static final Logger LOGGER = LoggerManager.getLogger(PlotterTopComponent.class); private File home; Model model; private Lookup.Result<Model> modelResult = null; private final XYSeries toleranceSeries = new XYSeries("Tolerance"); private final XYSeries dataSeries = new XYSeries("Deformation"); // private final FileWatcher watchThread = new FileWatcher(home, this); private ChartPanel chartPanel; public PlotterTopComponent() { initComponents(); // setName(Bundle.CTL_PlotterTopComponent()); // setToolTipText(Bundle.HINT_PlotterTopComponent()); } void init(TopComponent tc) { final XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries(dataSeries); dataset.addSeries(toleranceSeries); final JFreeChart chart = createChart(dataset); chartPanel = new ChartPanel(chart); tc.setLayout(new java.awt.BorderLayout()); tc.add(chartPanel, BorderLayout.CENTER); tc.validate(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle .getMessage(PlotterTopComponent.class, "PlotterTopComponent.jLabel1.text")); // NOI18N javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1) .addContainerGap(275, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1) .addContainerGap(274, Short.MAX_VALUE))); }// </editor-fold>//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel jLabel1; // End of variables declaration//GEN-END:variables @Override public void componentOpened() { modelResult = CentralLookup.getDefault().lookupResult(Model.class); modelResult.addLookupListener(this); home = WorkspaceManager.INSTANCE.getWorkspace(); // if (!watchThread.isAlive()) // watchThread.start(); } @Override public void componentClosed() { model.removeModelChangedListener(this); // watchThread.stopWatch(); } void writeProperties(java.util.Properties p) { p.setProperty("version", "1.0"); } void readProperties(java.util.Properties p) { String version = p.getProperty("version"); } private void clear() { toleranceSeries.clear(); dataSeries.clear(); } // @Override public void update(File home) { // for first rpt file if (model == null) { clear(); return; } File[] rptFiles = home.listFiles(filter()); // catch if there is no such file if (rptFiles.length == 0) { clear(); return; } File firstRPT = rptFiles[0]; Scanner scanner; try { scanner = new Scanner(firstRPT); scanner.useDelimiter("\\s+|\n"); } catch (FileNotFoundException ex) { clear(); return; } List<Map.Entry<Double, Double>> tmpList = new ArrayList<>(); for (int i = 0; scanner.hasNext(); i++) { String line = scanner.next(); try { double x1 = Double.valueOf(line); line = scanner.next(); double x2 = Double.valueOf(line); // System.out.println("x1=" + x1 + "\nx2=" + x2); tmpList.add(new AbstractMap.SimpleEntry<>(x1, x2)); } catch (NumberFormatException ex) { // only if it is the third or following line if (i > 1) { LOGGER.error("Error while parsing double from file: " + firstRPT.getAbsolutePath()); JOptionPane.showMessageDialog(this, "Error while parsing result file.", "Parsing error", JOptionPane.ERROR_MESSAGE); } } } if (tmpList.isEmpty()) { clear(); return; } fillData(tmpList, dataSeries, toleranceSeries); } abstract void fillData(List<Map.Entry<Double, Double>> tmpList, XYSeries dataSeries, XYSeries toleranceSeries); abstract FilenameFilter filter(); abstract JFreeChart createChart(XYDataset dataset); }