1 19 package org.netbeans.modules.versioning.system.cvss.ui.actions; 20 21 import org.netbeans.modules.versioning.spi.VCSContext; 22 import org.netbeans.modules.versioning.system.cvss.CvsModuleConfig; 23 import org.netbeans.modules.versioning.util.Utils; 24 import org.netbeans.lib.cvsclient.CVSRoot; 25 import org.openide.util.NbBundle; 26 import org.openide.util.HelpCtx; 27 import org.openide.DialogDescriptor; 28 import org.openide.DialogDisplayer; 29 30 import javax.swing.*; 31 import javax.swing.text.JTextComponent ; 32 import javax.swing.event.DocumentListener ; 33 import javax.swing.event.DocumentEvent ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 import java.awt.Dialog ; 37 import java.awt.Component ; 38 import java.io.File ; 39 import java.io.IOException ; 40 import java.util.*; 41 import java.util.logging.Logger ; 42 import java.util.logging.Level ; 43 44 47 public class ChangeCVSRootAction extends AbstractAction { 48 49 private final String RECENT_CHANGED_ROOTS = "recentChangedRoots"; 51 private final VCSContext ctx; 52 53 public ChangeCVSRootAction(String name, VCSContext ctx) { 54 super(name); 55 this.ctx = ctx; 56 } 57 58 public boolean isEnabled() { 59 return ctx.getRootFiles().size() > 0 && allDirectories(); 60 } 61 62 private boolean allDirectories() { 63 for (File file : ctx.getRootFiles()) { 64 if (!file.isDirectory()) return false; 65 } 66 return true; 67 } 68 69 public void actionPerformed(ActionEvent e) { 70 ResourceBundle loc = NbBundle.getBundle(ChangeCVSRootAction.class); 71 72 final ChangeCVSRootPanel panel = new ChangeCVSRootPanel(); 73 panel.getWorkingCopy().setText(getWorkingCopy()); 74 panel.getCurrentCVSRoot().setText(getCurrentRoot()); 75 76 List<String > roots = Utils.getStringList(CvsModuleConfig.getDefault().getPreferences(), RECENT_CHANGED_ROOTS); 77 panel.getNewRootCombo().setModel(new DefaultComboBoxModel(roots.toArray())); 78 if (roots.size() > 0) { 79 panel.getNewRootCombo().setSelectedItem(roots.get(0)); 80 } else { 81 panel.getNewRootCombo().setSelectedItem(getNewRoot()); 82 } 83 84 final JButton changeRoot = new JButton(loc.getString("CTL_CvsRootForm_Action_Change")); 85 changeRoot.setToolTipText(loc.getString("TT_CvsRootForm_Action_Change")); 86 JButton cancel = new JButton(loc.getString("CTL_CvsRootForm_Action_Cancel")); 87 cancel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ChangeCVSRootAction.class, "ACSD_CvsRootForm_Action_Cancel")); DialogDescriptor descriptor = new DialogDescriptor( 89 panel, 90 loc.getString("CTL_CvsRootForm_Title"), true, 92 new Object [] { changeRoot, cancel }, 93 changeRoot, 94 DialogDescriptor.BOTTOM_ALIGN, 95 null, 96 null); 97 descriptor.setClosingOptions(null); 98 descriptor.setHelpCtx(new HelpCtx(ChangeCVSRootAction.class)); 99 Dialog dialog = DialogDisplayer.getDefault().createDialog(descriptor); 100 dialog.getAccessibleContext().setAccessibleDescription(loc.getString("ACSD_CvsRootForm")); 101 102 Component c = panel.getNewRootCombo().getEditor().getEditorComponent(); 103 if (c instanceof JTextComponent ) { 104 ((JTextComponent ) c).getDocument().addDocumentListener(new DocumentListener () { 105 public void insertUpdate(DocumentEvent e) { 106 validate(panel, changeRoot); 107 } 108 109 public void removeUpdate(DocumentEvent e) { 110 validate(panel, changeRoot); 111 } 112 113 public void changedUpdate(DocumentEvent e) { 114 validate(panel, changeRoot); 115 } 116 }); 117 } 118 119 panel.getNewRootCombo().addActionListener(new ActionListener () { 120 public void actionPerformed(ActionEvent e) { 121 validate(panel, changeRoot); 122 } 123 }); 124 125 dialog.setVisible(true); 126 if (descriptor.getValue() != changeRoot) return; 127 128 final String newRoot = panel.getNewRootCombo().getEditor().getItem().toString(); 129 Utils.insert(CvsModuleConfig.getDefault().getPreferences(), RECENT_CHANGED_ROOTS, newRoot, 20); 130 131 Utils.createTask(new Runnable () { 132 public void run() { 133 rewrite(newRoot); 134 } 135 }).schedule(0); 136 } 137 138 private void rewrite(String newRoot) { 139 for (File root : ctx.getRootFiles()) { 140 CvsRootRewriter rewriter = new CvsRootRewriter(root, newRoot); 141 rewriter.rewrite(); 142 } 143 } 144 145 private String getCurrentRoot() { 146 StringBuilder sb = new StringBuilder (); 147 for (File file : ctx.getRootFiles()) { 148 try { 149 String root = CvsRootRewriter.getCvsRoot(file); 150 sb.append(root); 151 sb.append(", "); } catch (IOException e) { 153 Logger.getLogger(ChangeCVSRootAction.class.getName()).log(Level.WARNING, "", e); } 156 } 157 sb.delete(sb.length() - 2, sb.length()); 158 return sb.toString(); 159 } 160 161 private String getNewRoot() { 162 File file = ctx.getRootFiles().iterator().next(); 163 try { 164 return CvsRootRewriter.getCvsRoot(file); 165 } catch (IOException e) { 166 Logger.getLogger(ChangeCVSRootAction.class.getName()).log(Level.WARNING, "", e); return ""; } 169 } 170 171 private String getWorkingCopy() { 172 StringBuilder sb = new StringBuilder (); 173 for (File file : ctx.getRootFiles()) { 174 sb.append(file.getAbsolutePath()); 175 sb.append(", "); } 177 sb.delete(sb.length() - 2, sb.length()); 178 return sb.toString(); 179 } 180 181 public void validate(ChangeCVSRootPanel panel, JButton ok) { 182 try { 183 CVSRoot.parse(panel.getNewRootCombo().getEditor().getItem().toString()); 184 ok.setEnabled(true); 185 } catch (Exception e) { 186 ok.setEnabled(false); 187 } 188 } 189 } 190 | Popular Tags |