View Javadoc

1   /*
2    * JDiskCatalog
3    *
4    * Copyright 2007 Przemek Więch
5    *
6    * This file is part of JDiskCatalog.
7    *
8    * JDiskCatalog is free software; you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17  
18   * You should have received a copy of the GNU General Public License
19   * along with this program; if not, write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   */
22  
23  package net.sf.jdiskcatalog.ui;
24  
25  import java.awt.event.ActionEvent;
26  import java.awt.event.KeyEvent;
27  import java.awt.event.WindowAdapter;
28  import java.awt.event.WindowEvent;
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.FileOutputStream;
32  import java.io.IOException;
33  
34  import javax.swing.AbstractAction;
35  import javax.swing.JFileChooser;
36  import javax.swing.JFrame;
37  import javax.swing.JMenu;
38  import javax.swing.JMenuBar;
39  
40  import net.sf.jdiskcatalog.analysis.AnalysisManager;
41  import net.sf.jdiskcatalog.analysis.BasicFileAnalyser;
42  import net.sf.jdiskcatalog.analysis.ImageThumbnail;
43  import net.sf.jdiskcatalog.analysis.MimeTypeAnalyser;
44  import net.sf.jdiskcatalog.api.Document;
45  import net.sf.jdiskcatalog.api.Node;
46  import net.sf.jdiskcatalog.api.NodeInfo;
47  import net.sf.jdiskcatalog.api.ViewLayout;
48  import net.sf.jdiskcatalog.config.Configuration;
49  import net.sf.jdiskcatalog.io.GzipReader;
50  import net.sf.jdiskcatalog.io.GzipWriter;
51  import net.sf.jdiskcatalog.io.XmlReader;
52  import net.sf.jdiskcatalog.io.XmlWriter;
53  import net.sf.jdiskcatalog.model.DocumentImpl;
54  import net.sf.jdiskcatalog.model.NodeImpl;
55  import net.sf.jdiskcatalog.scanner.FileSystemScannerImpl;
56  import net.sf.jdiskcatalog.scanner.ZipScanner;
57  import net.sf.jdiskcatalog.view.DefaultViewLayout;
58  import net.sf.jdiskcatalog.view.FileListView;
59  import net.sf.jdiskcatalog.view.SimpleDetailsView;
60  import net.sf.jdiskcatalog.view.TreeView;
61  import net.sf.jdiskcatalog.view.ViewManager;
62  
63  
64  /**
65   * The main frame of the application.
66   *
67   * @author Przemek Więch <pwiech@losthive.or>
68   * @version $Id$
69   */
70  public class MainFrame extends JFrame
71  {
72  	private static final String SAVE_FILE = "test.xml.gz";
73  
74  	/**
75  	 * The currently open document.
76  	 */
77  	private Document document;
78  
79  	/**
80  	 * Application configuration.
81  	 */
82  	private Configuration config;
83  
84  	/**
85  	 * Analysis system.
86  	 */
87  	private AnalysisManager analysis;
88  
89  	/**
90  	 * Manages all document views.
91  	 */
92  	private ViewManager viewManager;
93  
94  	/**
95  	 * Constructs the frame.
96  	 */
97  	public MainFrame()
98  	{
99  		super("JDiskCatalog");
100 
101 		config = new Configuration();
102 		config.load();
103 
104 		JMenuBar menuBar = new JMenuBar();
105 		JMenu fileMenu = new JMenu("File");
106 		fileMenu.setMnemonic(KeyEvent.VK_F);
107 		fileMenu.add(new OpenAction());
108 		fileMenu.add(new SaveAction(false));
109 		fileMenu.add(new SaveAction(true));
110 		fileMenu.add(new ExitAction());
111 		menuBar.add(fileMenu);
112 
113 		JMenu editMenu = new JMenu("Edit");
114 		editMenu.setMnemonic(KeyEvent.VK_E);
115 		editMenu.add(new AddCollectionAction());
116 		editMenu.add(new AddDiskAction());
117 		menuBar.add(editMenu);
118 
119 		setJMenuBar(menuBar);
120 
121 		// TODO: create toolbar
122 
123 
124 		// set Content based on ViewConfiguration
125 		viewManager = new ViewManager();
126 		viewManager.add(new TreeView());
127 		viewManager.add(new FileListView());
128 		viewManager.add(new SimpleDetailsView());
129 
130 		ViewLayout layout = new DefaultViewLayout();
131 		setContentPane(layout.getViewPanel(viewManager));
132 
133 		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
134 		addWindowListener(new WindowCallback());
135 		pack();
136 
137 
138 		////////////////
139 
140 
141 		analysis = new AnalysisManager(config);
142 		analysis.add(new BasicFileAnalyser());
143 		//analysis.add(new CRCAnalyser());
144 		analysis.add(new ImageThumbnail());
145 		analysis.add(new MimeTypeAnalyser());
146 		analysis.add(new FileSystemScannerImpl());
147 		analysis.add(new ZipScanner());
148 
149 		try
150 		{
151 			document = new GzipReader(new XmlReader()).read(new FileInputStream(SAVE_FILE));
152 			System.out.println("File loaded");
153 		}
154 		catch (IOException e)
155 		{
156 			System.out.println("File load failed");
157 		}
158 
159 		if (document == null)
160 		{
161 			System.out.println("Creating new document");
162 			Node node = new NodeImpl("projects", NodeInfo.Type.DISK);
163 			try
164 			{
165 				analysis.scan(new File("e:/przemek/projects"), node);
166 			}
167 			catch (IOException e)
168 			{
169 				e.printStackTrace();
170 			}
171 			document = new DocumentImpl();
172 			document.add(node);
173 		}
174 
175 		viewManager.setDocument(document);
176 
177 	}
178 
179 	/**
180 	 * Callback for window closing event.
181 	 */
182 	private class WindowCallback extends WindowAdapter
183 	{
184 		@Override
185 		public void windowClosing(WindowEvent event)
186 		{
187 			new ExitAction().actionPerformed(null);
188 		}
189 	}
190 
191 	private class OpenAction extends AbstractAction
192 	{
193 		public OpenAction()
194 		{
195 			super("Open...");
196             putValue(MNEMONIC_KEY, KeyEvent.VK_O);
197 		}
198 
199 		public void actionPerformed(ActionEvent e)
200 		{
201 		}
202 	}
203 
204 	private class SaveAction extends AbstractAction
205 	{
206 		public SaveAction(boolean askForFilename)
207 		{
208 			super(askForFilename ? "Save as..." : "Save");
209 			if (askForFilename)
210 				putValue(MNEMONIC_KEY, KeyEvent.VK_A);
211 			else
212 				putValue(MNEMONIC_KEY, KeyEvent.VK_S);
213 		}
214 
215 		public void actionPerformed(ActionEvent e)
216 		{
217 		}
218 	}
219 
220 	private class ExitAction extends AbstractAction
221 	{
222 		public ExitAction()
223 		{
224 			super("Exit");
225             putValue(MNEMONIC_KEY, KeyEvent.VK_X);
226 		}
227 
228 		public void actionPerformed(ActionEvent e)
229 		{
230 			System.out.println("Saving configuration and database");
231 			try
232 			{
233 				config.save();
234 			}
235 			catch (IOException ex)
236 			{
237 				// TODO Auto-generated catch block
238 				ex.printStackTrace();
239 			}
240 			try
241 			{
242 				new GzipWriter(new XmlWriter()).write(document, new FileOutputStream(SAVE_FILE));
243 			}
244 			catch (IOException ex)
245 			{
246 				ex.printStackTrace();
247 			}
248 			dispose();
249 		}
250 	}
251 
252 	private class AddCollectionAction extends AbstractAction
253 	{
254 		public AddCollectionAction()
255 		{
256 			super("Add collection");
257             putValue(MNEMONIC_KEY, KeyEvent.VK_C);
258 		}
259 
260 		public void actionPerformed(ActionEvent e)
261 		{
262 		}
263 	}
264 
265 	private class AddDiskAction extends AbstractAction
266 	{
267 		public AddDiskAction()
268 		{
269 			super("Add disk...");
270             putValue(MNEMONIC_KEY, KeyEvent.VK_D);
271 		}
272 
273 		public void actionPerformed(ActionEvent e)
274 		{
275 			JFileChooser chooser = new JFileChooser();
276 			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
277 			chooser.showOpenDialog(MainFrame.this);
278 			File selected = chooser.getSelectedFile();
279 			if (selected == null)
280 				return;
281 
282 			Node node = new NodeImpl(selected.getName(), NodeInfo.Type.DISK);
283 			try
284 			{
285 				// TODO: Run in separate thread and show progress dialog
286 				analysis.scan(selected, node);
287 			}
288 			catch (IOException ex)
289 			{
290 				ex.printStackTrace();
291 			}
292 			document.add(node);
293 			viewManager.refresh();
294 		}
295 	}
296 }