001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.subversion.options;
042:
043: import java.awt.Dialog;
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.util.ArrayList;
047: import java.util.Iterator;
048: import java.util.List;
049: import javax.swing.JPanel;
050: import javax.swing.ListSelectionModel;
051: import javax.swing.event.DocumentEvent;
052: import javax.swing.event.ListSelectionEvent;
053: import javax.swing.event.ListSelectionListener;
054: import javax.swing.event.TableModelEvent;
055: import javax.swing.event.TableModelListener;
056: import javax.swing.table.DefaultTableModel;
057: import javax.swing.table.TableModel;
058: import org.netbeans.modules.subversion.SvnModuleConfig;
059: import java.util.regex.Pattern;
060: import javax.swing.event.DocumentListener;
061: import org.netbeans.modules.subversion.ui.wizards.URLPatternWizard;
062: import org.netbeans.modules.subversion.util.SvnUtils;
063: import org.openide.DialogDescriptor;
064: import org.openide.DialogDisplayer;
065: import org.openide.util.HelpCtx;
066: import org.openide.util.NbBundle;
067:
068: /**
069: *
070: * @author Tomas Stupka
071: */
072: public class AnnotationSettings implements ActionListener,
073: TableModelListener, ListSelectionListener {
074:
075: private final AnnotationSettingsPanel panel;
076: private DialogDescriptor dialogDescriptor;
077: private boolean valid;
078:
079: public AnnotationSettings() {
080:
081: panel = new AnnotationSettingsPanel();
082: getModel().addTableModelListener(this );
083: getSelectionModel().addListSelectionListener(this );
084: panel.upButton.setEnabled(false);
085: panel.downButton.setEnabled(false);
086: panel.removeButton.setEnabled(false);
087: panel.editButton.setEnabled(false);
088:
089: panel.upButton.addActionListener(this );
090: panel.downButton.addActionListener(this );
091: panel.newButton.addActionListener(this );
092: panel.removeButton.addActionListener(this );
093: panel.editButton.addActionListener(this );
094: panel.resetButton.addActionListener(this );
095: panel.wizardButton.addActionListener(this );
096:
097: panel.warningLabel.setVisible(false);
098: }
099:
100: private void setValid(boolean valid) {
101: if (!valid) {
102: panel.warningLabel.setText(NbBundle.getMessage(
103: AnnotationSettings.class,
104: "MSG_MissingFolderVariable"));
105: }
106: panel.warningLabel.setVisible(!valid);
107: panel.upButton.setVisible(valid);
108: panel.downButton.setVisible(valid);
109: panel.newButton.setVisible(valid);
110: panel.removeButton.setVisible(valid);
111: panel.editButton.setVisible(valid);
112: panel.resetButton.setVisible(valid);
113: panel.wizardButton.setVisible(valid);
114: panel.expresionsTable.setVisible(valid);
115: panel.expressionsPane.setVisible(valid);
116: panel.tableLabel.setVisible(valid);
117: }
118:
119: JPanel getPanel() {
120: return panel;
121: }
122:
123: void show(boolean valid) {
124: setValid(valid);
125:
126: String title = NbBundle.getMessage(SvnOptionsController.class,
127: "CTL_ManageLabels");
128: String accesibleDescription = NbBundle.getMessage(
129: SvnOptionsController.class, "ACSD_ManageLabels");
130: HelpCtx helpCtx = new HelpCtx(AnnotationSettings.class);
131:
132: dialogDescriptor = new DialogDescriptor(panel, title);
133: dialogDescriptor.setModal(true);
134: dialogDescriptor.setHelpCtx(helpCtx);
135: dialogDescriptor.setValid(valid);
136:
137: Dialog dialog = DialogDisplayer.getDefault().createDialog(
138: dialogDescriptor);
139: dialog.getAccessibleContext().setAccessibleDescription(
140: accesibleDescription);
141: //dialog.setModal(false);
142: dialog.setAlwaysOnTop(false);
143: dialog.setVisible(true);
144: }
145:
146: void update() {
147: reset(SvnModuleConfig.getDefault().getAnnotationExpresions());
148: }
149:
150: boolean isChanged() {
151: List<AnnotationExpression> storedExpressions = SvnModuleConfig
152: .getDefault().getAnnotationExpresions();
153: List<AnnotationExpression> expressions = getAnnotationExpressions();
154: return !SvnUtils.equals(storedExpressions, expressions);
155: }
156:
157: void applyChanges() {
158: List<AnnotationExpression> exps = getAnnotationExpressions();
159: SvnModuleConfig.getDefault().setAnnotationExpresions(exps);
160: }
161:
162: private List<AnnotationExpression> getAnnotationExpressions() {
163: TableModel model = panel.expresionsTable.getModel();
164: List<AnnotationExpression> exps = new ArrayList<AnnotationExpression>(
165: model.getRowCount());
166: for (int r = 0; r < model.getRowCount(); r++) {
167: String urlExp = (String) model.getValueAt(r, 0);
168: if (urlExp.trim().equals("")) {
169: continue;
170: }
171: String annotationExp = (String) model.getValueAt(r, 1);
172: exps.add(new AnnotationExpression(urlExp, annotationExp));
173: }
174: return exps;
175: }
176:
177: public void actionPerformed(ActionEvent evt) {
178: if (evt.getSource() == panel.upButton) {
179: onUpClick();
180: } else if (evt.getSource() == panel.downButton) {
181: onDownClick();
182: } else if (evt.getSource() == panel.newButton) {
183: onNewClick();
184: } else if (evt.getSource() == panel.removeButton) {
185: onRemoveClick();
186: } else if (evt.getSource() == panel.editButton) {
187: onEditClick();
188: } else if (evt.getSource() == panel.resetButton) {
189: onResetClick();
190: } else if (evt.getSource() == panel.wizardButton) {
191: onWizardClick();
192: }
193: }
194:
195: private void onUpClick() {
196: ListSelectionModel listSelectionModel = getSelectionModel();
197: int r = listSelectionModel.getMinSelectionIndex();
198: if (r > 0) {
199: DefaultTableModel model = getModel();
200: int rNew = r - 1;
201: model.moveRow(r, r, rNew);
202: listSelectionModel.setSelectionInterval(rNew, rNew);
203: }
204: }
205:
206: private void onDownClick() {
207: ListSelectionModel listSelectionModel = getSelectionModel();
208: int r = listSelectionModel.getMinSelectionIndex();
209: DefaultTableModel model = getModel();
210: if (r > -1 && r < model.getRowCount() - 1) {
211: int rNew = r + 1;
212: model.moveRow(r, r, rNew);
213: listSelectionModel.setSelectionInterval(rNew, rNew);
214: }
215: }
216:
217: private void onNewClick() {
218: EditAnnotationPanel editPanel = new EditAnnotationPanel();
219: String title = NbBundle.getMessage(AnnotationSettings.class,
220: "AnnotationSettings.new.title");
221: if (showEdit(editPanel, title)) {
222: addRow(editPanel.patternTextField.getText(),
223: editPanel.folderTextField.getText());
224: }
225: }
226:
227: private void addRow(String pattern, String folder) {
228: int r = getSelectionModel().getMinSelectionIndex();
229: if (r < 0) {
230: getModel().addRow(new String[] { pattern, folder });
231: } else {
232: getModel().insertRow(r, new String[] { pattern, folder });
233: }
234: }
235:
236: private void onRemoveClick() {
237: ListSelectionModel selectionModel = getSelectionModel();
238: int r = selectionModel.getMinSelectionIndex();
239: if (r > -1) {
240: getModel().removeRow(r);
241: }
242: int size = getModel().getRowCount();
243: if (size > 0) {
244: if (r > size - 1) {
245: r = size - 1;
246: }
247: selectionModel.setSelectionInterval(r, r);
248: }
249: }
250:
251: private void onEditClick() {
252: ListSelectionModel selectionModel = getSelectionModel();
253: int r = selectionModel.getMinSelectionIndex();
254: if (r < 0)
255: return;
256:
257: String pattern = (String) getModel().getValueAt(r, 0);
258: String folder = (String) getModel().getValueAt(r, 1);
259:
260: EditAnnotationPanel editPanel = new EditAnnotationPanel();
261: if (pattern != null)
262: editPanel.patternTextField.setText(pattern);
263: if (folder != null)
264: editPanel.folderTextField.setText(folder);
265:
266: String title = NbBundle.getMessage(AnnotationSettings.class,
267: "AnnotationSettings.edit.title");
268: if (showEdit(editPanel, title)) {
269: getModel().setValueAt(editPanel.patternTextField.getText(),
270: r, 0);
271: getModel().setValueAt(editPanel.folderTextField.getText(),
272: r, 1);
273: }
274: }
275:
276: private void onResetClick() {
277: reset(SvnModuleConfig.getDefault()
278: .getDefaultAnnotationExpresions());
279: }
280:
281: private void onWizardClick() {
282: URLPatternWizard wizard = new URLPatternWizard();
283: if (!wizard.show())
284: return;
285: String pattern = wizard.getPattern();
286: String folder;
287: if (wizard.useName()) {
288: folder = wizard.getRepositoryFolder();
289: } else {
290: folder = "\\1";
291: }
292: addRow(pattern, folder);
293: }
294:
295: private void reset(List<AnnotationExpression> exps) {
296:
297: getSelectionModel().setSelectionMode(
298: ListSelectionModel.SINGLE_SELECTION);
299: DefaultTableModel model = getModel();
300: model.setColumnCount(2);
301: model.setRowCount(exps.size());
302: int r = -1;
303: for (Iterator<AnnotationExpression> it = exps.iterator(); it
304: .hasNext();) {
305: AnnotationExpression annotationExpression = it.next();
306: r++;
307: model.setValueAt(annotationExpression.getUrlExp(), r, 0);
308: model.setValueAt(annotationExpression.getAnnotationExp(),
309: r, 1);
310: }
311: }
312:
313: private DefaultTableModel getModel() {
314: return (DefaultTableModel) panel.expresionsTable.getModel();
315: }
316:
317: private ListSelectionModel getSelectionModel() {
318: return panel.expresionsTable.getSelectionModel();
319: }
320:
321: public void tableChanged(TableModelEvent evt) {
322: if (evt.getType() == TableModelEvent.UPDATE) {
323: validateTable(evt.getFirstRow(), evt.getColumn());
324: }
325: }
326:
327: private void validateTable(int r, int c) {
328:
329: if (r < 0 || c != 0) {
330: return;
331: }
332:
333: valid = true;
334: String pattern = (String) getModel().getValueAt(r, c);
335: try {
336: Pattern.compile(pattern);
337: } catch (Exception e) {
338: valid = false;
339: }
340:
341: if (valid) {
342: panel.warningLabel.setVisible(false);
343: } else {
344: String label = NbBundle.getMessage(
345: AnnotationSettings.class,
346: "AnnotationSettingsPanel.warningLabel.text",
347: pattern);
348: panel.warningLabel.setText(label);
349: panel.warningLabel.setVisible(true);
350: }
351: if (dialogDescriptor != null) {
352: dialogDescriptor.setValid(valid);
353: }
354: }
355:
356: public void valueChanged(ListSelectionEvent evt) {
357: ListSelectionModel selectionModel = getSelectionModel();
358: int r = selectionModel.getMinSelectionIndex();
359: panel.upButton.setEnabled(r > 0);
360: panel.downButton.setEnabled(r > -1
361: && r < getModel().getRowCount() - 1);
362: panel.removeButton.setEnabled(r > -1);
363: panel.editButton.setEnabled(r > -1);
364: }
365:
366: private boolean showEdit(final EditAnnotationPanel editPanel,
367: String title) {
368: final DialogDescriptor dd = new DialogDescriptor(editPanel,
369: title);
370: dd.setModal(true);
371: dd.setValid(isValid(editPanel));
372: DocumentListener dl = new DocumentListener() {
373: public void insertUpdate(DocumentEvent arg0) {
374: validate();
375: }
376:
377: public void removeUpdate(DocumentEvent arg0) {
378: validate();
379: }
380:
381: public void changedUpdate(DocumentEvent arg0) {
382: validate();
383: }
384:
385: private void validate() {
386: dd.setValid(isValid(editPanel));
387: }
388: };
389: editPanel.patternTextField.getDocument()
390: .addDocumentListener(dl);
391: editPanel.folderTextField.getDocument().addDocumentListener(dl);
392:
393: Dialog dialog = DialogDisplayer.getDefault().createDialog(dd);
394: dialog.setVisible(true);
395: return dd.getValue() == dd.OK_OPTION;
396: }
397:
398: private boolean isValid(EditAnnotationPanel editPanel) {
399: return !(editPanel.patternTextField.getText().trim().equals("") || editPanel.folderTextField
400: .getText().trim().equals(""));
401: }
402:
403: }
|