1 19 package org.netbeans.modules.project.uiapi; 20 21 import java.awt.CardLayout ; 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import javax.swing.JFileChooser ; 26 import javax.swing.UIManager ; 27 import javax.swing.event.ChangeEvent ; 28 import javax.swing.event.ChangeListener ; 29 import javax.swing.event.DocumentListener ; 30 import javax.swing.event.DocumentEvent ; 31 import org.netbeans.api.progress.ProgressHandle; 32 import org.netbeans.api.progress.ProgressHandleFactory; 33 import org.netbeans.api.project.Project; 34 import org.netbeans.spi.project.support.ProjectOperations; 35 import org.netbeans.api.project.ProjectUtils; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.util.NbBundle; 39 40 43 public class ProjectCopyPanel extends javax.swing.JPanel implements DocumentListener , DefaultProjectOperationsImplementation.InvalidablePanel { 44 45 private Project project; 46 private boolean isMove; 47 private boolean invalid; 48 49 private List <ChangeListener > listeners; 50 private ProgressHandle handle; 51 52 55 public ProjectCopyPanel(ProgressHandle handle, Project project, boolean isMove) { 56 this.project = project; 57 this.isMove = isMove; 58 this.listeners = new ArrayList <ChangeListener >(); 59 this.handle = handle; 60 61 62 initComponents(); 63 setProject(); 64 projectName.getDocument().addDocumentListener(this); 65 projectLocation.getDocument().addDocumentListener(this); 66 67 if (isMove) { 68 nameLabel.setVisible(false); 69 projectName.setVisible(false); 70 } 71 72 if (Boolean.getBoolean("org.netbeans.modules.project.uiapi.DefaultProjectOperations.showProgress")) { 73 ((CardLayout ) progress.getLayout()).show(progress, "progress"); 74 } 75 } 76 77 public synchronized void addChangeListener(ChangeListener l) { 78 listeners.add(l); 79 } 80 81 public synchronized void removeChangeListener(ChangeListener l) { 82 listeners.remove(l); 83 } 84 85 90 private void initComponents() { 92 java.awt.GridBagConstraints gridBagConstraints; 93 94 jLabel1 = new javax.swing.JLabel (); 95 jLabel2 = new javax.swing.JLabel (); 96 projectLocation = new javax.swing.JTextField (); 97 nameLabel = new javax.swing.JLabel (); 98 projectName = new javax.swing.JTextField (); 99 browse = new javax.swing.JButton (); 100 jLabel4 = new javax.swing.JLabel (); 101 projectFolder = new javax.swing.JTextField (); 102 extSourcesWarning = new javax.swing.JLabel (); 103 errorMessage = new javax.swing.JLabel (); 104 progress = new javax.swing.JPanel (); 105 jPanel4 = new javax.swing.JPanel (); 106 progressImpl = new javax.swing.JPanel (); 107 jLabel5 = new javax.swing.JLabel (); 108 jPanel3 = new javax.swing.JPanel (); 109 110 setLayout(new java.awt.GridBagLayout ()); 111 112 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSD_Copy_Move_Panel", new Object [] {new Integer (isMove ? 1 : 0)})); 113 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "LBL_Copy_Move_Dialog_Text", new Object [] {new Integer (isMove ? 1 : 0), ProjectUtils.getInformation(project).getDisplayName()})); 114 gridBagConstraints = new java.awt.GridBagConstraints (); 115 gridBagConstraints.gridwidth = 2; 116 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 117 gridBagConstraints.weightx = 1.0; 118 add(jLabel1, gridBagConstraints); 119 120 jLabel2.setLabelFor(projectLocation); 121 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "LBL_Project_Location")); 122 gridBagConstraints = new java.awt.GridBagConstraints (); 123 gridBagConstraints.gridx = 0; 124 gridBagConstraints.gridy = 2; 125 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 126 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 12); 127 add(jLabel2, gridBagConstraints); 128 129 projectLocation.setColumns(30); 130 gridBagConstraints = new java.awt.GridBagConstraints (); 131 gridBagConstraints.gridx = 1; 132 gridBagConstraints.gridy = 2; 133 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 134 gridBagConstraints.weightx = 1.0; 135 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 5); 136 add(projectLocation, gridBagConstraints); 137 projectLocation.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSN_Project_Location", new Object [] {})); 138 projectLocation.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSD_Project_Location", new Object [] {})); 139 140 nameLabel.setLabelFor(projectName); 141 org.openide.awt.Mnemonics.setLocalizedText(nameLabel, org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "LBL_Project_Name")); 142 gridBagConstraints = new java.awt.GridBagConstraints (); 143 gridBagConstraints.gridx = 0; 144 gridBagConstraints.gridy = 1; 145 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 146 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 12); 147 add(nameLabel, gridBagConstraints); 148 149 projectName.setColumns(30); 150 gridBagConstraints = new java.awt.GridBagConstraints (); 151 gridBagConstraints.gridx = 1; 152 gridBagConstraints.gridy = 1; 153 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 154 gridBagConstraints.weightx = 1.0; 155 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 5); 156 add(projectName, gridBagConstraints); 157 projectName.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSN_Project_Name", new Object [] {})); 158 projectName.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSD_Project_Name", new Object [] {})); 159 160 org.openide.awt.Mnemonics.setLocalizedText(browse, org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "LBL_Browse", new Object [] {})); 161 browse.addActionListener(new java.awt.event.ActionListener () { 162 public void actionPerformed(java.awt.event.ActionEvent evt) { 163 browseActionPerformed(evt); 164 } 165 }); 166 167 gridBagConstraints = new java.awt.GridBagConstraints (); 168 gridBagConstraints.gridx = 2; 169 gridBagConstraints.gridy = 2; 170 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 0); 171 add(browse, gridBagConstraints); 172 browse.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSD_Browse", new Object [] {})); 173 174 jLabel4.setLabelFor(projectFolder); 175 org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "LBL_Project_Folder")); 176 gridBagConstraints = new java.awt.GridBagConstraints (); 177 gridBagConstraints.gridx = 0; 178 gridBagConstraints.gridy = 3; 179 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 180 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 12); 181 add(jLabel4, gridBagConstraints); 182 183 projectFolder.setColumns(30); 184 projectFolder.setEditable(false); 185 gridBagConstraints = new java.awt.GridBagConstraints (); 186 gridBagConstraints.gridx = 1; 187 gridBagConstraints.gridy = 3; 188 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 189 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 5); 190 add(projectFolder, gridBagConstraints); 191 projectFolder.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSN_Project_Folder", new Object [] {})); 192 projectFolder.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "ACSD_Project_Folder", new Object [] {})); 193 194 extSourcesWarning.setForeground(UIManager.getColor("nb.errorForeground")); 195 gridBagConstraints = new java.awt.GridBagConstraints (); 196 gridBagConstraints.gridx = 0; 197 gridBagConstraints.gridy = 4; 198 gridBagConstraints.gridwidth = 3; 199 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 200 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 201 add(extSourcesWarning, gridBagConstraints); 202 203 errorMessage.setForeground(UIManager.getColor("nb.errorForeground")); 204 gridBagConstraints = new java.awt.GridBagConstraints (); 205 gridBagConstraints.gridx = 0; 206 gridBagConstraints.gridy = 5; 207 gridBagConstraints.gridwidth = 3; 208 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 209 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 210 add(errorMessage, gridBagConstraints); 211 212 progress.setLayout(new java.awt.CardLayout ()); 213 214 progress.add(jPanel4, "not-progress"); 215 216 progressImpl.setLayout(new java.awt.GridBagLayout ()); 217 218 org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(ProjectCopyPanel.class, "LBL_Copying_Moving", new Object [] {isMove ? new Integer (1) : new Integer (0)})); 219 gridBagConstraints = new java.awt.GridBagConstraints (); 220 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 221 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 222 gridBagConstraints.weightx = 1.0; 223 progressImpl.add(jLabel5, gridBagConstraints); 224 225 jPanel3.setLayout(new java.awt.BorderLayout ()); 226 227 jPanel3.add(ProgressHandleFactory.createProgressComponent(handle)); 228 gridBagConstraints = new java.awt.GridBagConstraints (); 229 gridBagConstraints.gridx = 0; 230 gridBagConstraints.gridy = 1; 231 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 232 gridBagConstraints.weightx = 1.0; 233 progressImpl.add(jPanel3, gridBagConstraints); 234 235 progress.add(progressImpl, "progress"); 236 237 gridBagConstraints = new java.awt.GridBagConstraints (); 238 gridBagConstraints.gridx = 0; 239 gridBagConstraints.gridy = 6; 240 gridBagConstraints.gridwidth = 3; 241 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 242 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 243 gridBagConstraints.weightx = 1.0; 244 gridBagConstraints.weighty = 0.5; 245 add(progress, gridBagConstraints); 246 247 } 248 250 private void browseActionPerformed(java.awt.event.ActionEvent evt) { File current = new File (projectLocation.getText()); 253 JFileChooser chooser = new JFileChooser (current); 254 255 chooser.setMultiSelectionEnabled(false); 256 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 257 258 if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 259 projectLocation.setText(chooser.getSelectedFile().getAbsolutePath()); 260 } 261 } 263 264 private javax.swing.JButton browse; 266 private javax.swing.JLabel errorMessage; 267 private javax.swing.JLabel extSourcesWarning; 268 private javax.swing.JLabel jLabel1; 269 private javax.swing.JLabel jLabel2; 270 private javax.swing.JLabel jLabel4; 271 private javax.swing.JLabel jLabel5; 272 private javax.swing.JPanel jPanel3; 273 private javax.swing.JPanel jPanel4; 274 private javax.swing.JLabel nameLabel; 275 private javax.swing.JPanel progress; 276 private javax.swing.JPanel progressImpl; 277 private javax.swing.JTextField projectFolder; 278 private javax.swing.JTextField projectLocation; 279 private javax.swing.JTextField projectName; 280 282 private String lastComputedName; 283 284 private String computeValidProjectName(String projectLocation, String projectNamePrefix) { 285 File location = new File (projectLocation); 286 287 if (!location.exists()) { 288 lastComputedName = projectNamePrefix; 289 return projectNamePrefix; 290 } 291 292 int num = 1; 293 String projectName = projectNamePrefix; 294 if (new File (location, projectName).exists()) { 295 while (new File (location, projectName = projectNamePrefix + "_" + num).exists()) { 296 num++; 297 } 298 } 299 lastComputedName = projectName; 300 return projectName; 301 } 302 303 private void setProject() { 304 FileObject parent = project.getProjectDirectory().getParent(); 305 File parentFile = FileUtil.toFile(parent); 306 307 projectLocation.setText(parentFile.getAbsolutePath()); 308 309 if (isMove) { 310 projectName.setText(ProjectUtils.getInformation(project).getName()); 311 } else { 312 projectName.setText(computeValidProjectName(parentFile.getAbsolutePath(), ProjectUtils.getInformation(project).getName())); 313 } 314 315 updateProjectFolder(); 316 validateDialog(); 317 318 if (hasExternalSources() && !isMove) { 319 extSourcesWarning.setText(NbBundle.getMessage(ProjectCopyPanel.class, "WRN_External_Sources")); 320 invalid = true; 321 } 322 } 323 324 private boolean hasExternalSources() { 325 for (FileObject file : ProjectOperations.getDataFiles(project)) { 326 if (!FileUtil.isParentOf(project.getProjectDirectory(), file)) { 327 return true; 328 } 329 } 330 return false; 331 } 332 333 public String getNewName() { 334 return projectName.getText(); 335 } 336 337 public String getProjectFolderName() { 338 return project.getProjectDirectory().getNameExt(); 339 } 340 341 public File getNewDirectory() { 342 return new File (projectLocation.getText()); 343 } 344 345 public void changedUpdate(DocumentEvent e) { 346 } 348 349 public void insertUpdate(DocumentEvent e) { 350 if (e.getDocument().equals(projectLocation.getDocument())) { 351 if (lastComputedName != null && lastComputedName.equals(projectName.getText())) { 352 projectName.setText(computeValidProjectName(new File (projectLocation.getText()).getAbsolutePath(), 353 ProjectUtils.getInformation(project).getName())); 354 } 355 } 356 updateProjectFolder(); 357 validateDialog(); 358 } 359 360 public void removeUpdate(DocumentEvent e) { 361 if (e.getDocument().equals(projectLocation.getDocument())) { 362 if (lastComputedName != null && lastComputedName.equals(projectName.getText())) { 363 projectName.setText(computeValidProjectName(new File (projectLocation.getText()).getAbsolutePath(), 364 ProjectUtils.getInformation(project).getName())); 365 } 366 } 367 updateProjectFolder(); 368 validateDialog(); 369 } 370 371 private void updateProjectFolder() { 372 final File location = new File (projectLocation.getText()); 373 File projectFolderFile = location; 374 if (isMove) { 375 projectFolderFile = new File (location, project.getProjectDirectory().getNameExt()); 376 } else { 377 projectFolderFile = new File (location, projectName.getText()); 378 } 379 projectFolder.setText(projectFolderFile.getAbsolutePath()); 380 } 381 382 public boolean isPanelValid() { 383 return " ".equals(errorMessage.getText()) && !invalid; 384 } 385 386 private void validateDialog() { 387 if (invalid) { 388 return ; 390 } 391 392 String newError = computeError(); 393 boolean changed = false; 394 String currentError = errorMessage.getText(); 395 396 newError = newError != null ? newError : " "; 397 changed = !currentError.equals(newError); 398 399 errorMessage.setText(newError); 400 401 if (changed) { 402 ChangeListener [] listenersCopy; 403 404 synchronized (this) { 405 listenersCopy = listeners.toArray(new ChangeListener [0]); 406 } 407 ChangeEvent evt = new ChangeEvent (this); 408 409 for (ChangeListener l : listenersCopy) { 410 l.stateChanged(evt); 411 } 412 } 413 } 414 415 private String computeError() { 416 File location = new File (projectLocation.getText()); 417 return DefaultProjectOperationsImplementation.computeError(location, projectName.getText(), projectFolder.getText(), false); 418 } 419 420 public void showProgress() { 421 projectFolder.setEnabled(false); 422 projectLocation.setEnabled(false); 423 projectName.setEnabled(false); 424 browse.setEnabled(false); 425 426 ((CardLayout ) progress.getLayout()).show(progress, "progress"); 427 } 428 } 429 | Popular Tags |