New Jide License
[indepmod/experimental.git] / IndependentModeler / src / cz / cvut / promod / gui / dialogs / newProject / NewProjectDialog.java
blobc50b692cc830f99c8ca8939e9a6338eeda3b7f30
1 package cz.cvut.promod.gui.dialogs.newProject;
3 import com.jgoodies.binding.adapter.Bindings;
4 import com.jgoodies.binding.PresentationModel;
5 import com.jgoodies.binding.value.ValueModel;
7 import javax.swing.*;
8 import java.awt.event.ActionListener;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.KeyEvent;
11 import java.io.File;
12 import java.beans.PropertyChangeListener;
13 import java.beans.PropertyChangeEvent;
15 import cz.cvut.promod.services.projectService.utils.ProjectServiceUtils;
16 import cz.cvut.promod.services.projectService.ProjectService;
17 import cz.cvut.promod.services.projectService.results.AddProjectItemResult;
18 import cz.cvut.promod.services.projectService.treeProjectNode.ProjectRoot;
19 import cz.cvut.promod.services.ModelerSession;
20 import org.apache.log4j.Logger;
22 /**
23 * ProMod, master thesis project
24 * User: Petr Zverina, petr.zverina@gmail.com
25 * Date: 1:39:54, 20.10.2009
27 * The new project dialog.
29 public class NewProjectDialog extends NewProjectDialogView{
31 private final Logger LOG = Logger.getLogger(NewProjectDialog.class);
33 private final NewProjectDialogModel model = new NewProjectDialogModel();
34 private final PresentationModel<NewProjectDialogModel> presentation = new PresentationModel<NewProjectDialogModel>(model);
36 final ValueModel projectNameModel = presentation.getModel(NewProjectDialogModel.PROPERTY_PROJECT_NAME);
37 final ValueModel projectLocationModel = presentation.getModel(NewProjectDialogModel.PROPERTY_PROJECT_LOCATION);
39 public static String DUPLICITY_ERROR_LABEL =
40 ModelerSession.getCommonResourceBundle().getString("modeler.add.new.project.dialog.error.duplicity");
41 public static String EXISTING_PROJECT_FILE_ERROR_LABEL =
42 ModelerSession.getCommonResourceBundle().getString("modeler.add.new.project.dialog.error.existing.file");
43 public static String GENERAL_ERROR_LABEL =
44 ModelerSession.getCommonResourceBundle().getString("modeler.add.new.project.dialog.error.general");
45 public static String RELATIVE_PATH_ERROR_LABEL =
46 ModelerSession.getCommonResourceBundle().getString("modeler.add.new.project.dialog.error.relative");
49 public NewProjectDialog(){
50 final JFrame frame = ModelerSession.getFrame();
51 if(frame != null){
52 setLocationRelativeTo(frame);
55 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
57 initBinding();
58 initEventHandling();
60 model.generateInitialProjectName();
61 model.generateInitialProjectLocation();
63 getRootPane().setDefaultButton(createProjectButton);
65 setVisible(true);
68 /**
69 * Initialize bindings.
71 private void initBinding() {
72 Bindings.bind(projectNameTextField, projectNameModel);
73 Bindings.bind(projectLocationTextField, projectLocationModel);
76 /**
77 * Initialize event handling.
79 private void initEventHandling() {
80 projectLocationButton.addActionListener(new ActionListener(){
81 public void actionPerformed(ActionEvent e) {
83 final JFileChooser directoryChooser = new JFileChooser();
84 directoryChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
85 directoryChooser.setCurrentDirectory(new File(model.getProjectLocation()));
86 directoryChooser.setMultiSelectionEnabled(false);
88 final int returnVal = directoryChooser.showDialog(centerPanel, "create a new project here");
90 if(returnVal == directoryChooser.getApproveButtonMnemonic()){
91 model.setProjectLocation(directoryChooser.getSelectedFile().getAbsolutePath());
95 });
97 cancelButton.addActionListener(new ActionListener(){
98 public void actionPerformed(ActionEvent e) {
99 closeDialog();
103 createProjectButton.addActionListener(new ActionListener(){
104 public void actionPerformed(ActionEvent e) {
105 final String projectName = model.getProjectName();
108 * Test, whether there is no other files with ProMod project file extension.
110 final File projectLocation = new File(model.getProjectLocation());
112 // do not take relative paths
113 if(!projectLocation.isAbsolute()){
114 errorLabel.setText(RELATIVE_PATH_ERROR_LABEL);
115 return;
118 if(projectLocation.exists()){
119 for(final File file : projectLocation.listFiles()){
120 if(file.getAbsolutePath().endsWith(ProjectService.PROJECT_FILE_EXTENSION)){
121 LOG.error("Not possible to insert a new project file to an folder, where is already an existing file with project file extension.");
122 errorLabel.setText(EXISTING_PROJECT_FILE_ERROR_LABEL);
123 return;
128 AddProjectItemResult addProjectItemResult = ModelerSession.getProjectControlService().addProject(
129 new ProjectRoot(projectName, model.getProjectLocation()), true
132 switch (addProjectItemResult.getStatus()){
133 case SUCCESS:
134 LOG.info("New project has been created, project name: " + model.getProjectName() + ", project location: " + model.getProjectLocation() + ".");
135 closeDialog();
137 ModelerSession.getProjectControlService().synchronize(
138 addProjectItemResult.getTreePath(),
139 true, false, false, false
142 return;
143 case NAME_DUPLICITY:
144 LOG.error("Name duplicity error has occurred, " + projectName + ", " + model.getProjectLocation() + ".");
145 errorLabel.setText(DUPLICITY_ERROR_LABEL);
146 break;
147 case INVALID_NAME:
148 publicInvalidNameError(projectName);
149 break;
150 default:
151 LOG.error("Unknown AddProjectItemStatus return value.");
152 errorLabel.setText(GENERAL_ERROR_LABEL);
157 projectNameModel.addValueChangeListener(new PropertyChangeListener(){
158 public void propertyChange(PropertyChangeEvent evt) {
159 final String projectLocation = (String) projectLocationModel.getValue();
160 final String newProjectName = (String) evt.getNewValue();
161 final String oldProjectName = (String) evt.getOldValue();
163 if(projectLocation != null && projectLocation.endsWith(System.getProperty("file.separator") + oldProjectName)){
164 final String newProjectLocation = projectLocation.substring(0, projectLocation.length() - oldProjectName.length()) + newProjectName;
165 projectLocationModel.setValue(newProjectLocation);
170 getRootPane().registerKeyboardAction(new ActionListener(){
171 public void actionPerformed(ActionEvent actionEvent) {
172 setVisible(false);
173 dispose();
176 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
177 JComponent.WHEN_IN_FOCUSED_WINDOW);
181 * Shows the invalid name error
182 * @param projectName is the project name
184 private void publicInvalidNameError(final String projectName) {
185 if(!ProjectServiceUtils.isSyntacticallyCorrectName(projectName)){
186 errorLabel.setText(
187 ModelerSession.getCommonResourceBundle().getString("modeler.add.new.project.dialog.error.disallowed") +
188 ProjectServiceUtils.getDisallowedNameSymbols(',')
190 } else {
191 errorLabel.setText(ModelerSession.getCommonResourceBundle().getString("modeler.add.new.project.dialog.error.shortName"));
196 * Hides the dialog.
198 private void closeDialog() {
199 setVisible(false);
200 dispose();