KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > options > AnnotationSettings


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.subversion.options;
20
21 import java.awt.Dialog JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.ListSelectionModel JavaDoc;
29 import javax.swing.event.TableModelEvent JavaDoc;
30 import javax.swing.event.TableModelListener JavaDoc;
31 import javax.swing.table.DefaultTableModel JavaDoc;
32 import javax.swing.table.TableModel JavaDoc;
33 import org.netbeans.modules.subversion.Annotator;
34 import org.netbeans.modules.subversion.SvnModuleConfig;
35 import java.util.regex.Pattern JavaDoc;
36 import org.openide.DialogDescriptor;
37 import org.openide.DialogDisplayer;
38 import org.openide.util.HelpCtx;
39 import org.openide.util.NbBundle;
40
41 /**
42  *
43  * @author Tomas Stupka
44  */

45 public class AnnotationSettings implements ActionListener JavaDoc, TableModelListener JavaDoc {
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 JavaDoc 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 JavaDoc getPanel() {
70         return panel;
71     }
72         
73     void show() {
74         
75         String JavaDoc title = NbBundle.getMessage(SvnOptionsController.class, "CTL_ManageLabels");
76         String JavaDoc 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 JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
85         dialog.getAccessibleContext().setAccessibleDescription(accesibleDescription);
86         //dialog.setModal(false);
87
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 JavaDoc model = panel.expresionsTable.getModel();
99         List JavaDoc<AnnotationExpression> exps = new ArrayList JavaDoc<AnnotationExpression>(model.getRowCount());
100         for (int r = 0; r < model.getRowCount(); r++) {
101             String JavaDoc urlExp = (String JavaDoc) model.getValueAt(r, 0);
102             if(urlExp.trim().equals("")) {
103                 continue;
104             }
105             String JavaDoc annotationExp = (String JavaDoc) model.getValueAt(r, 1);
106             exps.add(new AnnotationExpression(urlExp, annotationExp));
107         }
108         SvnModuleConfig.getDefault().setAnnotationExpresions(exps);
109     }
110     
111     public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc listSelectionModel = getSelectionModel();
129         int r = listSelectionModel.getMinSelectionIndex();
130         if(r > 0) {
131             DefaultTableModel JavaDoc 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 JavaDoc listSelectionModel = getSelectionModel();
140         int r = listSelectionModel.getMinSelectionIndex();
141         DefaultTableModel JavaDoc 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 JavaDoc[] {"", ""});
153          } else {
154              getModel().insertRow(r, new String JavaDoc[] {"", ""});
155          }
156     }
157     
158     private void onRemoveClick() {
159         ListSelectionModel JavaDoc 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 JavaDoc annotationformat, List JavaDoc<AnnotationExpression> exps) {
178         panel.annotationTextField.setText(annotationformat);
179                 
180         getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
181         DefaultTableModel JavaDoc model = getModel();
182         model.setColumnCount(2);
183         model.setRowCount(exps.size());
184         int r = -1;
185         for (Iterator JavaDoc<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 JavaDoc getModel() {
194         return (DefaultTableModel JavaDoc) panel.expresionsTable.getModel();
195     }
196
197     private ListSelectionModel JavaDoc getSelectionModel() {
198         return panel.expresionsTable.getSelectionModel();
199     }
200     
201     private void onLabelsClick() {
202         LabelsPanel labelsPanel = new LabelsPanel();
203         List JavaDoc<LabelVariable> variables = new ArrayList JavaDoc<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 JavaDoc title = NbBundle.getMessage(AnnotationSettings.class, "AnnotationSettings.labelVariables.title");
214         String JavaDoc acsd = NbBundle.getMessage(AnnotationSettings.class, "AnnotationSettings.labelVariables.acsd");
215         
216         if(showDialog(labelsPanel, title, acsd)) {
217             
218             Object JavaDoc[] selection = (Object JavaDoc[])labelsPanel.labelsList.getSelectedValues();
219             
220             String JavaDoc variable = "";
221             for (int i = 0; i < selection.length; i++) {
222                 variable += "{" + ((LabelVariable)selection[i]).getVariable() + "}";
223             }
224
225             String JavaDoc annotation = panel.annotationTextField.getText();
226
227             int pos = panel.annotationTextField.getCaretPosition();
228             if(pos < 0) pos = annotation.length();
229
230             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc 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 JavaDoc pattern = (String JavaDoc) getModel().getValueAt(r, c);
257         try {
258             Pattern.compile(pattern);
259         } catch (Exception JavaDoc e) {
260             valid = false;
261         }
262         
263         if(valid) {
264             panel.warningLabel.setVisible(false);
265         } else {
266             String JavaDoc 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 JavaDoc panel, String JavaDoc title, String JavaDoc accesibleDescription) {
276         DialogDescriptor dialogDescriptor = new DialogDescriptor(panel, title);
277         dialogDescriptor.setModal(true);
278         dialogDescriptor.setValid(true);
279         
280         Dialog JavaDoc 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 JavaDoc description;
290         private String JavaDoc variable;
291          
292         public LabelVariable(String JavaDoc variable, String JavaDoc description) {
293             this.description = description;
294             this.variable = variable;
295         }
296          
297         public String JavaDoc toString() {
298             return description;
299         }
300         
301         public String JavaDoc getDescription() {
302             return description;
303         }
304         
305         public String JavaDoc getVariable() {
306             return variable;
307         }
308     }
309 }
310
Popular Tags