-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGUI.java
More file actions
156 lines (125 loc) · 5.43 KB
/
GUI.java
File metadata and controls
156 lines (125 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2009, StackFrame, LLC
// This code is licensed under GPL v2.0 http://www.gnu.org/licenses/gpl-2.0.html
package com.stackframe.pdfliberator;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Arrays;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
/**
* A GUI for PDFLiberator.
*
* @author Gene McCulley <a href="mailto:mcculley@stackframe.com">mcculley@stackframe.com</a>
*/
public class GUI {
private final Preferences prefs = Preferences.userNodeForPackage(getClass());
private GUI() {
}
private File chooserDirectory() {
String directoryName = prefs.get("directory", null);
if (directoryName != null) {
File directory = new File(directoryName);
if (directory.exists()) {
return directory;
}
}
return new File(System.getProperty("user.home"));
}
/**
* A simple implementation of FileFilter for dealing with files of a particular extension.
*/
private static class ExtensionFileFilter extends FileFilter {
private final String extension;
private ExtensionFileFilter(String extension) {
this.extension = extension;
}
@Override
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith("." + extension.toLowerCase());
}
@Override
public String getDescription() {
return extension + " files";
}
}
/**
* Instantiate the GUI.
*/
public static void launch() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final GUI gui = new GUI();
final JFrame frame = new JFrame();
final JTextPane text = new JTextPane();
text.setEditable(false);
frame.getContentPane().add(new JScrollPane(text));
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
// TODO: Use the OS X wrapper class to do the right thing for the menu bar.
menuBar.add(fileMenu);
JMenuItem liberateMenuItem = new JMenuItem("Liberate...");
fileMenu.add(liberateMenuItem);
liberateMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
liberateMenuItem.getToolkit().getMenuShortcutKeyMask()));
liberateMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JFileChooser chooser = new JFileChooser(gui.chooserDirectory());
chooser.setDialogTitle("Liberate");
chooser.setFileFilter(new ExtensionFileFilter("PDF"));
chooser.setMultiSelectionEnabled(true);
int result = chooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
gui.prefs.put("directory", chooser.getCurrentDirectory().getPath());
File[] selected = chooser.getSelectedFiles();
for (File file : selected) {
String message;
Color messageColor;
try {
PDFLiberator.liberate(file);
messageColor = Color.BLACK;
message = "Liberated " + file;
} catch (Exception e) {
messageColor = Color.RED;
message = "Error processing " + file + ": " + e;
}
SimpleAttributeSet set = new SimpleAttributeSet();
set.addAttribute(StyleConstants.Foreground, messageColor);
try {
text.getDocument().insertString(0, message + "\n", set);
} catch (BadLocationException ble) {
throw new AssertionError(ble);
}
}
}
}
});
JMenuItem quit = new JMenuItem("Quit");
fileMenu.add(quit);
quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, quit.getToolkit().getMenuShortcutKeyMask()));
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
frame.setSize(320, 240);
frame.setVisible(true);
}
});
}
}