/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

  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. package projectviewer;
  17. //standard GUI stuff
  18. import java.awt.Cursor;
  19. import java.awt.event.*;
  20. import java.io.File;
  21. import java.util.*;
  22. import javax.swing.*;
  23. import javax.swing.event.*;
  24. import javax.swing.filechooser.FileFilter;
  25. import javax.swing.tree.*;
  26. /**
  27. * Listen to all buttons and GUI events and respond to them.
  28. *
  29. * @version $Id: ViewerListener.java 5735 2001-07-18 12:32:29Z cyu $
  30. */
  31. class ViewerListener
  32. implements ActionListener, ItemListener
  33. {
  34. private ProjectViewer viewer;
  35. private Launcher launcher;
  36. private boolean paused;
  37. private FileFilter nonProjectFileFilter;
  38. /**
  39. * Create a new <code>ViewerListener</code>.
  40. */
  41. public ViewerListener(ProjectViewer instance, Launcher launcher) {
  42. this.viewer = instance;
  43. this.launcher = launcher;
  44. paused = false;
  45. }
  46. /**
  47. * Pause this listener. Any events received will be ignored.
  48. */
  49. public void pause() {
  50. paused = true;
  51. }
  52. /**
  53. * Resume this listener. Any events received will not be handled.
  54. */
  55. public void resume() {
  56. paused = false;
  57. }
  58. /**
  59. * Listen to specific GUI events.
  60. */
  61. public void actionPerformed(ActionEvent evt) {
  62. if ( paused ) return;
  63. viewer.setStatus(" ");
  64. Object source = evt.getSource();
  65. if (source == this.viewer.createProjectBtn) {
  66. this.createProject();
  67. } else if (source == this.viewer.deleteProjectBtn) {
  68. this.deleteSelectedProject();
  69. } else if (source == this.viewer.addFileBtn) {
  70. this.addFileToProject();
  71. } else if (source == this.viewer.removeFileBtn) {
  72. viewer.getCurrentProject().removeFile( viewer.getSelectedFile() );
  73. } else if (source == this.viewer.removeAllFilesBtn) {
  74. this.removeAllFilesFromProject();
  75. } else if (source == this.viewer.importFilesBtn) {
  76. getImporter().doImport();
  77. } else if (source == this.viewer.openAllBtn) {
  78. this.openAllFilesInProject();
  79. } else if (source == this.viewer.expandBtn) {
  80. viewer.expandAll();
  81. } else if (source == this.viewer.contractBtn) {
  82. viewer.collapseAll();
  83. } else if (source == this.viewer.configBtn) {
  84. this.showConfig();
  85. }
  86. }
  87. /**
  88. * Handle project combo changes.
  89. */
  90. public void itemStateChanged( ItemEvent evt ) {
  91. if ( paused ) return;
  92. if ( evt.getItem() instanceof Project )
  93. viewer.setCurrentProject( (Project) evt.getItem() );
  94. else
  95. viewer.setCurrentProject( null );
  96. }
  97. /**
  98. * Create a new Project
  99. */
  100. private void createProject() {
  101. String projectName = JOptionPane.showInputDialog( viewer,
  102. "Please enter a project name. You will also be prompted for a home directory." );
  103. if (projectName == null) return;
  104. if ( ProjectManager.getInstance().hasProject( projectName ) ) {
  105. JOptionPane.showMessageDialog( viewer,
  106. "There is currently a project with this name." );
  107. return;
  108. }
  109. JFileChooser chooser = new JFileChooser();
  110. chooser.setDialogTitle("Enter your home directory for \"" + projectName + "\"");
  111. chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
  112. if (chooser.showOpenDialog(viewer) == JFileChooser.CANCEL_OPTION) return;
  113. File prjHome = chooser.getSelectedFile();
  114. Project project = new Project(projectName, new ProjectDirectory(prjHome.getAbsolutePath()));
  115. ProjectManager.getInstance().addProject( project );
  116. viewer.setCurrentProject( project );
  117. int confirmed = JOptionPane.showConfirmDialog( this.viewer,
  118. "Do you want to import files from " + prjHome + "?",
  119. "Import files?",
  120. JOptionPane.YES_NO_OPTION );
  121. viewer.showWaitCursor();
  122. if (confirmed == JOptionPane.YES_OPTION)
  123. getImporter().doImport( prjHome );
  124. viewer.refresh();
  125. viewer.showDefaultCursor();
  126. }
  127. /**
  128. * Show the config dialog to the user.
  129. */
  130. public void showConfig() {
  131. JDialog dialog = new JDialog();
  132. dialog.setTitle("Config");
  133. dialog.getContentPane().add( new ProjectViewerPane( viewer ) );
  134. dialog.setSize( 350, 600 );
  135. dialog.setVisible(true);
  136. dialog.setEnabled(true);
  137. dialog.toFront();
  138. dialog.setVisible(true);
  139. }
  140. /**
  141. * Prompt the user to a file, get the current project, and then add the file
  142. * to the project.
  143. */
  144. private void addFileToProject() {
  145. JFileChooser chooser = viewer.createFileChooser();
  146. if ( nonProjectFileFilter == null ) {
  147. nonProjectFileFilter = new FileFilter() {
  148. public boolean accept( File f ) {
  149. return !viewer.getCurrentProject().isProjectFile( f.getAbsolutePath() );
  150. }
  151. public String getDescription() {
  152. return "Non Project Files";
  153. }
  154. };
  155. }
  156. chooser.setFileFilter( nonProjectFileFilter );
  157. //chooser.setAcceptAllFileFilterUsed(false); #JDK1.3
  158. if (chooser.showOpenDialog(this.viewer) != JFileChooser.APPROVE_OPTION) return;
  159. viewer.getCurrentProject().importFile(
  160. new ProjectFile( chooser.getSelectedFile().getAbsolutePath() ) );
  161. }
  162. /**
  163. * Prompt the user if they want to remove all projects from the user.
  164. */
  165. private void removeAllFilesFromProject() {
  166. int answer = JOptionPane.showConfirmDialog( viewer,
  167. "Are you sure you want to remove all files from the current project?",
  168. "Remove all files?",
  169. JOptionPane.YES_NO_OPTION);
  170. if (answer != JOptionPane.YES_OPTION)
  171. return;
  172. viewer.getCurrentProject().removeFiles();
  173. }
  174. /**
  175. * Delete all this project and select all projects.
  176. */
  177. private void deleteSelectedProject() {
  178. Project project = this.viewer.getCurrentProject();
  179. int confirmed =
  180. JOptionPane.showConfirmDialog( viewer,
  181. "Are you sure you want to delete the project: " + project + " ?",
  182. "Delete project?",
  183. JOptionPane.YES_NO_OPTION );
  184. if (confirmed != JOptionPane.YES_OPTION) return;
  185. ProjectManager.getInstance().removeProject( project );
  186. viewer.setCurrentProject( null );
  187. viewer.refresh();
  188. }
  189. /**
  190. * Returns an instance of {@link ProjectFilesImporter}.
  191. */
  192. private ProjectFileImporter getImporter() {
  193. return new ProjectFileImporter( viewer );
  194. }
  195. /**
  196. * Progmatically open all files under the current project...
  197. */
  198. private void openAllFilesInProject() {
  199. viewer.showDefaultCursor();
  200. for ( Iterator i = viewer.getCurrentProject().projectFiles(); i.hasNext(); )
  201. launcher.launchFile( (ProjectFile) i.next() );
  202. viewer.showDefaultCursor();
  203. }
  204. }