PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/netbeans-7.3/mercurial/src/org/netbeans/modules/mercurial/ui/wizards/CloneDestinationDirectoryPanel.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 285 lines | 188 code | 32 blank | 65 comment | 29 complexity | fb38122e74f31fa46eb4ea6c777c2dc7 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2009 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.mercurial.ui.wizards;
  45. import javax.swing.JPanel;
  46. import java.io.File;
  47. import java.awt.event.ActionEvent;
  48. import java.awt.event.ActionListener;
  49. import javax.swing.JFileChooser;
  50. import javax.swing.filechooser.FileFilter;
  51. import org.netbeans.modules.mercurial.HgModuleConfig;
  52. import org.netbeans.modules.versioning.util.AccessibleJFileChooser;
  53. import org.netbeans.spi.project.ui.support.ProjectChooser;
  54. import org.openide.util.NbBundle;
  55. public final class CloneDestinationDirectoryPanel extends JPanel implements ActionListener {
  56. private String message;
  57. /** Creates new form CloneVisualPanel2 */
  58. public CloneDestinationDirectoryPanel() {
  59. initComponents();
  60. directoryBrowseButton.addActionListener(this);
  61. scanForProjectsCheckBox.addActionListener(this);
  62. directoryField.setText(defaultWorkingDirectory().getPath());
  63. scanForProjectsCheckBox.setSelected(HgModuleConfig.getDefault().getShowCloneCompleted());
  64. }
  65. public String getName() {
  66. if (destinationDirectoryPanel == null) {
  67. return null;
  68. }
  69. return destinationDirectoryPanel.getName();
  70. }
  71. public String getDirectory() {
  72. return directoryField.getText();
  73. }
  74. public String getCloneName() {
  75. return nameField.getText();
  76. }
  77. public String getMessage() {
  78. return message;
  79. }
  80. /**
  81. * Validates user's input
  82. * @return <code>true</code> if directory does not exist, <code>false</code> otherwise
  83. */
  84. public boolean isInputValid() {
  85. String dir = directoryField.getText();
  86. String name = nameField.getText();
  87. File file = new File (dir, name);
  88. if (file.exists()) {
  89. message = NbBundle.getMessage(CloneDestinationDirectoryPanel.class, "MSG_TARGET_EXISTS"); // NOI18N
  90. return false;
  91. } else {
  92. message = "";
  93. return true;
  94. }
  95. }
  96. /** This method is called from within the constructor to
  97. * initialize the form.
  98. * WARNING: Do NOT modify this code. The content of this method is
  99. * always regenerated by the Form Editor.
  100. */
  101. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  102. private void initComponents() {
  103. destinationDirectoryPanel = new javax.swing.JPanel();
  104. directoryLabel = new javax.swing.JLabel();
  105. directoryBrowseButton = new javax.swing.JButton();
  106. nameLabel = new javax.swing.JLabel();
  107. jLabel1 = new javax.swing.JLabel();
  108. destinationDirectoryPanel.setName(org.openide.util.NbBundle.getMessage(CloneDestinationDirectoryPanel.class, "destinationDirectoryPanel.Name")); // NOI18N
  109. directoryLabel.setLabelFor(directoryField);
  110. org.openide.awt.Mnemonics.setLocalizedText(directoryLabel, org.openide.util.NbBundle.getMessage(CloneDestinationDirectoryPanel.class, "directoryLabel.Name")); // NOI18N
  111. directoryField.addActionListener(new java.awt.event.ActionListener() {
  112. public void actionPerformed(java.awt.event.ActionEvent evt) {
  113. directoryFieldActionPerformed(evt);
  114. }
  115. });
  116. org.openide.awt.Mnemonics.setLocalizedText(directoryBrowseButton, org.openide.util.NbBundle.getMessage(CloneDestinationDirectoryPanel.class, "directoryBrowseButton.Name")); // NOI18N
  117. nameLabel.setLabelFor(nameField);
  118. org.openide.awt.Mnemonics.setLocalizedText(nameLabel, org.openide.util.NbBundle.getMessage(CloneDestinationDirectoryPanel.class, "nameLabel.Name")); // NOI18N
  119. scanForProjectsCheckBox.setSelected(true);
  120. org.openide.awt.Mnemonics.setLocalizedText(scanForProjectsCheckBox, org.openide.util.NbBundle.getMessage(CloneDestinationDirectoryPanel.class, "CTL_Scan_After_Clone")); // NOI18N
  121. org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(CloneDestinationDirectoryPanel.class, "directoryTitleLabel.Name")); // NOI18N
  122. javax.swing.GroupLayout destinationDirectoryPanelLayout = new javax.swing.GroupLayout(destinationDirectoryPanel);
  123. destinationDirectoryPanel.setLayout(destinationDirectoryPanelLayout);
  124. destinationDirectoryPanelLayout.setHorizontalGroup(
  125. destinationDirectoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  126. .addGroup(destinationDirectoryPanelLayout.createSequentialGroup()
  127. .addGroup(destinationDirectoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  128. .addGroup(destinationDirectoryPanelLayout.createSequentialGroup()
  129. .addGroup(destinationDirectoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  130. .addComponent(nameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  131. .addComponent(directoryLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  132. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  133. .addGroup(destinationDirectoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  134. .addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
  135. .addComponent(directoryField, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE))
  136. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  137. .addComponent(directoryBrowseButton))
  138. .addComponent(scanForProjectsCheckBox)
  139. .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 597, javax.swing.GroupLayout.PREFERRED_SIZE))
  140. .addContainerGap())
  141. );
  142. destinationDirectoryPanelLayout.setVerticalGroup(
  143. destinationDirectoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  144. .addGroup(destinationDirectoryPanelLayout.createSequentialGroup()
  145. .addComponent(jLabel1)
  146. .addGap(20, 20, 20)
  147. .addGroup(destinationDirectoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  148. .addComponent(directoryLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  149. .addComponent(directoryBrowseButton)
  150. .addComponent(directoryField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  151. .addGap(28, 28, 28)
  152. .addGroup(destinationDirectoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  153. .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  154. .addComponent(nameLabel))
  155. .addGap(18, 18, 18)
  156. .addComponent(scanForProjectsCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
  157. .addGap(75, 75, 75))
  158. );
  159. scanForProjectsCheckBox.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(CloneDestinationDirectoryPanel.class, "ACSD_Scan_After_Clone")); // NOI18N
  160. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  161. this.setLayout(layout);
  162. layout.setHorizontalGroup(
  163. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  164. .addComponent(destinationDirectoryPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  165. );
  166. layout.setVerticalGroup(
  167. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  168. .addGroup(layout.createSequentialGroup()
  169. .addComponent(destinationDirectoryPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  170. .addContainerGap(62, Short.MAX_VALUE))
  171. );
  172. }// </editor-fold>//GEN-END:initComponents
  173. private void directoryFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_directoryFieldActionPerformed
  174. // TODO add your handling code here:
  175. }//GEN-LAST:event_directoryFieldActionPerformed
  176. public void actionPerformed(ActionEvent evt) {
  177. if (evt.getSource() == directoryBrowseButton) {
  178. onBrowseClick();
  179. } else if (evt.getSource() == scanForProjectsCheckBox) {
  180. HgModuleConfig.getDefault().setShowCloneCompleted(scanForProjectsCheckBox.isSelected());
  181. }
  182. }
  183. private void onBrowseClick() {
  184. File oldFile = defaultWorkingDirectory();
  185. JFileChooser fileChooser = new AccessibleJFileChooser(NbBundle.getMessage(CloneDestinationDirectoryPanel.class, "ACSD_BrowseFolder"), oldFile); // NO I18N
  186. fileChooser.setDialogTitle(NbBundle.getMessage(CloneDestinationDirectoryPanel.class, "Browse_title")); // NO I18N
  187. fileChooser.setMultiSelectionEnabled(false);
  188. FileFilter[] old = fileChooser.getChoosableFileFilters();
  189. for (int i = 0; i < old.length; i++) {
  190. FileFilter fileFilter = old[i];
  191. fileChooser.removeChoosableFileFilter(fileFilter);
  192. }
  193. fileChooser.addChoosableFileFilter(new FileFilter() {
  194. public boolean accept(File f) {
  195. return f.isDirectory();
  196. }
  197. public String getDescription() {
  198. return NbBundle.getMessage(CloneDestinationDirectoryPanel.class, "Folders"); // NOI18N
  199. }
  200. });
  201. fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  202. fileChooser.showDialog(this, NbBundle.getMessage(CloneDestinationDirectoryPanel.class, "OK_Button")); // NO I18N
  203. File f = fileChooser.getSelectedFile();
  204. if (f != null) {
  205. directoryField.setText(f.getAbsolutePath());
  206. }
  207. }
  208. /**
  209. * Returns file to be initally used.
  210. * <ul>
  211. * <li>first is takes text in workTextField
  212. * <li>then recent project folder
  213. * <li>finally <tt>user.home</tt>
  214. * <ul>
  215. */
  216. private File defaultWorkingDirectory() {
  217. File defaultDir = null;
  218. String current = directoryField.getText();
  219. if (current != null && !(current.trim().equals(""))) { // NOI18N
  220. File currentFile = new File(current);
  221. while (currentFile != null && currentFile.exists() == false) {
  222. currentFile = currentFile.getParentFile();
  223. }
  224. if (currentFile != null) {
  225. if (currentFile.isFile()) {
  226. defaultDir = currentFile.getParentFile();
  227. } else {
  228. defaultDir = currentFile;
  229. }
  230. }
  231. }
  232. if (defaultDir == null) {
  233. File projectFolder = ProjectChooser.getProjectsFolder();
  234. if (projectFolder.exists() && projectFolder.isDirectory()) {
  235. defaultDir = projectFolder;
  236. }
  237. }
  238. if (defaultDir == null) {
  239. defaultDir = new File(System.getProperty("user.home")); // NOI18N
  240. }
  241. return defaultDir;
  242. }
  243. // Variables declaration - do not modify//GEN-BEGIN:variables
  244. private javax.swing.JPanel destinationDirectoryPanel;
  245. private javax.swing.JButton directoryBrowseButton;
  246. final javax.swing.JTextField directoryField = new javax.swing.JTextField();
  247. private javax.swing.JLabel directoryLabel;
  248. private javax.swing.JLabel jLabel1;
  249. final javax.swing.JTextField nameField = new javax.swing.JTextField();
  250. private javax.swing.JLabel nameLabel;
  251. final javax.swing.JCheckBox scanForProjectsCheckBox = new javax.swing.JCheckBox();
  252. // End of variables declaration//GEN-END:variables
  253. }