1 /*
2  * $Id$
3  *
4  * Copyright 2007 Bruno Lowagie.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 package com.lowagie.rups.controller;
22 
23 import java.awt.Component;
24 import java.awt.Dimension;
25 import java.io.File;
26 import java.io.IOException;
27 import java.util.Observable;
28 
29 import javax.swing.JOptionPane;
30 import javax.swing.JScrollPane;
31 import javax.swing.JSplitPane;
32 import javax.swing.JTabbedPane;
33 import javax.swing.event.TreeSelectionEvent;
34 import javax.swing.event.TreeSelectionListener;
35 
36 import com.lowagie.rups.io.FileChooserAction;
37 import com.lowagie.rups.io.FileCloseAction;
38 import com.lowagie.rups.model.PdfFile;
39 import com.lowagie.rups.view.Console;
40 import com.lowagie.rups.view.PageSelectionListener;
41 import com.lowagie.rups.view.RupsMenuBar;
42 import com.lowagie.rups.view.itext.treenodes.PdfObjectTreeNode;
43 import com.lowagie.rups.view.itext.treenodes.PdfTrailerTreeNode;
44 import com.lowagie.text.DocumentException;
45 
46 /**
47  * This class controls all the GUI components that are shown in
48  * the RUPS application: the menu bar, the panels,...
49  */
50 public class RupsController extends Observable
51 	implements TreeSelectionListener, PageSelectionListener {
52 
53 	// member variables
54 
55 	/* file and controller */
56 	/** The Pdf file that is currently open in the application. */
57 	protected PdfFile pdfFile;
58 	/**
59 	 * Object with the GUI components for iText.
60 	 * @since	iText 5.0.0 (renamed from reader which was confusing because reader is normally used for a PdfReader instance)
61 	 */
62 	protected PdfReaderController readerController;
63 
64 	/* main components */
65 	/** The JMenuBar for the RUPS application. */
66 	protected RupsMenuBar menuBar;
67 	/** Contains all other components: the page panel, the outline tree, etc. */
68 	protected JSplitPane masterComponent;
69 
70 
71 	// constructor
72 	/**
73 	 * Constructs the GUI components of the RUPS application.
74 	 */
RupsController(Dimension dimension)75 	public RupsController(Dimension dimension) {
76 		// creating components and controllers
77         menuBar = new RupsMenuBar(this);
78         addObserver(menuBar);
79 		Console console = Console.getInstance();
80 		addObserver(console);
81 		readerController = new PdfReaderController(this, this);
82 		addObserver(readerController);
83 
84         // creating the master component
85 		masterComponent = new JSplitPane();
86 		masterComponent.setOrientation(JSplitPane.VERTICAL_SPLIT);
87 		masterComponent.setDividerLocation((int)(dimension.getHeight() * .70));
88 		masterComponent.setDividerSize(2);
89 
90 		JSplitPane content = new JSplitPane();
91 		masterComponent.add(content, JSplitPane.TOP);
92 		JSplitPane info = new JSplitPane();
93 		masterComponent.add(info, JSplitPane.BOTTOM);
94 
95 		content.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
96 		content.setDividerLocation((int)(dimension.getWidth() * .6));
97 		content.setDividerSize(1);
98         content.add(new JScrollPane(readerController.getPdfTree()), JSplitPane.LEFT);
99 		content.add(readerController.getNavigationTabs(), JSplitPane.RIGHT);
100 
101 		info.setDividerLocation((int) (dimension.getWidth() * .3));
102 		info.setDividerSize(1);
103 		info.add(readerController.getObjectPanel(), JSplitPane.LEFT);
104 		JTabbedPane editorPane = readerController.getEditorTabs();
105 		JScrollPane cons = new JScrollPane(console.getTextArea());
106 		editorPane.addTab("Console", null, cons, "Console window (System.out/System.err)");
107 		editorPane.setSelectedComponent(cons);
108 		info.add(editorPane, JSplitPane.RIGHT);
109 
110 	}
111 
112 	/** Getter for the menubar. */
getMenuBar()113 	public RupsMenuBar getMenuBar() {
114 		return menuBar;
115 	}
116 
117 	/** Getter for the master component. */
getMasterComponent()118 	public Component getMasterComponent() {
119 		return masterComponent;
120 	}
121 
122 	// Observable
123 
124 	/**
125 	 * @see java.util.Observable#notifyObservers(java.lang.Object)
126 	 */
127 	@Override
notifyObservers(Object obj)128 	public void notifyObservers(Object obj) {
129 		if (obj instanceof FileChooserAction) {
130 			File file = ((FileChooserAction)obj).getFile();
131 			try {
132 				pdfFile = new PdfFile(file);
133 				setChanged();
134 				super.notifyObservers(RupsMenuBar.OPEN);
135 				readerController.startObjectLoader(pdfFile);
136 			}
137 			catch(IOException ioe) {
138 				JOptionPane.showMessageDialog(masterComponent, ioe.getMessage(), "Dialog", JOptionPane.ERROR_MESSAGE);
139 			}
140 			catch (DocumentException de) {
141 				JOptionPane.showMessageDialog(masterComponent, de.getMessage(), "Dialog", JOptionPane.ERROR_MESSAGE);
142 			}
143 			return;
144 		}
145 		if (obj instanceof FileCloseAction) {
146 			pdfFile = null;
147 			setChanged();
148 			super.notifyObservers(RupsMenuBar.CLOSE);
149 			return;
150 		}
151 	}
152 
153 	// tree selection
154 
155 	/**
156 	 * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
157 	 */
valueChanged(TreeSelectionEvent evt)158 	public void valueChanged(TreeSelectionEvent evt) {
159 		Object selectednode = readerController.getPdfTree().getLastSelectedPathComponent();
160 		if (selectednode instanceof PdfTrailerTreeNode) {
161 			menuBar.update(this, RupsMenuBar.FILE_MENU);
162 			return;
163 		}
164 		if (selectednode instanceof PdfObjectTreeNode) {
165 			readerController.update(this, selectednode);
166 		}
167 	}
168 
169 	// page navigation
170 
171 	/**
172 	 * @see com.lowagie.rups.view.PageSelectionListener#gotoPage(int)
173 	 */
gotoPage(int pageNumber)174 	public int gotoPage(int pageNumber) {
175 		readerController.gotoPage(pageNumber);
176 		return pageNumber;
177 	}
178 }
179