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
;
8 import java
.awt
.event
.ActionListener
;
9 import java
.awt
.event
.ActionEvent
;
10 import java
.awt
.event
.KeyEvent
;
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
;
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();
52 setLocationRelativeTo(frame
);
55 setDefaultCloseOperation(JFrame
.DISPOSE_ON_CLOSE
);
60 model
.generateInitialProjectName();
61 model
.generateInitialProjectLocation();
63 getRootPane().setDefaultButton(createProjectButton
);
69 * Initialize bindings.
71 private void initBinding() {
72 Bindings
.bind(projectNameTextField
, projectNameModel
);
73 Bindings
.bind(projectLocationTextField
, projectLocationModel
);
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());
97 cancelButton
.addActionListener(new ActionListener(){
98 public void actionPerformed(ActionEvent e
) {
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
);
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
);
128 AddProjectItemResult addProjectItemResult
= ModelerSession
.getProjectControlService().addProject(
129 new ProjectRoot(projectName
, model
.getProjectLocation()), true
132 switch (addProjectItemResult
.getStatus()){
134 LOG
.info("New project has been created, project name: " + model
.getProjectName() + ", project location: " + model
.getProjectLocation() + ".");
137 ModelerSession
.getProjectControlService().synchronize(
138 addProjectItemResult
.getTreePath(),
139 true, false, false, false
144 LOG
.error("Name duplicity error has occurred, " + projectName
+ ", " + model
.getProjectLocation() + ".");
145 errorLabel
.setText(DUPLICITY_ERROR_LABEL
);
148 publicInvalidNameError(projectName
);
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
) {
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
)){
187 ModelerSession
.getCommonResourceBundle().getString("modeler.add.new.project.dialog.error.disallowed") +
188 ProjectServiceUtils
.getDisallowedNameSymbols(',')
191 errorLabel
.setText(ModelerSession
.getCommonResourceBundle().getString("modeler.add.new.project.dialog.error.shortName"));
198 private void closeDialog() {