1 19 package org.netbeans.modules.subversion.options; 20 21 import java.awt.Dialog ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import javax.swing.JPanel ; 28 import javax.swing.ListSelectionModel ; 29 import javax.swing.event.TableModelEvent ; 30 import javax.swing.event.TableModelListener ; 31 import javax.swing.table.DefaultTableModel ; 32 import javax.swing.table.TableModel ; 33 import org.netbeans.modules.subversion.Annotator; 34 import org.netbeans.modules.subversion.SvnModuleConfig; 35 import java.util.regex.Pattern ; 36 import org.openide.DialogDescriptor; 37 import org.openide.DialogDisplayer; 38 import org.openide.util.HelpCtx; 39 import org.openide.util.NbBundle; 40 41 45 public class AnnotationSettings implements ActionListener , TableModelListener { 46 47 private final AnnotationSettingsPanel panel; 48 private DialogDescriptor dialogDescriptor; 49 private boolean valid; 50 51 public AnnotationSettings() { 52 panel = new AnnotationSettingsPanel(); 53 54 String tooltip = NbBundle.getMessage(AnnotationSettings.class, "AnnotationSettingsPanel.annotationTextField.toolTipText", Annotator.LABELS); 55 panel.annotationTextField.setToolTipText(tooltip); 56 57 panel.labelsButton.addActionListener(this); 58 panel.upButton.addActionListener(this); 59 panel.downButton.addActionListener(this); 60 panel.newButton.addActionListener(this); 61 panel.removeButton.addActionListener(this); 62 panel.resetButton.addActionListener(this); 63 64 panel.warningLabel.setVisible(false); 65 66 getModel().addTableModelListener(this); 67 } 68 69 JPanel getPanel() { 70 return panel; 71 } 72 73 void show() { 74 75 String title = NbBundle.getMessage(SvnOptionsController.class, "CTL_ManageLabels"); 76 String accesibleDescription = NbBundle.getMessage(SvnOptionsController.class, "ACSD_ManageLabels"); 77 HelpCtx helpCtx = new HelpCtx(AnnotationSettings.class); 78 79 dialogDescriptor = new DialogDescriptor(panel, title); 80 dialogDescriptor.setModal(true); 81 dialogDescriptor.setHelpCtx(helpCtx); 82 dialogDescriptor.setValid(valid); 83 84 Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor); 85 dialog.getAccessibleContext().setAccessibleDescription(accesibleDescription); 86 dialog.setAlwaysOnTop(false); 88 dialog.setVisible(true); 89 } 90 91 void update() { 92 reset(SvnModuleConfig.getDefault().getAnnotationFormat(), SvnModuleConfig.getDefault().getAnnotationExpresions()); 93 } 94 95 void applyChanges() { 96 SvnModuleConfig.getDefault().setAnnotationFormat(panel.annotationTextField.getText()); 97 98 TableModel model = panel.expresionsTable.getModel(); 99 List <AnnotationExpression> exps = new ArrayList <AnnotationExpression>(model.getRowCount()); 100 for (int r = 0; r < model.getRowCount(); r++) { 101 String urlExp = (String ) model.getValueAt(r, 0); 102 if(urlExp.trim().equals("")) { 103 continue; 104 } 105 String annotationExp = (String ) model.getValueAt(r, 1); 106 exps.add(new AnnotationExpression(urlExp, annotationExp)); 107 } 108 SvnModuleConfig.getDefault().setAnnotationExpresions(exps); 109 } 110 111 public void actionPerformed(ActionEvent evt) { 112 if (evt.getSource() == panel.labelsButton) { 113 onLabelsClick(); 114 } else if (evt.getSource() == panel.upButton) { 115 onUpClick(); 116 } else if (evt.getSource() == panel.downButton) { 117 onDownClick(); 118 } else if (evt.getSource() == panel.newButton) { 119 onNewClick(); 120 } else if (evt.getSource() == panel.removeButton) { 121 onRemoveClick(); 122 } else if (evt.getSource() == panel.resetButton) { 123 onResetClick(); 124 } 125 } 126 127 private void onUpClick() { 128 ListSelectionModel listSelectionModel = getSelectionModel(); 129 int r = listSelectionModel.getMinSelectionIndex(); 130 if(r > 0) { 131 DefaultTableModel model = getModel(); 132 int rNew = r - 1; 133 model.moveRow(r, r, rNew) ; 134 listSelectionModel.setSelectionInterval(rNew, rNew); 135 } 136 } 137 138 private void onDownClick() { 139 ListSelectionModel listSelectionModel = getSelectionModel(); 140 int r = listSelectionModel.getMinSelectionIndex(); 141 DefaultTableModel model = getModel(); 142 if(r > -1 && r < model.getRowCount() - 1) { 143 int rNew = r + 1; 144 model.moveRow(r, r, rNew) ; 145 listSelectionModel.setSelectionInterval(rNew, rNew); 146 } 147 } 148 149 private void onNewClick() { 150 int r = getSelectionModel().getMinSelectionIndex(); 151 if(r < 0) { 152 getModel().addRow( new String [] {"", ""}); 153 } else { 154 getModel().insertRow(r, new String [] {"", ""}); 155 } 156 } 157 158 private void onRemoveClick() { 159 ListSelectionModel selectionModel = getSelectionModel(); 160 int r = selectionModel.getMinSelectionIndex(); 161 if(r > -1) { 162 getModel().removeRow(r); 163 } 164 int size = getModel().getRowCount(); 165 if(size > 0) { 166 if (r > size - 1) { 167 r = size - 1; 168 } 169 selectionModel.setSelectionInterval(r, r); 170 } 171 } 172 173 private void onResetClick() { 174 reset(SvnModuleConfig.getDefault().getDefaultAnnotationFormat(), SvnModuleConfig.getDefault().getDefaultAnnotationExpresions()); 175 } 176 177 private void reset(String annotationformat, List <AnnotationExpression> exps) { 178 panel.annotationTextField.setText(annotationformat); 179 180 getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 181 DefaultTableModel model = getModel(); 182 model.setColumnCount(2); 183 model.setRowCount(exps.size()); 184 int r = -1; 185 for (Iterator <AnnotationExpression> it = exps.iterator(); it.hasNext();) { 186 AnnotationExpression annotationExpression = it.next(); 187 r++; 188 model.setValueAt(annotationExpression.getUrlExp(), r, 0); 189 model.setValueAt(annotationExpression.getAnnotationExp(), r, 1); 190 } 191 } 192 193 private DefaultTableModel getModel() { 194 return (DefaultTableModel ) panel.expresionsTable.getModel(); 195 } 196 197 private ListSelectionModel getSelectionModel() { 198 return panel.expresionsTable.getSelectionModel(); 199 } 200 201 private void onLabelsClick() { 202 LabelsPanel labelsPanel = new LabelsPanel(); 203 List <LabelVariable> variables = new ArrayList <LabelVariable>(Annotator.LABELS.length); 204 for (int i = 0; i < Annotator.LABELS.length; i++) { 205 LabelVariable variable = new LabelVariable( 206 Annotator.LABELS[i], 207 "{" + Annotator.LABELS[i] + "} - " + NbBundle.getMessage(AnnotationSettings.class, "AnnotationSettings.label." + Annotator.LABELS[i]) 208 ); 209 variables.add(variable); 210 } 211 labelsPanel.labelsList.setListData(variables.toArray(new LabelVariable[variables.size()])); 212 213 String title = NbBundle.getMessage(AnnotationSettings.class, "AnnotationSettings.labelVariables.title"); 214 String acsd = NbBundle.getMessage(AnnotationSettings.class, "AnnotationSettings.labelVariables.acsd"); 215 216 if(showDialog(labelsPanel, title, acsd)) { 217 218 Object [] selection = (Object [])labelsPanel.labelsList.getSelectedValues(); 219 220 String variable = ""; 221 for (int i = 0; i < selection.length; i++) { 222 variable += "{" + ((LabelVariable)selection[i]).getVariable() + "}"; 223 } 224 225 String annotation = panel.annotationTextField.getText(); 226 227 int pos = panel.annotationTextField.getCaretPosition(); 228 if(pos < 0) pos = annotation.length(); 229 230 StringBuffer sb = new StringBuffer (annotation.length() + variable.length()); 231 sb.append(annotation.substring(0, pos)); 232 sb.append(variable); 233 if(pos < annotation.length()) { 234 sb.append(annotation.substring(pos, annotation.length())); 235 } 236 panel.annotationTextField.setText(sb.toString()); 237 panel.annotationTextField.requestFocus(); 238 panel.annotationTextField.setCaretPosition(pos + variable.length()); 239 240 } 241 } 242 243 public void tableChanged(TableModelEvent evt) { 244 if (evt.getType() == TableModelEvent.UPDATE) { 245 validateTable(evt.getFirstRow(), evt.getColumn()); 246 } 247 } 248 249 private void validateTable(int r, int c) { 250 251 if(r < 0 || c != 0) { 252 return; 253 } 254 255 valid = true; 256 String pattern = (String ) getModel().getValueAt(r, c); 257 try { 258 Pattern.compile(pattern); 259 } catch (Exception e) { 260 valid = false; 261 } 262 263 if(valid) { 264 panel.warningLabel.setVisible(false); 265 } else { 266 String label = NbBundle.getMessage(AnnotationSettings.class, "AnnotationSettingsPanel.warningLabel.text", pattern); 267 panel.warningLabel.setText(label); 268 panel.warningLabel.setVisible(true); 269 } 270 if(dialogDescriptor != null) { 271 dialogDescriptor.setValid(valid); 272 } 273 } 274 275 private boolean showDialog(JPanel panel, String title, String accesibleDescription) { 276 DialogDescriptor dialogDescriptor = new DialogDescriptor(panel, title); 277 dialogDescriptor.setModal(true); 278 dialogDescriptor.setValid(true); 279 280 Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor); 281 dialog.getAccessibleContext().setAccessibleDescription(accesibleDescription); 282 dialog.setVisible(true); 283 284 return DialogDescriptor.OK_OPTION.equals(dialogDescriptor.getValue()); 285 } 286 287 288 private class LabelVariable { 289 private String description; 290 private String variable; 291 292 public LabelVariable(String variable, String description) { 293 this.description = description; 294 this.variable = variable; 295 } 296 297 public String toString() { 298 return description; 299 } 300 301 public String getDescription() { 302 return description; 303 } 304 305 public String getVariable() { 306 return variable; 307 } 308 } 309 } 310 | Popular Tags |