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.UIManager ; 26 import javax.swing.event.ChangeEvent ; 27 import javax.swing.event.ChangeListener ; 28 import javax.swing.event.DocumentEvent ; 29 import javax.swing.event.DocumentListener ; 30 import org.netbeans.api.progress.ProgressHandle; 31 import org.netbeans.api.progress.ProgressHandleFactory; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.api.project.ProjectUtils; 34 import org.netbeans.modules.project.uiapi.DefaultProjectOperationsImplementation.InvalidablePanel; 35 import org.openide.filesystems.FileUtil; 36 37 40 public class DefaultProjectRenamePanel extends javax.swing.JPanel implements DocumentListener , InvalidablePanel { 41 42 private Project project; 43 private List <ChangeListener > listeners; 44 private ProgressHandle handle; 45 46 49 public DefaultProjectRenamePanel(ProgressHandle handle, Project project, String name) { 50 this.project = project; 51 this.handle = handle; 52 53 if (name == null) { 54 name = ProjectUtils.getInformation(project).getDisplayName(); 55 } 56 57 this.listeners = new ArrayList <ChangeListener >(); 58 59 initComponents(); 60 61 projectName.setText(name); 62 projectName.getDocument().addDocumentListener(this); 63 updateProjectFolder(); 64 validateDialog(); 65 66 if (Boolean.getBoolean("org.netbeans.modules.project.uiapi.DefaultProjectOperations.showProgress")) { 67 ((CardLayout ) progress.getLayout()).show(progress, "progress"); 68 } 69 } 70 71 public synchronized void addChangeListener(ChangeListener l) { 72 listeners.add(l); 73 } 74 75 public synchronized void removeChangeListener(ChangeListener l) { 76 listeners.remove(l); 77 } 78 79 84 private void initComponents() { 86 java.awt.GridBagConstraints gridBagConstraints; 87 88 jLabel1 = new javax.swing.JLabel (); 89 projectFolderLabel = new javax.swing.JLabel (); 90 projectName = new javax.swing.JTextField (); 91 projectFolder = new javax.swing.JTextField (); 92 alsoRenameFolder = new javax.swing.JCheckBox (); 93 jLabel3 = new javax.swing.JLabel (); 94 errorMessage = new javax.swing.JLabel (); 95 progress = new javax.swing.JPanel (); 96 jPanel4 = new javax.swing.JPanel (); 97 progressImpl = new javax.swing.JPanel (); 98 jLabel5 = new javax.swing.JLabel (); 99 jPanel3 = new javax.swing.JPanel (); 100 101 setLayout(new java.awt.GridBagLayout ()); 102 103 getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSD_Project_Rename", new Object [] {})); 104 jLabel1.setLabelFor(projectName); 105 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "LBL_Project_Name")); 106 gridBagConstraints = new java.awt.GridBagConstraints (); 107 gridBagConstraints.gridx = 0; 108 gridBagConstraints.gridy = 1; 109 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 110 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 12); 111 add(jLabel1, gridBagConstraints); 112 113 projectFolderLabel.setLabelFor(projectFolder); 114 org.openide.awt.Mnemonics.setLocalizedText(projectFolderLabel, org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "LBL_Project_Folder")); 115 gridBagConstraints = new java.awt.GridBagConstraints (); 116 gridBagConstraints.gridx = 0; 117 gridBagConstraints.gridy = 3; 118 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 119 gridBagConstraints.insets = new java.awt.Insets (6, 18, 0, 12); 120 add(projectFolderLabel, gridBagConstraints); 121 122 projectName.setColumns(30); 123 gridBagConstraints = new java.awt.GridBagConstraints (); 124 gridBagConstraints.gridx = 1; 125 gridBagConstraints.gridy = 1; 126 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 127 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 128 add(projectName, gridBagConstraints); 129 projectName.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSN_Project_Name", new Object [] {})); 130 projectName.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSD_Project_Name", new Object [] {})); 131 132 projectFolder.setColumns(30); 133 projectFolder.setEditable(false); 134 gridBagConstraints = new java.awt.GridBagConstraints (); 135 gridBagConstraints.gridx = 1; 136 gridBagConstraints.gridy = 3; 137 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 138 gridBagConstraints.insets = new java.awt.Insets (6, 0, 0, 0); 139 add(projectFolder, gridBagConstraints); 140 projectFolder.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSN_Project_Folder", new Object [] {})); 141 projectFolder.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSD_Project_Folder", new Object [] {})); 142 143 org.openide.awt.Mnemonics.setLocalizedText(alsoRenameFolder, org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "LBL_Also_Rename_Project_Folder")); 144 alsoRenameFolder.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 145 alsoRenameFolder.setMargin(new java.awt.Insets (0, 0, 0, 0)); 146 alsoRenameFolder.addActionListener(new java.awt.event.ActionListener () { 147 public void actionPerformed(java.awt.event.ActionEvent evt) { 148 alsoRenameFolderActionPerformed(evt); 149 } 150 }); 151 152 gridBagConstraints = new java.awt.GridBagConstraints (); 153 gridBagConstraints.gridx = 0; 154 gridBagConstraints.gridy = 2; 155 gridBagConstraints.gridwidth = 2; 156 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 157 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 158 add(alsoRenameFolder, gridBagConstraints); 159 alsoRenameFolder.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSN_Also_Rename_Project_Folder", new Object [] {})); 160 alsoRenameFolder.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "ACSD_Also_Rename_Project_Folder", new Object [] {})); 161 162 org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "LBL_Rename_Dialog_Text", new Object [] {ProjectUtils.getInformation(project).getDisplayName()})); 163 gridBagConstraints = new java.awt.GridBagConstraints (); 164 gridBagConstraints.gridwidth = 2; 165 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 166 add(jLabel3, gridBagConstraints); 167 168 errorMessage.setForeground(UIManager.getColor("nb.errorForeground")); 169 gridBagConstraints = new java.awt.GridBagConstraints (); 170 gridBagConstraints.gridx = 0; 171 gridBagConstraints.gridy = 4; 172 gridBagConstraints.gridwidth = 2; 173 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 174 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 175 gridBagConstraints.weightx = 1.0; 176 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 177 add(errorMessage, gridBagConstraints); 178 179 progress.setLayout(new java.awt.CardLayout ()); 180 181 progress.add(jPanel4, "not-progress"); 182 183 progressImpl.setLayout(new java.awt.GridBagLayout ()); 184 185 org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(DefaultProjectRenamePanel.class, "LBL_Renaming_Project", new Object [] {})); 186 gridBagConstraints = new java.awt.GridBagConstraints (); 187 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 188 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 189 gridBagConstraints.weightx = 1.0; 190 progressImpl.add(jLabel5, gridBagConstraints); 191 192 jPanel3.setLayout(new java.awt.BorderLayout ()); 193 194 jPanel3.add(ProgressHandleFactory.createProgressComponent(handle)); 195 gridBagConstraints = new java.awt.GridBagConstraints (); 196 gridBagConstraints.gridx = 0; 197 gridBagConstraints.gridy = 1; 198 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 199 gridBagConstraints.weightx = 1.0; 200 progressImpl.add(jPanel3, gridBagConstraints); 201 202 progress.add(progressImpl, "progress"); 203 204 gridBagConstraints = new java.awt.GridBagConstraints (); 205 gridBagConstraints.gridx = 0; 206 gridBagConstraints.gridy = 5; 207 gridBagConstraints.gridwidth = 2; 208 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 209 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 210 gridBagConstraints.weightx = 1.0; 211 gridBagConstraints.weighty = 1.0; 212 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 213 add(progress, gridBagConstraints); 214 215 } 216 218 private void alsoRenameFolderActionPerformed(java.awt.event.ActionEvent evt) { validateDialog(); 220 } 222 223 private javax.swing.JCheckBox alsoRenameFolder; 225 private javax.swing.JLabel errorMessage; 226 private javax.swing.JLabel jLabel1; 227 private javax.swing.JLabel jLabel3; 228 private javax.swing.JLabel jLabel5; 229 private javax.swing.JPanel jPanel3; 230 private javax.swing.JPanel jPanel4; 231 private javax.swing.JPanel progress; 232 private javax.swing.JPanel progressImpl; 233 private javax.swing.JTextField projectFolder; 234 private javax.swing.JLabel projectFolderLabel; 235 private javax.swing.JTextField projectName; 236 238 public String getNewName() { 239 return projectName.getText(); 240 } 241 242 public boolean getRenameProjectFolder() { 243 return alsoRenameFolder.isSelected(); 244 } 245 246 public void changedUpdate(DocumentEvent e) { 247 } 249 250 public void insertUpdate(DocumentEvent e) { 251 updateProjectFolder(); 252 validateDialog(); 253 } 254 255 public void removeUpdate(DocumentEvent e) { 256 updateProjectFolder(); 257 validateDialog(); 258 } 259 260 private void updateProjectFolder() { 261 File location = FileUtil.toFile(project.getProjectDirectory().getParent()); 262 File projectFolderFile = new File (location, projectName.getText()); 263 264 projectFolder.setText(projectFolderFile.getAbsolutePath()); 265 } 266 267 public boolean isPanelValid() { 268 return " ".equals(errorMessage.getText()); 269 } 270 271 private void validateDialog() { 272 String newError = computeError(); 273 boolean changed = false; 274 String currentError = errorMessage.getText(); 275 276 newError = newError != null ? newError : " "; 277 changed = !currentError.equals(newError); 278 279 errorMessage.setText(newError); 280 281 if (changed) { 282 ChangeListener [] listenersCopy; 283 284 synchronized (this) { 285 listenersCopy = listeners.toArray(new ChangeListener [0]); 286 } 287 ChangeEvent evt = new ChangeEvent (this); 288 289 for (ChangeListener l : listenersCopy) { 290 l.stateChanged(evt); 291 } 292 } 293 } 294 295 private String computeError() { 296 File location = FileUtil.toFile(project.getProjectDirectory().getParent()); 297 298 return DefaultProjectOperationsImplementation.computeError(location, projectName.getText(), !getRenameProjectFolder()); 299 } 300 301 public void showProgress() { 302 projectFolder.setEnabled(false); 303 projectName.setEnabled(false); 304 alsoRenameFolder.setEnabled(false); 305 progress.setVisible(true); 306 307 ((CardLayout ) progress.getLayout()).last(progress); 308 } 309 310 } 311 | Popular Tags |