Java tutorial
/* * Maui, Maltcms User Interface. * Copyright (C) 2008-2014, The authors of Maui. All rights reserved. * * Project website: http://maltcms.sf.net * * Maui may be used under the terms of either the * * GNU Lesser General Public License (LGPL) * http://www.gnu.org/licenses/lgpl.html * * or the * * Eclipse Public License (EPL) * http://www.eclipse.org/org/documents/epl-v10.php * * As a user/recipient of Maui, you may choose which license to receive the code * under. Certain files or entire directories may not be covered by this * dual license, but are subject to licenses compatible to both LGPL and EPL. * License exceptions are explicitly declared in all relevant files or in a * LICENSE file in the relevant directories. * * Maui 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. Please consult the relevant license documentation * for details. */ package net.sf.maltcms.common.charts.ui; import java.awt.BorderLayout; import java.awt.Color; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; import net.sf.maltcms.common.charts.api.CategoryChartBuilder; import net.sf.maltcms.common.charts.api.dataset.ACategoryDataset; import net.sf.maltcms.common.charts.api.overlay.SelectionOverlay; import net.sf.maltcms.common.charts.api.selection.category.CategoryMouseSelectionHandler; import net.sf.maltcms.common.charts.api.selection.InstanceContentSelectionHandler; import static net.sf.maltcms.common.charts.ui.Bundle.CTL_CategoryChartTopComponent; import static net.sf.maltcms.common.charts.ui.Bundle.HINT_CategoryChartTopComponent; import org.jfree.chart.ChartPanel; import org.netbeans.spi.navigator.NavigatorLookupHint; import org.openide.util.Lookup; import org.openide.util.NbBundle.Messages; import static org.openide.util.RequestProcessor.getDefault; import org.openide.util.Task; import org.openide.util.TaskListener; import org.openide.util.lookup.AbstractLookup; import org.openide.util.lookup.InstanceContent; import static org.openide.util.lookup.Lookups.fixed; import org.openide.util.lookup.ProxyLookup; import org.openide.windows.TopComponent; /** * Top component which displays a CategoryChart. */ @Messages({ "CTL_CategoryChartAction=CategoryChart", "CTL_CategoryChartTopComponent=CategoryChart Window", "HINT_CategoryChartTopComponent=This is a CategoryChart window" }) public final class CategoryChartTopComponent<TARGET> extends TopComponent { private ChartPanel panel; private InstanceContent content = new InstanceContent(); private Lookup lookup = new AbstractLookup(content); private InstanceContentSelectionHandler selectionHandler; public CategoryChartTopComponent(Class<TARGET> resultType, final ACategoryDataset<?, TARGET> dataset, final CategoryChartBuilder cb) { associateLookup(new ProxyLookup(lookup, fixed(new NavigatorLookupHint() { @Override public String getContentType() { return "application/jfreechart+overlay"; } }))); initComponents(); setName(CTL_CategoryChartTopComponent()); setToolTipText(HINT_CategoryChartTopComponent()); setEnabled(false); Task t = getDefault().create(new Runnable() { @Override public void run() { createChartPanel(cb, dataset, content); } }); t.addTaskListener(new CategoryChartLoaderTaskListener()); getDefault().post(t); } @Override public int getPersistenceType() { return PERSISTENCE_NEVER; } /** * 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() { jToolBar1 = new javax.swing.JToolBar(); clearSelection = new javax.swing.JButton(); setLayout(new java.awt.BorderLayout()); jToolBar1.setRollover(true); org.openide.awt.Mnemonics.setLocalizedText(clearSelection, org.openide.util.NbBundle .getMessage(CategoryChartTopComponent.class, "CategoryChartTopComponent.clearSelection.text")); // NOI18N clearSelection.setFocusable(false); clearSelection.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); clearSelection.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); clearSelection.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clearSelectionActionPerformed(evt); } }); jToolBar1.add(clearSelection); add(jToolBar1, java.awt.BorderLayout.PAGE_START); }// </editor-fold>//GEN-END:initComponents private void clearSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearSelectionActionPerformed if (selectionHandler != null) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { selectionHandler.clear(); } }); } }//GEN-LAST:event_clearSelectionActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton clearSelection; private javax.swing.JToolBar jToolBar1; // End of variables declaration//GEN-END:variables void writeProperties(java.util.Properties p) { // better to version settings since initial version as advocated at // http://wiki.apidesign.org/wiki/PropertyFiles p.setProperty("version", "1.0"); // TODO store your settings } void readProperties(java.util.Properties p) { String version = p.getProperty("version"); // TODO read your settings according to their version } private class CategoryChartLoaderTaskListener implements TaskListener { @Override public void taskFinished(Task task) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { panel = getLookup().lookup(ChartPanel.class); JScrollPane pane = new JScrollPane(panel); add(pane, BorderLayout.CENTER); selectionHandler = getLookup().lookup(InstanceContentSelectionHandler.class); setEnabled(true); invalidate(); revalidate(); } }); } } private void customizeChart(ChartPanel customPanel) { // ChartCustomizer.setSeriesColors(customPanel.getChart().getXYPlot(), 0.5f); // ChartCustomizer.setSeriesStrokes(customPanel.getChart().getXYPlot(), 2.0f); // ChartCustomizer.setSeriesShapes(customPanel.getChart().getXYPlot(), new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0)); } private void createChartPanel(final CategoryChartBuilder cb, final ACategoryDataset<?, TARGET> dataset, final InstanceContent content) { content.add(dataset); ChartPanel customPanel = cb.buildPanel(); SelectionOverlay so = new SelectionOverlay(Color.RED, Color.BLUE, 1.75f, 1.75f, 0.66f); customPanel.getChart().getCategoryPlot().getRangeAxis().addChangeListener(so); customPanel.getChart().getCategoryPlot().getDomainAxis().addChangeListener(so); InstanceContentSelectionHandler selectionHandler = new InstanceContentSelectionHandler(content, so, InstanceContentSelectionHandler.Mode.ON_CLICK, dataset); CategoryMouseSelectionHandler<TARGET> sl = new CategoryMouseSelectionHandler<>(dataset); sl.addSelectionChangeListener(so); sl.addSelectionChangeListener(selectionHandler); customPanel.addChartMouseListener(sl); customPanel.addOverlay(so); so.addChangeListener(customPanel); customizeChart(customPanel); content.add(customPanel); content.add(selectionHandler); content.add(sl); } }