KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > pdf > PDFOpenSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.pdf;
21
22 import java.io.*;
23
24 import org.openide.DialogDescriptor;
25 import org.openide.DialogDisplayer;
26 import org.openide.cookies.OpenCookie;
27 import org.openide.util.HelpCtx;
28 import org.openide.util.NbBundle;
29
30 /**
31  * Permits a PDF file to be opened in an external viewer.
32  *
33  * @author Jesse Glick
34  * @author Marian Petras
35  */

36 class PDFOpenSupport implements OpenCookie {
37
38     private static final String JavaDoc[] APP_DIRS = new String JavaDoc[] {
39             "/usr/bin", "/usr/local/bin" }; //NOI18N
40
private static final String JavaDoc[] VIEWER_NAMES = new String JavaDoc[] {
41             "evince", "xpdf", "kghostview", "ggv", "acroread" }; //NOI18N
42
private static final String JavaDoc[] NO_PATH_VIEWERS = new String JavaDoc[] {
43             "acroread", "open" }; //NOI18N
44
static final String JavaDoc FALLBACK_VIEWER_NAME = "acroread"; //NOI18N
45

46     private File f;
47     
48     /**
49      * @exception java.lang.IllegalArgumentException
50      * if the specified file does not exist or is not a plain file
51      */

52     public PDFOpenSupport(File f) {
53         this.f = f;
54     }
55
56     public void open() {
57         Settings sett = Settings.getDefault();
58         
59         File viewer = sett.getPDFViewer();
60         boolean viewerUnset = (viewer == null);
61         
62         if (viewerUnset) {
63             viewer = tryPredefinedViewers(f);
64             if (viewer != null) {
65                 sett.setPDFViewer(viewer);
66                 return;
67             }
68         }
69         
70         if (viewerUnset) {
71             viewer = new File(FALLBACK_VIEWER_NAME);
72         }
73         
74         boolean viewerFailed = false;
75         do {
76             try {
77                 Process JavaDoc p = Runtime.getRuntime().exec(
78                         new String JavaDoc[] {viewer.getPath(),
79                                       f.getAbsolutePath()
80                 });
81                 if (viewerUnset || viewerFailed) {
82                     sett.setPDFViewer(viewer);
83                 }
84                 break;
85                 // [PENDING] redirect p's output
86
} catch (IOException ioe) {
87                 viewerFailed = true;
88                 
89                 // Try to reconfigure.
90
String JavaDoc excmessage = ioe.getLocalizedMessage();
91                 /* [PENDING] does not work (no properties show in sheet, though node has them):
92                 Node n;
93                 try {
94                     n = new BeanNode (sett);
95                 } catch (IntrospectionException ie) {
96                     TopManager.getDefault ().notifyException (ie);
97                     return;
98                 }
99                 PropertySheet sheet = new PropertySheet ();
100                 sheet.setNodes (new Node[] { n });
101                 //TopManager.getDefault ().getNodeOperation ().explore (n);
102                  */

103                 ReconfigureReaderPanel configPanel
104                         = new ReconfigureReaderPanel(viewer, excmessage);
105                 String JavaDoc title = NbBundle.getMessage(
106                                        PDFOpenSupport.class,
107                                        "TITLE_pick_a_viewer"); //NOI18N
108
DialogDescriptor d = new DialogDescriptor(configPanel, title);
109                 if (DialogDisplayer.getDefault().notify(d)
110                         == DialogDescriptor.OK_OPTION) {
111                     sett.setPDFViewer(viewer = configPanel.getSelectedFile());
112                 } else {
113                     break;
114                 }
115             }
116         } while (true);
117     }
118
119     /**
120      */

121     private static File tryPredefinedViewers(File fileToOpen) {
122         for (int i = 0; i < APP_DIRS.length; i++) {
123
124             File dir = new File(APP_DIRS[i]);
125             if (!dir.exists() || !dir.isDirectory()) {
126                 continue;
127             }
128             
129             for (int j = 0; j < VIEWER_NAMES.length; j++) {
130                 String JavaDoc viewerPath = APP_DIRS[i] + File.separatorChar
131                                     + VIEWER_NAMES[j];
132                 File viewer = new File(viewerPath);
133                 if (!viewer.exists()) {
134                     continue;
135                 }
136                 
137                 try {
138                     Process JavaDoc p = Runtime.getRuntime().exec(
139                             new String JavaDoc[] {viewerPath,
140                                           fileToOpen.getAbsolutePath()});
141                     return viewer;
142                 } catch (IOException ex) {
143                     //never mind, try the next predefined viewer
144
}
145             }
146         }
147         
148         for (int i = 0; i < NO_PATH_VIEWERS.length; i++) {
149             try {
150                 Process JavaDoc p = Runtime.getRuntime().exec(
151                         new String JavaDoc[] {NO_PATH_VIEWERS[i],
152                                       fileToOpen.getAbsolutePath()});
153                 return new File(NO_PATH_VIEWERS[i]);
154             } catch (IOException ex) {
155                 //never mind, try the next predefined viewer
156
}
157         }
158         
159         return null;
160     }
161
162     
163 }
164
Popular Tags