Set module specification to 1.0.
[nbgit.git] / src / org / netbeans / modules / git / options / GitOptionsPanelController.java
blob18cd420245b5b70060a6fc180162a48583ae691a
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Contributor(s):
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
29 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
31 * If you wish your version of this file to be governed by only the CDDL
32 * or only the GPL Version 2, indicate your decision by adding
33 * "[Contributor] elects to include this software in this distribution
34 * under the [CDDL or GPL Version 2] license." If you do not indicate a
35 * single choice of license, a recipient has the option to distribute
36 * your version of this file under either the CDDL, the GPL Version 2 or
37 * to extend the choice of license to its licensees as provided above.
38 * However, if you add GPL Version 2 code and therefore, elected the GPL
39 * Version 2 license, then the option applies only if the new code is
40 * made subject to such option by the copyright holder.
42 package org.netbeans.modules.git.options;
44 import java.awt.BorderLayout;
45 import java.awt.Dialog;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.awt.event.MouseAdapter;
49 import java.awt.event.MouseEvent;
50 import java.beans.PropertyChangeListener;
51 import java.beans.PropertyChangeSupport;
52 import java.io.File;
53 import java.util.ArrayList;
54 import java.util.List;
55 import javax.swing.JButton;
56 import javax.swing.JComponent;
57 import javax.swing.JFileChooser;
58 import javax.swing.JOptionPane;
59 import javax.swing.filechooser.FileFilter;
60 import org.netbeans.modules.git.Git;
61 import org.netbeans.modules.git.GitAnnotator;
62 import org.netbeans.modules.git.GitModuleConfig;
63 import org.netbeans.modules.versioning.util.AccessibleJFileChooser;
64 import org.netbeans.spi.options.OptionsPanelController;
65 import org.openide.DialogDescriptor;
66 import org.openide.DialogDisplayer;
67 import org.openide.filesystems.FileUtil;
68 import org.openide.util.HelpCtx;
69 import org.openide.util.Lookup;
70 import org.openide.util.NbBundle;
72 final class GitOptionsPanelController extends OptionsPanelController implements ActionListener {
74 private GitPanel panel;
75 private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
76 private boolean changed;
78 public GitOptionsPanelController() {
80 panel = new GitPanel(this);
81 panel.execPathBrowseButton.addActionListener(this);
82 panel.exportFilenameBrowseButton.addActionListener(this);
84 String tooltip = NbBundle.getMessage(GitPanel.class, "GitPanel.annotationTextField.toolTipText", GitAnnotator.LABELS); // NOI18N
86 panel.annotationTextField.setToolTipText(tooltip);
87 panel.addButton.addActionListener(this);
88 panel.manageButton.addActionListener(this);
91 public void update() {
92 getPanel().load();
93 changed = false;
96 public void applyChanges() {
97 if (!validateFields()) return;
99 getPanel().store();
100 // {folder} variable setting
101 Git.getInstance().getGitAnnotator().refresh();
102 Git.getInstance().refreshAllAnnotations();
104 changed = false;
107 public void cancel() {
108 // need not do anything special, if no changes have been persisted yet
111 public boolean isValid() {
112 return getPanel().valid();
115 public boolean isChanged() {
116 return changed;
119 public HelpCtx getHelpCtx() {
120 return new HelpCtx(GitOptionsPanelController.class);
123 public JComponent getComponent(Lookup masterLookup) {
124 return getPanel();
127 public void addPropertyChangeListener(PropertyChangeListener l) {
128 pcs.addPropertyChangeListener(l);
131 public void removePropertyChangeListener(PropertyChangeListener l) {
132 pcs.removePropertyChangeListener(l);
135 public void actionPerformed(ActionEvent evt) {
136 if (evt.getSource() == panel.execPathBrowseButton) {
137 onExecPathBrowseClick();
138 } else if (evt.getSource() == panel.exportFilenameBrowseButton) {
139 onExportFilenameBrowseClick();
140 } else if (evt.getSource() == panel.addButton) {
141 onAddClick();
142 } else if (evt.getSource() == panel.manageButton) {
143 onManageClick();
147 private File getExportFile() {
148 String execPath = panel.exportFilenameTextField.getText();
149 return FileUtil.normalizeFile(new File(execPath));
152 private File getExecutableFile() {
153 String execPath = panel.executablePathTextField.getText();
154 return FileUtil.normalizeFile(new File(execPath));
157 private Boolean validateFields() {
158 String username = panel.emailTextField.getText();
159 if (!GitModuleConfig.getDefault().isUserNameValid(username)) {
160 JOptionPane.showMessageDialog(null,
161 NbBundle.getMessage(GitPanel.class, "MSG_WARN_USER_NAME_TEXT"), // NOI18N
162 NbBundle.getMessage(GitPanel.class, "MSG_WARN_FIELD_TITLE"), // NOI18N
163 JOptionPane.WARNING_MESSAGE);
164 return false;
166 String execpath = panel.executablePathTextField.getText();
167 if (!GitModuleConfig.getDefault().isExecPathValid(execpath)) {
168 JOptionPane.showMessageDialog(null,
169 NbBundle.getMessage(GitPanel.class, "MSG_WARN_EXEC_PATH_TEXT"), // NOI18N
170 NbBundle.getMessage(GitPanel.class, "MSG_WARN_FIELD_TITLE"), // NOI18N
171 JOptionPane.WARNING_MESSAGE);
172 return false;
174 return true;
177 private void onExportFilenameBrowseClick() {
178 File oldFile = getExecutableFile();
179 JFileChooser fileChooser = new AccessibleJFileChooser(NbBundle.getMessage(GitOptionsPanelController.class, "ACSD_ExportBrowseFolder"), oldFile); // NOI18N
180 fileChooser.setDialogTitle(NbBundle.getMessage(GitOptionsPanelController.class, "ExportBrowse_title")); // NOI18N
181 fileChooser.setMultiSelectionEnabled(false);
182 FileFilter[] old = fileChooser.getChoosableFileFilters();
183 for (int i = 0; i < old.length; i++) {
184 FileFilter fileFilter = old[i];
185 fileChooser.removeChoosableFileFilter(fileFilter);
187 fileChooser.showDialog(panel, NbBundle.getMessage(GitOptionsPanelController.class, "OK_Button")); // NOI18N
188 File f = fileChooser.getSelectedFile();
189 if (f != null) {
190 panel.exportFilenameTextField.setText(f.getAbsolutePath());
194 private void onExecPathBrowseClick() {
195 File oldFile = getExportFile();
196 JFileChooser fileChooser = new AccessibleJFileChooser(NbBundle.getMessage(GitOptionsPanelController.class, "ACSD_BrowseFolder"), oldFile); // NOI18N
197 fileChooser.setDialogTitle(NbBundle.getMessage(GitOptionsPanelController.class, "Browse_title")); // NOI18N
198 fileChooser.setMultiSelectionEnabled(false);
199 FileFilter[] old = fileChooser.getChoosableFileFilters();
200 for (int i = 0; i < old.length; i++) {
201 FileFilter fileFilter = old[i];
202 fileChooser.removeChoosableFileFilter(fileFilter);
204 fileChooser.addChoosableFileFilter(new FileFilter() {
205 public boolean accept(File f) {
206 return f.isDirectory();
208 public String getDescription() {
209 return NbBundle.getMessage(GitOptionsPanelController.class, "GitExec");// NOI18N
212 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
213 fileChooser.showDialog(panel, NbBundle.getMessage(GitOptionsPanelController.class, "OK_Button")); // NOI18N
214 File f = fileChooser.getSelectedFile();
215 if (f != null) {
216 panel.executablePathTextField.setText(f.getAbsolutePath());
220 private GitPanel getPanel() {
221 if (panel == null) {
222 panel = new GitPanel(this);
224 return panel;
227 void changed() {
228 if (!changed) {
229 changed = true;
230 pcs.firePropertyChange(OptionsPanelController.PROP_CHANGED, false, true);
232 pcs.firePropertyChange(OptionsPanelController.PROP_VALID, null, null);
235 private class LabelVariable {
236 private String description;
237 private String variable;
239 public LabelVariable(String variable, String description) {
240 this.description = description;
241 this.variable = variable;
244 @Override
245 public String toString() {
246 return description;
249 public String getDescription() {
250 return description;
253 public String getVariable() {
254 return variable;
258 private void onAddClick() {
259 LabelsPanel labelsPanel = new LabelsPanel();
260 List<LabelVariable> variables = new ArrayList<LabelVariable>(GitAnnotator.LABELS.length);
261 for (int i = 0; i < GitAnnotator.LABELS.length; i++) {
262 LabelVariable variable = new LabelVariable(
263 GitAnnotator.LABELS[i],
264 "{" + GitAnnotator.LABELS[i] + "} - " + NbBundle.getMessage(GitPanel.class, "GitPanel.label." + GitAnnotator.LABELS[i]) // NOI18N
266 variables.add(variable);
268 labelsPanel.labelsList.setListData(variables.toArray(new LabelVariable[variables.size()]));
270 String title = NbBundle.getMessage(GitPanel.class, "GitPanel.labelVariables.title"); // NOI18N
271 String acsd = NbBundle.getMessage(GitPanel.class, "GitPanel.labelVariables.acsd"); // NOI18N
273 DialogDescriptor dialogDescriptor = new DialogDescriptor(labelsPanel, title);
274 dialogDescriptor.setModal(true);
275 dialogDescriptor.setValid(true);
277 final Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
278 dialog.getAccessibleContext().setAccessibleDescription(acsd);
280 labelsPanel.labelsList.addMouseListener(new MouseAdapter() {
281 @Override
282 public void mouseClicked(MouseEvent e) {
283 if(e.getClickCount() == 2) {
284 dialog.setVisible(false);
287 });
289 dialog.setVisible(true);
291 if(DialogDescriptor.OK_OPTION.equals(dialogDescriptor.getValue())) {
293 Object[] selection = (Object[])labelsPanel.labelsList.getSelectedValues();
295 String variable = ""; // NOI18N
296 for (int i = 0; i < selection.length; i++) {
297 variable += "{" + ((LabelVariable)selection[i]).getVariable() + "}"; // NOI18N
300 String annotation = panel.annotationTextField.getText();
302 int pos = panel.annotationTextField.getCaretPosition();
303 if(pos < 0) pos = annotation.length();
305 StringBuffer sb = new StringBuffer(annotation.length() + variable.length());
306 sb.append(annotation.substring(0, pos));
307 sb.append(variable);
308 if(pos < annotation.length()) {
309 sb.append(annotation.substring(pos, annotation.length()));
311 panel.annotationTextField.setText(sb.toString());
312 panel.annotationTextField.requestFocus();
313 panel.annotationTextField.setCaretPosition(pos + variable.length());
317 private void onManageClick() {
318 final PropertiesPanel panel = new PropertiesPanel();
320 final PropertiesTable propTable;
322 propTable = new PropertiesTable(panel.labelForTable, PropertiesTable.PROPERTIES_COLUMNS, new String[] { PropertiesTableModel.COLUMN_NAME_VALUE});
324 panel.setPropertiesTable(propTable);
326 JComponent component = propTable.getComponent();
328 panel.propsPanel.setLayout(new BorderLayout());
330 panel.propsPanel.add(component, BorderLayout.CENTER);
332 GitExtProperties gitProperties = new GitExtProperties(panel, propTable, null) ;
334 DialogDescriptor dd = new DialogDescriptor(panel, NbBundle.getMessage(GitOptionsPanelController.class, "CTL_PropertiesDialog_Title", null), true, null); // NOI18N
335 final JButton okButton = new JButton(NbBundle.getMessage(GitOptionsPanelController.class, "CTL_Properties_Action_OK")); // NOI18N
336 okButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(GitOptionsPanelController.class, "CTL_Properties_Action_OK")); // NOI18N
337 final JButton cancelButton = new JButton(NbBundle.getMessage(GitOptionsPanelController.class, "CTL_Properties_Action_Cancel")); // NOI18N
338 cancelButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(GitOptionsPanelController.class, "CTL_Properties_Action_Cancel")); // NOI18N
339 dd.setOptions(new Object[] {okButton, cancelButton}); // NOI18N
340 dd.setHelpCtx(new HelpCtx(GitOptionsPanelController.class));
341 panel.putClientProperty("contentTitle", null); // NOI18N
342 panel.putClientProperty("DialogDescriptor", dd); // NOI18N
343 Dialog dialog = DialogDisplayer.getDefault().createDialog(dd);
344 dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(GitOptionsPanelController.class, "CTL_PropertiesDialog_Title")); // NOI18N
346 dialog.pack();
347 dialog.setVisible(true);
348 if (dd.getValue() == okButton) {
349 gitProperties.setProperties();