View Javadoc

1   package gov.nasa.pds.ltdt.gui;
2   
3   import gov.nasa.pds.ltdt.gui.configuration.LTDTKeys;
4   import gov.nasa.pds.ltdt.gui.util.Utility;
5   
6   import java.awt.event.ActionEvent;
7   import java.io.IOException;
8   import java.util.List;
9   import java.util.Properties;
10  
11  import javax.swing.AbstractAction;
12  import javax.swing.Icon;
13  import javax.swing.JOptionPane;
14  
15  /***
16   * Open Project Action
17   * @author jwang
18   *
19   */
20  public class OpenProjectAction extends AbstractAction{
21  	
22  	private MainWindow window =null;
23  	private Properties props;
24  	
25  	/***
26  	 * Constructor.
27  	 * @param name
28  	 * @param image
29  	 * @param desc
30  	 * @param mnemonic
31  	 * @param props
32  	 * @param window
33  	 */
34  	public OpenProjectAction 
35  	  (String name, Icon image, String desc, Integer mnemonic, Properties props, MainWindow window) {
36  		
37  
38  		super(name, image);
39  		putValue(SHORT_DESCRIPTION,desc); // tooltip
40  		putValue(MNEMONIC_KEY,mnemonic);  // accelarator key	
41  		
42  		this.props = props;
43  		this.window = window;
44  
45  		
46  	}
47  
48  	public void actionPerformed(ActionEvent ae) {
49  
50  		//String selectedProject = null;
51  		int status=0;
52  		boolean projectListStarted=true; // true if there are one or more projects registered with LTDT
53  
54  		// check if there's any registered projects
55  		projectListStarted = isProjectListStarted(window);
56  		
57  		if (!projectListStarted && "false".equals(props.getProperty(LTDTKeys.PROJECTALTERED))) {
58  			new ProjectGalleryDialog (props, window, LTDTKeys.PROJECTGALLERYTITLE, LTDTKeys.NEWFROMTEMPLATETABINDEX);
59  			
60  		}
61  		else {
62  			status = saveAlteredProject(props, window);
63  			
64  			if (status==LTDTKeys.SUCCESS) {
65  				new ProjectGalleryDialog (props, window, LTDTKeys.PROJECTGALLERYTITLE, LTDTKeys.OPENPROJECTTABINDEX);
66  				
67  			}
68  		}
69  
70  
71  	}
72  	
73  	private boolean isProjectListStarted (MainWindow window) {
74  		
75  		try {
76  			if (Utility.getKeyList(LTDTKeys.LTDTPROPFILE).size()==0 ) {
77  				
78  				return false;
79  			}
80  			else 
81  				return true;
82  		}
83  		catch (IOException ex) {
84  			//do nothing
85  		}
86  		return true;
87  	}
88  
89  	private int saveAlteredProject(Properties props, MainWindow window) {
90  				
91  		// if PROJECTALTERED is true, ask if the user want to save the edited project
92  		if ("true".equals(props.getProperty(LTDTKeys.PROJECTALTERED))) {
93  			int response=JOptionPane.showConfirmDialog(window, 
94  					"Do you want to save changes made to this project\nbefore opening an existing project?",
95  					LTDTKeys.PROJECTMODIFIEDTITLE, JOptionPane.YES_NO_CANCEL_OPTION);
96  			switch (response) {
97  				case JOptionPane.YES_OPTION:
98  					
99  					//if ("Untitled".equals(props.getProperty(LTDTKeys.PROJECTNAME)))
100 						//new ProjectCreateDialog(props, window, LTDTKeys.SAVEASTITLE, LTDTKeys.SAVEAS);
101 					//else {
102 						ProjectSave.save(props, window);
103 					//}
104 						break;
105 				
106 				case JOptionPane.CLOSED_OPTION:
107 				case JOptionPane.CANCEL_OPTION:
108 					return LTDTKeys.CANCELLED;
109 				case JOptionPane.NO_OPTION:
110 				default:
111 					break;								
112 			}			
113 			return LTDTKeys.SUCCESS;
114 		}
115 		return LTDTKeys.SUCCESS;
116 	}
117 	
118 	
119 	private String selectProject() {
120 		String selectedProject=null;
121 		List projNames=null;
122 		
123 		try {
124 			projNames = Utility.getSortedProjectList(LTDTKeys.LTDTPROPFILE);
125 		}
126 		catch (IOException e) {} // do nothing
127 		
128 		
129 		String [] projNamesArray = new String[projNames.size()];
130 		for (int i=0; i<projNames.size(); i++) {
131 			projNamesArray[i] = (String)projNames.get(i);
132 		}
133 		selectedProject = (String) JOptionPane.showInputDialog(
134 				window, 
135 				"Project Names:",
136 				"Choose a project", 
137 				JOptionPane.QUESTION_MESSAGE,
138 				null, 
139 				projNamesArray, 
140 				projNamesArray[0]	);
141 		return selectedProject;
142 	}
143 	
144 
145 	
146 	
147 }