001: package it.stefanochizzolini.clown.samples.gui;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Dimension;
005: import java.awt.Toolkit;
006: import java.awt.event.KeyEvent;
007: import java.awt.event.MouseAdapter;
008: import java.awt.event.MouseEvent;
009: import java.io.File;
010: import java.io.FileInputStream;
011: import java.util.Properties;
012:
013: import javax.swing.JFileChooser;
014: import javax.swing.JFrame;
015: import javax.swing.JMenu;
016: import javax.swing.JMenuBar;
017: import javax.swing.JMenuItem;
018: import javax.swing.JPanel;
019: import javax.swing.JScrollPane;
020: import javax.swing.JSplitPane;
021: import javax.swing.JTabbedPane;
022: import javax.swing.JTextPane;
023: import javax.swing.SpringLayout;
024: import javax.swing.filechooser.FileFilter;
025:
026: public class SampleViewer {
027: private class PdfFileFilter extends FileFilter {
028: public boolean accept(File file) {
029: if (file.isDirectory())
030: return true;
031:
032: return file.getName().endsWith(".pdf");
033: }
034:
035: public String getDescription() {
036: return "PDF files";
037: }
038: }
039:
040: private static final String ClassName = SampleViewer.class
041: .getName();
042:
043: private static final String PropertiesFilePath = "PDFClownGuiSamples.properties";
044:
045: private static final String Properties_InputPath = ClassName
046: + ".inputPath";
047:
048: public static void main(String args[]) {
049: Properties properties = new Properties();
050: try {
051: properties.load(new FileInputStream(PropertiesFilePath));
052: } catch (Exception e) {
053: throw new RuntimeException(
054: "An exception occurred while loading the properties file (\""
055: + PropertiesFilePath + "\").", e);
056: }
057:
058: try {
059: SampleViewer sampleViewer = new SampleViewer(properties
060: .getProperty(Properties_InputPath));
061: sampleViewer.show();
062: } catch (Exception e) {
063: e.printStackTrace();
064: }
065: }
066:
067: private String inputPath;
068:
069: private JFrame window;
070: private PdfInspectorSample domInspector;
071:
072: public SampleViewer(String inputPath) {
073: this .inputPath = new java.io.File(inputPath).getAbsolutePath();
074:
075: initialize();
076: }
077:
078: public void show() {
079: window.setVisible(true);
080: }
081:
082: private void initialize() {
083: window = new JFrame("PDF Clown Sample Viewer");
084: SpringLayout springLayout = new SpringLayout();
085:
086: window.getContentPane().setLayout(springLayout);
087: window.setName("main");
088: window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
089: window.setFocusable(true);
090: {
091: Dimension screenSize = Toolkit.getDefaultToolkit()
092: .getScreenSize();
093: Dimension windowSize = new Dimension((int) (screenSize
094: .getWidth() * .6),
095: (int) (screenSize.getHeight() * .6));
096: int x = (int) (screenSize.getWidth() - windowSize.width) / 2;
097: int y = (int) (screenSize.getHeight() - windowSize.height) / 2;
098:
099: window.setBounds(x, y, windowSize.width, windowSize.height);
100: }
101:
102: {
103: final JTabbedPane mainTabbedPane = new JTabbedPane();
104: mainTabbedPane.setPreferredSize(new Dimension(0, 400));
105: mainTabbedPane.setMinimumSize(new Dimension(0, 300));
106: window.getContentPane().add(mainTabbedPane,
107: BorderLayout.CENTER);
108:
109: final JMenuBar menuBar = new JMenuBar();
110: window.setJMenuBar(menuBar);
111: final JMenu fileMenu = new JMenu();
112: menuBar.add(fileMenu);
113: fileMenu.setName("fileMenu");
114: fileMenu.setText("File");
115: fileMenu.setMnemonic(KeyEvent.VK_F);
116: {
117: final JMenuItem openMenuItem = new JMenuItem();
118: openMenuItem.setText("Open...");
119: fileMenu.add(openMenuItem);
120: openMenuItem.addMouseListener(new MouseAdapter() {
121: public void mousePressed(MouseEvent e) {
122: JFileChooser fileChooser = new JFileChooser(
123: // System.getProperty("user.dir")
124: inputPath + java.io.File.separator
125: + "pdf");
126: fileChooser.setDialogTitle("Open PDF file");
127: fileChooser
128: .addChoosableFileFilter(new PdfFileFilter());
129: switch (fileChooser.showDialog(null, "Open")) {
130: case JFileChooser.APPROVE_OPTION:
131: File file = fileChooser.getSelectedFile();
132:
133: domInspector.open(file);
134: break;
135: }
136: }
137: });
138: fileMenu.addSeparator();
139: final JMenuItem exitMenuItem = new JMenuItem();
140: exitMenuItem.setText("Exit");
141: fileMenu.add(exitMenuItem);
142: exitMenuItem.addMouseListener(new MouseAdapter() {
143: public void mousePressed(MouseEvent e) {
144: System.exit(1);
145: }
146: });
147: }
148:
149: springLayout.putConstraint(SpringLayout.SOUTH,
150: mainTabbedPane, 0, SpringLayout.SOUTH, window
151: .getContentPane());
152: springLayout.putConstraint(SpringLayout.EAST,
153: mainTabbedPane, 0, SpringLayout.EAST, window
154: .getContentPane());
155: springLayout.putConstraint(SpringLayout.NORTH,
156: mainTabbedPane, 0, SpringLayout.NORTH, window
157: .getContentPane());
158: springLayout.putConstraint(SpringLayout.WEST,
159: mainTabbedPane, 0, SpringLayout.WEST, window
160: .getContentPane());
161:
162: domInspector = new PdfInspectorSample();
163: mainTabbedPane.addTab("DOM Inspector", null, domInspector,
164: null);
165: }
166: }
167: }
|