diff --git a/utilities/src/org/netbeans/modules/pdf/Bundle.properties b/utilities/src/org/netbeans/modules/pdf/Bundle.properties --- a/utilities/src/org/netbeans/modules/pdf/Bundle.properties +++ b/utilities/src/org/netbeans/modules/pdf/Bundle.properties @@ -45,14 +45,8 @@ Services/MIMEResolver/org-netbeans-modules-pdf-mime-resolver.xml=PDF Document Objects # PDFOpenSupport -TITLE_pick_a_viewer=PDF Viewer Not Found ... +EXC_could_not_open=Could not open PDF -# ReconfigureReaderPanel -ACS_Field=N/A -LBL_select_viewer=Select -LBL_reconfigure=\ - The IDE needs a PDF viewer application, such as Acrobat Reader. \ - Please select an executable program to view PDF files with. # {0} - exception type # {1} - detail message LBL_existing_exception=Details of the problem: {0}: {1} diff --git a/utilities/src/org/netbeans/modules/pdf/PDFOpenSupport.java b/utilities/src/org/netbeans/modules/pdf/PDFOpenSupport.java --- a/utilities/src/org/netbeans/modules/pdf/PDFOpenSupport.java +++ b/utilities/src/org/netbeans/modules/pdf/PDFOpenSupport.java @@ -41,22 +41,18 @@ * Version 2 license, then the option applies only if the new code is * made subject to such option by the copyright holder. */ - package org.netbeans.modules.pdf; +import java.awt.Desktop; import java.io.File; import java.io.IOException; -import org.openide.DialogDescriptor; import org.openide.DialogDisplayer; +import org.openide.NotifyDescriptor; import org.openide.cookies.OpenCookie; -import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.openide.loaders.DataObject; import org.openide.loaders.DataObjectNotFoundException; -import org.openide.util.Exceptions; import org.openide.util.NbBundle; -import org.openide.util.Utilities; -import static org.openide.util.Utilities.OS_MAC; /** * Permits a PDF file to be opened in an external viewer. @@ -66,16 +62,9 @@ */ class PDFOpenSupport implements OpenCookie { - private static final String DEFAULT_MACOS_VIEWER = "open"; //NOI18N - private static final String[] APP_DIRS = new String[] { - "/usr/bin", "/usr/local/bin" }; //NOI18N - private static final String[] VIEWER_NAMES = new String[] { - "evince", "xpdf", "kghostview", "ggv", "acroread" }; //NOI18N - static final String FALLBACK_VIEWER_NAME = "acroread"; //NOI18N - private File f; private DataObject dObj; - + /** * @exception java.lang.IllegalArgumentException * if the specified file does not exist or is not a plain file @@ -94,160 +83,17 @@ } public void open() { - if(dObj != null){ + if (dObj != null) { f = FileUtil.toFile(dObj.getPrimaryFile()); } - final String filePath = f.getAbsolutePath(); - - if (Utilities.isWindows()) { - /* - * To run the PDF viewer, we need to execute: - * - * cmd.exe /C start - * - * This works well except for cases that there is a space - * in the file's path. In this case, the file's path must be - * enclosed to quotes to make the command-line interpreter (cmd.exe) - * consider it a single argument. BUT: If the first argument - * to the "start" command is enclosed in quotes, it is considered - * to be a window title. So, to make sure the file path argument - * is not considered a window title, we must pass some dummy - * argument enclosed in quotes as the first argument and the - * actual file path as the second argument (of the 'start' command): - * - * cmd.exe /C start "PDF Viewer" - * - * (see also bug #122221) - */ - tryCommand(new String[] {"cmd.exe", "/C", "start", //NOI18N - "\"PDF Viewer\"", //win.title //NOI18N - filePath}); - return; + + try { + Desktop.getDesktop().open(f); + } catch (IOException ex) { + String msg = NbBundle.getMessage(PDFOpenSupport.class, "EXC_could_not_open"); + NotifyDescriptor nd = new NotifyDescriptor.Exception(ex, msg); + DialogDisplayer.getDefault().notify(nd); } - Settings sett = Settings.getDefault(); - - File viewer = sett.getPDFViewer(); - boolean viewerUnset = (viewer == null); - - if (viewerUnset && (Utilities.getOperatingSystem() == OS_MAC)) { - String cmd = DEFAULT_MACOS_VIEWER; - if (tryCommand(new String[] {cmd, filePath})) { - sett.setPDFViewer(new File(cmd)); - return; - } - } - - if (viewerUnset) { - viewer = tryPredefinedViewers(filePath); - if (viewer != null) { - sett.setPDFViewer(viewer); - return; - } - } - - if (viewerUnset) { - viewer = new File(FALLBACK_VIEWER_NAME); - } - - boolean viewerFailed = false; - do { - try { - tryCommandExc(new String[] {viewer.getPath(), filePath}); - if (viewerUnset || viewerFailed) { - sett.setPDFViewer(viewer); - } - break; - } catch (IOException ioe) { - viewerFailed = true; - - // Try to reconfigure. - String excmessage = ioe.getLocalizedMessage(); - /* [PENDING] does not work (no properties show in sheet, though node has them): - Node n; - try { - n = new BeanNode (sett); - } catch (IntrospectionException ie) { - TopManager.getDefault ().notifyException (ie); - return; - } - PropertySheet sheet = new PropertySheet (); - sheet.setNodes (new Node[] { n }); - //TopManager.getDefault ().getNodeOperation ().explore (n); - */ - ReconfigureReaderPanel configPanel - = new ReconfigureReaderPanel(viewer, excmessage); - String title = NbBundle.getMessage( - PDFOpenSupport.class, - "TITLE_pick_a_viewer"); //NOI18N - DialogDescriptor d = new DialogDescriptor(configPanel, title); - if (DialogDisplayer.getDefault().notify(d) - == DialogDescriptor.OK_OPTION) { - sett.setPDFViewer(viewer = configPanel.getSelectedFile()); - } else { - break; - } - } - } while (true); } - - /** - */ - private static File tryPredefinedViewers(String filePath) { - for (int i = 0; i < APP_DIRS.length; i++) { - - File dir = new File(APP_DIRS[i]); - if (!dir.exists() || !dir.isDirectory()) { - continue; - } - - for (int j = 0; j < VIEWER_NAMES.length; j++) { - String viewerPath = APP_DIRS[i] + File.separatorChar - + VIEWER_NAMES[j]; - File viewer = new File(viewerPath); - if (!viewer.exists()) { - continue; - } - - if (tryCommand(new String[] {viewerPath, filePath})) { - return viewer; - } - //else: never mind, try the next predefined viewer - } - } - - return null; - } - - /** - * Tries to execute the specified command and arguments. - * - * @param cmdArray array containing the command to call and its arguments - * @return {@code true} if the execution was successful, - * {@code false} otherwise - */ - private static boolean tryCommand(final String[] cmdArray) { - try { - tryCommandExc(cmdArray); - return true; - } catch (IOException ioe) { - return false; - } - } - - /** - * Tries to execute the specified command and arguments. - * - * @param cmdArray array containing the command to call and its arguments - * @return {@code true} if the execution was successful, - * {@code false} otherwise - * @exception java.io.IOException - * - */ - private static void tryCommandExc(final String[] cmdArray) - throws IOException { - Runtime.getRuntime().exec(cmdArray); - // [PENDING] redirect the process' output - } - } diff --git a/utilities/src/org/netbeans/modules/pdf/ReconfigureReaderPanel.form b/utilities/src/org/netbeans/modules/pdf/ReconfigureReaderPanel.form deleted file mode 100644 --- a/utilities/src/org/netbeans/modules/pdf/ReconfigureReaderPanel.form +++ /dev/null @@ -1,76 +0,0 @@ - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/utilities/src/org/netbeans/modules/pdf/ReconfigureReaderPanel.java b/utilities/src/org/netbeans/modules/pdf/ReconfigureReaderPanel.java deleted file mode 100644 --- a/utilities/src/org/netbeans/modules/pdf/ReconfigureReaderPanel.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. - * - * Oracle and Java are registered trademarks of Oracle and/or its affiliates. - * Other names may be trademarks of their respective owners. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common - * Development and Distribution License("CDDL") (collectively, the - * "License"). You may not use this file except in compliance with the - * License. You can obtain a copy of the License at - * http://www.netbeans.org/cddl-gplv2.html - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the - * specific language governing permissions and limitations under the - * License. When distributing the software, include this License Header - * Notice in each file and include the License file at - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the GPL Version 2 section of the License file that - * accompanied this code. If applicable, add the following below the - * License Header, with the fields enclosed by brackets [] replaced by - * your own identifying information: - * "Portions Copyrighted [year] [name of copyright owner]" - * - * Contributor(s): - * - * The Original Software is NetBeans. The Initial Developer of the Original - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun - * Microsystems, Inc. All Rights Reserved. - * - * If you wish your version of this file to be governed by only the CDDL - * or only the GPL Version 2, indicate your decision by adding - * "[Contributor] elects to include this software in this distribution - * under the [CDDL or GPL Version 2] license." If you do not indicate a - * single choice of license, a recipient has the option to distribute - * your version of this file under either the CDDL, the GPL Version 2 or - * to extend the choice of license to its licensees as provided above. - * However, if you add GPL Version 2 code and therefore, elected the GPL - * Version 2 license, then the option applies only if the new code is - * made subject to such option by the copyright holder. - */ - -package org.netbeans.modules.pdf; - -import java.io.File; -import java.util.ResourceBundle; -import javax.swing.JFileChooser; - -import org.openide.util.NbBundle; - -/** Simple panel for a dialog asking users to choose a PDF viewer. - * Offers a file chooser. - * @author Jesse Glick - */ -public class ReconfigureReaderPanel extends javax.swing.JPanel { - - private Settings settings; - private final File defaultViewer; - private ResourceBundle bundle = NbBundle.getBundle (ReconfigureReaderPanel.class); - - public ReconfigureReaderPanel (File defaultViewer, String exceptionMessage) { - settings = Settings.getDefault (); - this.defaultViewer = defaultViewer; - initComponents (); - initAccessibility (); - } - - private void initAccessibility() { - this.getAccessibleContext().setAccessibleDescription(bundle.getString ("LBL_reconfigure")); - viewerField.getAccessibleContext().setAccessibleDescription(bundle.getString ("ACS_Field")); - chooseButton.getAccessibleContext().setAccessibleDescription(bundle.getString ("ACS_LBL_choose")); - } - - /** 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. - */ - // //GEN-BEGIN:initComponents - private void initComponents() { - java.awt.GridBagConstraints gridBagConstraints; - - topLabel = new javax.swing.JLabel(); - currentViewerLabel = new javax.swing.JLabel(); - viewerField = new javax.swing.JTextField(); - chooseButton = new javax.swing.JButton(); - - setLayout(new java.awt.GridBagLayout()); - - topLabel.setText(bundle.getString ("LBL_reconfigure")); - topLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridwidth = 3; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; - gridBagConstraints.insets = new java.awt.Insets(12, 12, 12, 11); - add(topLabel, gridBagConstraints); - - currentViewerLabel.setText(bundle.getString ("LBL_current_viewer")); - currentViewerLabel.setLabelFor(viewerField); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 0; - gridBagConstraints.gridy = 1; - gridBagConstraints.insets = new java.awt.Insets(0, 12, 12, 12); - add(currentViewerLabel, gridBagConstraints); - - viewerField.setText(settings.getPDFViewer() != null ? settings.getPDFViewer().getPath() : PDFOpenSupport.FALLBACK_VIEWER_NAME); - viewerField.addFocusListener(new java.awt.event.FocusAdapter() { - public void focusGained(java.awt.event.FocusEvent evt) { - viewerFieldFocusGained(evt); - } - }); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 1; - gridBagConstraints.gridy = 1; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.weightx = 1.0; - gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0); - add(viewerField, gridBagConstraints); - - org.openide.awt.Mnemonics.setLocalizedText(chooseButton, org.openide.util.NbBundle.getMessage(ReconfigureReaderPanel.class, "LBL_choose")); // NOI18N - chooseButton.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - chooseButtonActionPerformed(evt); - } - }); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 2; - gridBagConstraints.gridy = 1; - gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; - gridBagConstraints.insets = new java.awt.Insets(0, 5, 12, 11); - add(chooseButton, gridBagConstraints); - }// //GEN-END:initComponents - - // accessibility - private void viewerFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_viewerFieldFocusGained - viewerField.selectAll(); - }//GEN-LAST:event_viewerFieldFocusGained - - private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseButtonActionPerformed - File dir = defaultViewer.getParentFile(); - JFileChooser chooser = (dir != null) - ? new JFileChooser(dir) - : new JFileChooser(); - String chooserTitle = bundle.getString("LBL_select_viewer"); //NOI18N - if (chooser.showDialog (this, chooserTitle) - == JFileChooser.APPROVE_OPTION) { - viewerField.setText(chooser.getSelectedFile().getPath()); - } - }//GEN-LAST:event_chooseButtonActionPerformed - - /** - */ - File getSelectedFile() { - return new File(viewerField.getText().trim()); - } - - // Variables declaration - do not modify//GEN-BEGIN:variables - private javax.swing.JButton chooseButton; - private javax.swing.JLabel currentViewerLabel; - private javax.swing.JLabel topLabel; - private javax.swing.JTextField viewerField; - // End of variables declaration//GEN-END:variables - -}