Java tutorial
/* * Copyright 2015 Patrik Karlsson. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package se.trixon.toolbox.photokml; import java.awt.Desktop; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.Calendar; import javax.swing.filechooser.FileNameExtensionFilter; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.SystemUtils; import org.netbeans.api.settings.ConvertAsProperties; import org.openide.util.HelpCtx; import org.openide.util.NbBundle; import org.openide.windows.IOProvider; import org.openide.windows.OutputWriter; import org.openide.windows.TopComponent; import se.trixon.almond.dialogs.Message; import se.trixon.almond.dialogs.SimpleDialog; import se.trixon.almond.dictionary.Dict; import se.trixon.almond.icon.Pict; import se.trixon.toolbox.core.Toolbox; import se.trixon.toolbox.core.base.ToolTopComponent; /** * Top component which displays something. */ @ConvertAsProperties(dtd = "-//se.trixon.toolbox.photokml//PhotoKml//EN", autostore = false) @TopComponent.Description(preferredID = "PhotoKmlTopComponent", //iconBase="SET/PATH/TO/ICON/HERE", persistenceType = TopComponent.PERSISTENCE_ONLY_OPENED) @TopComponent.Registration(mode = "editor", openAtStartup = false) public final class PhotoKmlTopComponent extends ToolTopComponent implements OperationListener { private static final int ICON_SIZE = TOOLBAR_ICON_SIZE; private final PhotoKmlController mController; private File mDestination; private final Options mOptions = Options.INSTANCE; public PhotoKmlTopComponent() { mBundle = NbBundle.getBundle(PhotoKmlTopComponent.class); mToolName = mBundle.getString("Tool-Name"); initComponents(); setName(mToolName); mController = new PhotoKmlController(this); init(); } @Override public HelpCtx getHelpCtx() { return new HelpCtx(mHelpId); } private void init() { mHelpId = "se.trixon.toolbox.photokml.about"; startButton.setIcon(Pict.Actions.ARROW_RIGHT.get(ICON_SIZE)); startButton.setToolTipText(Dict.START.getString()); viewButton.setIcon(Pict.Categories.APPLICATIONS_INTERNET.get(ICON_SIZE)); saveLogButton.setIcon(Pict.Actions.DOCUMENT_SAVE.get(ICON_SIZE)); saveLogButton.setEnabled(false); helpButton.setIcon(Pict.Actions.HELP_CONTENTS.get(ICON_SIZE)); helpButton.setToolTipText(Dict.HELP.getString()); } private void requestKmlFileObject() { FileNameExtensionFilter filter = new FileNameExtensionFilter("Keyhole Markup Language (*.kml)", "kml"); SimpleDialog.clearFilters(); SimpleDialog.addFilter(filter); SimpleDialog.setFilter(filter); SimpleDialog.setParent(this); if (mDestination == null) { SimpleDialog.setPath(FileUtils.getUserDirectory()); } else { SimpleDialog.setPath(mDestination.getParentFile()); SimpleDialog.setSelectedFile(new File("")); } if (SimpleDialog.saveFile(new String[] { "kml" })) { start(SimpleDialog.getPath()); } } private void requestLogFileObject() { FileNameExtensionFilter filter = new FileNameExtensionFilter(Dict.LOG_FILES.getString() + " (*.log)", "log"); SimpleDialog.clearFilters(); SimpleDialog.addFilter(filter); SimpleDialog.setFilter(filter); SimpleDialog.setParent(this); File file = new File(mDestination.getParentFile(), FilenameUtils.getBaseName(mDestination.getName()) + ".log"); SimpleDialog.setPath(file.getParentFile()); SimpleDialog.setSelectedFile(file); if (SimpleDialog.saveFile(new String[] { "log" })) { saveLogFile(SimpleDialog.getPath()); } } private void saveLogFile(File file) { try { FileUtils.writeLines(file, Arrays.asList(mLogBuilder.toString().split("\n"))); } catch (IOException ex) { Message.error(Dict.IO_ERROR_TITLE.getString(), String.format(Dict.FILE_ERROR_WRITE_MESSAGE.getString(), file.getAbsolutePath())); } } private void start(File destination) { mLogBuilder = new StringBuilder(); mDestination = destination; if (mInputOutput != null) { mInputOutput.closeInputOutput(); } mInputOutput = IOProvider.getDefault().getIO(mToolName, false); mInputOutput.select(); if (validPathPrefix()) { appendLog(Toolbox.getDefaultDateFormat().format(Calendar.getInstance().getTime()) + "\n"); Operation operation = new Operation(this, destination); operation.start(); } else { String message = mBundle.getString("errorRelativePaths"); appendLog(message); Message.error(Dict.IO_ERROR_TITLE.getString(), message); } } private boolean validPathPrefix() { boolean result = true; String destPrefix = FilenameUtils.getPrefix(mDestination.getAbsolutePath()); for (String string : mOptions.getSourcePaths().split(SystemUtils.PATH_SEPARATOR)) { if (!destPrefix.equalsIgnoreCase(FilenameUtils.getPrefix(string))) { result = false; break; } } return result; } /** * 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() { toolBar = new javax.swing.JToolBar(); startButton = new javax.swing.JButton(); viewButton = new javax.swing.JButton(); saveLogButton = new javax.swing.JButton(); jSeparator1 = new javax.swing.JToolBar.Separator(); helpButton = new javax.swing.JButton(); configPanel = new se.trixon.toolbox.photokml.config.ConfigPanel(); toolBar.setFloatable(false); toolBar.setRollover(true); startButton.setFocusable(false); startButton.setHorizontalAlignment(javax.swing.SwingConstants.LEADING); startButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { startButtonActionPerformed(evt); } }); toolBar.add(startButton); viewButton.setToolTipText(org.openide.util.NbBundle.getMessage(PhotoKmlTopComponent.class, "PhotoKmlTopComponent.viewButton.toolTipText")); // NOI18N viewButton.setEnabled(false); viewButton.setFocusable(false); viewButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); viewButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); viewButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { viewButtonActionPerformed(evt); } }); toolBar.add(viewButton); saveLogButton.setToolTipText(org.openide.util.NbBundle.getMessage(PhotoKmlTopComponent.class, "PhotoKmlTopComponent.saveLogButton.toolTipText")); // NOI18N saveLogButton.setFocusable(false); saveLogButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); saveLogButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); saveLogButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveLogButtonActionPerformed(evt); } }); toolBar.add(saveLogButton); toolBar.add(jSeparator1); helpButton.setFocusable(false); helpButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); helpButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); helpButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { helpButtonActionPerformed(evt); } }); toolBar.add(helpButton); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(toolBar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(configPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(toolBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(configPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE))); }// </editor-fold>//GEN-END:initComponents private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed if (configPanel.hasValidSettings()) { //start(new File(System.getProperty("user.home"), "PhotoKml.kml")); requestKmlFileObject(); } }//GEN-LAST:event_startButtonActionPerformed private void saveLogButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveLogButtonActionPerformed requestLogFileObject(); }//GEN-LAST:event_saveLogButtonActionPerformed private void viewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewButtonActionPerformed new Thread(() -> { try { Desktop.getDesktop().open(mDestination); } catch (IOException | IllegalArgumentException ex) { Message.error(Dict.FILE_NOT_FOUND_TITLE.getString(), String.format(Dict.FILE_NOT_FOUND_MESSAGE.getString(), mDestination.getAbsolutePath())); } catch (Exception ex) { Message.error(Dict.ERROR.getString(), ex.getLocalizedMessage()); } }).start(); }//GEN-LAST:event_viewButtonActionPerformed private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed displayHelp(mHelpId); }//GEN-LAST:event_helpButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private se.trixon.toolbox.photokml.config.ConfigPanel configPanel; private javax.swing.JButton helpButton; private javax.swing.JToolBar.Separator jSeparator1; private javax.swing.JButton saveLogButton; private javax.swing.JButton startButton; private javax.swing.JToolBar toolBar; private javax.swing.JButton viewButton; // End of variables declaration//GEN-END:variables void writeProperties(java.util.Properties p) { p.setProperty("version", "1.0"); } void readProperties(java.util.Properties p) { String version = p.getProperty("version"); } @Override public void onOperationFailed(String message) { // startButton.setEnabled(true); // saveLogButton.setEnabled(true); // configPanel.setEnabled(true); // makeBusy(false); onOperationFinished(message); viewButton.setEnabled(false); } @Override public void onOperationFinished(String message) { startButton.setEnabled(true); saveLogButton.setEnabled(true); viewButton.setEnabled(true); makeBusy(false); new Thread(() -> { configPanel.setEnabled(true); Message.information(Dict.OPERATION_COMPLETED.getString(), message); }).start(); } @Override public void onOperationInterrupted() { startButton.setEnabled(true); saveLogButton.setEnabled(true); configPanel.setEnabled(true); makeBusy(false); } @Override public void onOperationLog(String message) { appendLog(message); } @Override public void onOperationProcessingStarted() { appendLog(configPanel.getHeaderBuilder().toString()); } @Override public void onOperationStarted() { startButton.setEnabled(false); saveLogButton.setEnabled(false); viewButton.setEnabled(false); configPanel.setEnabled(false); makeBusy(true); } }