/plugins/ProjectViewer/tags/projectviewer_1_0_2/projectviewer/ViewerListener.java
# · Java · 246 lines · 145 code · 42 blank · 59 comment · 48 complexity · 847f0a39f10b655e96e6430af4d4b206 MD5 · raw file
- /*
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- package projectviewer;
- //standard GUI stuff
- import java.awt.Cursor;
- import java.awt.event.*;
- import java.io.File;
- import java.util.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import javax.swing.filechooser.FileFilter;
- import javax.swing.tree.*;
- /**
- * Listen to all buttons and GUI events and respond to them.
- *
- * @version $Id: ViewerListener.java 5735 2001-07-18 12:32:29Z cyu $
- */
- class ViewerListener
- implements ActionListener, ItemListener
- {
- private ProjectViewer viewer;
- private Launcher launcher;
- private boolean paused;
- private FileFilter nonProjectFileFilter;
-
-
- /**
- * Create a new <code>ViewerListener</code>.
- */
- public ViewerListener(ProjectViewer instance, Launcher launcher) {
- this.viewer = instance;
- this.launcher = launcher;
- paused = false;
- }
-
- /**
- * Pause this listener. Any events received will be ignored.
- */
- public void pause() {
- paused = true;
- }
-
- /**
- * Resume this listener. Any events received will not be handled.
- */
- public void resume() {
- paused = false;
- }
-
- /**
- * Listen to specific GUI events.
- */
- public void actionPerformed(ActionEvent evt) {
- if ( paused ) return;
-
- viewer.setStatus(" ");
- Object source = evt.getSource();
-
- if (source == this.viewer.createProjectBtn) {
- this.createProject();
-
- } else if (source == this.viewer.deleteProjectBtn) {
- this.deleteSelectedProject();
-
- } else if (source == this.viewer.addFileBtn) {
- this.addFileToProject();
-
- } else if (source == this.viewer.removeFileBtn) {
- viewer.getCurrentProject().removeFile( viewer.getSelectedFile() );
-
- } else if (source == this.viewer.removeAllFilesBtn) {
- this.removeAllFilesFromProject();
-
- } else if (source == this.viewer.importFilesBtn) {
- getImporter().doImport();
-
- } else if (source == this.viewer.openAllBtn) {
- this.openAllFilesInProject();
-
- } else if (source == this.viewer.expandBtn) {
- viewer.expandAll();
-
- } else if (source == this.viewer.contractBtn) {
- viewer.collapseAll();
-
- } else if (source == this.viewer.configBtn) {
- this.showConfig();
-
- }
- }
-
- /**
- * Handle project combo changes.
- */
- public void itemStateChanged( ItemEvent evt ) {
- if ( paused ) return;
- if ( evt.getItem() instanceof Project )
- viewer.setCurrentProject( (Project) evt.getItem() );
- else
- viewer.setCurrentProject( null );
- }
- /**
- * Create a new Project
- */
- private void createProject() {
- String projectName = JOptionPane.showInputDialog( viewer,
- "Please enter a project name. You will also be prompted for a home directory." );
- if (projectName == null) return;
- if ( ProjectManager.getInstance().hasProject( projectName ) ) {
- JOptionPane.showMessageDialog( viewer,
- "There is currently a project with this name." );
- return;
- }
-
- JFileChooser chooser = new JFileChooser();
- chooser.setDialogTitle("Enter your home directory for \"" + projectName + "\"");
- chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
- if (chooser.showOpenDialog(viewer) == JFileChooser.CANCEL_OPTION) return;
- File prjHome = chooser.getSelectedFile();
- Project project = new Project(projectName, new ProjectDirectory(prjHome.getAbsolutePath()));
- ProjectManager.getInstance().addProject( project );
- viewer.setCurrentProject( project );
- int confirmed = JOptionPane.showConfirmDialog( this.viewer,
- "Do you want to import files from " + prjHome + "?",
- "Import files?",
- JOptionPane.YES_NO_OPTION );
- viewer.showWaitCursor();
- if (confirmed == JOptionPane.YES_OPTION)
- getImporter().doImport( prjHome );
- viewer.refresh();
- viewer.showDefaultCursor();
- }
-
- /**
- * Show the config dialog to the user.
- */
- public void showConfig() {
- JDialog dialog = new JDialog();
- dialog.setTitle("Config");
- dialog.getContentPane().add( new ProjectViewerPane( viewer ) );
- dialog.setSize( 350, 600 );
- dialog.setVisible(true);
- dialog.setEnabled(true);
- dialog.toFront();
- dialog.setVisible(true);
- }
-
- /**
- * Prompt the user to a file, get the current project, and then add the file
- * to the project.
- */
- private void addFileToProject() {
- JFileChooser chooser = viewer.createFileChooser();
- if ( nonProjectFileFilter == null ) {
- nonProjectFileFilter = new FileFilter() {
- public boolean accept( File f ) {
- return !viewer.getCurrentProject().isProjectFile( f.getAbsolutePath() );
- }
- public String getDescription() {
- return "Non Project Files";
- }
- };
- }
- chooser.setFileFilter( nonProjectFileFilter );
- //chooser.setAcceptAllFileFilterUsed(false); #JDK1.3
- if (chooser.showOpenDialog(this.viewer) != JFileChooser.APPROVE_OPTION) return;
-
- viewer.getCurrentProject().importFile(
- new ProjectFile( chooser.getSelectedFile().getAbsolutePath() ) );
- }
- /**
- * Prompt the user if they want to remove all projects from the user.
- */
- private void removeAllFilesFromProject() {
- int answer = JOptionPane.showConfirmDialog( viewer,
- "Are you sure you want to remove all files from the current project?",
- "Remove all files?",
- JOptionPane.YES_NO_OPTION);
- if (answer != JOptionPane.YES_OPTION)
- return;
-
- viewer.getCurrentProject().removeFiles();
- }
- /**
- * Delete all this project and select all projects.
- */
- private void deleteSelectedProject() {
- Project project = this.viewer.getCurrentProject();
- int confirmed =
- JOptionPane.showConfirmDialog( viewer,
- "Are you sure you want to delete the project: " + project + " ?",
- "Delete project?",
- JOptionPane.YES_NO_OPTION );
-
- if (confirmed != JOptionPane.YES_OPTION) return;
-
- ProjectManager.getInstance().removeProject( project );
- viewer.setCurrentProject( null );
- viewer.refresh();
- }
- /**
- * Returns an instance of {@link ProjectFilesImporter}.
- */
- private ProjectFileImporter getImporter() {
- return new ProjectFileImporter( viewer );
- }
- /**
- * Progmatically open all files under the current project...
- */
- private void openAllFilesInProject() {
- viewer.showDefaultCursor();
- for ( Iterator i = viewer.getCurrentProject().projectFiles(); i.hasNext(); )
- launcher.launchFile( (ProjectFile) i.next() );
- viewer.showDefaultCursor();
- }
- }